forked from edizquierdo/CE_locomotion
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
executable file
·309 lines (274 loc) · 9.32 KB
/
main.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
// =============================================================
// Evolving Locomotion
// Dec 2021
// Eduardo Izquierdo
// =============================================================
#include <iostream>
#include <iomanip>
#include <math.h>
#include "TSearch.h"
#include "VectorMatrix.h"
#include "Worm.h"
#include <stdio.h>
#define PRINTTOFILE
int skip_steps = 10;
using namespace std;
// Integration parameters
const int Duration = 24;
const double Transient = 8.0;
const double StepSize = 0.005;
const int N_curvs = 23;
// Fitness traj
const double AvgSpeed = 0.0001; //0.00022; // Average speed of the worm in meters per seconds
const double BBCfit = AvgSpeed*Duration;
// Genotype -> Phenotype Mapping Ranges
const double BiasRange = 16.0; //15.0;
const double SCRange = 16.0; //15.0;
const double CSRange = 16.0; //15.0;
const double ESRange = 2.0;
const double SRmax = 200.0;
const double NMJmax = 0.8; //1.2;
const double NMJmin = 0.0;
const int SR_A = 1;
const int SR_B = 2;
// Size of genotype
int VectSize = 17;
// ------------------------------------
// Genotype-Phenotype Mapping
// ------------------------------------
void GenPhenMapping(TVector<double> &gen, TVector<double> &phen)
{
// Parameters for the Stretch Receptors
phen(SR_A) = MapSearchParameter(gen(SR_A), 0.0, SRmax);
phen(SR_B) = MapSearchParameter(gen(SR_B), 0.0, SRmax);
// Bias
int k=3;
for (int i = 1; i <= 3; i++){
phen(k) = MapSearchParameter(gen(k), -BiasRange, BiasRange);k++;
}
// Self connections
for (int i = 1; i <= 3; i++){
phen(k) = MapSearchParameter(gen(k), -SCRange, SCRange);k++;
}
// DA, DB, VA, VB Chemical synapses (excitatory)
for (int i = 1; i <= 2; i++){
phen(k) = MapSearchParameter(gen(k), 0.0, CSRange);k++;
}
// VD Chemical synapses (Inhibitory)
for (int i = 1; i <= 2; i++){
phen(k) = MapSearchParameter(gen(k), -CSRange, 0.0);k++;
}
// Interunits Gap junctions
for (int i = 1; i <= 2; i++){
phen(k) = MapSearchParameter(gen(k), 0.0, ESRange);k++;
}
// Excitatory NMJ Weight
for (int i = 1; i <= 2; i++){
phen(k) = MapSearchParameter(gen(k), NMJmin, NMJmax);k++;
}
// Inhibitory NMJ Weight
for (int i = 1; i <= 1; i++){
phen(k) = MapSearchParameter(gen(k), -NMJmax, -NMJmin);k++;
}
}
// ------------------------------------
// Fitness function
// ------------------------------------
double Evaluation(TVector<double> &v, RandomState &rs, int direction){
double fitA,fitB;
double bodyorientation, anglediff;
double movementorientation, distancetravelled = 0, temp;
double distance;
double xt, xtp, oxt, fxt;
double yt, ytp, oyt, fyt;
// Genotype-Phenotype Mapping
TVector<double> phenotype(1, VectSize);
GenPhenMapping(v, phenotype);
Worm w(phenotype, 1);
w.InitializeState(rs);
if (direction == 1){
w.AVA_output = 0.0;
w.AVB_output = 1.0;
}
else{
w.AVA_output = 1.0;
w.AVB_output = 0.0; // Command Interneuron Activation Backward
}
// Transient
for (double t = 0.0; t <= Transient; t += StepSize){
w.Step(StepSize, 1);
}
xt = w.CoMx(); yt = w.CoMy();
oxt = w.CoMx(); oyt = w.CoMy();
// Run
for (double t = 0.0; t <= Duration; t += StepSize) {
w.Step(StepSize, 1);
// Current and past centroid position
xtp = xt; ytp = yt;
xt = w.CoMx(); yt = w.CoMy();
// Integration error check
if (isnan(xt) || isnan(yt) || sqrt(pow(xt-xtp,2)+pow(yt-ytp,2)) > 10*AvgSpeed*StepSize) {return 0.0;}
// Velocity Fitness
bodyorientation = w.Orientation(); // Orientation of the body position
movementorientation = atan2(yt-ytp,xt-xtp); // Orientation of the movement
anglediff = movementorientation - bodyorientation; // Check how orientations align
if (direction == 1){
temp = cos(anglediff) > 0.0 ? 1.0 : -1.0; // Add to fitness only movement forward
}
else{
temp = cos(anglediff) > 0.0 ? -1.0 : 1.0; // Add to fitness only movement backward
}
distancetravelled += temp * sqrt(pow(xt-xtp,2)+pow(yt-ytp,2));
}
fxt = w.CoMx(); fyt = w.CoMy();
distance = sqrt(pow(oxt-fxt,2)+pow(oyt-fyt,2));
fitA = 1 - (fabs(BBCfit - distance)/BBCfit);
fitA = (fitA > 0)? fitA : 0.0;
fitB = 1 - (fabs(BBCfit-distancetravelled)/BBCfit);
fitB = (fitB > 0)? fitB : 0.0;
return fitB;
}
// ------------------------------------
// Fitness Function
// ------------------------------------
double EvaluationFunction(TVector<double> &v, RandomState &rs){
double sra = v(SR_A);
double srb = v(SR_B);
double fitnessForward, fitnessBackward;
v(SR_A)= -1.0;
v(SR_B)= srb;
fitnessForward = Evaluation(v, rs, 1);
// v(SR_A)= sra;
// v(SR_B)= -1.0;
// fitnessBackward = Evaluation(v, rs, -1);
// return (fitnessForward + fitnessBackward)/2;
return fitnessForward;
// return fitnessBackward;
}
// ------------------------------------
// Plotting
// ------------------------------------
double save_traces(TVector<double> &v, RandomState &rs){
ofstream curvfile("curv.dat");
ofstream bodyfile("body.dat");
ofstream actfile("act.dat");
// Genotype-Phenotype Mapping
TVector<double> phenotype(1, VectSize);
GenPhenMapping(v, phenotype);
double sra = phenotype(SR_A);
double srb = phenotype(SR_B);
Worm w(phenotype, 1);
ofstream phenfile("phenotype.dat");
w.DumpParams(phenfile);
w.InitializeState(rs);
w.sr.SR_A_gain = 0.0;
w.sr.SR_B_gain = srb;
w.AVA_output = w.AVA_inact;
w.AVB_output = w.AVB_act;
for (double t = 0.0; t <= Transient + Duration; t += StepSize){
w.Step(StepSize, 1);
w.DumpBodyState(bodyfile, skip_steps);
w.DumpCurvature(curvfile, skip_steps);
w.DumpActState(actfile, skip_steps);
}
w.sr.SR_A_gain = 0.0;
w.sr.SR_B_gain = 0.0;
for (double t = 0.0; t <= (12); t += StepSize){
w.Step(StepSize, 1);
w.DumpBodyState(bodyfile, skip_steps);
w.DumpCurvature(curvfile, skip_steps);
w.DumpActState(actfile, skip_steps);
}
w.sr.SR_A_gain = sra;
w.sr.SR_B_gain = 0.0;
w.AVA_output = w.AVA_act;
w.AVB_output = w.AVB_inact;
for (double t = 0.0; t <= (20); t += StepSize){
w.Step(StepSize, 1);
w.DumpBodyState(bodyfile, skip_steps);
w.DumpCurvature(curvfile, skip_steps);
w.DumpActState(actfile, skip_steps);
}
w.sr.SR_A_gain = 0.0;
w.sr.SR_B_gain = 0.0;
for (double t = 0.0; t <= (12); t += StepSize){
w.Step(StepSize, 1);
w.DumpBodyState(bodyfile, skip_steps);
w.DumpCurvature(curvfile, skip_steps);
w.DumpActState(actfile, skip_steps);
}
bodyfile.close();
curvfile.close();
actfile.close();
return 0;
}
// ------------------------------------
// Display functions
// ------------------------------------
void EvolutionaryRunDisplay(int Generation, double BestPerf, double AvgPerf, double PerfVar)
{
cout << BestPerf << " " << AvgPerf << " " << PerfVar << endl;
}
void ResultsDisplay(TSearch &s)
{
TVector<double> bestVector;
ofstream BestIndividualFile;
bestVector = s.BestIndividual();
BestIndividualFile.open("best.gen.dat");
BestIndividualFile << setprecision(32);
BestIndividualFile << bestVector << endl;
BestIndividualFile.close();
}
// ------------------------------------
// The main program
// ------------------------------------
int main (int argc, const char* argv[])
{
std::cout << std::setprecision(10);
long randomseed = static_cast<long>(time(NULL));
if (argc == 2)
randomseed += atoi(argv[1]);
TSearch s(VectSize);
// save the seed to a file
ofstream seedfile;
seedfile.open ("seed.dat");
seedfile << randomseed << endl;
seedfile.close();
// configure the search
s.SetRandomSeed(randomseed);
s.SetPopulationStatisticsDisplayFunction(EvolutionaryRunDisplay);
s.SetSearchResultsDisplayFunction(ResultsDisplay);
s.SetSelectionMode(RANK_BASED); //{FITNESS_PROPORTIONATE,RANK_BASED}
s.SetReproductionMode(GENETIC_ALGORITHM); // {HILL_CLIMBING, GENETIC_ALGORITHM}
s.SetPopulationSize(96);
s.SetMaxGenerations(10);
s.SetMutationVariance(0.05); // For 71 parameters, an estimated avg change of 0.25 for weights (mapped to 15).
s.SetCrossoverProbability(0.5);
s.SetCrossoverMode(UNIFORM); //{UNIFORM, TWO_POINT}
s.SetMaxExpectedOffspring(1.1);
s.SetElitistFraction(0.02);
s.SetSearchConstraint(1);
s.SetReEvaluationFlag(0);
// redirect standard output to a file
#ifdef PRINTTOFILE
ofstream evolfile;
evolfile.open("fitness.dat");
cout.rdbuf(evolfile.rdbuf());
#endif
// Code to run simulation:
InitializeBodyConstants();
s.SetEvaluationFunction(EvaluationFunction);
s.ExecuteSearch();
#ifdef PRINTTOFILE
evolfile.close();
#endif
RandomState rs;
long seed = static_cast<long>(time(NULL));
rs.SetRandomSeed(seed);
ifstream Best;
Best.open("best.gen.dat");
TVector<double> best(1, VectSize);
Best >> best;
save_traces(best, rs);
return 0;
}