| 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 |
package dr.math; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public class DifferentialEvolution extends MultivariateMinimum |
| 38 |
{ |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public double F = 0.7 ; |
| 49 |
|
| 50 |
|
| 51 |
public double CR = 0.9 ; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
public int prin = 0; |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public DifferentialEvolution (int dim) |
| 72 |
{ |
| 73 |
this(dim, 5*dim); |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
public DifferentialEvolution (int dim, int popSize) |
| 83 |
{ |
| 84 |
|
| 85 |
|
| 86 |
dimension = dim; |
| 87 |
populationSize = popSize; |
| 88 |
|
| 89 |
numFun = 0; |
| 90 |
|
| 91 |
|
| 92 |
currentPopulation = new double[populationSize][dimension]; |
| 93 |
nextPopulation = new double[populationSize][dimension]; |
| 94 |
costs = new double[populationSize]; |
| 95 |
trialVector = new double[dimension]; |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
numr = 3; |
| 100 |
r = new int[numr]; |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
public void optimize(MultivariateFunction func, double[] xvec, double tolfx, double tolx) |
| 107 |
{ |
| 108 |
optimize(func,xvec,tolfx,tolx,null); |
| 109 |
} |
| 110 |
public void optimize(MultivariateFunction func, double[] xvec, double tolfx, double tolx, MinimiserMonitor monitor) |
| 111 |
{ |
| 112 |
f = func; |
| 113 |
x = xvec; |
| 114 |
|
| 115 |
|
| 116 |
firstGeneration (); |
| 117 |
|
| 118 |
stopCondition(fx, x, tolfx, tolx, true); |
| 119 |
|
| 120 |
while (true) |
| 121 |
{ |
| 122 |
boolean xHasChanged; |
| 123 |
do |
| 124 |
{ |
| 125 |
xHasChanged = nextGeneration (); |
| 126 |
|
| 127 |
if (maxFun > 0 && numFun > maxFun) |
| 128 |
{ |
| 129 |
break; |
| 130 |
} |
| 131 |
|
| 132 |
if (prin > 1 && currGen % 20 == 0) |
| 133 |
{ |
| 134 |
printStatistics(); |
| 135 |
} |
| 136 |
if(monitor!=null) { |
| 137 |
monitor.newMinimum(fx,xvec); |
| 138 |
} |
| 139 |
} |
| 140 |
while (!xHasChanged); |
| 141 |
|
| 142 |
|
| 143 |
if (stopCondition(fx, x, tolfx, tolx, false) || |
| 144 |
(maxFun > 0 && numFun > maxFun)) |
| 145 |
{ |
| 146 |
break; |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
if (prin > 0) printStatistics(); |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
private MultivariateFunction f; |
| 158 |
private int currGen; |
| 159 |
private double fx; |
| 160 |
private double[] x; |
| 161 |
|
| 162 |
|
| 163 |
private int dimension; |
| 164 |
|
| 165 |
|
| 166 |
private int populationSize; |
| 167 |
|
| 168 |
|
| 169 |
private double trialCost; |
| 170 |
private double[] costs; |
| 171 |
private double[] trialVector; |
| 172 |
private double[][] currentPopulation; |
| 173 |
private double[][] nextPopulation; |
| 174 |
|
| 175 |
|
| 176 |
private int numr; |
| 177 |
private int[] r; |
| 178 |
|
| 179 |
private void printStatistics() |
| 180 |
{ |
| 181 |
|
| 182 |
double meanCost = 0.0; |
| 183 |
for (int i = 0; i < populationSize; i++) |
| 184 |
{ |
| 185 |
meanCost += costs[i]; |
| 186 |
} |
| 187 |
meanCost = meanCost/populationSize; |
| 188 |
|
| 189 |
|
| 190 |
double varCost = 0.0; |
| 191 |
for (int i = 0; i < populationSize; i++) |
| 192 |
{ |
| 193 |
double tmp = (costs[i]-meanCost); |
| 194 |
varCost += tmp*tmp; |
| 195 |
} |
| 196 |
varCost = varCost/(populationSize-1); |
| 197 |
|
| 198 |
System.out.println(); |
| 199 |
System.out.println(); |
| 200 |
System.out.println(); |
| 201 |
System.out.println("Smallest value: " + fx); |
| 202 |
System.out.println(); |
| 203 |
for (int k = 0; k < dimension; k++) |
| 204 |
{ |
| 205 |
System.out.println("x[" + k + "] = " + x[k]); |
| 206 |
} |
| 207 |
System.out.println(); |
| 208 |
System.out.println("Current Generation: " + currGen); |
| 209 |
System.out.println("Function evaluations: " + numFun); |
| 210 |
System.out.println("Populations size (populationSize): " + populationSize); |
| 211 |
System.out.println("Average value: " + meanCost); |
| 212 |
System.out.println("Variance: " + varCost); |
| 213 |
|
| 214 |
System.out.println("Weight factor (F): " + F); |
| 215 |
System.out.println("Crossing-over (CR): " + CR); |
| 216 |
System.out.println(); |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
private void firstGeneration() |
| 221 |
{ |
| 222 |
currGen = 1; |
| 223 |
|
| 224 |
|
| 225 |
for (int i = 0; i < populationSize; i++) |
| 226 |
{ |
| 227 |
for (int j = 0; j < dimension; j++ ) |
| 228 |
{ |
| 229 |
double min = f.getLowerBound(j); |
| 230 |
double max = f.getUpperBound(j); |
| 231 |
|
| 232 |
double diff = max - min; |
| 233 |
|
| 234 |
|
| 235 |
currentPopulation[i][j] = min + diff* MathUtils.nextDouble(); |
| 236 |
} |
| 237 |
costs[i] = f.evaluate(currentPopulation[i]); |
| 238 |
} |
| 239 |
numFun += populationSize; |
| 240 |
|
| 241 |
findSmallestCost (); |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
private double checkBounds(double param, int numParam) |
| 246 |
{ |
| 247 |
if (param < f.getLowerBound(numParam)) |
| 248 |
{ |
| 249 |
return f.getLowerBound(numParam); |
| 250 |
} |
| 251 |
else if (param > f.getUpperBound(numParam)) |
| 252 |
{ |
| 253 |
return f.getUpperBound(numParam); |
| 254 |
} |
| 255 |
else |
| 256 |
{ |
| 257 |
return param; |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
private boolean nextGeneration() |
| 263 |
{ |
| 264 |
boolean updateFlag = false; |
| 265 |
int best = 0; |
| 266 |
double[][] swap; |
| 267 |
|
| 268 |
currGen++; |
| 269 |
|
| 270 |
|
| 271 |
for (int r0 = 0; r0 < populationSize; r0++) |
| 272 |
{ |
| 273 |
|
| 274 |
|
| 275 |
r[0] = r0; |
| 276 |
for (int k = 1; k < numr; k++) |
| 277 |
{ |
| 278 |
r[k] = randomInteger (populationSize-k); |
| 279 |
for (int l = 0; l < k; l++) |
| 280 |
{ |
| 281 |
if (r[k] >= r[l]) |
| 282 |
{ |
| 283 |
r[k]++; |
| 284 |
} |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
copy(trialVector, currentPopulation[r0]); |
| 289 |
int n = randomInteger (dimension); |
| 290 |
for (int i = 0; i < dimension; i++) |
| 291 |
{ |
| 292 |
|
| 293 |
if (MathUtils.nextDouble() < CR || i == dimension - 1) |
| 294 |
{ |
| 295 |
|
| 296 |
|
| 297 |
trialVector[n] = trialVector[n] + |
| 298 |
F*(x[n] - trialVector[n]) + |
| 299 |
F*(currentPopulation[r[1]][n] - currentPopulation[r[2]][n]); |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
} |
| 314 |
n = (n+1) % dimension; |
| 315 |
} |
| 316 |
|
| 317 |
|
| 318 |
for (int i = 0; i < dimension; i++) |
| 319 |
{ |
| 320 |
trialVector[i] = checkBounds(trialVector[i], i); |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
trialCost = f.evaluate(trialVector); |
| 326 |
if (trialCost < costs[r0]) |
| 327 |
{ |
| 328 |
|
| 329 |
costs[r0] = trialCost; |
| 330 |
copy(nextPopulation[r0], trialVector); |
| 331 |
|
| 332 |
|
| 333 |
if (trialCost < fx) |
| 334 |
{ |
| 335 |
fx = trialCost; |
| 336 |
best = r0; |
| 337 |
updateFlag = true; |
| 338 |
} |
| 339 |
} |
| 340 |
else |
| 341 |
{ |
| 342 |
|
| 343 |
copy(nextPopulation[r0], currentPopulation[r0]); |
| 344 |
} |
| 345 |
} |
| 346 |
numFun += populationSize; |
| 347 |
|
| 348 |
|
| 349 |
if (updateFlag) |
| 350 |
{ |
| 351 |
copy(x, nextPopulation[best]); |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
swap = currentPopulation; |
| 356 |
currentPopulation = nextPopulation; |
| 357 |
nextPopulation = swap; |
| 358 |
|
| 359 |
return updateFlag; |
| 360 |
} |
| 361 |
|
| 362 |
|
| 363 |
private void findSmallestCost() |
| 364 |
{ |
| 365 |
int best = 0; |
| 366 |
fx = costs[0]; |
| 367 |
|
| 368 |
for (int i = 1; i < populationSize; i++) |
| 369 |
{ |
| 370 |
if (costs[i] < fx) |
| 371 |
{ |
| 372 |
fx = costs[i]; |
| 373 |
best = i; |
| 374 |
} |
| 375 |
} |
| 376 |
copy(x, currentPopulation[best]); |
| 377 |
} |
| 378 |
|
| 379 |
|
| 380 |
private int randomInteger(int n) |
| 381 |
{ |
| 382 |
return MathUtils.nextInt(n); |
| 383 |
} |
| 384 |
} |
| 385 |
|