00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 package org.sci2s.eamhco;
00011 import java.util.*;
00012
00013
00014
00019 public class SFLSDEGenerator{
00020
00021
00022
00023 private int PopulationSize;
00024 private int Dimension;
00025 private int MaxIter;
00026 private double ScalingFactor;
00027 private double CrossOverRate;
00028 private int Strategy;
00029 private long semilla;
00030
00031
00032 private double PI = 3.14159265358;
00033
00034 private double tau[] = new double[4];
00035 private double Fl, Fu;
00036
00037 private int iterSFGSS;
00038 private int iterSFHC;
00039
00040
00045 public SFLSDEGenerator(int poblacion, int dimension, int iteraciones, double F, double CR, int strg)
00046 {
00047 this.PopulationSize = poblacion;
00048 this.Dimension = dimension;
00049 this.MaxIter = iteraciones;
00050 this.ScalingFactor = F;
00051 this.CrossOverRate = CR;
00052 this.Strategy = strg;
00053
00054
00055 this.Fl = 0.1;
00056 this.Fu = 0.9;
00057 this.tau[0] = 0.1;
00058 this.tau[1] = 0.1;
00059 this.tau[2] = 0.03;
00060 this.tau[3] = 0.07;
00061
00062 }
00063
00064
00065
00066
00067
00068
00069
00070
00071 public void inic_vector_sin(int vector[], int without){
00072
00073 for(int i=0; i<vector.length; i++)
00074 if(i!=without)
00075 vector[i] = i;
00076 }
00077
00078
00079 public void desordenar_vector_sin(int vector[]){
00080 int tmp, pos;
00081 for(int i=0; i<vector.length-1; i++){
00082 pos = RandomGenerator.Randint(0, vector.length-1);
00083 tmp = vector[i];
00084 vector[i] = vector[pos];
00085 vector[pos] = tmp;
00086 }
00087 }
00088
00089
00090
00091
00092 public Prototype mutant(ArrayList<Prototype> population, int actual, int mejor, double SFi){
00093
00094
00095 Prototype mutant = new Prototype();
00096 Prototype r1,r2,r3,r4,r5, resta, producto, resta2, producto2, result, producto3, resta3;
00097
00098
00099
00100 int lista[] = new int[population.size()];
00101 inic_vector_sin(lista,actual);
00102 desordenar_vector_sin(lista);
00103
00104
00105
00106 r1 = population.get(lista[0]);
00107 r2 = population.get(lista[1]);
00108 r3 = population.get(lista[2]);
00109 r4 = population.get(lista[3]);
00110 r5 = population.get(lista[4]);
00111
00112
00113
00114 switch(this.Strategy){
00115 case 1:
00116 resta = r2.sub(r3);
00117 producto = resta.mul(SFi);
00118 mutant = producto.add(r1);
00119 break;
00120
00121 case 2:
00122
00123 resta = r2.sub(r3);
00124 producto = resta.mul(SFi);
00125 mutant = population.get(mejor).add(producto);
00126 break;
00127
00128 case 3:
00129 resta = r1.sub(r2);
00130 resta2 = population.get(mejor).sub(population.get(actual));
00131
00132 producto = resta.mul(SFi);
00133 producto2 = resta2.mul(SFi);
00134
00135 result = population.get(actual).add(producto);
00136 mutant = result.add(producto2);
00137
00138 break;
00139
00140 case 4:
00141 resta = r1.sub(r2);
00142 resta2 = r3.sub(r4);
00143
00144 producto = resta.mul(SFi);
00145 producto2 = resta2.mul(SFi);
00146
00147 result = population.get(mejor).add(producto);
00148 mutant = result.add(producto2);
00149 break;
00150
00151 case 5:
00152 resta = r2.sub(r3);
00153 resta2 = r4.sub(r5);
00154
00155 producto = resta.mul(SFi);
00156 producto2 = resta2.mul(SFi);
00157
00158 result = r1.add(producto);
00159 mutant = result.add(producto2);
00160
00161 break;
00162
00163 case 6:
00164 resta = r1.sub(r2);
00165 resta2 = r3.sub(r4);
00166 resta3 =population.get(mejor).sub(population.get(actual));
00167
00168 producto = resta.mul(SFi);
00169 producto2 = resta2.mul(SFi);
00170 producto3 = resta3.mul(SFi);
00171
00172 result = population.get(actual).add(producto);
00173 result = result.add(producto2);
00174 mutant = result.add(producto3);
00175 break;
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190 }
00191
00192
00193
00194
00195
00196
00197
00198 return mutant;
00199 }
00200
00209 public double lsff(double Fi, double CRi, ArrayList<Prototype> population, int actual, int mejor){
00210 Prototype resta, producto, mutant;
00211 Prototype crossover;
00212 double FitnessFi = 0;
00213
00214
00215
00216 mutant = new Prototype();
00217 mutant = mutant(population, actual, mejor, Fi);
00218
00219
00220
00221
00222 crossover = new Prototype(population.get(actual));
00223
00224 for(int j=0; j< (population.get(actual)).numberOfInputs(); j++){
00225
00226 double randNumber = RandomGenerator.Randdouble(0, 1);
00227
00228 if(randNumber<CRi){
00229 crossover.setInput(j, mutant.getInput(j));
00230 }
00231 }
00232
00233
00234
00235
00236
00237 FitnessFi = fitness(crossover);
00238
00239 return FitnessFi;
00240 }
00241
00242
00243
00249 public Prototype SFGSS(ArrayList<Prototype> population, int actual, int mejor, double CRi){
00250 double a=0.1, b=1;
00251 double fi1=0, fi2=0, fitnessFi1=0, fitnessFi2=0;
00252 double phi = (1+ Math.sqrt(5))/5;
00253 double scaling;
00254 Prototype crossover, resta, producto, mutant;
00255
00256 for (int i=0; i<this.iterSFGSS; i++){
00257
00258 fi1 = b - (b-a)/phi;
00259 fi2 = a + (b-a)/phi;
00260
00261 fitnessFi1 = lsff(fi1, CRi, population,actual,mejor);
00262 fitnessFi2 = lsff(fi2, CRi,population,actual,mejor);
00263
00264 if(fitnessFi1> fitnessFi2){
00265 b = fi2;
00266 }else{
00267 a = fi1;
00268 }
00269
00270 }
00271
00272
00273 if(fitnessFi1> fitnessFi2){
00274 scaling = fi1;
00275 }else{
00276 scaling = fi2;
00277 }
00278
00279
00280
00281 mutant = new Prototype();
00282 mutant = mutant(population, actual, mejor, scaling);
00283
00284
00285
00286
00287 crossover = new Prototype(population.get(actual));
00288
00289 for(int j=0; j< (population.get(actual)).numberOfInputs(); j++){
00290
00291 double randNumber = RandomGenerator.Randdouble(0, 1);
00292
00293 if(randNumber<CRi){
00294 crossover.setInput(j, mutant.getInput(j));
00295 }
00296 }
00297
00298
00299
00300 return crossover;
00301 }
00302
00313 public Prototype SFHC(ArrayList<Prototype> population, int actual, int mejor, double SFi, double CRi){
00314 double fitnessFi1, fitnessFi2, fitnessFi3, bestFi;
00315 Prototype crossover, resta, producto, mutant;
00316 double h= 0.5;
00317
00318
00319 for (int i=0; i<this.iterSFHC; i++){
00320
00321 fitnessFi1 = lsff(SFi-h, CRi, population,actual,mejor);
00322 fitnessFi2 = lsff(SFi, CRi, population,actual,mejor);
00323 fitnessFi3 = lsff(SFi+h, CRi, population,actual,mejor);
00324
00325 if(fitnessFi1 >= fitnessFi2 && fitnessFi1 >= fitnessFi3){
00326 bestFi = SFi-h;
00327 }else if(fitnessFi2 >= fitnessFi1 && fitnessFi2 >= fitnessFi3){
00328 bestFi = SFi;
00329 h = h/2;
00330 }else{
00331 bestFi = SFi;
00332 }
00333
00334 SFi = bestFi;
00335 }
00336
00337
00338
00339 mutant = new Prototype();
00340 mutant = mutant(population, actual, mejor, SFi);
00341
00342
00343
00344
00345 crossover = new Prototype(population.get(actual));
00346
00347 for(int j=0; j< (population.get(actual)).numberOfInputs(); j++){
00348
00349 double randNumber = RandomGenerator.Randdouble(0, 1);
00350
00351 if(randNumber<CRi){
00352 crossover.setInput(j, mutant.getInput(j));
00353 }
00354 }
00355
00356
00357
00358 return crossover;
00359
00360 }
00361
00362
00363
00364
00365 double calc_rastrigin (double x[])
00366 {
00367 int i;
00368 double res;
00369 res = 0.0;
00370 for (i=0; i<this.Dimension; i++)
00371 {
00372 res += (x[i]*x[i] - 10.0*Math.cos(2.0*this.PI*x[i]) + 10.0);
00373 }
00374 return (res-(330));
00375 }
00376
00377
00378
00379
00380
00381 double fitness(Prototype p){
00382
00383 return calc_rastrigin(p.getInputs());
00384
00385 }
00386
00392 public void execute()
00393 {
00394 System.out.print("\nThe algorithm SFLSDE is starting...\n Computing...\n");
00395
00396
00397
00398 ArrayList<Prototype> population = new ArrayList<Prototype>();
00399 Prototype mutation = new Prototype();
00400 Prototype crossover = new Prototype();
00401 double ScalingFactor[] = new double[this.PopulationSize];
00402 double CrossOverRate[] = new double[this.PopulationSize];
00403 double fitness[] = new double[PopulationSize];
00404
00405
00406
00407
00408 for(int i=0; i< PopulationSize; i++){
00409 Prototype cromosoma = new Prototype(this.Dimension,1);
00410
00411 for(int j=0; j< this.Dimension; j++){
00412 cromosoma.setInput(j, RandomGenerator.Randdouble(-200, 200));
00413 }
00414
00415
00416 fitness[i]= fitness(cromosoma);
00417
00418 population.add(cromosoma);
00419 }
00420
00421
00422
00423
00424
00425
00426 double bestFitness=fitness[0];
00427 int bestFitnessIndex=0;
00428
00429 for(int i=1; i< PopulationSize;i++){
00430 if(fitness[i]<bestFitness){
00431 bestFitness = fitness[i];
00432 bestFitnessIndex=i;
00433 }
00434
00435 }
00436
00437
00438 boolean cruceExp [] = new boolean[PopulationSize];
00439
00440
00441
00442 for(int i=0; i< this.PopulationSize; i++){
00443 ScalingFactor[i] = RandomGenerator.Randdouble(0, 1);
00444 CrossOverRate[i] = RandomGenerator.Randdouble(0, 1);
00445 }
00446
00447
00448 double randj[] = new double[5];
00449
00450 for(int iter=0; iter< MaxIter; iter++){
00451
00452 for(int i=0; i<PopulationSize; i++){
00453
00454
00455 for(int j=0; j<5; j++){
00456 randj[j] = RandomGenerator.Randdouble(0, 1);
00457 }
00458
00459
00460
00461
00462 if(i==bestFitnessIndex && randj[4] < tau[2]){
00463
00464
00465 crossover = SFGSS(population, i, bestFitnessIndex, CrossOverRate[i]);
00466
00467
00468 }else if(i==bestFitnessIndex && tau[2] <= randj[4] && randj[4] < tau[3]){
00469
00470
00471 crossover = SFHC(population, i, bestFitnessIndex, ScalingFactor[i], CrossOverRate[i]);
00472
00473 }else {
00474
00475
00476
00477 if(randj[1] < tau[0]){
00478 ScalingFactor[i] = this.Fl + this.Fu*randj[0];
00479 }
00480
00481
00482
00483 if(randj[3] < tau[1]){
00484 CrossOverRate[i] = randj[2];
00485 }
00486
00487
00488
00489 mutation = new Prototype();
00490
00491
00492
00493 mutation = mutant(population, i, bestFitnessIndex, ScalingFactor[i]);
00494
00495
00496
00497 crossover = new Prototype(population.get(i));
00498
00499 for(int j=0; j< (population.get(i)).numberOfInputs(); j++){
00500
00501 double randNumber = RandomGenerator.Randdouble(0, 1);
00502
00503 if(randNumber<CrossOverRate[i]){
00504 crossover.setInput(j, mutation.getInput(j));
00505 }
00506 }
00507
00508
00509 double trialVector = fitness(crossover);
00510
00511
00512
00513
00514
00515
00516
00517
00518 if(trialVector < fitness[i]){
00519
00520 population.set(i, crossover);
00521 fitness[i] = trialVector;
00522 }
00523
00524
00525 if(fitness[i]<bestFitness){
00526 bestFitness = fitness[i];
00527 bestFitnessIndex=i;
00528
00529
00530 }
00531
00532
00533
00534 }
00535
00536
00537
00538
00539
00540
00541 }
00542
00543
00544 }
00545
00546
00547
00548
00549 System.out.println("Best -> "+ fitness[bestFitnessIndex]);
00550
00551
00552 }
00553
00554
00555
00556 public void leerConfiguracion (String ficheroScript) {
00557
00558 String fichero, linea, token;
00559 StringTokenizer lineasFichero, tokens;
00560 byte line[];
00561 int i, j;
00562
00563 fichero = Fichero.leeFichero (ficheroScript);
00564 lineasFichero = new StringTokenizer (fichero,"\n\r");
00565
00566
00567
00568 linea = lineasFichero.nextToken();
00569 tokens = new StringTokenizer (linea, "=");
00570 tokens.nextToken();
00571 semilla = Long.parseLong(tokens.nextToken().substring(1));
00572
00573 linea = lineasFichero.nextToken();
00574 tokens = new StringTokenizer (linea, "=");
00575 tokens.nextToken();
00576 this.PopulationSize = Integer.parseInt(tokens.nextToken().substring(1));
00577
00578
00579 linea = lineasFichero.nextToken();
00580 tokens = new StringTokenizer (linea, "=");
00581 tokens.nextToken();
00582 this.Dimension = Integer.parseInt(tokens.nextToken().substring(1));
00583
00584
00585
00586 linea = lineasFichero.nextToken();
00587 tokens = new StringTokenizer (linea, "=");
00588 tokens.nextToken();
00589 this.MaxIter = Integer.parseInt(tokens.nextToken().substring(1));
00590
00591 linea = lineasFichero.nextToken();
00592 tokens = new StringTokenizer (linea, "=");
00593 tokens.nextToken();
00594 this.iterSFGSS = Integer.parseInt(tokens.nextToken().substring(1));
00595
00596 linea = lineasFichero.nextToken();
00597 tokens = new StringTokenizer (linea, "=");
00598 tokens.nextToken();
00599 this.iterSFHC = Integer.parseInt(tokens.nextToken().substring(1));
00600
00601 linea = lineasFichero.nextToken();
00602 tokens = new StringTokenizer (linea, "=");
00603 tokens.nextToken();
00604 this.Fl = Double.parseDouble(tokens.nextToken().substring(1));
00605
00606
00607 linea = lineasFichero.nextToken();
00608 tokens = new StringTokenizer (linea, "=");
00609 tokens.nextToken();
00610 this.Fu = Double.parseDouble(tokens.nextToken().substring(1));
00611
00612 tau = new double[4];
00613
00614 linea = lineasFichero.nextToken();
00615 tokens = new StringTokenizer (linea, "=");
00616 tokens.nextToken();
00617 this.tau[0] = Double.parseDouble(tokens.nextToken().substring(1));
00618
00619 linea = lineasFichero.nextToken();
00620 tokens = new StringTokenizer (linea, "=");
00621 tokens.nextToken();
00622 this.tau[1] = Double.parseDouble(tokens.nextToken().substring(1));
00623
00624 linea = lineasFichero.nextToken();
00625 tokens = new StringTokenizer (linea, "=");
00626 tokens.nextToken();
00627 this.tau[2] = Double.parseDouble(tokens.nextToken().substring(1));
00628
00629 linea = lineasFichero.nextToken();
00630 tokens = new StringTokenizer (linea, "=");
00631 tokens.nextToken();
00632 this.tau[3] = Double.parseDouble(tokens.nextToken().substring(1));
00633
00634 linea = lineasFichero.nextToken();
00635 tokens = new StringTokenizer (linea, "=");
00636 tokens.nextToken();
00637 this.Strategy = Integer.parseInt(tokens.nextToken().substring(1));
00638
00639
00640 }
00641
00642
00646 public static void main(String[] args)
00647 {
00648
00649 RandomGenerator.setSeed(818469644);
00650
00651
00652 int Poblacion=100, dimension=10, iteraciones=100000, estrategia =1;
00653 double F=.5, CR=.5;
00654
00655 SFLSDEGenerator differential = new SFLSDEGenerator(Poblacion,dimension,iteraciones, F,CR,estrategia);
00656
00657 if(args.length == 1){
00658 differential.leerConfiguracion(args[0]);
00659 }
00660
00661
00662 differential.execute();
00663
00664
00665 }
00666
00667 }