-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.cc
386 lines (322 loc) · 9.36 KB
/
main.cc
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
#include <fstream>
#include <stdio.h>
#include "glob.h"
#include "gen.h"
#include <string.h> //added by MJZaki for strcmp
using namespace std;
// prototypes
void command_line(TransPar &par);
void get_args(TransPar &par, int argc, char **argv);
void gen_rules(TransPar &par);
Transaction *mk_tran(StringSetIter &lits, LINT tlen, LINT NPATS,
Taxonomy *tax = NULL); //MJZ: added NPATS
void command_line(TaxPar &par);
void get_args(TaxPar &par, int argc, char **argv);
void gen_taxrules(TaxPar &par);
void command_line(SeqPar &par);
void get_args(SeqPar &par, int argc, char **argv);
void print_version(void);
void gen_seq(SeqPar &par);
CustSeq *mk_seq(Cid cid, StringSetIter &lseq, StringSet &lits, LINT slen, LINT tlen);
char data_file[256];
char pat_file[256];
char tax_file[256];
//added by MJZaki
char ntpc_file[256];
char conf_file[256];
void memory_err(void)
{
cout << "A memory allocation error occurred. \n";
exit(1);
}
int main(int argc, char **argv)
{
set_new_handler(memory_err);
if (strcmp(argv[1], "lit") == 0) {
// For Rules
TransPar par;
get_args(par, argc, argv); // get arguments
gen_rules(par); // generate rules (really, just transactions)
}
else if (strcmp(argv[1], "seq") == 0) {
// For Sequences
SeqPar par;
get_args(par, argc, argv); // get arguments
gen_seq(par); // generate sequences
}
else if (strcmp(argv[1], "tax") == 0) {
// For Rules with Taxonomies
TaxPar par;
get_args(par, argc, argv); // get arguments
gen_taxrules(par); // generate rules (really, just transactions)
}
else if (strcmp(argv[1], "-version") == 0) {
print_version();
return 0;
}
else {
cerr << "Synthetic Data Generation, ";
print_version();
cerr << "Usage: " << argv[0] << " lit|tax|seq [options]\n";
cerr << " " << argv[0]
<< " lit|tax|seq -help For more detailed list of options\n";
return 1;
}
return 0;
}
// Generate Transactions
//
void gen_rules(TransPar &par)
{
StringSet *lits;
StringSetIter *patterns;
Transaction *trans;
PoissonDist *tlen;
ofstream data_fp;
ofstream pat_fp;
ofstream conf_fp; //added by MJZaki
data_fp.open(data_file);
pat_fp.open(pat_file);
conf_fp.open(conf_file); //added by MJZaki
if (data_fp.fail() || pat_fp.fail() || conf_fp.fail()) {
cerr << "Error opening output file" << endl;
exit(1);
}
lits = new StringSet(par.nitems, par.lits);
// Reset random seed generator for before generating transactions
if (par.seed < 0) RandSeed::set_seed(par.seed);
tlen = new PoissonDist(par.tlen-1);
par.write(pat_fp);
lits->display(pat_fp);
patterns = new StringSetIter(*lits);
LINT NTRANS=0;
//Transaction::set_print_cid(FALSE); // added by me to suppress cid
for (LINT i = 0; i < par.ntrans; i ++)
{
trans = mk_tran(*patterns, (*tlen)()+1,par.lits.npats);
if (trans->tid < par.mintid) par.mintid = trans->tid;
if (trans->tid > par.maxtid) par.maxtid = trans->tid;
if (par.ascii)
trans->write_asc(data_fp);
else
trans->write(data_fp);
//cout << "TRANS SZ " << trans->size() << endl;
if (trans->size() > 0) NTRANS++; //added by me: repeat if trans empty
else i--;
delete trans;
}
data_fp.close();
pat_fp.close();
//added by MJZaki
if (par.ascii){
conf_fp << NTRANS << "\n";
conf_fp << par.nitems << "\n";
conf_fp << par.tlen << "\n";
conf_fp << par.mintid << "\n";
conf_fp << par.maxtid << "\n";
}
else{
cout << "WRITING " << NTRANS << " "<< par.nitems << endl;
conf_fp.write((char *)&NTRANS, sizeof(LINT));
conf_fp.write((char *)&par.nitems, sizeof(LINT));
conf_fp.write((char *)&par.tlen, sizeof(FLOAT));
conf_fp.write((char *)&par.mintid, sizeof(LINT));
conf_fp.write((char *)&par.maxtid, sizeof(LINT));
}
conf_fp.close();
}
// Generate Transactions and Taxonomy
//
void gen_taxrules(TaxPar &par)
{
Taxonomy *tax;
StringSet *lits;
StringSetIter *patterns;
Transaction *trans;
PoissonDist *tlen;
ofstream data_fp;
ofstream pat_fp;
ofstream tax_fp;
ofstream conf_fp; //added by MJZaki
data_fp.open(data_file);
pat_fp.open(pat_file);
tax_fp.open(tax_file);
conf_fp.open(conf_file); //added by MJZaki
if (data_fp.fail() || pat_fp.fail() || tax_fp.fail() || conf_fp.fail()) {
cerr << "Error opening output file" << endl;
exit(1);
}
// generate taxonomy and write it to file
tax = new Taxonomy(par.nitems, par.nroots, par.fanout, par.depth_ratio);
if (par.ascii)
tax->write_asc(tax_fp);
else
tax->write(tax_fp);
tlen = new PoissonDist(par.tlen-1);
lits = new StringSet(par.nitems, par.lits, tax);
par.write(pat_fp);
lits->display(pat_fp);
patterns = new StringSetIter(*lits);
LINT NTRANS=0;
for (LINT i = 0; i < par.ntrans; i ++)
{
trans = mk_tran(*patterns, (*tlen)()+1, par.lits.npats, tax);
if (par.ascii)
trans->write_asc(data_fp);
else
trans->write(data_fp);
if (trans->size() > 0) NTRANS++;//added by me: repeat if trans empty
else i--;
delete trans;
delete trans;
}
data_fp.close();
pat_fp.close();
tax_fp.close();
//added by MJZaki
if (par.ascii){
conf_fp << NTRANS << "\n";
conf_fp << par.nitems << "\n";
conf_fp << par.tlen << "\n";
}
else{
conf_fp.write((char *)&NTRANS, sizeof(LINT));
conf_fp.write((char *)&par.nitems, sizeof(LINT));
int t = (int) par.tlen;
conf_fp.write((char *)&t, sizeof(LINT));
}
conf_fp.close();
}
// Generate a transaction
//
Transaction *mk_tran(StringSetIter &lits, // table of patterns
LINT tlen, // transaction length
LINT NPATS,
Taxonomy *tax
)
{
Transaction *trans;
StringP pat;
if (tlen > Transaction::MAXNITEMS)
tlen = Transaction::MAXNITEMS; //MJZ: can't exceed nitems
trans = new Transaction(tlen);
LINT patcnt=0; //MJZ
while (trans->size() < tlen && patcnt < NPATS)
{
patcnt++;
//cout << trans->size() << " " << tlen << " HERE2\n";
pat = lits.get_pat(); // get a pattern
if ( !trans->add(*pat) ) {
// this pattern didn't fit in the transaction
lits.unget_pat();
break;
}
}
return trans;
}
// Generate Sequences
//
void gen_seq(SeqPar &par)
{
StringSet *lseq; // potentially large sequences
StringSetIter *patterns;
StringSet *lits; // potentially large itemsets
CustSeq *cust; //
PoissonDist *slen;
PoissonDist *tlen;
ofstream data_fp;
ofstream pat_fp;
ofstream conf_fp; //added by MJZaki
ofstream ntpc_fp; //added by MJZaki
srand48(0);
data_fp.open(data_file);
pat_fp.open(pat_file);
conf_fp.open(conf_file); //added by MJZaki
ntpc_fp.open(ntpc_file); //added by MJZaki
LINT *NTPC = new LINT[par.ncust]; //added by MJZaki
LINT tottrans=0;
if (data_fp.fail() || pat_fp.fail() || ntpc_fp.fail() || conf_fp.fail()) {
cerr << "Error opening output file" << endl;
exit(1);
}
slen = new PoissonDist(par.slen-1);
tlen = new PoissonDist(par.tlen-1);
lits = new StringSet(par.nitems, par.lits);
lseq = new StringSet(par.lits.npats, par.lseq, NULL, par.rept, par.rept_var);
// pat_fp << "Large Itemsets:" << endl;
// lits->write(pat_fp);
// pat_fp << endl << endl << "Sequences:" << endl;
par.write(pat_fp);
lseq->display(pat_fp, *lits);
patterns = new StringSetIter(*lseq);
LINT NCUST=0;
LINT i;
for (i = 0; i < par.ncust; i ++)
{
if (i%1000 == 0)
cout << "DONE " << i << endl;
cust = mk_seq(i+1, *patterns, *lits, (*slen)()+1, (*tlen)()+1);
if (cust->cid < par.mincustid) par.mincustid = cust->cid;
if (cust->cid > par.maxcustid) par.maxcustid = cust->cid;
if (par.ascii)
NTPC[NCUST] = cust->write_asc(data_fp);
else
NTPC[NCUST] = cust->write(data_fp);
tottrans += NTPC[NCUST];
if (NTPC[NCUST] > 0) NCUST++;//added by MJZaki: repeat if trans empty
else i--;
delete cust;
}
data_fp.close();
pat_fp.close();
//added by MJZaki
if (par.ascii){
conf_fp << NCUST << "\n";
conf_fp << par.nitems << "\n";
conf_fp << par.slen << "\n";
conf_fp << par.tlen << "\n";
conf_fp << tottrans << "\n";
conf_fp << par.mincustid << "\n";
conf_fp << par.maxcustid << "\n";
}
else{
conf_fp.write((char *)&NCUST, sizeof(LINT));
conf_fp.write((char *)&par.nitems, sizeof(LINT));
conf_fp.write((char *)&par.slen, sizeof(FLOAT));
conf_fp.write((char *)&par.tlen, sizeof(FLOAT));
conf_fp.write((char *)&tottrans, sizeof(LINT));
conf_fp.write((char *)&par.mincustid, sizeof(LINT));
conf_fp.write((char *)&par.maxcustid, sizeof(LINT));
}
conf_fp.close();
if (par.ascii){
for (i=0; i < NCUST; i++)
ntpc_fp << NTPC[i] << " ";
ntpc_fp << endl;
}
else ntpc_fp.write((char *)NTPC, NCUST*sizeof(LINT));
ntpc_fp.close();
delete [] NTPC;
}
// Generate a customer-sequence
//
CustSeq *mk_seq(Cid cid, // customer-id
StringSetIter &lseq, // table of large sequences
StringSet &lits, // table of large itemsets
LINT slen, // sequence length
LINT tlen // avg. transaction length
)
{
CustSeq *cust;
StringP pat;
cust = new CustSeq(cid, slen, tlen);
while (cust->size() < slen * tlen)
{
pat = lseq.get_pat(); // get a pattern
if ( !cust->add(*pat, lits) ) { // transaction full
lseq.unget_pat();
break;
}
}
return cust;
}