-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
179 lines (146 loc) · 6.12 KB
/
Copy pathmain.cpp
File metadata and controls
179 lines (146 loc) · 6.12 KB
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
#include <iostream>
#include <fstream>
#include <random>
#include "GPStruct.h"
#include <vector>
#include <sstream>
#include <chrono>
#include <algorithm>
#include <numeric>
#include <cstdlib>
class Dataset {
public:
std::vector<std::vector<double>> data;
// 0: boolean, 1: float
std::vector<std::pair<std::string, int>> columnTypes;
};
Dataset* fetchDataset(std::string datasetName) {
if (datasetName.empty()) {
std::cerr << "No dataset name provided" << std::endl;
return nullptr;
}
std::ifstream file(datasetName);
try {
if (!file.is_open()) {
std::cerr << "Failed to open dataset: " << datasetName << std::endl;
return nullptr;
}
} catch (const std::exception& e) {
std::cerr << "Error opening file: " << e.what() << std::endl;
return nullptr;
}
std::string line;
std::getline(file, line);
std::vector<std::pair<std::string, int>> columnTypes;
std::stringstream ss(line);
std::string token;
std::vector<std::string> booleanColumns = {"sex", "antivirals", "fatigue", "malaise", "anorexia", "histology"};
while (std::getline(ss, token, '\t')) {
token.erase(0, token.find_first_not_of(" \t"));
token.erase(token.find_last_not_of(" \t") + 1);
if (token.empty() || token == "target") continue;
// Check if token is a boolean column
int type = (std::find(booleanColumns.begin(), booleanColumns.end(), token) != booleanColumns.end()) ? 0 : 1;
columnTypes.push_back({token, type});
}
std::vector<std::vector<double>> fullDataset;
while (std::getline(file, line)) {
std::stringstream ss(line);
std::string token;
std::vector<double> rowData;
while (std::getline(ss, token, '\t')) {
try {
rowData.push_back(std::stod(token));
} catch (const std::invalid_argument&) {
rowData.push_back(0.0); // In case of an invalid number, default to 0
}
}
fullDataset.push_back(rowData);
}
file.close();
Dataset* ds = new Dataset();
ds->data = fullDataset;
ds->columnTypes = columnTypes;
return ds;
}
void run() {
Dataset* dataset = fetchDataset("hepatitis_cleaned.tsv");
if (!dataset) {
std::cerr << "Failed to load datasets" << std::endl;
return;
}
// std::ofstream file("outputs.csv", std::ios::trunc);
// if (!file.is_open()) {
// std::cerr << "Failed to open outputs.csv" << std::endl;
// }
// file << "run,generation,populationFitness,bestTree,action,structured" << std::endl;
// file.close();
std::vector<std::string> columnNames;
for (const auto& name : dataset->columnTypes) {
columnNames.push_back(name.first);
}
// setup for normal GP
int populationSize = 35;
int maxDepth = 7; // initial depth, can grow indefinitely
int maxGenerations = 80;
std::vector<double> applicationRates = {0.6, 0.25}; // crossoverRate, mutationRate
int tournamentSize = 7;
// setup for structure-based GP
int populationSizeStruct = 35;
int maxDepthStruct = 6; // initial depth, can grow indefinitely
int maxGenerationsStruct = 110;
std::vector<double> applicationRatesStruct = {0.5, 0.25}; // crossoverRate, mutationRate
int tournamentSizeStruct = 7;
int runs = 1; // only 1 run for submission purposes
std::vector<GPStruct*> gps;
std::vector<GPStruct*> gp_structs;
gps.resize(runs);
gp_structs.resize(runs);
std::vector<double> bestFitness(runs);
std::vector<double> bestFitnessStruct(runs);
std::vector<double> avgDuration(runs);
std::vector<double> avgDurationStruct(runs);
for (int i = 0; i < runs; i++) {
std::srand(i);
// normal GP
auto start = std::chrono::high_resolution_clock::now();
gps[i] = new GPStruct(populationSize, dataset->data, maxGenerations, maxDepth, applicationRates, tournamentSize, dataset->columnTypes, i);
gps[i]->train(i);
bestFitness[i] = gps[i]->test(i);
auto end = std::chrono::high_resolution_clock::now();
// reset the seed
std::srand(i);
// structure-based GP
auto start_struct = std::chrono::high_resolution_clock::now();
gp_structs[i] = new GPStruct(populationSizeStruct, dataset->data, maxGenerationsStruct, maxDepthStruct, applicationRatesStruct, tournamentSizeStruct, dataset->columnTypes, i);
gp_structs[i]->train(i, true);
bestFitnessStruct[i] = gp_structs[i]->test(i);
auto end_struct = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsed = end - start;
std::chrono::duration<double> elapsed2 = end_struct - start_struct;
std::cout << "Normal GP for run " << i+1 << " completed in " << elapsed.count() << " seconds" << std::endl;
std::cout << "Structure-based GP for run " << i+1 << " completed in " << elapsed2.count() << " seconds" << std::endl;
std::cout << "___________________" << std::endl;
avgDuration[i] = elapsed.count();
avgDurationStruct[i] = elapsed2.count();
}
// print the results
std::cout << "Testing Results:" << std::endl;
std::cout << "Run\tBest BACC\tStruct-BACC\tTime\t\tStructTime" << std::endl;
for (int i = 0; i < runs; i++) {
std::cout << i+1 << "\t" << std::to_string(bestFitness[i]) << "\t" << std::to_string(bestFitnessStruct[i]) << "\t" << std::to_string(avgDuration[i]) << "\t" << std::to_string(avgDurationStruct[i]) << std::endl;
}
std::cout << "___________________" << std::endl;
std::cout << "Average Best BACC: " << std::accumulate(bestFitness.begin(), bestFitness.end(), 0.0) / runs << std::endl;
std::cout << "Average Struct-BACC: " << std::accumulate(bestFitnessStruct.begin(), bestFitnessStruct.end(), 0.0) / runs << std::endl;
std::cout << "Average Duration: " << std::accumulate(avgDuration.begin(), avgDuration.end(), 0.0) / runs << " seconds (Total duration: " << std::accumulate(avgDuration.begin(), avgDuration.end(), 0.0) << " seconds)" << std::endl;
std::cout << "Average Struct Duration: " << std::accumulate(avgDurationStruct.begin(), avgDurationStruct.end(), 0.0) / runs << " seconds (Total duration: " << std::accumulate(avgDurationStruct.begin(), avgDurationStruct.end(), 0.0) << " seconds)" << std::endl;
std::cout << "___________________" << std::endl;
for (int i = 0; i < runs; i++) {
delete gps[i];
}
}
int main() {
run();
return 0;
}