-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.cpp
More file actions
executable file
·211 lines (168 loc) · 6.6 KB
/
main.cpp
File metadata and controls
executable file
·211 lines (168 loc) · 6.6 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
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
/**
* @file
* @author andy@impv.au
* @version 1.0
* @brief Entry point and harness for the memetic algorithm
*
* @bug Need to add .devcontainer and .vscode configurations files to the list of files
* @bug Need to change all clone() functions to copy constructors
*
*/
// Local
#include <memetico/args.h>
#include <memetico/globals.h>
#include <memetico/helpers/rng.h>
#include <memetico/data/data_set.h>
#include <memetico/optimise/objective.h>
#include <memetico/models/regression.h>
#include <memetico/models/cont_frac_dd.h>
#include <memetico/models/branch_cont_frac_dd.h>
#include <memetico/population/pop.h>
#include <memetico/global_types.h>
#include <mpi.h>
// Std
#include <cstdlib>
using namespace std;
// Logic Globals
bool meme::GPU = false;
uint_fast32_t meme::SEED = 42;
size_t meme::GENERATIONS = 200;
double meme::MUTATE_RATE = 0.2;
size_t meme::LOCAL_SEARCH_INTERVAL = 1;
size_t meme::STALE_RESET = 5;
bool meme::INT_ONLY = false;
size_t meme::LOCAL_SEARCH_RUNS = 4;
size_t meme::NELDER_MEAD_STALE = 10;
size_t meme::NELDER_MEAD_MOVES = 1500;
double meme::LOCAL_SEARCH_DATA_PCT = 0;
double meme::PENALTY = 0;
size_t meme::GEN = 0;
long int meme::MAX_TIME = 10*60;
long int meme::RUN_TIME = 0;
double meme::EPSILON = 0;
size_t meme::DEPTH = 4;
size_t meme::POCKET_DEPTH = 1;
size_t meme::DIVERSITY_COUNT = 3;
// Derivative Globals
size_t meme::IFR = 0;
string meme::IN_DER = "exact";
size_t meme::MAX_DER_ORD = 3;
DynamicDepthType meme::DYNAMIC_DEPTH_TYPE = DynamicNone;
// File Globals
string meme::TRAIN_FILE = "sinx.csv";
string meme::TEST_FILE = "sinx.csv";
string meme::LOG_DIR = "out/";
ofstream meme::master_log;
// Technical Globals
size_t meme::PREC = 18;
bool meme::DEBUG = false;
// Global Heplers
RandReal meme::RANDREAL;
RandInt meme::RANDINT;
// Local Helpers
FILE* meme::STD_OUT;
FILE* meme::STD_ERR;
/**
* Entry point of the application
*
* @param argc number of program arguments accessible in argv
* @param argv array of arguments
* @return int program succces
*/
int main(int argc, char *argv[]) {
// Initialise MPI functionality
// int mpi_size, mpi_rank;
// MPI_Init(&argc, &argv);
// MPI_Comm_size(MPI_COMM_WORLD, &mpi_size);
// MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank);
cout << setprecision(18);
// Parse arguments
args::load_args(argc, argv);
cout << "Seed: " << meme::SEED << endl;
RandInt ri = RandInt(meme::SEED);
RandReal rr = RandReal(meme::SEED);
RandInt::RANDINT = &ri;
RandReal::RANDREAL = &rr;
Model::FORMAT = PrintType::PrintExcel;
// Read Train and Test, Output for local copy
DataSet train = DataSet(meme::TRAIN_FILE, meme::GPU);
train.load();
train.csv(meme::LOG_DIR+to_string(meme::SEED)+".Train.csv");
DataSet test = DataSet(meme::TEST_FILE, meme::GPU);
test.load();
test.csv(meme::LOG_DIR+to_string(meme::SEED)+".Test.csv");
// Approximate derivative
/*
if( meme::IN_DER == "app-fd" )
train.compute_app_der(meme::MAX_DER_ORD);
train.normalise();
*/
// Copy IVs to Model
for(size_t i = 0; i < DataSet::IVS.size(); i++)
ModelType::IVS.push_back(DataSet::IVS[i]);
// Create Population & run
Population<ModelType> p(&train);
p.run();
// Evaluate without derviative information for return
meme::MAX_DER_ORD = 0;
train = DataSet(meme::TRAIN_FILE, meme::GPU);
train.load();
// Write Run.log
ofstream log(meme::LOG_DIR+to_string(meme::SEED)+".Run.log");
if (log.is_open()) {
log << setprecision(meme::PREC);
// Get results
vector<size_t> all;
double train_score = ModelType::OBJECTIVE(&p.best_soln, &train, all);
double test_score = ModelType::OBJECTIVE(&p.best_soln, &test, all);
// Log data
log << "Seed,Score,Train MSE,Test MSE,Dur,Model" << endl;
log << meme::SEED;
log << "," << MemeticModel<DataType>::OBJECTIVE_NAME;
log << "," << train_score;
log << "," << test_score;
log << "," << meme::RUN_TIME/1000.0;
log << "," << p.best_soln << endl;
// While we are here, inform the user
cout << endl << endl;
cout << "Finished after " << meme::RUN_TIME << " milliseconds on seed " << meme::SEED << endl;
cout << "====================================================" << endl << endl;
cout << "Best Model Found" << endl;
cout << p.best_soln << endl << endl;
Model::FORMAT = PrintType::PrintLatex;
cout << p.best_soln << endl << endl;
cout << " Train MSE: " << train_score << endl;
cout << " Test MSE: " << test_score << endl << endl;
cout << "====================================================" << endl << endl;
}
// MPI_Finalize(); // Close out MPI
// Write Training results
ofstream train_log(meme::LOG_DIR+to_string(meme::SEED)+".Train.Predict.csv");
if (train_log.is_open()) {
train_log << setprecision(meme::PREC) << "y";
for(size_t i = 0; i < DataSet::IVS.size(); i++)
train_log << "," << DataSet::IVS[i];
train_log << ",yd" << endl;
for(size_t i = 0; i < train.get_count(); i++) {
train_log << train.y[i];
for(size_t j = 0; j < DataSet::IVS.size(); j++)
train_log << "," << train.samples[i][j];
train_log << "," << p.root_agent->get_pocket().evaluate(train.samples[i]) << endl;
}
}
// Write Testing results
ofstream test_log(meme::LOG_DIR+to_string(meme::SEED)+".Test.Predict.csv");
if (test_log.is_open()) {
test_log << setprecision(meme::PREC) << "y";
for(size_t i = 0; i < DataSet::IVS.size(); i++)
test_log << "," << DataSet::IVS[i];
test_log << ",yd" << endl;
for(size_t i = 0; i < test.get_count(); i++) {
test_log << test.y[i];
for(size_t j = 0; j < DataSet::IVS.size(); j++)
test_log << "," << test.samples[i][j];
test_log << "," << p.root_agent->get_pocket().evaluate(test.samples[i]) << endl;
}
}
return EXIT_SUCCESS;
}