00001
00002
00003
00004
00005
00006
00007
00008
00009 package org.sci2s.eamhco;
00010 import java.util.*;
00011
00012
00013
00018 public class OBDEGenerator {
00019
00020
00021
00022 private int PopulationSize;
00023 private long semilla;
00024 private int Dimension;
00025 private int MaxIter;
00026 private double ScalingFactor;
00027 private double CrossOverRate;
00028 private int Strategy;
00029 private String CrossoverType;
00030 private double JumpingRate;
00031
00032 private double PI = 3.14159265358;
00033
00034 private double A, B;
00035
00040 public OBDEGenerator(int poblacion, int dimension, int iteraciones, double F, double CR, int strg, String CrossType, double JR)
00041 {
00042 this.PopulationSize = poblacion;
00043 this.Dimension = dimension;
00044 this.MaxIter = iteraciones;
00045 this.ScalingFactor = F;
00046 this.CrossOverRate = CR;
00047 this.Strategy = strg;
00048 this.CrossoverType = CrossType;
00049 this.JumpingRate = JR;
00050
00051 this.A = -5.12;
00052 this.B = 5.12;
00053 }
00054
00055
00056
00057
00058
00059
00060 public void inic_vector_sin(int vector[], int without){
00061
00062 for(int i=0; i<vector.length; i++)
00063 if(i!=without)
00064 vector[i] = i;
00065 }
00066
00067
00068 public void desordenar_vector_sin(int vector[]){
00069 int tmp, pos;
00070 for(int i=0; i<vector.length-1; i++){
00071 pos = RandomGenerator.Randint(0, vector.length-1);
00072 tmp = vector[i];
00073 vector[i] = vector[pos];
00074 vector[pos] = tmp;
00075 }
00076 }
00077
00078
00088 public Prototype mutant(ArrayList<Prototype> population, int actual, int mejor){
00089
00090
00091 Prototype mutant = new Prototype();
00092 Prototype r1,r2,r3,r4,r5, resta, producto, resta2, producto2, result, producto3, resta3;
00093
00094
00095
00096 int lista[] = new int[population.size()];
00097 inic_vector_sin(lista,actual);
00098 desordenar_vector_sin(lista);
00099
00100
00101
00102 r1 = population.get(lista[0]);
00103 r2 = population.get(lista[1]);
00104 r3 = population.get(lista[2]);
00105 r4 = population.get(lista[3]);
00106 r5 = population.get(lista[4]);
00107
00108 double SFi = this.ScalingFactor;
00109
00110 switch(this.Strategy){
00111 case 1:
00112 resta = r2.sub(r3);
00113 producto = resta.mul(SFi);
00114 mutant = producto.add(r1);
00115 break;
00116
00117 case 2:
00118
00119 resta = r2.sub(r3);
00120 producto = resta.mul(SFi);
00121 mutant = population.get(mejor).add(producto);
00122 break;
00123
00124 case 3:
00125 resta = r1.sub(r2);
00126 resta2 = population.get(mejor).sub(population.get(actual));
00127
00128 producto = resta.mul(SFi);
00129 producto2 = resta2.mul(SFi);
00130
00131 result = population.get(actual).add(producto);
00132 mutant = result.add(producto2);
00133
00134 break;
00135
00136 case 4:
00137 resta = r1.sub(r2);
00138 resta2 = r3.sub(r4);
00139
00140 producto = resta.mul(SFi);
00141 producto2 = resta2.mul(SFi);
00142
00143 result = population.get(mejor).add(producto);
00144 mutant = result.add(producto2);
00145 break;
00146
00147 case 5:
00148 resta = r2.sub(r3);
00149 resta2 = r4.sub(r5);
00150
00151 producto = resta.mul(SFi);
00152 producto2 = resta2.mul(SFi);
00153
00154 result = r1.add(producto);
00155 mutant = result.add(producto2);
00156
00157 break;
00158
00159 case 6:
00160 resta = r1.sub(r2);
00161 resta2 = r3.sub(r4);
00162 resta3 =population.get(mejor).sub(population.get(actual));
00163
00164 producto = resta.mul(SFi);
00165 producto2 = resta2.mul(SFi);
00166 producto3 = resta3.mul(SFi);
00167
00168 result = population.get(actual).add(producto);
00169 result = result.add(producto2);
00170 mutant = result.add(producto3);
00171 break;
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186 }
00187
00188
00189
00190
00191
00192
00193
00194 return mutant;
00195 }
00196
00197
00198
00199
00200
00201
00202 double calc_rastrigin (double x[])
00203 {
00204 int i;
00205 double res;
00206 res = 0.0;
00207 for (i=0; i<this.Dimension; i++)
00208 {
00209 res += (x[i]*x[i] - 10.0*Math.cos(2.0*this.PI*x[i]) + 10.0);
00210 }
00211 return (res-(330));
00212 }
00213
00214
00215
00216
00217
00218
00222 double fitness(Prototype p){
00223
00224 return calc_rastrigin(p.getInputs());
00225
00226 }
00227
00235 public int[] mejoresParticulas(ArrayList<Prototype> population,double fitness[]){
00236 int number = this.PopulationSize;
00237 int index[] = new int[number];
00238 int ind= 0;
00239 double mejor = Double.MIN_VALUE;
00240 double acc;
00241
00242 for(int i=0; i< population.size(); i++){
00243 acc =fitness[i];
00244
00245 if(acc > mejor )
00246 {
00247 ind = i;
00248 mejor = acc;
00249 }
00250 }
00251 index[0] = ind;
00252
00253 for (int j=1; j<number; j++){
00254 mejor = Double.MIN_VALUE;
00255 for(int i=0; i< population.size(); i++){
00256 acc = fitness[i];
00257
00258
00259 if(acc > mejor && acc < fitness[index[j-1]])
00260 {
00261 ind = i;
00262 mejor = acc;
00263 }
00264 }
00265 index[j] = ind;
00266
00267 }
00268 return index;
00269 }
00270
00271
00278 public void execute()
00279 {
00280 System.out.print("\nThe algorithm OBDE is starting...\n Computing...\n");
00281
00282
00283 ArrayList<Prototype> population = new ArrayList<Prototype>();
00284 ArrayList<Prototype> Opp_population = new ArrayList<Prototype>();
00285 Prototype mutation = new Prototype();
00286 Prototype crossover = new Prototype();
00287
00288 double fitness[] = new double[PopulationSize];
00289 double fitnessTogether[] = new double[PopulationSize*2];
00290
00291
00292
00293
00294 for(int i=0; i< PopulationSize; i++){
00295 Prototype cromosoma = new Prototype(this.Dimension,1);
00296
00297 for(int j=0; j< this.Dimension; j++){
00298 cromosoma.setInput(j, RandomGenerator.Randdouble(this.A, this.B));
00299 }
00300
00301
00302 fitnessTogether[i]= fitness(cromosoma);
00303 fitnessTogether[i+PopulationSize]= fitness(cromosoma);
00304
00305
00306 population.add(cromosoma);
00307 Opp_population.add(cromosoma.opposite());
00308
00309
00310
00311
00312 }
00313
00314
00315 ArrayList <Prototype> Together = new ArrayList<Prototype> ();
00316
00317 Together.addAll(population);
00318 Together.addAll(Opp_population);
00319
00320
00321
00322 int indices[]= mejoresParticulas(Together, fitnessTogether);
00323
00324
00325 for(int i=0; i<indices.length; i++){
00326
00327 population.set(i, Together.get(indices[i]));
00328
00329 }
00330
00331
00332
00333
00334
00335
00336
00337 double bestFitness=fitness[0];
00338 int bestFitnessIndex=0;
00339
00340 for(int i=1; i< PopulationSize;i++){
00341 if(fitness[i]<bestFitness){
00342 bestFitness = fitness[i];
00343 bestFitnessIndex=i;
00344 }
00345
00346 }
00347
00348 boolean cruceExp [] = new boolean[PopulationSize];
00349
00350 for(int iter=0; iter< MaxIter; iter++){
00351
00352
00353
00354
00355 if(this.CrossoverType.equals("Exponential")){
00356
00357 for(int i=0; i<PopulationSize; i++){
00358 cruceExp[i] = false;
00359 }
00360
00361 }
00362
00363
00364
00365 for(int i=0; i<PopulationSize; i++){
00366
00367
00368 mutation = new Prototype();
00369
00370
00371
00372 mutation = mutant(population, i, bestFitnessIndex);
00373
00374
00375
00376 crossover = new Prototype(population.get(i));
00377
00378 for(int j=0; j< (population.get(i)).numberOfInputs(); j++){
00379
00380 if(this.CrossoverType.equals("Binomial")){
00381 double randNumber = RandomGenerator.Randdouble(0, 1);
00382
00383 if(randNumber< this.CrossOverRate){
00384 crossover.setInput(j, mutation.getInput(j));
00385 }
00386 }else if(this.CrossoverType.equals("Exponential")){
00387 int startingPoint = RandomGenerator.Randint(0, PopulationSize);
00388
00389 int L=0;
00390 do{
00391 L++;
00392 }while(RandomGenerator.Randdouble(0, 1)<this.CrossOverRate && (L< population.get(i).numberOfInputs()));
00393
00394 for(int m=startingPoint; m< startingPoint+L; m++){
00395 crossover.setInput(m%population.get(i).numberOfInputs(), mutation.getInput(j));
00396 }
00397 }else if(this.CrossoverType.equals("Arithmetic")){
00398 Prototype resta = mutation.sub(population.get(i));
00399 crossover = population.get(i).add(resta.mul(RandomGenerator.Randdouble(0, 1)));
00400 }else{
00401 System.err.println("ERROR, Crossover Type incorrect.");
00402 }
00403
00404
00405
00406 }
00407
00408
00409
00410
00411
00412
00413
00414 double trialVector = fitness(crossover);
00415
00416
00417
00418
00419
00420
00421
00422
00423 if(trialVector < fitness[i]){
00424
00425 population.set(i, crossover);
00426 fitness[i] = trialVector;
00427 }
00428
00429
00430 if(fitness[i]<bestFitness){
00431 bestFitness = fitness[i];
00432 bestFitnessIndex=i;
00433
00434
00435 }
00436
00437
00438 }
00439
00440 double randNumber = RandomGenerator.Randdouble(0, 1);
00441
00442 if(randNumber < this.JumpingRate){
00443
00444 ArrayList<Prototype> populationAux = new ArrayList<Prototype>(population);
00445
00446
00447
00448
00449
00450 double min[] = new double [population.get(0).numberOfInputs()];
00451 double max[] = new double [population.get(0).numberOfInputs()];
00452
00453
00454 for(int j=0; j<population.get(0).numberOfInputs(); j++ ){
00455 min[j] = Double.MAX_VALUE;
00456 max[j] = Double.MIN_VALUE;
00457 }
00458
00459
00460 for(int i=0; i< population.size(); i++){
00461 for(int j=0; j< population.get(0).numberOfInputs(); j++){
00462
00463 if(population.get(i).getInput(j)<min[j])
00464 {
00465 min[j] = population.get(i).getInput(j);
00466 }
00467 if(population.get(i).getInput(j)>max[j])
00468 {
00469 max[j] = population.get(i).getInput(j);
00470 }
00471
00472 }
00473 }
00474
00475
00476
00477 for(int i=0; i< population.size();i++){
00478 population.get(i).opposite(min, max);
00479 }
00480
00481
00482 Together = new ArrayList<Prototype> ();
00483
00484 Together.addAll(population);
00485 Together.addAll(Opp_population);
00486
00487
00488
00489 indices = mejoresParticulas(Together, fitnessTogether);
00490
00491
00492 for(int i=0; i<indices.length; i++){
00493
00494 population.set(i, Together.get(indices[i]));
00495
00496 }
00497
00498
00499
00500
00501
00502 }
00503
00504 }
00505
00506 bestFitness = 0;
00507 bestFitnessIndex = 0;
00508 for(int i=0; i< PopulationSize;i++){
00509
00510
00511 if(fitness[i]>bestFitness){
00512 bestFitness = fitness[i];
00513 bestFitnessIndex=i;
00514 }
00515
00516 }
00517
00518 System.out.println("Best -> "+ fitness[bestFitnessIndex]);
00519
00520 }
00521
00522
00523 public void leerConfiguracion (String ficheroScript) {
00524
00525 String fichero, linea, token;
00526 StringTokenizer lineasFichero, tokens;
00527 byte line[];
00528 int i, j;
00529
00530 fichero = Fichero.leeFichero (ficheroScript);
00531 lineasFichero = new StringTokenizer (fichero,"\n\r");
00532
00533
00534
00535 linea = lineasFichero.nextToken();
00536 tokens = new StringTokenizer (linea, "=");
00537 tokens.nextToken();
00538 semilla = Long.parseLong(tokens.nextToken().substring(1));
00539
00540 linea = lineasFichero.nextToken();
00541 tokens = new StringTokenizer (linea, "=");
00542 tokens.nextToken();
00543 this.PopulationSize = Integer.parseInt(tokens.nextToken().substring(1));
00544
00545
00546 linea = lineasFichero.nextToken();
00547 tokens = new StringTokenizer (linea, "=");
00548 tokens.nextToken();
00549 this.Dimension = Integer.parseInt(tokens.nextToken().substring(1));
00550
00551
00552 linea = lineasFichero.nextToken();
00553 tokens = new StringTokenizer (linea, "=");
00554 tokens.nextToken();
00555 this.MaxIter = Integer.parseInt(tokens.nextToken().substring(1));
00556
00557 linea = lineasFichero.nextToken();
00558 tokens = new StringTokenizer (linea, "=");
00559 tokens.nextToken();
00560 this.ScalingFactor = Double.parseDouble(tokens.nextToken().substring(1));
00561
00562 linea = lineasFichero.nextToken();
00563 tokens = new StringTokenizer (linea, "=");
00564 tokens.nextToken();
00565 this.CrossOverRate = Double.parseDouble(tokens.nextToken().substring(1));
00566
00567 linea = lineasFichero.nextToken();
00568 tokens = new StringTokenizer (linea, "=");
00569 tokens.nextToken();
00570 this.JumpingRate = Double.parseDouble(tokens.nextToken().substring(1));
00571
00572 linea = lineasFichero.nextToken();
00573 tokens = new StringTokenizer (linea, "=");
00574 tokens.nextToken();
00575 this.Strategy = Integer.parseInt(tokens.nextToken().substring(1));
00576
00577 linea = lineasFichero.nextToken();
00578 tokens = new StringTokenizer (linea, "=");
00579 tokens.nextToken();
00580 this.CrossoverType = tokens.nextToken().substring(1);
00581
00582
00583 }
00584
00588 public static void main(String[] args)
00589 {
00590
00591 RandomGenerator.setSeed(818469644);
00592
00593
00594 int Poblacion=100, dimension=30, iteraciones=500000, estrategia =1;
00595 double F=.5, CR=.9, JR=0.3;
00596 String CrossType = "Binomial";
00597
00598 OBDEGenerator differential = new OBDEGenerator(Poblacion,dimension,iteraciones, F,CR,estrategia, CrossType, JR);
00599
00600 if(args.length == 1){
00601 differential.leerConfiguracion(args[0]);
00602 }
00603
00604 differential.execute();
00605
00606
00607 }
00608
00609 }