00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 package org.sci2s.eamhco;
00011
00012 import java.util.ArrayList;
00013 import java.util.StringTokenizer;
00014
00015
00016
00017
00018
00019
00029 public class DEGenerator {
00030
00031
00032
00033 private int PopulationSize;
00034 private int Dimension;
00035 private int MaxIter;
00036 private double ScalingFactor;
00037 private double CrossOverRate;
00038 private int Strategy;
00039 private String CrossoverType;
00040 private long semilla;
00041
00042 private double PI = 3.14159265358;
00043
00044
00049 public DEGenerator(int poblacion, int dimension, int iteraciones, double F, double CR, int strg, String CrossType)
00050 {
00051 this.PopulationSize = poblacion;
00052 this.Dimension = dimension;
00053 this.MaxIter = iteraciones;
00054 this.ScalingFactor = F;
00055 this.CrossOverRate = CR;
00056 this.Strategy = strg;
00057 this.CrossoverType = CrossType;
00058 RandomGenerator.setSeed(this.semilla);
00059
00060 }
00061
00062
00063
00064
00065
00066
00067 public void inic_vector_sin(int vector[], int without){
00068
00069 for(int i=0; i<vector.length; i++)
00070 if(i!=without)
00071 vector[i] = i;
00072 }
00073
00074
00075 public void desordenar_vector_sin(int vector[]){
00076 int tmp, pos;
00077 for(int i=0; i<vector.length-1; i++){
00078 pos = RandomGenerator.Randint(0, vector.length-1);
00079 tmp = vector[i];
00080 vector[i] = vector[pos];
00081 vector[pos] = tmp;
00082 }
00083 }
00084
00085
00095 public Prototype mutant(ArrayList<Prototype> population, int actual, int mejor){
00096
00097
00098 Prototype mutant = new Prototype();
00099 Prototype r1,r2,r3,r4,r5, resta, producto, resta2, producto2, result, producto3, resta3;
00100
00101
00102
00103 int lista[] = new int[population.size()];
00104 inic_vector_sin(lista,actual);
00105 desordenar_vector_sin(lista);
00106
00107
00108
00109 r1 = population.get(lista[0]);
00110 r2 = population.get(lista[1]);
00111 r3 = population.get(lista[2]);
00112 r4 = population.get(lista[3]);
00113 r5 = population.get(lista[4]);
00114
00115 double SFi = this.ScalingFactor;
00116
00117 switch(this.Strategy){
00118 case 1:
00119 resta = r2.sub(r3);
00120 producto = resta.mul(SFi);
00121 mutant = producto.add(r1);
00122 break;
00123
00124 case 2:
00125
00126 resta = r2.sub(r3);
00127 producto = resta.mul(SFi);
00128 mutant = population.get(mejor).add(producto);
00129 break;
00130
00131 case 3:
00132 resta = r1.sub(r2);
00133 resta2 = population.get(mejor).sub(population.get(actual));
00134
00135 producto = resta.mul(SFi);
00136 producto2 = resta2.mul(SFi);
00137
00138 result = population.get(actual).add(producto);
00139 mutant = result.add(producto2);
00140
00141 break;
00142
00143 case 4:
00144 resta = r1.sub(r2);
00145 resta2 = r3.sub(r4);
00146
00147 producto = resta.mul(SFi);
00148 producto2 = resta2.mul(SFi);
00149
00150 result = population.get(mejor).add(producto);
00151 mutant = result.add(producto2);
00152 break;
00153
00154 case 5:
00155 resta = r2.sub(r3);
00156 resta2 = r4.sub(r5);
00157
00158 producto = resta.mul(SFi);
00159 producto2 = resta2.mul(SFi);
00160
00161 result = r1.add(producto);
00162 mutant = result.add(producto2);
00163
00164 break;
00165
00166 case 6:
00167 resta = r1.sub(r2);
00168 resta2 = r3.sub(r4);
00169 resta3 =population.get(mejor).sub(population.get(actual));
00170
00171 producto = resta.mul(SFi);
00172 producto2 = resta2.mul(SFi);
00173 producto3 = resta3.mul(SFi);
00174
00175 result = population.get(actual).add(producto);
00176 result = result.add(producto2);
00177 mutant = result.add(producto3);
00178 break;
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193 }
00194
00195
00196
00197
00198
00199
00200
00201 return mutant;
00202 }
00203
00204
00205
00206
00207
00208 double calc_rastrigin (double x[])
00209 {
00210 int i;
00211 double res;
00212 res = 0.0;
00213 for (i=0; i<this.Dimension; i++)
00214 {
00215 res += (x[i]*x[i] - 10.0*Math.cos(2.0*this.PI*x[i]) + 10.0);
00216 }
00217 return (res-(330));
00218 }
00219
00220
00221
00222
00223
00224
00228 double fitness(Prototype p){
00229
00230 return calc_rastrigin(p.getInputs());
00231
00232 }
00233
00234
00235
00241 public void execute()
00242 {
00243 System.out.print("\nThe algorithm DE is starting...\n Computing...\n");
00244
00245
00246
00247 ArrayList<Prototype> population = new ArrayList<Prototype>();
00248 Prototype mutation = new Prototype();
00249 Prototype crossover = new Prototype();
00250
00251 double fitness[] = new double[PopulationSize];
00252
00253
00254
00255
00256 for(int i=0; i< PopulationSize; i++){
00257 Prototype cromosoma = new Prototype(this.Dimension,1);
00258
00259 for(int j=0; j< this.Dimension; j++){
00260 cromosoma.setInput(j, RandomGenerator.Randdouble(-200, 200));
00261 }
00262
00263
00264 fitness[i]= fitness(cromosoma);
00265
00266 population.add(cromosoma);
00267 }
00268
00269
00270
00271
00272
00273
00274 double bestFitness=fitness[0];
00275 int bestFitnessIndex=0;
00276
00277 for(int i=1; i< PopulationSize;i++){
00278 if(fitness[i]<bestFitness){
00279 bestFitness = fitness[i];
00280 bestFitnessIndex=i;
00281 }
00282
00283 }
00284
00285
00286 boolean cruceExp [] = new boolean[PopulationSize];
00287
00288
00289 for(int iter=0; iter< MaxIter; iter++){
00290
00291
00292
00293
00294 if(this.CrossoverType.equals("Exponential")){
00295
00296 for(int i=0; i<PopulationSize; i++){
00297 cruceExp[i] = false;
00298 }
00299
00300 }
00301
00302
00303
00304 for(int i=0; i<PopulationSize; i++){
00305
00306
00307 mutation = new Prototype();
00308
00309
00310
00311 mutation = mutant(population, i, bestFitnessIndex);
00312
00313
00314
00315 crossover = new Prototype(population.get(i));
00316
00317 for(int j=0; j< (population.get(i)).numberOfInputs(); j++){
00318
00319 if(this.CrossoverType.equals("Binomial")){
00320 double randNumber = RandomGenerator.Randdouble(0, 1);
00321
00322 if(randNumber< this.CrossOverRate){
00323 crossover.setInput(j, mutation.getInput(j));
00324 }
00325 }else if(this.CrossoverType.equals("Exponential")){
00326 int startingPoint = RandomGenerator.Randint(0, PopulationSize);
00327
00328 int L=0;
00329 do{
00330 L++;
00331 }while(RandomGenerator.Randdouble(0, 1)<this.CrossOverRate && (L< population.get(i).numberOfInputs()));
00332
00333 for(int m=startingPoint; m< startingPoint+L; m++){
00334 crossover.setInput(m%population.get(i).numberOfInputs(), mutation.getInput(j));
00335 }
00336 }else if(this.CrossoverType.equals("Arithmetic")){
00337 Prototype resta = mutation.sub(population.get(i));
00338 crossover = population.get(i).add(resta.mul(RandomGenerator.Randdouble(0, 1)));
00339 }else{
00340 System.err.println("ERROR, Crossover Type incorrect.");
00341 }
00342
00343
00344
00345 }
00346
00347
00348
00349
00350
00351
00352
00353 double trialVector = fitness(crossover);
00354
00355
00356
00357
00358
00359
00360
00361
00362 if(trialVector < fitness[i]){
00363
00364 population.set(i, crossover);
00365 fitness[i] = trialVector;
00366 }
00367
00368
00369 if(fitness[i]<bestFitness){
00370 bestFitness = fitness[i];
00371 bestFitnessIndex=i;
00372
00373
00374 }
00375
00376
00377 }
00378
00379
00380
00381
00382
00383
00384
00385 }
00386
00387 System.out.println("Best -> "+ fitness[bestFitnessIndex]);
00388
00389
00390 }
00391
00392
00393
00394 public void leerConfiguracion (String ficheroScript) {
00395
00396 String fichero, linea, token;
00397 StringTokenizer lineasFichero, tokens;
00398 byte line[];
00399 int i, j;
00400
00401 fichero = Fichero.leeFichero (ficheroScript);
00402 lineasFichero = new StringTokenizer (fichero,"\n\r");
00403
00404
00405
00406 linea = lineasFichero.nextToken();
00407 tokens = new StringTokenizer (linea, "=");
00408 tokens.nextToken();
00409 semilla = Long.parseLong(tokens.nextToken().substring(1));
00410
00411 linea = lineasFichero.nextToken();
00412 tokens = new StringTokenizer (linea, "=");
00413 tokens.nextToken();
00414 this.PopulationSize = Integer.parseInt(tokens.nextToken().substring(1));
00415
00416
00417 linea = lineasFichero.nextToken();
00418 tokens = new StringTokenizer (linea, "=");
00419 tokens.nextToken();
00420 this.Dimension = Integer.parseInt(tokens.nextToken().substring(1));
00421
00422
00423 linea = lineasFichero.nextToken();
00424 tokens = new StringTokenizer (linea, "=");
00425 tokens.nextToken();
00426 this.MaxIter = Integer.parseInt(tokens.nextToken().substring(1));
00427
00428 linea = lineasFichero.nextToken();
00429 tokens = new StringTokenizer (linea, "=");
00430 tokens.nextToken();
00431 this.ScalingFactor = Double.parseDouble(tokens.nextToken().substring(1));
00432
00433 linea = lineasFichero.nextToken();
00434 tokens = new StringTokenizer (linea, "=");
00435 tokens.nextToken();
00436 this.CrossOverRate = Double.parseDouble(tokens.nextToken().substring(1));
00437
00438 linea = lineasFichero.nextToken();
00439 tokens = new StringTokenizer (linea, "=");
00440 tokens.nextToken();
00441 this.Strategy = Integer.parseInt(tokens.nextToken().substring(1));
00442
00443 linea = lineasFichero.nextToken();
00444 tokens = new StringTokenizer (linea, "=");
00445 tokens.nextToken();
00446 this.CrossoverType = tokens.nextToken().substring(1);
00447
00448 }
00449
00450
00454 public static void main(String[] args)
00455 {
00456
00457
00458 RandomGenerator.setSeed(939393828);
00459 int Poblacion=100, dimension=30, iteraciones=500000, estrategia =3;
00460 double F=.5, CR=.5;
00461 String CrossType = "Binomial";
00462
00463 DEGenerator differential;
00464
00465 differential = new DEGenerator(Poblacion,dimension,iteraciones, F,CR,estrategia, CrossType);
00466
00467 if(args.length == 1){
00468 differential.leerConfiguracion(args[0]);
00469 }
00470
00471 differential.execute();
00472
00473
00474 }
00475
00476 }