-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgen.c
900 lines (735 loc) · 21.5 KB
/
gen.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
#include <cassert>
#include <cfloat>
#include <cmath>
#include <cstring>
#include <random>
#include "gen.h"
#include "glob.h"
//------------------------------- Parameters -------------------------------
void PatternPar::write(ostream &fp)
{
fp << "\tNumber of patterns = " << npats << endl;
fp << "\tAverage length of pattern = " << patlen << endl;
fp << "\tCorrelation between consecutive patterns = " << corr << endl;
fp << "\tAverage confidence in a rule = " << conf << endl;
fp << "\tVariation in the confidence = " << conf_var << endl;
}
void TransPar::write(ostream &fp)
{
fp << "Number of transactions in database = " << ntrans << endl;
fp << "Average transaction length = " << tlen << endl;
fp << "Number of items = " << nitems << endl;
fp << "Large Itemsets:" << endl;
lits.write(fp);
fp << endl;
}
// calculate the number of roots, given the number of levels
void TaxPar::calc_values(void)
{
LINT nset;
nset = 0;
nset += (nlevels != 0);
nset += (fanout != 0);
nset += (nroots != 0);
switch (nset)
{
case 0: // fill in defaults
nroots = 250;
fanout = 5;
return;
case 1: // need to fill in 1 value
assert (nlevels == 0);
if (fanout == 0)
fanout = 5;
else if (nroots == 0)
nroots = 250;
return;
case 2:
if (nlevels == 0) // all set!
return;
if (fanout != 0) { // calculate nroots
// g++ added explicit cast to LINT below
nroots = LINT (nitems / (1 + pow(DOUBLE(fanout), DOUBLE(nlevels-1))));
if (nroots < 1)
nroots = 1;
}
else if (nroots != 0) { // calculate fanout
FLOAT temp;
temp = (FLOAT)nitems / nroots - 1;
temp = log((DOUBLE)temp) / (nlevels - 1);
fanout = exp((DOUBLE)temp);
}
case 3: // all set!
return;
}
}
void TaxPar::write(ostream &fp)
{
fp << "Number of transactions in database = " << ntrans << endl;
fp << "Average transaction length = " << tlen << endl;
fp << "Number of items = " << nitems << endl;
fp << "Number of roots = " << nroots << endl;
fp << "Number of levels = " << nlevels << endl;
fp << "Average fanout = " << fanout << endl;
fp << "Large Itemsets:" << endl;
lits.write(fp);
fp << endl;
}
void SeqPar::write(ostream &fp)
{
fp << "Number of customers in database = " << ncust << endl;
fp << "Average sequence length = " << slen << endl;
fp << "Average transaction length = " << tlen << endl;
fp << "Number of items = " << nitems << endl;
fp << "Repetition-level = " << rept << endl;
fp << "Variation in repetition-level = " << rept_var << endl;
fp << "Large Itemsets:" << endl;
lits.write(fp);
fp << "Large Sequences:" << endl;
lseq.write(fp);
fp << endl;
}
//------------------------------ Taxonomy ------------------------------
const LINT Taxonomy::item_len = 7;
//
// Constructor: reads taxonomy file and builds taxonomy as a DAG
//
Taxonomy::Taxonomy(LINT num_items, // total number of items
LINT my_num_roots, // number of roots
FLOAT fanout, // average fanout
FLOAT my_depth_ratio // average ratio of ....
)
: nitems(num_items), nroots(my_num_roots), depth(my_depth_ratio)
{
LINT i, j;
LINT next_child;
poisson_distribution<LINT> nchildren(fanout - 1); // string length
// allocate memory
par = new LINT [nitems];
child_start = new LINT [nitems];
child_end = new LINT [nitems];
next_child = nroots;
// initialize parents (or lack thereof) for roots
for (i = 0; i < nroots; i++)
par[i] = -1;
// set up all the interior nodes
for (i = 0, j = next_child; i < nitems && next_child < nitems; i++)
{
child_start[i] = next_child;
next_child += nchildren(generator) + 1;
if (next_child > nitems)
next_child = nitems;
child_end[i] = next_child;
for (; j < next_child; j++)
par[j] = i;
}
// initialize children (or lack thereof) for all the leaves
for (; i < nitems; i++)
child_start[i] =
child_end[i] = -1;
}
Taxonomy::~Taxonomy(void)
{
delete [] par;
delete [] child_start;
delete [] child_end;
}
void Taxonomy::write(ofstream &fp)
{
for (LINT i = 0; i < nitems; i++)
if (par[i] >= 0) {
assert(i != par[i]);
fp.write((char *)&i, sizeof(LINT));
fp.write((char *)&par[i], sizeof(LINT));
}
}
void Taxonomy::write_asc(ofstream &fp)
{
for (LINT i = 0; i < nitems; i++)
if (par[i] >= 0) {
assert(i != par[i]);
fp << setw(item_len) << i << " " << setw(item_len) << par[i] << endl;
}
}
void Taxonomy::display(ofstream &fp)
{
fp << "Taxonomy: " << endl;
for (LINT i = 0; i < nitems && child_start[i] > 0; i++)
fp << i << " " << child_start[i] << " " << child_end[i]-1 << endl;
fp << endl;
}
//------------------------------- ItemSet -------------------------------
ItemSet::ItemSet(LINT num_items, // number of items
Taxonomy *ptax // taxonomy (optional)
)
: nitems(num_items), tax(ptax)
{
LINT i, j;
cum_prob = new FLOAT [nitems];
if (tax)
tax_prob = new FLOAT [nitems];
else
tax_prob = NULL;
for (i = 0; i < nitems; i++)
cum_prob[i] = get_unit_exponential_deviate(); // prob. that this pattern will be picked
if (tax) { // weight(itm) += wieght(children)
// normalize probabilities for the roots and for children
normalize(cum_prob, 0, tax->num_roots()-1);
for (i = 0; i < nitems && tax->num_children(i) > 0; i++)
normalize(cum_prob, tax->first_child(i), tax->last_child(i));
// calulate cumulative probabilities for children
for (i = 0; i < nitems; i++)
tax_prob[i] = cum_prob[i];
for (i = 1; i < nitems; i++)
if (tax->num_children(i) > 0)
for (j = tax->first_child(i); j < tax->last_child(i); j++)
tax_prob[j+1] += tax_prob[j];
// set real probabilities
for (i = tax->num_roots(); i < nitems; i++)
cum_prob[i] *= cum_prob[ tax->parent(i) ] * tax->depth_ratio();
}
// normalize probabilites (why -- see get_pat)
normalize(cum_prob, 0, nitems-1);
for (i = 1; i < nitems; i++) // calulate cumulative probabilities
cum_prob[i] += cum_prob[i-1];
}
ItemSet::~ItemSet()
{
delete [] cum_prob;
}
// normalize probabilities between low and high
//
void ItemSet::normalize(FLOAT prob[], LINT low, LINT high)
{
FLOAT tot;
LINT i;
// normalize probabilites
tot = 0;
for (i = low; i <= high; i++)
tot += prob[i];
for (i = low; i <= high; i++)
prob[i] /= tot;
}
// returns a pattern chosen at random
//
Item ItemSet::get_item(void)
{
FLOAT r;
LINT i;
// find the desired pattern using cum_prob table
r = get_uniform_deviate();
// want item i such that cum_prob[i-1] < r <= cum_prob[i];
// g++ added cast to LINT below (two lines)
i = LINT (r * nitems); // guess location of item
i += LINT ((r-cum_prob[i]) * nitems); // refine guess
if (i >= nitems) // check boundaries
i = nitems-1;
if (i < 0)
i = 0;
while ( i < (nitems-1) && r > cum_prob[i] ) // find item
i++;
while ( i > 0 && r <= cum_prob[i-1] )
i--;
return i;
}
// if no taxonomy, returns itm
//
Item ItemSet::specialize(Item itm)
{
FLOAT r;
LINT i, nchildren;
Item first, last;
if (!tax) // no taxonomy
return itm;
nchildren = tax->num_children(itm);
if (nchildren == 0) // no children
return itm;
first = tax->child(itm, 0);
last = tax->child(itm, nchildren-1);
// find the desired pattern using cum_prob table
r = get_uniform_deviate();
// g++ added cast to LINT
i = LINT (first + r * nchildren);
if (i == last)
i--;
while ( i < last && r > tax_prob[i] )
i++;
while ( i > first && r < tax_prob[i-1] )
i--;
return specialize(i);
}
FLOAT ItemSet::weight(Item itm) // returns prob. of choosing item
{
if (itm == 0)
return cum_prob[itm];
else
return cum_prob[itm] - cum_prob[itm-1];
}
void ItemSet::display(ofstream &fp)
{
// if (tax != NULL)
// tax->display(fp);
fp << "Items:" << endl;
fp << setprecision(3);
if (tax != NULL) {
if (cum_prob[0] * nitems > 10)
fp << 0 << " " << cum_prob[0] * nitems << " "
<< tax->first_child(0) << " " << tax->last_child(0) << endl;
for (LINT i = 1; i < nitems; i++)
if ((cum_prob[i]-cum_prob[i-1]) * nitems > 10)
fp << i << " " << (cum_prob[i]-cum_prob[i-1]) * nitems << " "
<< tax->first_child(i) << " " << tax->last_child(i) << endl;
}
else {
if (cum_prob[0] * nitems > 5)
fp << 0 << " " << cum_prob[0] * nitems << endl;
for (LINT i = 1; i < nitems; i++)
if ((cum_prob[i]-cum_prob[i-1]) * nitems > 5)
fp << i << " " << (cum_prob[i]-cum_prob[i-1]) * nitems << endl;
}
fp << setprecision(0);
fp << endl;
}
//--------------------------------- String ---------------------------------
String::String(LINT n) // number of items
: nitems(n)
{
items = new LINT [nitems];
// rval = new FLOAT [nitems];
// ritems = new LINT [nitems];
}
String::~String(void)
{
delete [] items;
// delete [] rval;
// delete [] ritems;
}
void String::display(ofstream &fp, LINT prob_comp)
{
fp << setw(6) << prob_comp * prob << " " << setw(6) << conf << " ";
for(LINT i = 0; i < nitems; i++)
fp << " " << items[i];
fp << endl;
return;
}
void String::display(ofstream &fp, StringSet &lits, LINT prob_comp)
{
LINT i, j;
StringP lstr;
fp << setw(6) << prob_comp * prob << " " << setw(6) << conf << " ";
for(i = 0; i < nitems; i++)
{
fp << " << ";
lstr = lits.get_pat(items[i]);
for (j = 0; j < lstr->nitems; j++)
fp << lstr->items[j] << " ";
fp << ">>";
}
fp << endl;
return;
}
// shuffles items in string
//
// void String::shuffle(void)
// {
// UniformDist ud;
// LINT i, j, ival;
// FLOAT fval;
// // associate a random value with each item
// for (i = 0; i < nitems; i++)
// rval[i] = ud();
// // sort items according to the values in rval
// for (i = 0; i < nitems; i++ )
// {
// ival = items[i]; fval = rval[i];
// for ( j = i; j > 0 && rval[j-1] > fval; j-- ) {
// items[j] = items[j-1];
// rval[j] = rval[j-1];
// }
// items[j] = ival;
// rval[j] = fval;
// }
// }
//------------------------------- StringSet -------------------------------
StringSet::StringSet(LINT nitems, // number of items
PatternPar par, // npats, patlen, corr, conf & conf_var
Taxonomy *ptax, // taxonomy (optional)
FLOAT rept, // repetition-level
FLOAT rept_var // variation in repetition-level
)
: tax(ptax)
{
normal_distribution<double> conf(par.conf, sqrt(par.conf_var));
poisson_distribution<LINT> len(par.patlen - 1); // string length
normal_distribution<double> repeat(rept, sqrt(rept_var));
items = new ItemSet(nitems, tax); // associate probabilities with items
LINT i, j, num_same;
FLOAT tot;
npats = par.npats;
// last_pat = 0;
pat = new StringP [npats];
for (i = 0; i < npats; i++)
{
pat[i] = new String(1 + len(generator));
// fill correlated items
if (par.corr > 0 && i > 0) { // correlated patterns
// each pattern has some items same as the previous pattern
num_same = LINT( pat[i]->size() * par.corr * get_unit_exponential_deviate() + 0.5 );
if ( num_same > pat[i-1]->size() )
num_same = pat[i-1]->size();
if ( num_same > pat[i]->size() )
num_same = pat[i]->size();
// choose num_same items at random from previous pattern
Choose shuffle(pat[i-1]->size(), num_same);
for (j = 0; j < num_same; j++)
pat[i]->items[j] = pat[i-1]->item( shuffle.pos(j) );
// pat[i-1]->shuffle(num_same);
// for (j = 0; j < num_same; j++)
// pat[i]->items[j] = pat[i-1]->rand_item(j);
}
else { // no correlation
num_same = 0;
}
if (rept == 0) {
// fill remaining items at random
for (j = num_same; j < pat[i]->size(); j++)
pat[i]->items[j] = items->get_item();
// pat[i]->items[j] = LINT(1 + nitems * rand());
}
else {
// some items are repetitions
FLOAT rept_lvl = repeat(generator);
for (j = num_same; j < pat[i]->size(); j++)
if ( j > 0 && get_uniform_deviate() < rept_lvl ) // pick a previous item
pat[i]->items[j] = pat[i]->items[ LINT(j * get_uniform_deviate()) ];
else // pick random item
pat[i]->items[j] = items->get_item();
}
pat[i]->prob = get_unit_exponential_deviate(); // prob. that this pattern will be picked
pat[i]->conf = conf(generator); // used in Transaction::add and CustSeq::add
// to decide how many items to drop from
// this pattern to corrupt it
}
if (tax) {
// weight probabilites with geometric mean of probabilities of items
for (i = 0; i < npats; i++)
{
DOUBLE weight = 1;
for (j = 0; j < pat[i]->size(); j++)
weight *= items->weight(pat[i]->items[j]);
// cerr << "WEIGHT = " << weight;
weight = pow(weight, DOUBLE(1)/pat[i]->size());
// cerr << " " << weight << endl;
pat[i]->prob *= weight;
}
}
// normalize probabilites (why -- see get_pat)
cum_prob = new FLOAT [npats];
tot = 0;
for (i = 0; i < npats; i++)
tot += pat[i]->prob;
for (i = 0; i < npats; i++)
pat[i]->prob /= tot;
// calulate cumulative probabilities
cum_prob[0] = pat[0]->prob;
for (i = 1; i < npats; i++)
cum_prob[i] = cum_prob[i-1] + pat[i]->prob;
// cerr << cum_prob[npats-1] << endl << flush;
// allocate space for answer
LINT maxlen = 0;
for (i = 1; i < npats; i++)
if (pat[i]->size() > maxlen)
maxlen = pat[i]->size();
answer = new String(maxlen);
}
StringSet::~StringSet()
{
LINT i;
for (i = 0; i < npats; i++)
delete pat[i];
delete [] pat;
}
// specialize each item in pattern #i and store result in answer
//
StringP StringSet::specialize(LINT i)
{
answer->set_size( pat[i]->size() );
answer->set_conf_lvl( pat[i]->conf_lvl() );
for (LINT j = 0; j < pat[i]->size(); j++)
answer->set_item(j, items->specialize( pat[i]->item(j) ));
return answer;
}
// returns pattern #i
//
StringP StringSet::get_pat(LINT i)
{
if (!tax)
return pat[i];
else
return specialize(i);
}
void StringSet::display(ofstream &fp)
{
LINT i;
items->display(fp);
fp << "ItemSets:" << endl;
fp << setprecision(3);
// too lazy to do a sort, so print high-prob. patterns first
for (i = 0; i < npats; i++)
if (pat[i]->prob * npats > 10)
pat[i]->display(fp, npats);
for (i = 0; i < npats; i++)
if (pat[i]->prob * npats <= 10 && pat[i]->prob * npats > 1)
pat[i]->display(fp, npats);
fp << setprecision(0);
fp << endl;
}
void StringSet::display(ofstream &fp, StringSet &lits)
{
LINT i;
fp << setprecision(3);
// too lazy to do a sort, so print high-prob. patterns first
for (i = 0; i < npats; i++)
if (pat[i]->prob * npats > 6)
pat[i]->display(fp, lits, npats);
for (i = 0; i < npats; i++)
if (pat[i]->prob * npats <= 6)
pat[i]->display(fp, lits, npats);
fp << setprecision(0);
}
//------------------------------- StringSet -------------------------------
// returns a pattern chosen at random
//
StringP StringSetIter::get_pat(void)
{
FLOAT r;
LINT i = 0;
if (last_pat < 0) {
last_pat = -last_pat;
if (!strset->tax)
return strset->pat[last_pat];
else
return strset->specialize(last_pat);
}
// find the desired pattern using cum_prob table
r = get_uniform_deviate();
// g++ LINT my love once again
i = LINT (r * strset->npats);
if (i == strset->npats)
i--;
while ( i < (strset->npats-1) && r > strset->cum_prob[i] )
i++;
while ( i > 0 && r < strset->cum_prob[i-1] )
i--;
last_pat = i;
if (!strset->tax)
return strset->pat[i];
else
return strset->specialize(i);
}
void StringSetIter::unget_pat(void)
{
last_pat = -last_pat;
}
//------------------------------ Transaction ------------------------------
// static variables
const LINT Transaction::cid_len = 10;
const LINT Transaction::tid_len = 10;
const LINT Transaction::item_len = 10;
LINT Transaction::tid = 0;
Transaction::Transaction(LINT sz)
: tlen(sz), nitems(0), maxsize(5 * sz)
{
// maximum size of a transaction is 5 * sz
items = new LINT [maxsize];
}
Transaction::~Transaction()
{
delete [] items;
}
void Transaction::sort(void)
{
LINT val;
LINT i, j;
for (i = 1; i < nitems; i++ )
{
val = items[i];
for ( j = i; j > 0 && items[j-1] > val; j-- )
items[j] = items[j-1];
items[j] = val;
}
}
bool Transaction::add_item(LINT itm)
{
LINT i;
for (i = 0; i < nitems; i++)
if ( items[i] == itm ) return false;
if (nitems >= maxsize) { // allocate more memory
LINT *old_items = items;
maxsize *= 2;
items = new LINT [maxsize];
for (i = 0; i < nitems; i++)
items[i] = old_items[i];
delete [] old_items;
}
items[nitems++] = itm;
return true;
}
// adds pattern to transaction
// returns true if pattern was added, false else
//
bool Transaction::add(String &pat, bool corrupt)
{
LINT i, patlen;
// corrupt the pattern by reducing its length;
// conf_lvl for a pattern is decided at the time of pattern creation
patlen = pat.size();
if ( corrupt )
while ( patlen > 0 && get_uniform_deviate() > pat.conf_lvl() )
patlen--;
// in half of the cases, we drop the pattern that won't fit
if ( patlen+nitems > tlen ) // not enough space left
if ( get_uniform_deviate() > 0.5 )
return false;
// pick "patlen" items at random from pattern
// if ( patlen < pat.size() )
Choose shuffle(pat.size(), patlen);
for (i = 0; i < patlen; i++)
add_item( pat.item(shuffle.pos(i)) ); // allocates extra space if necessary
// pat.shuffle(patlen);
// for (i = 0; i < patlen; i++)
// add_item( pat.rand_item(i) ); // allocates extra space if necessary
return true;
}
void Transaction::write(ofstream &fp, LINT cid)
{
if ( nitems == 0 )
return;
sort();
tid++;
if (cid == 0) // no customer-id; set cust-id to trans-id
cid = tid;
fp.write((char *)&cid, sizeof(LINT));
fp.write((char *)&tid, sizeof(LINT));
fp.write((char *)&nitems, sizeof(LINT));
fp.write((char *)items, nitems * sizeof(LINT));
}
void Transaction::write_asc(ofstream &fp, LINT cid)
{
if ( nitems == 0 )
return;
sort();
tid++;
if (cid == 0) // no customer-id; set cust-id to trans-id
cid = tid;
for (LINT i = 0; i < nitems; i++) {
fp << setw(cid_len) << cid << " ";
fp << setw(tid_len) << tid << " ";
fp << setw(item_len) << items[i] << endl;
}
}
//------------------------------ CustSeq ------------------------------
CustSeq::CustSeq(Cid cust_id, LINT seq_len, LINT trans_len)
: cid(cust_id), slen(seq_len), tlen(trans_len), ntrans(seq_len), nitems(0),
maxsize(5 * seq_len)
{
// we reallocate memory if necessary
trans = new TransactionP [maxsize];
for (LINT i = 0; i < maxsize; i++)
trans[i] = NULL;
}
CustSeq::~CustSeq()
{
for (LINT i = 0; i < maxsize; i++)
if ( trans[i] )
delete trans[i];
delete [] trans;
}
// adds pattern to CustSeq
// returns true if pattern was added, false else
// REWORK!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//
bool CustSeq::add(String &pat, StringSet &lits)
{
LINT i, patlen;
LINT pos;
LINT newitems, olditems;
bool corrupt; // if true, corrupt transactions too
if (get_uniform_deviate() > pat.conf_lvl())
corrupt = true; // corrupt transactions
else
corrupt = false; // don't corrupt transactions
// corrupt the pattern by reducing its length;
// conf_lvl for a pattern is decided at the time of pattern creation
patlen = pat.size();
if ( corrupt )
while ( patlen > 0 && get_uniform_deviate() > pat.conf_lvl() )
patlen--;
if ( patlen == 0 ) // no trans. left in sequence
return true;
// allows transactions to be dropped randomly from the sequence
// if ( patlen < pat.size() )
Choose shuffle(pat.size(), patlen);
// pat.shuffle(patlen);
// calculate # of items in pattern
for (newitems = 0, i = 0; i < patlen; i++)
newitems += lits.get_pat( pat.item( shuffle.pos(i) ) )->size();
// newitems += lits.get_pat( pat.rand_item(i) )->size();
// in half of the cases, we drop the pattern that won't fit
if ( (patlen > slen) || (newitems + nitems > slen * tlen) )
if ( get_uniform_deviate() > 0.5 )
return false;
if ( patlen > maxsize ) { // need to allocate more memory
TransactionP *old_trans = trans;
LINT oldsize = maxsize;
maxsize = patlen*2;
trans = new TransactionP [maxsize];
for (i = 0; i < oldsize; i++)
trans[i] = old_trans[i];
for (; i < maxsize; i++)
trans[i] = NULL;
delete [] old_trans;
}
// add new sequence
Choose *shuffle1 = NULL;
if (ntrans > patlen)
shuffle1 = new Choose(ntrans, patlen);
for (i = 0; i < patlen; i++)
{
if ( shuffle1 )
pos = shuffle1->pos(i);
else
pos = i;
if ( trans[pos] == NULL )
trans[pos] = new Transaction(tlen);
olditems = trans[pos]->size();
trans[pos]->add( *lits.get_pat(pat.item( shuffle.pos(i) )), corrupt );
// trans[pos]->add( *lits.get_pat(pat.rand_item(i)), corrupt );
nitems += trans[pos]->size() - olditems; // update count of #items
}
delete shuffle1;
// pos = ud() * ntrans / patlen;
// for (i = 0; i < patlen; i++)
// {
// if ( trans[pos] == NULL )
// trans[pos] = new Transaction(tlen);
// olditems = trans[pos]->size();
// trans[pos]->add( *lits.get_pat(pat.item( shuffle.pos(i) )), corrupt );
// // trans[pos]->add( *lits.get_pat(pat.rand_item(i)), corrupt );
// nitems += trans[pos]->size() - olditems; // update count of #items
// pos += 1 + ud() * ntrans / patlen;
// }
return true;
}
void CustSeq::write(ofstream &fp)
{
for (LINT i = 0; i < ntrans-1; i++)
if ( trans[i] != NULL && trans[i]->size() > 0 )
trans[i]->write(fp, cid);
}
void CustSeq::write_asc(ofstream &fp)
{
for (LINT i = 0; i < ntrans-1; i++)
if ( trans[i] != NULL && trans[i]->size() > 0 )
trans[i]->write_asc(fp, cid);
}