-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathirifitnessfunction.cpp
More file actions
393 lines (347 loc) · 13.1 KB
/
irifitnessfunction.cpp
File metadata and controls
393 lines (347 loc) · 13.1 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
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#include "irifitnessfunction.h"
#include "collisionmanager.h"
/******************************************************************************/
/******************************************************************************/
CIriFitnessFunction::CIriFitnessFunction(const char* pch_name,
CSimulator* pc_simulator,
unsigned int un_collisions_allowed_per_epuck)
:
CFitnessFunction(pch_name, pc_simulator)
{
/* Check number of robots */
m_pcSimulator = pc_simulator;
TEpuckVector* pvecEpucks=m_pcSimulator->GetEpucks();
if ( pvecEpucks->size() == 0 )
{
printf("No Robot, so fitness function can not be computed.\n Exiting...\n");
fflush(stdout);
exit(0);
}
else if (pvecEpucks->size()>1)
{
printf("More than 1 robot, and fitness is not prepared for it.\n Exiting...\n");
}
m_pcEpuck=(*pvecEpucks)[0];
m_unNumberOfSteps = 0;
m_fComputedFitness = 0.0;
}
/******************************************************************************/
/******************************************************************************/
CIriFitnessFunction::~CIriFitnessFunction(){
}
/******************************************************************************/
/******************************************************************************/
double CIriFitnessFunction::GetFitness()
{
/* If you need to check the collisions of the robot, here are the total number of
* collisions done by the robot in the simulations */
int coll = (CCollisionManager::GetInstance()->GetTotalNumberOfCollisions());
/* Get the fitness divided by the number of steps */
double fit = ( m_fComputedFitness / (double) m_unNumberOfSteps ) * (1 - ((double) (fmin(coll,10.0)/10.0)));
/* If fitness less than 0, put it to 0 */
if ( fit < 0.0 ) fit = 0.0;
return fit;
}
/******************************************************************************/
/******************************************************************************/
void CIriFitnessFunction::SimulationStep(unsigned int n_simulation_step, double f_time, double f_step_interval)
{
/* See Evolutionary Robotics Book */
/* This is the function to be implemented */
/* f = V * ( 1 - sqrt(Delta(v)) ) * (1 - i)
* V relates to the maximum speed
* Delta(v) relates to the movement on the same direction
* i relates to the maximum sensor value
*/
/* Get actual SPEED of the left and right wheel */
double leftSpeed = 0.0;
double rightSpeed = 0.0;
m_pcEpuck->GetWheelSpeed(&leftSpeed,&rightSpeed);
leftSpeed = 0.5 + ( leftSpeed / ( 2.0 * m_pcEpuck->GetMaxWheelSpeed()) );
rightSpeed = 0.5 + ( rightSpeed / ( 2.0 * m_pcEpuck->GetMaxWheelSpeed()) );
/* Eval maximum speed partial fitness */
double maxSpeedEval = (fabs(leftSpeed - 0.5) + fabs(rightSpeed - 0.5));
/* Eval same direction partial fitness */
double sameDirectionEval = 1 - sqrt(fabs(leftSpeed - rightSpeed));
/* Eval SENSORS */
/* Where the Max PROXIMITY sensor will be stored*/
double maxProxSensorEval = 0.0;
/* Where the Max LIGHT sensor will be stored*/
double maxLightSensorEval = 0.0;
/* Where the Max BLUE LIGHT sensor will be stored*/
double maxBlueLightSensorEval = 0.0;
/* Where the Max RED LIGHT sensor will be stored*/
double maxRedLightSensorEval = 0.0;
/* Where the Max CONTACT sensor will be stored*/
double maxContactSensorEval = 0.0;
/* Where the GROUND MEMORY will be stored */
double* groundMemory;
/* Where the GROUND will be stored */
double* ground;
/* whre the BATTERY will be sotored */
double *battery;
/* whre the BLUE BATTERY will be sotored */
double *blueBattery;
/* whre the RED BATTERY will be sotored */
double *redBattery;
double blueLightS0=0;
double blueLightS7=0;
double lightS0=0;
double lightS7=0;
double redLightS0=0;
double redLightS7=0;
double redLightS6=0;
/* Auxiluar variables */
unsigned int unThisSensorsNumberOfInputs;
double* pfThisSensorInputs;
/* Go in all the sensors */
TSensorVector vecSensors = m_pcEpuck->GetSensors();
for (TSensorIterator i = vecSensors.begin(); i != vecSensors.end(); i++)
{
/* Check type of sensor */
switch ( (*i)->GetType() )
{
/* If sensor is PROXIMITY */
case SENSOR_PROXIMITY:
/* Get the number of inputs */
unThisSensorsNumberOfInputs = (*i)->GetNumberOfInputs();
/* Get the actual values */
pfThisSensorInputs = (*i)->GetComputedSensorReadings();
/* For every input */
for (int j = 0; j < unThisSensorsNumberOfInputs; j++)
{
/* If reading bigger than maximum */
if ( pfThisSensorInputs[j] > maxProxSensorEval )
{
/* Store maximum value */
maxProxSensorEval = pfThisSensorInputs[j];
}
}
break;
/* If sensor is GROUND_MEMORY */
case SENSOR_GROUND_MEMORY:
/* Get the actual value */
groundMemory = (*i)->GetComputedSensorReadings();
break;
/* If sensor is GROUND */
case SENSOR_GROUND:
/* Get actual values */
ground = (*i)->GetComputedSensorReadings();
break;
/* If sensor is LIGHT */
case SENSOR_REAL_LIGHT:
/* Get number of inputs */
unThisSensorsNumberOfInputs = (*i)->GetNumberOfInputs();
/* Get the actual values */
pfThisSensorInputs = (*i)->GetComputedSensorReadings();
/* For every input */
for (int j = 0; j < unThisSensorsNumberOfInputs; j++)
{
/* If reading bigger than maximum */
if ( pfThisSensorInputs[j] > maxLightSensorEval )
{
/* Store maximum value */
maxLightSensorEval = pfThisSensorInputs[j];
}
if (j==0)
lightS0 = pfThisSensorInputs[j];
else if (j==7)
lightS7 = pfThisSensorInputs[j];
}
break;
case SENSOR_REAL_BLUE_LIGHT:
unThisSensorsNumberOfInputs = (*i)->GetNumberOfInputs();
pfThisSensorInputs = (*i)->GetComputedSensorReadings();
for (int j = 0; j < unThisSensorsNumberOfInputs; j++)
{
if ( pfThisSensorInputs[j] > maxBlueLightSensorEval )
{
maxBlueLightSensorEval = pfThisSensorInputs[j];
}
if (j==0)
blueLightS0 = pfThisSensorInputs[j];
else if (j==7)
blueLightS7 = pfThisSensorInputs[j];
}
break;
case SENSOR_REAL_RED_LIGHT:
unThisSensorsNumberOfInputs = (*i)->GetNumberOfInputs();
pfThisSensorInputs = (*i)->GetComputedSensorReadings();
for (int j = 0; j < unThisSensorsNumberOfInputs; j++)
{
if ( pfThisSensorInputs[j] > maxRedLightSensorEval )
{
maxRedLightSensorEval = pfThisSensorInputs[j];
}
if (j==0)
redLightS0 = pfThisSensorInputs[j];
else if (j==7)
redLightS7 = pfThisSensorInputs[j];
else if (j==6)
redLightS6 = pfThisSensorInputs[j];
}
break;
/* If sensor is BATTERY */
case SENSOR_BATTERY:
battery = (*i)->GetComputedSensorReadings();
break;
case SENSOR_BLUE_BATTERY:
blueBattery = (*i)->GetComputedSensorReadings();
break;
case SENSOR_RED_BATTERY:
redBattery = (*i)->GetComputedSensorReadings();
break;
/* If sensor is CONTACT */
case SENSOR_CONTACT:
/* Get number of inputs */
unThisSensorsNumberOfInputs = (*i)->GetNumberOfInputs();
/* Get actual values */
pfThisSensorInputs = (*i)->GetComputedSensorReadings();
/* For every input */
for (int j = 0; j < unThisSensorsNumberOfInputs; j++)
{
/* If reading bigger than maximum */
if ( pfThisSensorInputs[j] > maxContactSensorEval )
{
/* Store maximum value */
maxContactSensorEval = pfThisSensorInputs[j];
}
}
break;
}
}
/* FROM HERE YOU NEED TO CREATE YOU FITNESS */
double umbralBatt=0.3;
double coef1= 0.25;
double coef2= 0.75;
//FITNESS -1 (iriNeuronTemplate.txt)
// double fitness= coef1*(maxSpeedEval* sameDirectionEval);
// if(battery[0]<umbralBatt){
// fitness+= (coef2 * ( lightS0 + lightS7)*maxLightSensorEval);
// umbralBatt=0.8;
// }
// if (battery[0]>umbralBatt){
// //fitness+=coef2*(maxSpeedEval* sameDirectionEval*maxLightSensorEval);
// fitness+= (coef2 * (blueLightS0 + blueLightS7)*maxBlueLightSensorEval);
// umbralBatt=0.3;
// }
//FITNESS 0(15%) Y 1(20%) (iriNeuronTemplate.txt)
// double fitness= coef1*(maxSpeedEval* sameDirectionEval);
// if(battery[0]<umbralBatt){
// fitness+= (coef2 * ( lightS0 + lightS7)*(battery[0])*maxLightSensorEval);
// umbralBatt=0.8;
// }
// if (battery[0]>umbralBatt){
// //fitness+=coef2*(maxSpeedEval* sameDirectionEval*maxLightSensorEval);
// fitness+= (coef2 * (blueLightS0 + blueLightS7)*(1-battery[0])*maxBlueLightSensorEval);
// umbralBatt=0.3;
// }
///*Fitness 2 (iriNeuronTemplate.txt)
// double fitness= coef1*(maxSpeedEval* sameDirectionEval);
// if(battery[0]<umbralBatt){
// fitness+= (coef2 * ((( lightS0 + lightS7)/2)+(battery[0]))/2);
// umbralBatt=0.8;
// }
// if (battery[0]>umbralBatt){
// //fitness+=coef2*(maxSpeedEval* sameDirectionEval*maxLightSensorEval);
// fitness+= (coef2 * (((blueLightS0 + blueLightS7)/2)+(1-battery[0]))/2);
// umbralBatt=0.3;
// }
//*/
// ///*Fitness 3 (iriNeuronTemplate.txt)
// double fitness= coef1*(maxSpeedEval* sameDirectionEval);
// if(battery[0]<umbralBatt){
// fitness+= (coef2 * ((( lightS0 + lightS7)/2)+(battery[0])+maxLightSensorEval)/3);
// umbralBatt=0.8;
// }
// if (battery[0]>umbralBatt){
// //fitness+=coef2*(maxSpeedEval* sameDirectionEval*maxLightSensorEval);
// fitness+= (coef2 * (((blueLightS0 + blueLightS7)/2)+(1-battery[0])+maxBlueLightSensorEval)/3);
// umbralBatt=0.3;
// }
// //*/
///*Fitness 4 AÑADIMOS LUZ ROJA SENSORES DELANTEROS (iriNeuron2Template.txt)
// double fitness= coef1*(maxSpeedEval* sameDirectionEval);
// if((battery[0]<umbralBatt)&(maxRedLightSensorEval==0.0)){
// fitness+= (coef2 * ((( lightS0 + lightS7)/2)+(battery[0])+maxLightSensorEval)/3);
// umbralBatt=0.8;
// }
// if ((battery[0]>umbralBatt)&(maxRedLightSensorEval==0.0)){
// //fitness+=coef2*(maxSpeedEval* sameDirectionEval*maxLightSensorEval);
// fitness+= (coef2 * (((blueLightS0 + blueLightS7)/2)+(1-battery[0])+maxBlueLightSensorEval)/3);
// umbralBatt=0.3;
// }
// if(maxRedLightSensorEval!=0.0){
// fitness+= (coef2*((((redLightS0+redLightS7)/2)+maxRedLightSensorEval))/2);
// }
//*/
///*Fitness 5 AÑADIMOS LUZ ROJA SENSORES PARA DERECHA 7 Y 6 (iriNeuron2Template.txt)
// double fitness= coef1*(maxSpeedEval* sameDirectionEval);
// if((battery[0]<umbralBatt)&(maxRedLightSensorEval==0.0)){
// fitness+= (coef2 * ((( lightS0 + lightS7)/2)+(battery[0])+maxLightSensorEval)/3);
// umbralBatt=0.8;
// }
// if ((battery[0]>umbralBatt)&(maxRedLightSensorEval==0.0)){
// //fitness+=coef2*(maxSpeedEval* sameDirectionEval*maxLightSensorEval);
// fitness+= (coef2 * (((blueLightS0 + blueLightS7)/2)+(1-battery[0])+maxBlueLightSensorEval)/3);
// umbralBatt=0.3;
// }
// if(maxRedLightSensorEval!=0.0){
// fitness+= (coef2*((((redLightS6+redLightS7)/2)+maxRedLightSensorEval))/2);
// }
//*/
//FITNESS 6 (iriNeuron3Template.txt)
double fitness= coef1*(maxSpeedEval* sameDirectionEval);
if(battery[0]<umbralBatt){
fitness+= (coef2 * ( lightS0 + lightS7)*(battery[0])*maxLightSensorEval);
umbralBatt=0.8;
}
if (battery[0]>umbralBatt){
//fitness+=coef2*(maxSpeedEval* sameDirectionEval*maxLightSensorEval);
fitness+= (coef2 * (blueLightS0 + blueLightS7)*(1-battery[0])*maxBlueLightSensorEval);
umbralBatt=0.3;
}
// ///*Fitness 7 (iriNeuron4Template.txt)
// double fitness= coef1*(maxSpeedEval* sameDirectionEval);
// if(battery[0]<umbralBatt){
// fitness+= (coef2 * ((( lightS0 + lightS7)/2)+(battery[0])+maxLightSensorEval)/3);
// umbralBatt=0.8;
// }
// if (battery[0]>umbralBatt){
// //fitness+=coef2*(maxSpeedEval* sameDirectionEval*maxLightSensorEval);
// fitness+= (coef2 * (((blueLightS0 + blueLightS7)/2)+(1-battery[0])+maxBlueLightSensorEval)/3);
// umbralBatt=0.3;
// }
// //*/
// ///*Fitness 8 (iriNeuron3Template.txt)
// double fitness= coef1*(maxSpeedEval* sameDirectionEval);
// if(battery[0]<umbralBatt){
// fitness+= (coef2 * ((( lightS0 + lightS7)/2)+(battery[0])+maxLightSensorEval)/3);
// umbralBatt=0.8;
// }
// if (battery[0]>umbralBatt){
// //fitness+=coef2*(maxSpeedEval* sameDirectionEval*maxLightSensorEval);
// fitness+= (coef2 * (((blueLightS0 + blueLightS7)/2)+(1-battery[0])+maxBlueLightSensorEval)/3);
// umbralBatt=0.3;
// }
// //*/
//FITNESS 9 (iriNeuron5Template.txt)
// double fitness= coef1*(maxSpeedEval* sameDirectionEval);
// if((battery[0]<umbralBatt)&(maxRedLightSensorEval==0.0)){
// fitness+= (coef2 * ( lightS0 + lightS7)*(battery[0])*maxLightSensorEval);
// umbralBatt=0.8;
// }
// if ((battery[0]>umbralBatt)&(maxRedLightSensorEval==0.0)){
// //fitness+=coef2*(maxSpeedEval* sameDirectionEval*maxLightSensorEval);
// fitness+= (coef2 * (blueLightS0 + blueLightS7)*(1-battery[0])*maxBlueLightSensorEval);
// umbralBatt=0.3;
// }
// if(maxRedLightSensorEval!=0.0){
// fitness+= (coef2*((((redLightS6+redLightS7)/2)+maxRedLightSensorEval))/2);
// }
// /* TO HERE YOU NEED TO CREATE YOU FITNESS */
// m_unNumberOfSteps++;
// m_fComputedFitness += fitness;
}
/******************************************************************************/
/******************************************************************************/