00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 package org.sci2s.eamhco;
00011
00012 import java.util.*;
00013
00014
00015
00016
00021 public class SADEGenerator {
00022
00023
00024
00025
00026 private int k;
00027
00028 private int PopulationSize;
00029 private int Dimension;
00030 private int MaxIter;
00031 private double ScalingFactor;
00032 private double CrossOverRate[];
00033 private int LearningPeriod;
00034 private int Strategy;
00035 private int numberOfStrategies = 4;
00036
00037 private double PI = 3.14159265358;
00038 private long semilla;
00039
00044 public SADEGenerator(int poblacion, int dimension, int iteraciones, int LP)
00045 {
00046 this.PopulationSize = poblacion;
00047 this.Dimension = dimension;
00048 this.MaxIter = iteraciones;
00049 this.LearningPeriod = LP;
00050
00051 }
00052
00053
00054
00055
00056
00057
00058
00059 public void inic_vector_sin(int vector[], int without){
00060
00061 for(int i=0; i<vector.length; i++)
00062 if(i!=without)
00063 vector[i] = i;
00064 }
00065
00066
00067 public void desordenar_vector_sin(int vector[]){
00068 int tmp, pos;
00069 for(int i=0; i<vector.length-1; i++){
00070 pos = RandomGenerator.Randint(0, vector.length-1);
00071 tmp = vector[i];
00072 vector[i] = vector[pos];
00073 vector[pos] = tmp;
00074 }
00075 }
00076
00077
00078
00087 public Prototype mutant(ArrayList<Prototype> population, int actual, int mejor){
00088
00089 Prototype mutant = new Prototype();
00090 Prototype r1,r2,r3,r4,r5, resta, producto, resta2, producto2, result, producto3, resta3;
00091
00092
00093
00094 int lista[] = new int[population.size()];
00095 inic_vector_sin(lista,actual);
00096 desordenar_vector_sin(lista);
00097
00098
00099
00100 r1 = population.get(lista[0]);
00101 r2 = population.get(lista[1]);
00102 r3 = population.get(lista[2]);
00103 r4 = population.get(lista[3]);
00104 r5 = population.get(lista[4]);
00105
00106 double SFi = this.ScalingFactor;
00107
00108 switch(this.Strategy){
00109 case 1:
00110 resta = r2.sub(r3);
00111 producto = resta.mul(SFi);
00112 mutant = producto.add(r1);
00113 break;
00114
00115 case 2:
00116 resta = r2.sub(r3);
00117 resta2 = r4.sub(r5);
00118
00119 producto = resta.mul(SFi);
00120 producto2 = resta2.mul(SFi);
00121
00122 result = r1.add(producto);
00123 mutant = result.add(producto2);
00124 break;
00125
00126
00127 case 3:
00128 resta = r1.sub(r2);
00129 resta2 = r3.sub(r4);
00130 resta3 =population.get(mejor).sub(population.get(actual));
00131
00132 producto = resta.mul(SFi);
00133 producto2 = resta2.mul(SFi);
00134 producto3 = resta3.mul(SFi);
00135
00136 result = population.get(actual).add(producto);
00137 result = result.add(producto2);
00138 mutant = result.add(producto3);
00139
00140
00141
00142 break;
00143
00144 case 4:
00145 resta = r1.sub(population.get(actual));
00146 resta2 = r2.sub(r3);
00147
00148 producto = resta.mul(RandomGenerator.Randdouble(0, 1));
00149 producto2 = resta2.mul(this.ScalingFactor);
00150
00151 result = population.get(actual).add(producto);
00152 mutant = result.add(producto2);
00153
00154 break;
00155
00156
00157
00158
00159 }
00160
00161
00162
00163
00164
00165
00166 return mutant;
00167 }
00168
00169
00170 public double Skg (int strategy, int successRate[][], int failureRate[][]){
00171 double numerator=0, denominator=0;
00172
00173 for(int k=0; k<this.LearningPeriod; k++){
00174 numerator += successRate[k][strategy];
00175 denominator += successRate[k][strategy] + failureRate[k][strategy];
00176 }
00177
00178 double SKG = numerator/denominator + 0.01;
00179
00180 return SKG;
00181 }
00182
00183
00184
00185
00186
00187 public double updateProbability(int strategy,int successRate[][],int failureRate[][]){
00188
00189 double numerator=0, denominator =0;
00190
00191 numerator =Skg(strategy,successRate, failureRate);
00192
00193 for(int i =0; i< this.numberOfStrategies;i++){
00194 denominator+=Skg(i,successRate, failureRate);
00195 }
00196
00197
00198 return (numerator/denominator);
00199 }
00200
00206 public int selectStrategy(double ProbabilityStrategy[]){
00207
00208 double random=RandomGenerator.Randdouble(0, 1);
00209
00210 double aux =0;
00211 boolean end = false;
00212 int selected=1;
00213
00214 for(int i=0; i< this.numberOfStrategies && !end; i++){
00215 aux += ProbabilityStrategy[i];
00216
00217 if(random <= aux){
00218 selected = i+1;
00219 end =true;
00220 }
00221 }
00222
00223 return selected;
00224 }
00225
00226
00227
00228 double calc_rastrigin (double x[])
00229 {
00230 int i;
00231 double res;
00232 res = 0.0;
00233 for (i=0; i<this.Dimension; i++)
00234 {
00235 res += (x[i]*x[i] - 10.0*Math.cos(2.0*this.PI*x[i]) + 10.0);
00236 }
00237 return (res-(330));
00238 }
00239
00240
00241
00242
00243
00244 double fitness(Prototype p){
00245
00246 return calc_rastrigin(p.getInputs());
00247
00248 }
00249
00250
00257 public void execute()
00258 {
00259 System.out.print("\nThe algorithm SADE is starting...\n Computing...\n");
00260
00261
00262
00263
00264
00265
00266
00267 ArrayList<Prototype> population = new ArrayList<Prototype>();
00268 Prototype mutation = new Prototype();
00269 Prototype crossover = new Prototype();
00270
00271
00272
00273
00274 int kStrategies = this.numberOfStrategies;
00275
00276 double ProbabilityStrategy[] = new double[kStrategies];
00277 double CRmk[] = new double[kStrategies];
00278 double CRmemory[][] = new double[this.LearningPeriod][];
00279 double CRm=0.5;
00280
00281 int successRate[][] = new int[this.LearningPeriod][];
00282 int failureRate[][] = new int[this.LearningPeriod][];
00283
00284 for(int i=0; i<this.LearningPeriod; i++){
00285 successRate[i] = new int[kStrategies];
00286 failureRate[i] = new int[kStrategies];
00287 CRmemory[i] = new double[kStrategies];
00288 for(int j=0; j<kStrategies; j++){
00289 successRate[i][j] = 1;
00290 failureRate[i][j] = 1;
00291 CRmemory[i][j] = 0.5;
00292 }
00293 }
00294
00295
00296
00297
00298
00299 for(int i=0; i<ProbabilityStrategy.length; i++){
00300 ProbabilityStrategy[i] = 1./kStrategies;
00301 CRmk[i] = 0.5;
00302 }
00303
00304
00305 this.CrossOverRate= new double[this.numberOfStrategies];
00306 double F[] = new double[this.PopulationSize];
00307
00308 double fitness[] = new double[PopulationSize];
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318 for(int i=0; i< PopulationSize; i++){
00319 Prototype cromosoma = new Prototype(this.Dimension,1);
00320
00321 for(int j=0; j< this.Dimension; j++){
00322 cromosoma.setInput(j, RandomGenerator.Randdouble(-200, 200));
00323 }
00324
00325
00326 fitness[i]= fitness(cromosoma);
00327
00328 population.add(cromosoma);
00329 }
00330
00331
00332
00333
00334
00335
00336 double bestFitness=fitness[0];
00337 int bestFitnessIndex=0;
00338
00339 for(int i=1; i< PopulationSize;i++){
00340 if(fitness[i]<bestFitness){
00341 bestFitness = fitness[i];
00342 bestFitnessIndex=i;
00343 }
00344
00345 }
00346
00347
00348
00349 for(int iter=1; iter<= MaxIter; iter++){
00350
00351
00352
00353 if(iter%this.LearningPeriod == 0){
00354
00355 for(int k=0; k<kStrategies; k++){
00356
00357
00358 ProbabilityStrategy[k]= updateProbability(k, successRate, failureRate);
00359
00360
00361
00362 successRate[iter%this.LearningPeriod][k]= 0;
00363 failureRate[iter%this.LearningPeriod][k] =0;
00364 }
00365
00366
00367 }
00368
00369
00370
00371
00372
00373
00374
00375 for(int i=0; i< this.PopulationSize; i++){
00376 F[i]= RandomGenerator.RandGaussian()*0.3 + 0.5;
00377 }
00378
00379
00380
00381
00382 if(iter%this.LearningPeriod == 0){
00383
00384 for(int k=0; k< this.numberOfStrategies; k++){
00385 CRmk[k] = 0 ;
00386
00387 for(int m=0; m< this.LearningPeriod; m++){
00388 CRmk[k] += CRmemory[m][k];
00389 }
00390
00391 CRmk[k] /= this.LearningPeriod;
00392 }
00393
00394 }
00395
00396 for (int k=0; k< this.numberOfStrategies; k++){
00397 this.CrossOverRate[k] = RandomGenerator.RandGaussian()*0.1 + CRmk[k];
00398
00399 while( this.CrossOverRate[k] < 0 || this.CrossOverRate[k]>1){
00400 this.CrossOverRate[k] = RandomGenerator.RandGaussian()*0.1 + CRmk[k];
00401 }
00402 }
00403
00404
00405
00406
00407 for(int i=0; i<PopulationSize; i++){
00408
00409 this.Strategy = selectStrategy(ProbabilityStrategy);
00410
00411
00412
00413 this.ScalingFactor = F[i];
00414
00415 mutation = new Prototype();
00416
00417
00418 mutation = mutant(population, i,bestFitnessIndex);
00419
00420
00421
00422
00423 crossover = new Prototype(population.get(i));
00424
00425 if(this.Strategy !=4){
00426
00427 for(int j=0; j< (population.get(i)).numberOfInputs(); j++){
00428
00429 double randNumber = RandomGenerator.Randdouble(0, 1);
00430
00431 if(randNumber<CrossOverRate[this.Strategy]){
00432 crossover.setInput(j, mutation.getInput(j));
00433 }
00434 }
00435
00436 }else{
00437 crossover = mutation;
00438 }
00439
00440
00441 double trialVector = fitness(crossover);
00442
00443
00444
00445
00446
00447
00448
00449
00450 if(trialVector < fitness[i]){
00451
00452 successRate[iter%this.LearningPeriod][this.Strategy-1]++;
00453 CRmemory[iter%this.LearningPeriod][this.Strategy-1] = this.CrossOverRate[this.Strategy-1];
00454 population.set(i, crossover);
00455 fitness[i] = trialVector;
00456 }else{
00457
00458 failureRate[iter%this.LearningPeriod][this.Strategy-1]++;
00459 }
00460
00461
00462 if(fitness[i]<bestFitness){
00463 bestFitness = fitness[i];
00464 bestFitnessIndex=i;
00465
00466
00467 }
00468
00469
00470 }
00471
00472
00473
00474 }
00475
00476
00477 System.out.println("Best -> "+ fitness[bestFitnessIndex]);
00478
00479
00480
00481 }
00482
00483
00484
00485 public void leerConfiguracion (String ficheroScript) {
00486
00487 String fichero, linea, token;
00488 StringTokenizer lineasFichero, tokens;
00489 byte line[];
00490 int i, j;
00491
00492 fichero = Fichero.leeFichero (ficheroScript);
00493 lineasFichero = new StringTokenizer (fichero,"\n\r");
00494
00495
00496
00497 linea = lineasFichero.nextToken();
00498 tokens = new StringTokenizer (linea, "=");
00499 tokens.nextToken();
00500 semilla = Long.parseLong(tokens.nextToken().substring(1));
00501
00502 linea = lineasFichero.nextToken();
00503 tokens = new StringTokenizer (linea, "=");
00504 tokens.nextToken();
00505 this.PopulationSize = Integer.parseInt(tokens.nextToken().substring(1));
00506
00507
00508 linea = lineasFichero.nextToken();
00509 tokens = new StringTokenizer (linea, "=");
00510 tokens.nextToken();
00511 this.Dimension = Integer.parseInt(tokens.nextToken().substring(1));
00512
00513
00514 linea = lineasFichero.nextToken();
00515 tokens = new StringTokenizer (linea, "=");
00516 tokens.nextToken();
00517 this.MaxIter = Integer.parseInt(tokens.nextToken().substring(1));
00518
00519 linea = lineasFichero.nextToken();
00520 tokens = new StringTokenizer (linea, "=");
00521 tokens.nextToken();
00522 this.LearningPeriod = Integer.parseInt(tokens.nextToken().substring(1));
00523
00524
00525 }
00526
00527
00531 public static void main(String[] args)
00532 {
00533
00534 RandomGenerator.setSeed(818469644);
00535
00536
00537 int Poblacion=100, dimension=10, iteraciones=100000, LP = 50;
00538
00539 SADEGenerator differential = new SADEGenerator(Poblacion,dimension,iteraciones, LP);
00540
00541
00542 if(args.length == 1){
00543 differential.leerConfiguracion(args[0]);
00544 }
00545
00546
00547 differential.execute();
00548
00549
00550 }
00551
00552 }