-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.cpp
400 lines (353 loc) · 10.7 KB
/
functions.cpp
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
/*
Modified by: Gabriel Pereyra and Stephen Brikiatis
Class: CSI-281-03
Assignment: Final Project
Date Assigned: 12/12/2015
Due Date: 11/16/2015
Description:
An implementaion for Apriori. Reads a file and finds correlations within the data
and then outputs that data to a text file. This system also times the data extraction
and is open to variable minimum thresholds.
Certification of Authenticity:
I certify that this is entirely my own work, except where I have given
fully-documented references to the work of others. I understand the
definition and consequences of plagiarism and acknowledge that the assessor
of this assignment may, for the purpose of assessing this assignment:
- Reproduce this assignment and provide a copy to another member of
academic staff; and/or
- Communicate a copy of this assignment to a plagiarism checking
service (which may then retain a copy of this assignment on its
database for the purpose of future plagiarism checking)
*/
#include "Header.h"
/* Author: Gabriel Pereyra
* Pre: User inputs a string
* Post: True is returned if string matches file on database, false if not.
* Purpose: To validate user input for the filename
*************************************************************************/
bool checkUserInput(string input)
{
bool validate = false;
for (int i = 0; i < DATABASE_SIZE; i++)
{
if (input == database[i])
{
validate = true;
break;
}
}
return validate;
}
/* Author: Gabriel Pereyra
* Pre: An array of correlations is populated
* Post: Updates correlation relevance depending on the occurance number
* returns false if data mining is done (not enough correlations left)
* or true if there is still mining to be done.
* Purpose: To go through the correlation array and determine which
correlations are relevant
*************************************************************************/
bool correlationRelevance(Correlation currentCorrelations[], int correlationSize, int minOccurance)
{
int count = 0;
for (int i = 0; i < correlationSize; i++)
{
if (currentCorrelations[i].getOccurance() < minOccurance)
{
currentCorrelations[i].setRelevant(false);
}
else
{
count++;
}
}
if (count <= 1)
{
return false;
}
else
{
return true;
}
}
/* Author: Gabriel Pereyra
* Pre: A correlation with updated relevance is passed in
* Post: Creates a new list of correlations from the previously relevant ones
* returns new size of array.
* Purpose: To create a new population of correlations of incrementing length
*************************************************************************/
int createCorrelations(Correlation currentCore[], int coreArraySize, int comboLength)
{
int tmpSize = 0;
int newArraySize = 0;
Correlation* tmpList = new Correlation[coreArraySize];
//make array of relevant correlations
for (int i = 0; i < coreArraySize; i++)
{
if (currentCore[i].getRelevant() == true)
{
tmpList[tmpSize] = currentCore[i];
tmpSize++;
}
}
//clear current correlation array
for (int i = 0; i < coreArraySize; i++)
{
currentCore[i].clear();
}
if (comboLength == 2) //if combonation length is 2
{
for (int i = 0; i < tmpSize - 1; i++)
{
int n = 1;
while (i + n != tmpSize - 1)
{
int firstNum = tmpList[i].getItem(0);
int secondNum = tmpList[i + n].getItem(0);
currentCore[newArraySize].addItem(firstNum);
currentCore[newArraySize].addItem(secondNum);
newArraySize++;
n++;
}
}
}
else //if combonation length is bigger than 2
{
//make new array, logic is hard
for (int i = 0; i < tmpSize - 1; i++)
{
bool check = true;//flag for staying in loop
int n = 1;//variable that determines how far to go in the array
while (check)
{
for (int k = 0; k < comboLength - 2; k++)
{
if (tmpList[i].getItem(k) != tmpList[i + n].getItem(k))
check = false;
}
if (check != false)
{
//add all items except last one
for (int j = 0; j < comboLength - 1; j++)
{
currentCore[newArraySize].addItem(tmpList[i].getItem(j));
}
//add last one
currentCore[newArraySize].addItem(tmpList[i + n].getItem(comboLength - 2));
n++;//increment check range
newArraySize++; //increment new array size
if (n + i == tmpSize) //break out of loop if out of bounds
check = false;
}
}
}
}
return newArraySize;
}
/* Author: Gabriel Pereyra
* Pre: Data mining loop has begun
* Post: Creates a new list of all possible single digit correlations
* returns new size of array.
* Purpose: To have a starting correlation list to work with
*************************************************************************/
int populateInitCorrelations(Correlation correlations[], int maxSize)
{
for (int i = 0; i < maxSize; i++)
{
correlations[i].add(i, 0);
correlations[i].setSize(1);
}
return maxSize;
}
/* Author: Gabriel Pereyra
* Pre: A transaction array with updated relevance is passed in
* Post: Updates array to only include relevant transactions
* returns new size of array
* Purpose: To prune list of transactions for efficiency
*************************************************************************/
int populateNewTransactions(Transaction newBasket[], int transactionSize)
{
Transaction *tmpList = new Transaction[transactionSize]; //tmp list to copy relevant transactions
int tmpSize = 0; //keeps track of new transactions to determine new size
//fill temp list
for (int i = 0; i < transactionSize; i++)
{
if (newBasket[i].getRelevant() == true)
{
tmpList[tmpSize] = newBasket[i];
tmpSize++;
}
}
//populate actual list
for (int j = 0; j < tmpSize; j++)
{
newBasket[j] = tmpList[j];
}
return tmpSize;
}
/* Author: Gabriel Pereyra
* Pre: A data file is passed in
* Post: Populates transaction array with data from the file
* returns new size of array
* Purpose: To transfer data from file to be mined from
*************************************************************************/
int populateWithFile(Transaction transactions[], string fileName)
{
//tmp variables for storing the data
int transNum;
int tmpItem;
int tmpSize = 0; //size of array
ifstream data;
data.open(fileName);
while (!data.eof())
{
data >> transNum;
data >> tmpItem;
transactions[transNum - 1].addItem(tmpItem); //-1 cause array positioning
if (tmpSize != transNum)
{
tmpSize++;
}
}
data.close();
return tmpSize;
}
/* Author: Gabriel Pereyra
* Pre: An updated correlation array is passed in
* Post: Prints relevant current correlations and their occurances
* on the output file
* Purpose: To print relevant correlation data to an outputfile
*************************************************************************/
void printCorrelations(Correlation currentBasket[], int size, ofstream &output)
{
int correlationNum = 1;
for (int i = 0; i < size; i++)
{
if (currentBasket[i].getRelevant() == true)
{
output << correlationNum << " [";
for (int j = 0; j < currentBasket[i].getSize(); j++)
{
output << " " << currentBasket[i].getItem(j);
}
output << " ] Occurances: ";
output << currentBasket[i].getOccurance() << " ";
output << endl;
correlationNum++;
}
}
output << endl << endl;
}
/* Author: Gabriel Pereyra
* Pre: A time double is passed in
* Post: Prints time taken to perform operation to the file
* Purpose: To print the time it takes to perform the mining to an output file
*************************************************************************/
void printTime(double time, int minOccurance, ofstream &output)
{
output << "Minimum Occurance: " << minOccurance << endl;
output << "Time Taken to Mine: " << time << " Seconds";
}
/* Author: Gabriel Pereyra
* Pre: An updated correlation array and transaction array is passed in
* Post: Transaction array is pruned so that only relevant transactions remain
* Purpose: To compare current transactions to relevant correlations to shorten and prune
* the current transaction list
*************************************************************************/
void transactionRelevance(Transaction currentTransactions[], int transactionSize, Correlation currentCorrelations[], int correlationSize)
{
for (int i = 0; i < transactionSize; i++)
{
updateRelevant(currentTransactions[i], currentCorrelations, correlationSize);
}
}
void updateRelevant(Transaction ¤tTransaction, Correlation currentCorrelations[], int correlationSize)
{
bool isRelevant;
int correlNum;
int transNum;
//loop accessing correlations in basket
for (int i = 0; i < correlationSize; i++)
{
isRelevant = false;
for (int j = 0; j < currentCorrelations[i].getSize(); j++)
{
correlNum = currentCorrelations[i].getItem(j);
for (int k = 0; k < currentTransaction.getSize(); k++)
{
transNum = currentTransaction.getItem(k);
if (correlNum == transNum)
{
isRelevant = true;
break;
}
}
if (isRelevant == true && j == currentCorrelations[i].getSize() - 1)
{
//if item was found, and is last item in correlation, set relevance
currentTransaction.setRelevant(true);
}
else if (isRelevant == false)
{
//leave correlation, move to next one
break;
}
else
{
isRelevant = false;
}
}
if (isRelevant == true)
{
break;
}
}
}
/* Author: Gabriel Pereyra
* Pre: An updated correlation array and transaction array is passed in
* Post: Occurance members of correlations in the correlation array are updated to reflect
* their occurance in the transaction array that is passed in
* Purpose: To compare correlations and transactions to determine the occurance of
* each correlation in array
*************************************************************************/
void updateOccurances(Correlation currentCorrelations[], int correlationSize, Transaction currentTransactions[], int transactionSize)
{
for (int i = 0; i < correlationSize; i++)
{
checkOccurance(currentCorrelations[i], currentTransactions, transactionSize);
}
}
void checkOccurance(Correlation ¤tCorrelation, Transaction currentTransactions[], int transactionSize)
{
bool check = false;
int correlNum;
int transNum;
for (int i = 0; i < transactionSize; i++)
{
for (int j = 0; j < currentCorrelation.getSize(); j++)
{
correlNum = currentCorrelation.getItem(j);
for (int k = 0; k < currentTransactions[i].getSize(); k++)
{
transNum = currentTransactions[i].getItem(k);
if (correlNum == transNum)
{
check = true;
break;
}
}
if (check == true && j == currentCorrelation.getSize() - 1)
{
currentCorrelation.setOccurance(currentCorrelation.getOccurance() + 1);
check = false;
}
else if (check == false)
{
break;
}
else
{
check = false;
}
}
}
}