-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexperiment.cpp
339 lines (280 loc) · 9.75 KB
/
experiment.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
#include <mpi.h>
#include "LocalProgressObserver.h"
#include "Distributor.h"
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include "AdvXMLParser.h"
#include "GAEngine.h"
#include "GAEngine.cpp"
#include "VirtualExperiment.h"
#include "Utils.h"
using namespace std;
using namespace AdvXMLParser;
using namespace iface::cellml_api;
using namespace iface::cellml_services;
ObjRef<iface::cellml_api::CellMLBootstrap> bootstrap; //CellML api bootstrap
ObjRef<iface::cellml_services::CellMLIntegrationService> cis;
VariablesHolder var_template; //template for the variables, just holds names of the variables
int verbosity=0; // verbosity initialised to 0
void usage(const char *name)
{
std::cerr << "Error: usage: " << name << " <experiment definition xml> [-v [-v [...]]]\n";
std::cerr << "Where -v increases the verbosity of the output" << std::endl;
}
//Open and read XML configuration file
//nSize is assigned the size of file
//return the contents of the file as a null-terminated char array
char *OpenXmlFile(const char *name,long& nSize)
{
FILE *f=fopen(name,"rb"); // open file "name" for reading
char *pBuffer=NULL; // initialise a buffer for storing C-string to a nullptr
//check for file open error
if(!f)
return NULL;
//obtain file size
fseek(f,0,SEEK_END);
nSize=ftell(f);
fseek(f,0,SEEK_SET);
//allocate memory to contain the whole file
pBuffer=new char[nSize+1];
size_t result = fread(pBuffer,nSize,1,f); //copy the file into buffer (usage of fread)
// if result number differs from the count parameter, either a reading error occurred
// or the end-of-file was reached while reading.
if (result != 1) {
return NULL;
}
pBuffer[nSize]=0; // null terminate the char array buffer
int proc;
MPI_Comm_rank(MPI_COMM_WORLD, &proc);
if(!proc)
{
printf("Input file: %s \n", name);
printf("%s", pBuffer);
}
fclose(f);
return pBuffer;
}
// Initialise GA engine
// returns number of generations to run GA; -1 if any error occurred
int SetAndInitEngine(GAEngine<COMP_FUNC >& ga, const Element& elem)
{
//Get GA parameters from XML file
int initPopulation=atoi(elem.GetAttribute("InitialPopulation").GetValue().c_str());
double mutation=atof(elem.GetAttribute("Mutation_proportion").GetValue().c_str());
double cross=atof(elem.GetAttribute("Crossover_proportion").GetValue().c_str());
int generations=atoi(elem.GetAttribute("Generations").GetValue().c_str());
// Check for default and limit for params
if(!initPopulation)
initPopulation=100;
if(cross>1.0)
cross=1.0;
if(mutation>1.0)
mutation=1.0;
if(generations<0)
{
std::cerr << "Error: SetAndInitEngine: invalid value for Generations: setting to default 0" << std::endl;
generations=0;
}
ga.prob_cross()=cross;
ga.prob_mutate()=mutation;
ga.part_cross()=(int)((double)initPopulation*cross);
ga.part_mutate()=(int)((double)initPopulation*mutation);
// Read alleles information
for(int i=0;;i++)
{
const Element& al=elem("Alleles",0)("Allele",i); // get sub-Allele element in XML
std::wstring name;
if(al.IsNull())
break; // no more alleles specified
name=convert(al.GetAttribute("Name").GetValue());
ga.AddAllele(name);
// Validate parameter limits for non-negativity
double min_lim=atof(al.GetAttribute("LowerBound").GetValue().c_str());
double max_lim=atof(al.GetAttribute("UpperBound").GetValue().c_str());
if(min_lim>max_lim || min_lim < 0)
{
std::cerr << "Error: SetAndInitEngine: invalid limits for Allele[" << i
<< "]: UpperBound should not be less than LowerBound: Value cannot be negative as well."
<< currentDateTime() << std::endl;
return -1;
}
ga.AddLimit(name,min_lim,max_lim);
var_template(name,0.0); // update allele list in var_template
}
ga.set_borders(initPopulation); // set max population of GA and initialise the population with default genomes
// return the num of generations to run GA
return generations;
}
//Initialise var_template with Alleles in XML; all param (allele) names are updated into var_template
void initialize_template_var(const Element& elem)
{
// Read alleles information in XML
for(int i=0;;i++)
{
const Element& al=elem("Alleles",0)("Allele",i);
std::wstring name;
if(al.IsNull())
break; // no more alleles specified
name=convert(al.GetAttribute("Name").GetValue());
// Add this parameter as an allele in var_template
var_template(name,0.0);
}
}
// Observer callback to process workitem in the GA context
bool observer(WorkItem *w,double answer,void *g)
{
GAEngine<COMP_FUNC> *ga=(GAEngine<COMP_FUNC> *)g; // GAEngine typecasting
ga->process_workitem(w,answer); // assign 'answer' as fitness to the genome corresponding to the workitem w
return true;
}
double do_compute(std::vector<double>& val)
{
// fill-up the tmp's allele values with supplied data
var_template.fillup(val);
// Evaluate the chromosome fitness
return VEGroup::instance().Evaluate(var_template);
}
// Slave process
// Returns only when appropriate quit command is received from the master
void run_slave(int proc)
{
double req;
MPI_Status stat;
std::vector<double> data;
var_template.collate(data);
while(1)
{
//check if data is received
MPI_Probe(MPI_ANY_SOURCE,MPI_ANY_TAG,MPI_COMM_WORLD,&stat);
if(stat.MPI_TAG==TAG_QUIT)
{
//Quit signal received
break;
}
//Receive compute request and process it
MPI_Recv(&data[0],data.size(),MPI_DOUBLE,MPI_ANY_SOURCE,MPI_ANY_TAG,MPI_COMM_WORLD,&stat);
req=do_compute(data);
//returns the result of the computations
MPI_Send(&req,1,MPI_DOUBLE,0,0,MPI_COMM_WORLD);
}
}
int main(int argc,char *argv[])
{
char *pBuffer=NULL;
long nSize=0;
GAEngine<COMP_FUNC > ga; // initialise GA engine for the program
int proc,nproc;
int generations=0;
const char *filename=NULL;
MPI_Init(&argc,&argv);
if(argc<2)
{
// warn user for incorrect usage at command line
usage(argv[0]);
return -1;
}
for(int i=1;i<argc;i++)
{
if(!strcmp(argv[i],"-v"))
// if an arg string is "-v" increment verbosity
verbosity++;
else
// other arg string becomes the filename
filename=argv[i];
}
MPI_Comm_rank(MPI_COMM_WORLD, &proc);
MPI_Comm_size(MPI_COMM_WORLD, &nproc);
// proc for unique seed for each processor, proc + 1 to overcome single processor 0 seed.
srand(time(NULL)* (proc + 1)); // unique seed for RNG
//Load and initialise CellML API
bootstrap=CreateCellMLBootstrap();
cis=CreateIntegrationService();
// Read input file and store contents in buffer
if((pBuffer=OpenXmlFile(filename,nSize)) == NULL)
{
std::cerr << "Error: main: error opening input file " << argv[1] << ": " << currentDateTime() << std::endl;
return -1;
}
// Read success: pBuffer is a C-string containing file and nSize is size of file
//Load the experiments package: Virtual Experiment data and GA parameters
try
{
Parser parser;
ObjRef<CellMLComponentSet> comps; // TODO comps not referenced elsewhere in project
// Parse the XML contents in buffer
auto_ptr<Document> pDoc(parser.Parse(pBuffer,nSize)); // can throw an exception
// Get the root of the XML structure
const Element& root=pDoc->GetRoot();
// Load virtual experiments
// load all virtual experiments in the XML file
for(int i=0;;i++)
{
// load the VE data in file
VirtualExperiment *vx=VirtualExperiment::LoadExperiment(root("VirtualExperiments",0)("VirtualExperiment",i));
if(!vx)
break; // loaded all the VE in file
// Quit program with err msg if VE is invalid
if (!vx->isValid())
return -1;
// add each VE into the group singleton
VEGroup::instance().add(vx);
}
// If no experiments is added to the VEGroup, exit the program
if (!VEGroup::instance().getExperimentCount())
return -1;
// load the GA parameters from file and initialise the engine
if(!proc)
{
// Master processor initialises the GA engine
// get max generations (-1 for errors) and GA parameters
generations=SetAndInitEngine(ga,root("GA",0));
}
else
{
// The slaves initialise the template variable holder to create gene-pool
initialize_template_var(root("GA",0));
}
}
catch(ParsingException &e)
{
std::cerr << "Error: main: parsing error at line " << e.GetLine() << std::endl;
}
delete [] pBuffer; // free memory used to store file
// Wait until all the clients are ready
MPI_Barrier(MPI_COMM_WORLD);
// Only master task needs GA engine to be initialised and used
if(!proc)
{
// Print a summary of configuration of GA engine and VEs
ga.print_config(generations);
VEGroup::instance().print_summary();
// Validate and run GA
if(generations<0)
{
std::cerr << "Error: main: invalid settings for the Genetic Algorithm: " << currentDateTime() << std::endl;
}
else
{
// Initialise the population in GA engine
ga.Initialise();
// Run GA
ga.RunGenerations(generations);
// Print best genome from the run
VariablesHolder v;
double bf=ga.GetBest(v);
std::cout << "==========================================\n";
fprintf(stdout,"BEST GENOME (%lf):\n",bf);
v.print(stdout);
std::cout << "==========================================\n";
}
Distributor::instance().finish(); // request end of service to all slaves
}
else
{
run_slave(proc);
}
MPI_Barrier(MPI_COMM_WORLD);
MPI_Finalize();
return 0;
}