1
1
#include " secondary-pop-nodes/annealer/SimulatedAnnealer.hpp"
2
2
#include < string>
3
3
#include < sstream>
4
+ #include < iostream>
4
5
#include < random>
5
6
#include < cmath>
6
7
@@ -98,15 +99,15 @@ void SimulatedAnnealer::init(bool maximize, TemperatureSchedule * schedule) {
98
99
}
99
100
100
101
Individual ** SimulatedAnnealer::newPopulation () {
101
- Individual ** population = (Individual**)malloc (
102
+ Individual ** nextPopulation = (Individual**)malloc (
102
103
sizeof (Individual*)*populationSize
103
104
);
104
105
105
106
for (int i = 0 ; i < populationSize; i++) {
106
- population [i] = getNeighbour (myPopulation[i]); ;
107
+ nextPopulation [i] = getNeighbour (myPopulation[i]);
107
108
}
108
109
109
- return population ;
110
+ return nextPopulation ;
110
111
}
111
112
112
113
int SimulatedAnnealer::compareNeighbourliness (
@@ -202,7 +203,8 @@ Individual * SimulatedAnnealer::getNeighbour(Individual * target) {
202
203
choice = neighbourDistribution (generator);
203
204
}
204
205
205
- if (neighbours[choice] == NULL ) return target;
206
+ if (neighbours[choice] == NULL ) return target->deepCopy ();
207
+
206
208
int targetFitness = target->checkFitness ();
207
209
int neighbourFitness = neighbours[choice]->checkFitness ();
208
210
@@ -222,22 +224,21 @@ Individual * SimulatedAnnealer::getNeighbour(Individual * target) {
222
224
float temp = schedule->currentTemp (currentGeneration);
223
225
if (temp == 0 ) {
224
226
delete (neighbour);
225
- return target;
227
+ return target-> deepCopy () ;
226
228
}
227
229
228
230
int delta = maximize ? neighbourFitness - targetFitness :
229
231
targetFitness - neighbourFitness;
230
232
231
- float probability = exp (((float )- delta)/temp);
233
+ float probability = exp (((float )delta)/temp);
232
234
233
235
uniform_real_distribution<float >
234
236
goingWrongWayDistribution (0 , 1 );
235
237
236
238
if (goingWrongWayDistribution (generator) < probability) {
237
239
return neighbour;
238
240
} else {
239
- delete (neighbour);
240
- return target;
241
+ return target->deepCopy ();
241
242
}
242
243
}
243
244
}
0 commit comments