-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbscan.cpp
2250 lines (1799 loc) · 78.3 KB
/
dbscan.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
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
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <iostream>
#include <math.h>
#include <fstream>
#include <sstream>
#include <chrono>
#include <omp.h>
#include <atomic>
#include <string>
#include <stdio.h>
#include <time.h>
#define UNDEFINED -2
#define NOISE -1
using namespace std;
using namespace std::chrono;
class Punkt{
public:
double *x;
int cluster = UNDEFINED;
int cluster2 = UNDEFINED;
int cluster3 = UNDEFINED;
void Przydziel(int);
~Punkt();
};
void Punkt::Przydziel(int N){
x = new double[N];
}
Punkt::~Punkt(){
delete [] x;
}
struct VP{
public:
int index_parent_node; // Index wezla nadrzednego
int index; // Index w tablicy wejsciowej
int L_kid = -1;
int R_kid = -1;
int L_kid_N = 0;
int R_kid_N = 0;
int l_r = 0; // Znak czy Lewo(-1) / Prawo(1) => Left = -1 / Right = 1
double mu = 0.0; // Mediana odleglosci od punktu do pozostalych
double left_bound = 0.0;
double right_bound = 0.0;
};
// Get current date/time, format is YYYY-MM-DD.HH:mm:ss
const std::string currentDateTime() {
time_t now = time(0);
struct tm tstruct;
char buf[80];
tstruct = *localtime(&now);
strftime(buf, sizeof(buf), "%Y-%m-%dT%X", &tstruct);
return buf;
}
int *Negative_Global;
void Import_CSV_Metadata_SKRYPT(Punkt *pkt, int &ile_linii, int &ile_x, string &file_in, int &start_ind, int &start_linia, char *plik_in, char FirstRowYN, char FirstColYN);
void Import_CSV_Metadata(Punkt *pkt, int &ile_linii, int &ile_x, string &file_in, int &start_ind, int &start_linia);
void Import_CSV_File(Punkt *pkt, int ile_linii, int ile_x, string file_in, int start_ind, int start_linia);
void Show_Imported_First3(Punkt *pkt, int ile_linii, int ile_x);
void Show_Imported_All(Punkt *pkt, int ile_linii, int ile_x);
void Show_Clustered(Punkt *pkt, int ile_linii, int ile_x);
void Show_Clustered_All(Punkt *pkt, int ile_linii, int ile_x);
void Show_C_All(Punkt *pkt, int ile_linii, int ile_x, int DBSCAN_yes, int VP1_yes, int VP2_yes);
void Show_C_All_SKRYPT(Punkt *pkt, int ile_linii, int ile_x, int DBSCAN_yes, int VP1_yes, int VP2_yes, int *noise, int *cc);
void Save_File_SKRYPT(Punkt *pkt, int ile_linii, int ile_x, char *plik_out, int algoyrtm);
void Save_File(Punkt *pkt, int ile_linii, int ile_x);
void Clear_N_S(int *N_tab, int *S_tab, int ile_linii);
void Clear_Cluster(Punkt *pkt, int ile_linii);
void Show_VP_Tree(VP *VP_tree, int Tree_Counter);
void Save_VP_Tree(VP *VP_tree, int Tree_Counter);
double DistFunc(Punkt *pkt1, Punkt *pkt2, int ile_x);
int RangeQuery(Punkt *pkt, int *N_tab, int ile_linii, int Qindex, double Eps, int ile_x);
int S_N_Merge(int *S_tab, int *N_tab, int S_licznik, int N_licznik, int ile_linii);
void DBSCAN_Origin(Punkt *pkt, int *S_tab, int *N_tab, double Eps, int ile_linii, int minN, int C, int ile_x);
//void DBSCAN_Origin_OMP(Punkt *pkt, int *S_tab, int *N_tab, double Eps, int ile_linii, int minN, int C, int ile_x);
double RandVal(double low,double high);
long iRandVal(long low,long high);
double Mediana(Punkt *pkt, double Mediana_tab[], int P_tab[], int D_tab[], int D_rozmiar, int ile_x, int p_counter);
double Mediana_VAR(Punkt *pkt, double Mediana_tab[], int P_tab[], int D_tab[], int D_rozmiar, int ile_x, int p_counter);
int Select_VP(Punkt *pkt, int *S_VP_tab, int S_rozmiar, double P_proc_S, double D_proc_S, int ile_x);
void Make_VP_Tree(VP *VP_tree, Punkt *pkt, int *S_VP_tab, int S_rozmiar, double P_proc_S, double D_proc_S, int ile_x, int *Tree_Counter, int lr, int index_parentnode);
void Make_VP_Tree_2(VP *VP_tree, Punkt *pkt, int *S_VP_tab, int S_rozmiar, double P_proc_S, double D_proc_S, int ile_x, int *Tree_Counter, int lr, int index_parentnode);
void kNN_TreeDist(VP *VP_tree, Punkt *pkt, int *N_tab, int ile_linii, int Qindex, double Eps, int minN, int ile_x, int TC, int *kNN);
int RangeQuery_Tree(VP *VP_tree,Punkt *pkt, int *N_tab, int ile_linii, int Qindex, double Eps, int minN, int ile_x);
void DBSCAN_VP_TREE(VP *VP_tree, Punkt *pkt, int *S_tab, int *N_tab, double Eps, int ile_linii, int minN, int C, int ile_x);
int RangeQuery_Tree_2(VP *VP_tree,Punkt *pkt, int *N_tab, int ile_linii, int Qindex, double Eps, int minN, int ile_x);
void DBSCAN_VP_TREE_2(VP *VP_tree, Punkt *pkt, int *S_tab, int *N_tab, double Eps, int ile_linii, int minN, int C, int ile_x);
// --- MAIN --- MAIN --- MAIN --- MAIN --- MAIN --- MAIN --- MAIN --- MAIN --- MAIN --- MAIN --- MAIN --- MAIN --- MAIN --- MAIN --- MAIN ---//
int main(int argc, char *argv[])
{
ofstream logsfile;
// logsfile.open("logs.txt", std::ios_base::app); // append instead of overwrite
// logsfile << currentDateTime() << ",";
// logsfile.close();
cout << endl << "Hello, This is DBSCAN (VP-TREE) program" << endl;
Punkt *pkt; // Wskaznik na "Punkt" --> potem przypisana tablica pod wskaznik
int *N_tab; // Tablica z indeksami sasiadow przy RangeQuery
int *S_tab; // Tablica "Seed" --> rozszerzajacy sie obszar sasiadow wokol punktu P
int ile_linii = 0; // Number of Rows - Number of points
int ile_x = 0; // Number of Columns - Number of co-ordinates for every point
string file_in; // String for File to Import - Variable to carry information between functions
int start_ind = 0; // Flag for import - take first COLUMN - Variable to carry information between functions
int start_linia = 0; // Flag for import - take first ROW - Variable to carry information between functions
for (int i = 0; i < argc; i++)
{
cout << i << ": " << argv[i] << endl;
}
if (argc > 1) // ------------------------------------------------------------------------------- // WERSJA DO SKRYPTU
{
// Import Metadata from CSV File
Import_CSV_Metadata_SKRYPT(pkt, ile_linii, ile_x, file_in, start_ind, start_linia, argv[1], argv[2][0], argv[3][0]);
// Allocation Memory
pkt = new Punkt[ile_linii]; // Memory Initialization
for(int i=0; i<ile_linii; i++)
{
pkt[i].Przydziel(ile_x); // Memory Initialization
}
// Import Data from CSV File
Import_CSV_File(pkt, ile_linii, ile_x, file_in, start_ind, start_linia);
// Show first 3 rows from memory - Imported Structure
Show_Imported_First3(pkt, ile_linii, ile_x);
// Show ALL Imported Data
// Show_Imported_All(pkt, ile_linii, ile_x);
int DBSCAN_yes = 0;
int VP1_yes = 0;
int VP2_yes = 0;
if (atoi(argv[4]) == 1)
{
DBSCAN_yes = 1;
}
if (atoi(argv[4]) == 2)
{
VP1_yes = 1;
}
if (atoi(argv[4]) == 3)
{
VP2_yes = 1;
}
cout << "Calculate: Original DBSCAN ? [y/n] : ";
cout << DBSCAN_yes << endl;
cout << "Calculate: VP_Tree (ver 1) ? [y/n] : ";
cout << VP1_yes << endl;
cout << "Calculate: VP_Tree (ver 2) ? [y/n] : ";
cout << VP2_yes << endl;
int C = 0; // Cluster Counter
double Eps = atof(argv[5]); // Max distance between points
int minN = atoi(argv[6]); // Minimal number of Neighbors
double P_proc_S = atof(argv[7]);
double D_proc_S = atof(argv[8]);
cout << endl << "Epsilon (double): ";
cout << Eps << endl;
cout << "Minimal number of Neighbors = minN (int)): ";
cout << minN << endl;
cout << "P Random Sample of S (0-1): " ;
cout << P_proc_S << endl;
cout << "D Random Sample of S (0-1): " ;
cout << D_proc_S << endl;
logsfile.open("logs.txt", std::ios_base::app); // append instead of overwrite
logsfile << currentDateTime() << "," << argv[1] << "," << argv[9] << "," << argv[4] << "," << argv[5] << "," << argv[6] << "," << argv[7] << "," << argv[8] << ",";
logsfile.close();
cout << endl << currentDateTime() << "," << argv[1] << "," << argv[9] << "," << argv[4] << "," << argv[5] << "," << argv[6] << "," << argv[7] << "," << argv[8] << "," << endl;
Negative_Global = new int[ile_linii];
N_tab = new int[ile_linii]; // Neighbors tab - index for N in pkt tab
S_tab = new int[ile_linii]; // Seed tab - index for S in pkt tab
for(int i=0; i<ile_linii; i++)
{
Negative_Global[i] = -1; // Czyszczenie listy indeksow sasiadow
}
double Time_Build = 0.0;
double Time_Search = 0.0;
double Time_All = 0.0;
cout << endl << "* Start * currentDateTime() = " << currentDateTime() << endl;
// --- Original DBSCAN ------------------------------------------------------------------------------------------------------------------------------- //
if(DBSCAN_yes == 1)
{
Clear_N_S(N_tab, S_tab, ile_linii); // Clear Neighbors Tab && Seed Tab
auto start = high_resolution_clock::now(); // Time - START
// DBSCAN --- START //
DBSCAN_Origin(pkt, S_tab, N_tab, Eps, ile_linii, minN, C, ile_x); // DBSCAN - Origin - Function
// DBSCAN --- END //
auto stop = high_resolution_clock::now(); // Time - STOP
auto duration = duration_cast<microseconds>(stop - start); // Time - Caltulation
// cout << endl << "DBSCAN Time: " << duration.count() << " us" << endl; // Time - show Function duration
cout << endl << "DBSCAN Time: " << (double) duration.count() / 1000000.0 << " s" << endl;
Time_Search = (double) duration.count() / 1000000.0;
Time_All = Time_Search;
}
// Show every point and his Cluster number
// Show_Clustered(pkt, ile_linii, ile_x);
// --- VP-TREE - ver 1 - Simple Condition ------------------------------------------------------------------------------------------------------------- //
int Tree_Counter = -1; //
int L_R = 0; // Nie wiadomo czy lewe/prawe bo jestesmy na poczatku drzewa
int index_parentnode = -1; // Nie ma wczesniejszego "Rodzica"
int *S_VP_tab;
S_VP_tab = new int[ile_linii];
for (int i = 0; i < ile_linii; i++) // Wpisywanie indeksow do startowej tablicy S do tworzenia drzewa VP-Tree
{
S_VP_tab[i] = i;
}
// DBSCAN VP_Tree - ver 1 ----------
if(VP1_yes == 1)
{
Clear_N_S(N_tab, S_tab, ile_linii); // Clear Neighbors Tab && Seed Tab
VP *VP_tree; // Memory for VP_Tree
VP_tree = new VP[ile_linii];
Tree_Counter = -1; //
L_R = 0; // Nie wiadomo czy lewe/prawe bo jestesmy na poczatku drzewa
index_parentnode = -1; // Nie ma wczesniejszego "Rodzica"
C = 0;
auto start2 = high_resolution_clock::now(); // Time - START
// VP_TREE - CREATE - START //
Make_VP_Tree(VP_tree, pkt, S_VP_tab, ile_linii, P_proc_S, D_proc_S, ile_x, &Tree_Counter, L_R, index_parentnode);
// VP_TREE - CREATE - END //
auto stop2 = high_resolution_clock::now(); // Time - STOP
auto duration2 = duration_cast<microseconds>(stop2 - start2); // Time - Caltulation
// cout << endl << "MAKE_VP_TREE Time: " << duration2.count() << " us" << endl; // Time - show Function duration
cout << endl << "MAKE_VP_TREE Time: " << (double)duration2.count() / 1000000.0 << " s" << endl; // Time - show Function duration
auto start1 = high_resolution_clock::now(); // Time - START
// DBSCAN_VP-TREE --- START //
DBSCAN_VP_TREE(VP_tree,pkt, S_tab, N_tab, Eps, (Tree_Counter+1), minN, C, ile_x); // DBSCAN - VP-Tree - ver 1 - Function
// DBSCAN_VP-TREE --- END //
auto stop1 = high_resolution_clock::now(); // Time - STOP
auto duration1 = duration_cast<microseconds>(stop1 - start1); // Time - Caltulation
//cout << "DBSCAN_VP_TREE Time: " << duration1.count() << " us" << endl; // Time - show Function duration
cout << "DBSCAN_VP_TREE Time: " << (double)duration1.count() / 1000000.0 << " s" << endl; // Time - show Function duration
cout << "-> DBSCAN_VP_TREE Time (SUM): " << (double)(duration1.count()+duration2.count()) / 1000000.0 << " s" << endl;
// Show_VP_Tree(VP_tree, Tree_Counter);
// Save_VP_Tree(VP_tree, Tree_Counter);
Time_Build = (double) duration2.count() / 1000000.0;
Time_Search = (double) duration1.count() / 1000000.0;
Time_All = (double) (duration1.count()+duration2.count()) / 1000000.0;
delete [] VP_tree;
}
// --- VP-TREE --------------------------------------------------------------------------------------------------------------- //
// --- VP-TREE - ver 2 - Improved Condition ------------------------------------------------------------------------------------------------------------- //
if(VP2_yes == 1)
{
Clear_N_S(N_tab, S_tab, ile_linii); // Clear Neighbors Tab && Seed Tab
for (int i = 0; i < ile_linii; i++) // Wpisywanie indeksow do startowej tablicy S do tworzenia drzewa VP-Tree
{
S_VP_tab[i] = i;
}
VP *VP_tree2; // Memory for VP_Tree
VP_tree2 = new VP[ile_linii];
Tree_Counter = -1; //
L_R = 0; // Nie wiadomo czy lewe/prawe bo jestesmy na poczatku drzewa
index_parentnode = -1; // Nie ma wczesniejszego "Rodzica"
C = 0;
auto start4 = high_resolution_clock::now(); // Time - START
// VP_TREE - CREATE - START //
Make_VP_Tree_2(VP_tree2, pkt, S_VP_tab, ile_linii, P_proc_S, D_proc_S, ile_x, &Tree_Counter, L_R, index_parentnode);
// VP_TREE - CREATE - END //
auto stop4 = high_resolution_clock::now(); // Time - STOP
auto duration4 = duration_cast<microseconds>(stop4 - start4); // Time - Caltulation
//cout << endl << "MAKE_VP_TREE_2 Time: " << duration4.count() << " us" << endl; // Time - show Function duration
cout << endl << "MAKE_VP_TREE_2 Time: " << (double)duration4.count() / 1000000.0 << " s" << endl; // Time - show Function duration
auto start3 = high_resolution_clock::now(); // Time - START
// DBSCAN_VP-TREE --- START //
DBSCAN_VP_TREE_2(VP_tree2,pkt, S_tab, N_tab, Eps, (Tree_Counter+1), minN, C, ile_x); // DBSCAN - VP-Tree - ver 2 - Function
// DBSCAN_VP-TREE --- END //
auto stop3 = high_resolution_clock::now(); // Time - STOP
auto duration3 = duration_cast<microseconds>(stop3 - start3); // Time - Caltulation
//cout << "DBSCAN_VP_TREE_2 Time: " << duration3.count() << " us" << endl; // Time - show Function duration
cout << "DBSCAN_VP_TREE_2 Time: " << (double)duration3.count() / 1000000.0 << " s" << endl; // Time - show Function duration
cout << "-> DBSCAN_VP_TREE_2 Time (SUMA): " << (double)(duration3.count()+duration4.count()) / 1000000.0 << " s" << endl; // Time - show Function duration
// Show_VP_Tree(VP_tree2, Tree_Counter);
// Save_VP_Tree(VP_tree2, Tree_Counter);
Time_Build = (double) duration4.count() / 1000000.0;
Time_Search = (double) duration3.count() / 1000000.0;
Time_All = (double) (duration3.count()+duration4.count()) / 1000000.0;
delete [] VP_tree2;
}
// --- VP-TREE ver 2 --------------------------------------------------------------------------------------------------------------- //
cout << endl << "* End * currentDateTime() = " << currentDateTime() << endl;
int noise = 0;
int cc = 0;
if(DBSCAN_yes == 1 || VP1_yes == 1 || VP2_yes == 1)
{
// Show_Clustered_All(pkt, ile_linii, ile_x);
Show_C_All_SKRYPT(pkt, ile_linii, ile_x, DBSCAN_yes, VP1_yes, VP2_yes, &noise, &cc);
Save_File_SKRYPT(pkt, ile_linii, ile_x, argv[9], atoi(argv[4])); // Saving CSV - with Cluster data
}
logsfile.open("logs.txt", std::ios_base::app); // append instead of overwrite
logsfile << currentDateTime() << "," << Time_Build << "," << Time_Search << "," << Time_All << "," << noise << "," << cc-1 << endl;
logsfile.close();
cout << currentDateTime() << "," << Time_Build << "," << Time_Search << "," << Time_All << "," << noise << "," << cc-1 << endl << endl;
// delete - Destroy array/pointers
delete [] S_VP_tab;
delete [] Negative_Global;
delete [] S_tab;
delete [] N_tab;
delete [] pkt;
}else{ // -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- // WERSJA DLA UZYTKOWNIKA // ------------------------------------------------------------------------------- //
// -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- // WERSJA DLA UZYTKOWNIKA // ------------------------------------------------------------------------------- //
// -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- // WERSJA DLA UZYTKOWNIKA // ------------------------------------------------------------------------------- //
// Import Metadata from CSV File
Import_CSV_Metadata(pkt, ile_linii, ile_x, file_in, start_ind, start_linia);
// Allocation Memory
pkt = new Punkt[ile_linii]; // Memory Initialization
for(int i=0; i<ile_linii; i++)
{
pkt[i].Przydziel(ile_x); // Memory Initialization
}
// Import Data from CSV File
Import_CSV_File(pkt, ile_linii, ile_x, file_in, start_ind, start_linia);
// Show first 3 rows from memory - Imported Structure
Show_Imported_First3(pkt, ile_linii, ile_x);
// Show ALL Imported Data
// Show_Imported_All(pkt, ile_linii, ile_x);
int DBSCAN_yes = 0;
int VP1_yes = 0;
int VP2_yes = 0;
int flaga_wybor = 1;
char DBSCAN_if = 0;
while(flaga_wybor) // While - for: Original DBSCAN - y / n
{
cout << "Calculate: Original DBSCAN ? [y/n] : ";
cin >> DBSCAN_if;
switch (DBSCAN_if)
{
case 'y':
DBSCAN_yes = 1;
flaga_wybor = 0;
break;
case 'n':
DBSCAN_yes = 0;
flaga_wybor = 0;
break;
default:
cout << "Wrong character" << endl;
break;
}
}
flaga_wybor = 1;
while(flaga_wybor) // While - for: DBSCAN with VP_Tree (ver 1) - y / n
{
cout << "Calculate: DBSCAN + VP_Tree (ver 1) ? [y/n] : ";
cin >> DBSCAN_if;
switch (DBSCAN_if)
{
case 'y':
VP1_yes = 1;
flaga_wybor = 0;
break;
case 'n':
VP1_yes = 0;
flaga_wybor = 0;
break;
default:
cout << "Wrong character" << endl;
break;
}
}
flaga_wybor = 1;
while(flaga_wybor) // While - for: DBSCAN with VP_Tree (ver 2) - y / n
{
cout << "Calculate: DBSCAN + VP_Tree (ver 2) ? [y/n] : ";
cin >> DBSCAN_if;
switch (DBSCAN_if)
{
case 'y':
VP2_yes = 1;
flaga_wybor = 0;
break;
case 'n':
VP2_yes = 0;
flaga_wybor = 0;
break;
default:
cout << "Wrong character" << endl;
break;
}
}
int C = 0; // Cluster Counter
double Eps = 0.5; // Max distance between points
int minN = 4; // Minimal number of Neighbors
double P_proc_S = 0.0;
double D_proc_S = 0.0;
if(DBSCAN_yes == 1 || VP1_yes == 1 || VP2_yes == 1)
{
cout << endl << "Epsilon (double): ";
cin >> Eps;
do{
cout << "Minimal number of Neighbors = minN (int)): ";
cin >> minN;
}while(minN < 1);
}else{
cout << "Oh, you don't want to perform any clustering operation. Bye Bye!" << endl;
delete [] pkt;
}
if(VP1_yes == 1 || VP2_yes == 1)
{
do{
cout << "P Random Sample of S (0-1): " ;
cin >> P_proc_S;
}while(P_proc_S < 0.0 || P_proc_S > 1.0);
do{
cout << "D Random Sample of S (0-1): " ;
cin >> D_proc_S;
}while(D_proc_S < 0.0 || D_proc_S > 1.0);
}
Negative_Global = new int[ile_linii];
N_tab = new int[ile_linii]; // Neighbors tab - index for N in pkt tab
S_tab = new int[ile_linii]; // Seed tab - index for S in pkt tab
for(int i=0; i<ile_linii; i++)
{
Negative_Global[i] = -1; // Czyszczenie listy indeksow sasiadow
}
// --- Original DBSCAN ------------------------------------------------------------------------------------------------------------------------------- //
if(DBSCAN_yes == 1)
{
Clear_N_S(N_tab, S_tab, ile_linii); // Clear Neighbors Tab && Seed Tab
auto start = high_resolution_clock::now(); // Time - START
// DBSCAN --- START //
DBSCAN_Origin(pkt, S_tab, N_tab, Eps, ile_linii, minN, C, ile_x); // DBSCAN - Origin - Function
// DBSCAN --- END //
auto stop = high_resolution_clock::now(); // Time - STOP
auto duration = duration_cast<microseconds>(stop - start); // Time - Caltulation
cout << endl << "DBSCAN Time: " << duration.count() << " us" << endl; // Time - show Function duration
cout << "DBSCAN Time: " << (double) duration.count() / 1000000.0 << " s" << endl;
}
// Show every point and his Cluster number
// Show_Clustered(pkt, ile_linii, ile_x);
// --- VP-TREE - ver 1 - Simple Condition ------------------------------------------------------------------------------------------------------------- //
int Tree_Counter = -1; //
int L_R = 0; // Nie wiadomo czy lewe/prawe bo jestesmy na poczatku drzewa
int index_parentnode = -1; // Nie ma wczesniejszego "Rodzica"
int *S_VP_tab;
S_VP_tab = new int[ile_linii];
for (int i = 0; i < ile_linii; i++) // Wpisywanie indeksow do startowej tablicy S do tworzenia drzewa VP-Tree
{
S_VP_tab[i] = i;
}
// DBSCAN VP_Tree - ver 1 ----------
if(VP1_yes == 1)
{
Clear_N_S(N_tab, S_tab, ile_linii); // Clear Neighbors Tab && Seed Tab
VP *VP_tree; // Memory for VP_Tree
VP_tree = new VP[ile_linii];
Tree_Counter = -1; //
L_R = 0; // Nie wiadomo czy lewe/prawe bo jestesmy na poczatku drzewa
index_parentnode = -1; // Nie ma wczesniejszego "Rodzica"
C = 0;
auto start2 = high_resolution_clock::now(); // Time - START
// VP_TREE - CREATE - START //
Make_VP_Tree(VP_tree, pkt, S_VP_tab, ile_linii, P_proc_S, D_proc_S, ile_x, &Tree_Counter, L_R, index_parentnode);
// VP_TREE - CREATE - END //
auto stop2 = high_resolution_clock::now(); // Time - STOP
auto duration2 = duration_cast<microseconds>(stop2 - start2); // Time - Caltulation
cout << endl << "MAKE_VP_TREE Time: " << duration2.count() << " us" << endl; // Time - show Function duration
cout << "MAKE_VP_TREE Time: " << (double)duration2.count() / 1000000.0 << " s" << endl; // Time - show Function duration
auto start1 = high_resolution_clock::now(); // Time - START
// DBSCAN_VP-TREE --- START //
DBSCAN_VP_TREE(VP_tree,pkt, S_tab, N_tab, Eps, (Tree_Counter+1), minN, C, ile_x); // DBSCAN - VP-Tree - ver 1 - Function
// DBSCAN_VP-TREE --- END //
auto stop1 = high_resolution_clock::now(); // Time - STOP
auto duration1 = duration_cast<microseconds>(stop1 - start1); // Time - Caltulation
cout << "DBSCAN_VP_TREE Time: " << duration1.count() << " us" << endl; // Time - show Function duration
cout << "DBSCAN_VP_TREE Time: " << (double)duration1.count() / 1000000.0 << " s" << endl; // Time - show Function duration
cout << "DBSCAN_VP_TREE Time (SUMA): " << (double)(duration1.count()+duration2.count()) / 1000000.0 << " s" << endl;
// Show_VP_Tree(VP_tree, Tree_Counter);
Save_VP_Tree(VP_tree, Tree_Counter);
delete [] VP_tree;
}
// --- VP-TREE --------------------------------------------------------------------------------------------------------------- //
// --- VP-TREE - ver 2 - Improved Condition ------------------------------------------------------------------------------------------------------------- //
if(VP2_yes == 1)
{
Clear_N_S(N_tab, S_tab, ile_linii); // Clear Neighbors Tab && Seed Tab
for (int i = 0; i < ile_linii; i++) // Wpisywanie indeksow do startowej tablicy S do tworzenia drzewa VP-Tree
{
S_VP_tab[i] = i;
}
VP *VP_tree2; // Memory for VP_Tree
VP_tree2 = new VP[ile_linii];
Tree_Counter = -1; //
L_R = 0; // Nie wiadomo czy lewe/prawe bo jestesmy na poczatku drzewa
index_parentnode = -1; // Nie ma wczesniejszego "Rodzica"
C = 0;
auto start4 = high_resolution_clock::now(); // Time - START
// VP_TREE - CREATE - START //
Make_VP_Tree_2(VP_tree2, pkt, S_VP_tab, ile_linii, P_proc_S, D_proc_S, ile_x, &Tree_Counter, L_R, index_parentnode);
// VP_TREE - CREATE - END //
auto stop4 = high_resolution_clock::now(); // Time - STOP
auto duration4 = duration_cast<microseconds>(stop4 - start4); // Time - Caltulation
cout << endl << "MAKE_VP_TREE_2 Time: " << duration4.count() << " us" << endl; // Time - show Function duration
cout << "MAKE_VP_TREE_2 Time: " << (double)duration4.count() / 1000000.0 << " s" << endl; // Time - show Function duration
auto start3 = high_resolution_clock::now(); // Time - START
// DBSCAN_VP-TREE --- START //
DBSCAN_VP_TREE_2(VP_tree2,pkt, S_tab, N_tab, Eps, (Tree_Counter+1), minN, C, ile_x); // DBSCAN - VP-Tree - ver 2 - Function
// DBSCAN_VP-TREE --- END //
auto stop3 = high_resolution_clock::now(); // Time - STOP
auto duration3 = duration_cast<microseconds>(stop3 - start3); // Time - Caltulation
cout << "DBSCAN_VP_TREE_2 Time: " << duration3.count() << " us" << endl; // Time - show Function duration
cout << "DBSCAN_VP_TREE_2 Time: " << (double)duration3.count() / 1000000.0 << " s" << endl; // Time - show Function duration
cout << "DBSCAN_VP_TREE_2 Time (SUMA): " << (double)(duration3.count()+duration4.count()) / 1000000.0 << " s" << endl; // Time - show Function duration
// Show_VP_Tree(VP_tree2, Tree_Counter);
Save_VP_Tree(VP_tree2, Tree_Counter);
delete [] VP_tree2;
}
// --- VP-TREE ver 2 --------------------------------------------------------------------------------------------------------------- //
if(DBSCAN_yes == 1 || VP1_yes == 1 || VP2_yes == 1)
{
// Show_Clustered_All(pkt, ile_linii, ile_x);
Show_C_All(pkt, ile_linii, ile_x, DBSCAN_yes, VP1_yes, VP2_yes);
Save_File(pkt, ile_linii, ile_x); // Saving CSV - with Cluster data
}
// delete - Destroy array/pointers
delete [] S_VP_tab;
delete [] Negative_Global;
delete [] S_tab;
delete [] N_tab;
delete [] pkt;
}
}
// --- End of Main --- //
// --- FUNCTION --- FUNCTION --- FUNCTION --- FUNCTION --- FUNCTION --- FUNCTION --- FUNCTION --- FUNCTION --- FUNCTION --- FUNCTION --- FUNCTION --- FUNCTION --- FUNCTION --- FUNCTION --- FUNCTION --- FUNCTION --- FUNCTION --- FUNCTION --- FUNCTION --- //
void Import_CSV_Metadata_SKRYPT(Punkt *pkt, int &ile_linii, int &ile_x, string &file_in, int &start_ind, int &start_linia, char *plik_wejscie, char FirstRowYN, char FirstColYN)
{
string file1 = plik_wejscie; // Name of File to Import
string line, word, temp, struktura; // Temporary strings for import
//char FirstColYN, FirstRowYN; // FirstColYN - char for: Import First Column? // FirstRowYN - char for: Import First Row?
int index = 0; // Counter for every "word" in imported line
int counter_tab = 0; // Counter for "x" in every point
int start_index = 0; // Start index - if User don't want First Column
int ktora_linia = 0; // Row Counter for importing
int exist = 0;
do{
//cout << "What file you want? : ";
//cin >> file1;
if (FILE *file = fopen(file1.c_str(), "r")) {
fclose(file);
exist = 1;
}else{
cout << "Warning: Can't find this file" << endl;
}
}while(exist != 1);
cout << "--- Open file: " << file1 << " --> ";
fstream plik; // FStream for import file
plik.open(file1); // Open import file
if(plik.good())
{
//cout << plik.tellg() << endl;
while(!plik.eof())
{
while(getline(plik, line, '\n')) // Calculate how many lines are in the file
{
ile_linii++;
}
}
plik.clear();
plik.seekg(0, ios::beg); // Clear buffer and go to the beginning of the file
for(int i = 0; i < 3; i++) // Import first 3 lines from file - to show them later
{
ile_x = 0;
getline(plik, line, '\n');
stringstream sstream_temp(line);
while(getline(sstream_temp, word, ','))
{
// cout << word << " ";
struktura = struktura + word + " | ";
ile_x++;
}
struktura = struktura + '\n';
}
// cout << "Ile kolumn: " << ile_x << endl;
// cout << "Wierszy w pliku: " << ile_linii << endl;
cout << endl << "***" << endl << "File Structure: " << endl << "Columns: " << ile_x << endl << "Rows: " << ile_linii << endl << "Structure: " << endl << struktura << "***" << endl; // Show - File Structure
cout << "Import 1st row = Header? [y/n] : ";
cout << FirstRowYN << endl;
switch (FirstRowYN)
{
case 'y':
ktora_linia = 0;
break;
case 'n':
ktora_linia = 1;
ile_linii--;
break;
default:
cout << "Wrong character" << endl;
break;
}
cout << "Import 1st column? [y/n] : ";
cout << FirstColYN << endl;
switch (FirstColYN)
{
case 'y':
start_index = 0;
break;
case 'n':
start_index = 1;
ile_x--;
break;
default:
cout << "Wrong character" << endl;
break;
}
}else{
cout << "Can't open file" << endl;
}
file_in = file1;
start_linia = ktora_linia;
start_ind = start_index;
}
// --- WERSJA DLA UZYTKOWNIKA ---
void Import_CSV_Metadata(Punkt *pkt, int &ile_linii, int &ile_x, string &file_in, int &start_ind, int &start_linia)
{
string file1 = "DataCpp.csv"; // Name of File to Import
string line, word, temp, struktura; // Temporary strings for import
char FirstColYN, FirstRowYN; // FirstColYN - char for: Import First Column? // FirstRowYN - char for: Import First Row?
int index = 0; // Counter for every "word" in imported line
int counter_tab = 0; // Counter for "x" in every point
int start_index = 0; // Start index - if User don't want First Column
int ktora_linia = 0; // Row Counter for importing
int exist = 0;
do{
cout << "What file you want? : ";
cin >> file1;
if (FILE *file = fopen(file1.c_str(), "r")) {
fclose(file);
exist = 1;
}else{
cout << "Warning: Can't find this file" << endl;
}
}while(exist != 1);
cout << "--- Open file: " << file1 << " --> ";
fstream plik; // FStream for import file
plik.open(file1); // Open import file
if(plik.good())
{
//cout << plik.tellg() << endl;
while(!plik.eof())
{
while(getline(plik, line, '\n')) // Calculate how many lines are in the file
{
ile_linii++;
}
}
plik.clear();
plik.seekg(0, ios::beg); // Clear buffer and go to the beginning of the file
for(int i = 0; i < 3; i++) // Import first 3 lines from file - to show them later
{
ile_x = 0;
getline(plik, line, '\n');
stringstream sstream_temp(line);
while(getline(sstream_temp, word, ','))
{
// cout << word << " ";
struktura = struktura + word + " | ";
ile_x++;
}
struktura = struktura + '\n';
}
// cout << "Ile kolumn: " << ile_x << endl;
// cout << "Wierszy w pliku: " << ile_linii << endl;
cout << endl << "***" << endl << "File Structure: " << endl << "Columns: " << ile_x << endl << "Rows: " << ile_linii << endl << "Structure: " << endl << struktura << "***" << endl; // Show - File Structure
int flaga_wybor = 1; // Flag reset
while(flaga_wybor) // While - for: First Row Import - y / n
{
cout << "Import 1st row = Header? [y/n] : ";
cin >> FirstRowYN;
switch (FirstRowYN)
{
case 'y':
ktora_linia = 0;
flaga_wybor = 0;
break;
case 'n':
ktora_linia = 1;
ile_linii--;
flaga_wybor = 0;
break;
default:
cout << "Wrong character" << endl;
break;
}
}
flaga_wybor = 1; // Flag reset
while(flaga_wybor) // While - for: First Column Import - y / n
{
cout << "Import 1st column? [y/n] : ";
cin >> FirstColYN;
switch (FirstColYN)
{
case 'y':
start_index = 0;
flaga_wybor = 0;
break;
case 'n':
start_index = 1;
ile_x--;
flaga_wybor = 0;
break;
default:
cout << "Wrong character" << endl;
break;
}
}
}else{
cout << "Can't open file" << endl;
}
file_in = file1;
start_linia = ktora_linia;
start_ind = start_index;
}
// Import File
void Import_CSV_File(Punkt *pkt, int ile_linii, int ile_x, string file_in, int start_ind, int start_linia)
{
// string file1 = file_in; // Name of File to Import
string line, word; // Temporary strings for import
int index = 0; // Counter for every "word" in imported line
int counter_tab = 0; // Counter for "x" in every point
int start_index = start_ind; // Start index - if User don't want First Column
int ktora_linia = start_linia; // Row Counter for importing
fstream plik; // FStream for import file
plik.open(file_in); // Open import file
if(plik.good())
{
plik.clear();
plik.seekg(0, ios::beg); // Clear buffer and go to the beginning of the file
if(ktora_linia == 1){ // Get Line if FirstRowYN is NO - dont import first line
getline(plik, line, '\n');
ktora_linia = 0; // Counter reset
}
while(!plik.eof())
{
while(getline(plik, line, '\n')) // Get every line from file
{
stringstream sstream(line);
index = 0;
counter_tab = 0;
// cout<<ktora_linia<<": "<<endl; // Show - which point is being imported
while (getline(sstream, word, ',')) // Get every "word" (number) from line
{
if(index>=start_index)
{
pkt[ktora_linia].x[counter_tab] = atof(word.c_str()); // Conversion string to double
// cout<<index<<" "<<counter_tab<<" "<<word <<" = " <<pkt[ktora_linia].x[counter_tab] <<endl; // Show - data from memory while importing
counter_tab++;
}
index++;
}
ktora_linia++;
}
}
cout << "Close file: Imported "<<ile_x<<"D data" << endl;
}else{
cout << "Can't open file" << endl;
}
plik.close();
}
void Show_Imported_All(Punkt *pkt, int ile_linii, int ile_x)
{
cout << endl << "***" << endl << "ALL - Imported Structure in Memory: " << endl << "Columns: " << ile_x << endl << "Rows: " << ile_linii << endl << "Structure in Memory: " << endl;
for(int i=0; i<ile_linii; i++)
{
cout<<i<<": ";
for (int j = 0; j < ile_x; j++)
{
cout << "x[" << j << "] = " << pkt[i].x[j] << " | ";
}
cout << endl;
}
}
void Show_Imported_First3(Punkt *pkt, int ile_linii, int ile_x)
{
cout << endl << "***" << endl << "First3 - Imported Structure in Memory: " << endl << "Columns: " << ile_x << endl << "Rows: " << ile_linii << endl << "Structure in Memory: " << endl; // Show - Imported Structure in Memory
for(int i=0; i<3; i++)
{
cout << i << ": ";
for (int j = 0; j < ile_x; j++)
{
cout << "x[" << j << "] = " << pkt[i].x[j] << " | ";
}
cout << endl;
}
cout << "***" << endl;
}
void Show_Clustered(Punkt *pkt, int ile_linii, int ile_x)
{
cout << endl;
for(int i=0; i<ile_linii; i++)
{
cout << i << ": ";
for (int j = 0; j < ile_x; j++)
{
cout << pkt[i].x[j] << ",";
}
cout << pkt[i].cluster << endl;
}
}
void Show_Clustered_All(Punkt *pkt, int ile_linii, int ile_x)
{
cout << endl;
for(int i=0; i<ile_linii; i++)
{
cout << i << ": ";
for (int j = 0; j < ile_x; j++)
{
cout << pkt[i].x[j] << ",";