-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
executable file
·218 lines (185 loc) · 5.57 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
#include "TSearch.h"
#include "LeggedAgent.h"
#include "CTRNN.h"
#include "random.h"
#include <iostream>
#define PRINTOFILE
// Task params
const double StepSize = 0.05;
const double RunDuration = 220.0;
// Task params
const double StepSizeAnalysis = 0.01;
const double RunDurationAnalysis = 2200.0;
// EA params
const int POPSIZE = 10;
const int GENS = 50; // --- 5000
// const int POPSIZE = 96;
// const int GENS = 10000; // --- 5000
const double MUTVAR = 0.01; // --- 0.05
const double CROSSPROB = 0.0;
const double EXPECTED = 1.1;
const double ELITISM = 0.05;
// Nervous system params
const int N = 3;
const double WR = 8.0; // absolute weight range from 0
const double BR = 8.0; // absolute bias range from 0
// const double BR = 16.0; // absolute bias range from 0
const double TMIN = 1.0; // minimum time constant
const double TMAX = 10.0; // maximum time constant
int VectSize = N*N + 2*N;
// ------------------------------------
// Genotype-Phenotype Mapping Functions
// ------------------------------------
void GenPhenMapping(TVector<double> &gen, TVector<double> &phen)
{
int k = 1;
// TODO: Parallelize this, may need to upgrade to C++17 see: https://stackoverflow.com/questions/36246300/parallel-loops-in-c
// Time-constants
for (int i = 1; i <= N; i++) {
phen(k) = MapSearchParameter(gen(k), TMIN, TMAX);
k++;
}
// Bias
for (int i = 1; i <= N; i++) {
phen(k) = MapSearchParameter(gen(k), -BR, BR);
k++;
}
// Weights
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++) {
phen(k) = MapSearchParameter(gen(k), -WR, WR);
k++;
}
}
}
// ------------------------------------
// Fitness function
// ------------------------------------
double FitnessFunction(TVector<double> &genotype, RandomState &rs)
{
// Map genootype to phenotype
TVector<double> phenotype;
phenotype.SetBounds(1, VectSize);
GenPhenMapping(genotype, phenotype);
// Create the agent
LeggedAgent Insect;
// Instantiate the nervous system
Insect.NervousSystem.SetCircuitSize(N);
int k = 1;
// Time-constants
for (int i = 1; i <= N; i++) {
Insect.NervousSystem.SetNeuronTimeConstant(i,phenotype(k));
k++;
}
// Bias
for (int i = 1; i <= N; i++) {
Insect.NervousSystem.SetNeuronBias(i,phenotype(k));
k++;
}
// Weights
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++) {
Insect.NervousSystem.SetConnectionWeight(i,j,phenotype(k));
k++;
}
}
Insect.Reset(0, 0, 0); // NOTE: Might be unnecessary?
// Run the agent
for (double time = 0; time < RunDuration; time += StepSize) {
Insect.Step(StepSize);
cout << Insect.Leg.JointX << " " << Insect.Leg.JointY << " ";
cout << Insect.Leg.FootX << " " << Insect.Leg.FootY << " ";
cout << Insect.Leg.FootState << endl;
}
return Insect.cx/RunDuration;
}
// ------------------------------------
// Display functions
// ------------------------------------
void EvolutionaryRunDisplay(int Generation, double BestPerf, double AvgPerf, double PerfVar)
{
cout << Generation << " " << BestPerf << " " << AvgPerf << " " << PerfVar << endl;
}
void ResultsDisplay(TSearch &s)
{
TVector<double> bestVector;
ofstream BestIndividualFile;
TVector<double> phenotype;
phenotype.SetBounds(1, VectSize);
// Save the genotype of the best individual
bestVector = s.BestIndividual();
BestIndividualFile.open("best.gen.dat");
BestIndividualFile << bestVector << endl;
BestIndividualFile.close();
// Also show the best individual in the Circuit Model form
BestIndividualFile.open("best.ns.dat");
GenPhenMapping(bestVector, phenotype);
LeggedAgent Insect;
// Instantiate the nervous system
Insect.NervousSystem.SetCircuitSize(N);
int k = 1;
// Time-constants
for (int i = 1; i <= N; i++) {
Insect.NervousSystem.SetNeuronTimeConstant(i,phenotype(k));
k++;
}
// Bias
for (int i = 1; i <= N; i++) {
Insect.NervousSystem.SetNeuronBias(i,phenotype(k));
k++;
}
// Weights
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++) {
Insect.NervousSystem.SetConnectionWeight(i,j,phenotype(k));
k++;
}
}
BestIndividualFile << Insect.NervousSystem << endl;
for (int i = 1; i <= VectSize; i++) {
BestIndividualFile << phenotype(i) << " ";
}
BestIndividualFile << endl;
BestIndividualFile.close();
}
// ------------------------------------
// The main program
// ------------------------------------
int main (int argc, const char* argv[]) {
// long randomseed = static_cast<long>(time(NULL));
// if (argc == 2)
// randomseed += atoi(argv[1]);
long randomseed = 765234;
TSearch s(VectSize);
#ifdef PRINTOFILE
ofstream file;
file.open("evol.dat");
cout.rdbuf(file.rdbuf());
#endif
// Configure the search
s.SetRandomSeed(randomseed);
s.SetSearchResultsDisplayFunction(ResultsDisplay);
s.SetPopulationStatisticsDisplayFunction(EvolutionaryRunDisplay);
s.SetSelectionMode(RANK_BASED);
s.SetReproductionMode(GENETIC_ALGORITHM);
s.SetPopulationSize(POPSIZE);
s.SetMaxGenerations(GENS);
s.SetCrossoverProbability(CROSSPROB);
s.SetCrossoverMode(UNIFORM);
s.SetMutationVariance(MUTVAR);
s.SetMaxExpectedOffspring(EXPECTED);
s.SetElitistFraction(ELITISM);
s.SetSearchConstraint(1);
// Run Stage 1
s.SetEvaluationFunction(FitnessFunction);
s.ExecuteSearch();
// Analysis of best evolved circuit
// ifstream genefile;
// genefile.open( "best.gen.dat");
// TVector<double> genotype(1, VectSize);
// genefile >> genotype;
// Map(genotype);
// // Traces(genotype);
// genefile.close();
return 0;
}