00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 package org.sci2s.eamhco;
00011
00012 import java.util.*;
00013
00014
00020 public class JADEGenerator {
00021
00022
00023 private long semilla;
00024
00025 private int PopulationSize;
00026 private int Dimension;
00027 private int MaxIter;
00028 private double ScalingFactor;
00029 private double CrossOverRate[];
00030 private int Strategy;
00031 private String CrossoverType;
00032
00033 private double p;
00034 private double c;
00035
00036 protected int numberOfbetters;
00037
00038 private double PI = 3.14159265358;
00039
00046 public JADEGenerator(int poblacion, int dimension, int iteraciones, double p, double c)
00047 {
00048 this.PopulationSize = poblacion;
00049 this.Dimension = dimension;
00050 this.MaxIter = iteraciones;
00051 this.p = p;
00052 this.c = c;
00053
00054 this.numberOfbetters= (int) (this.p*PopulationSize);
00055 if( numberOfbetters <1) numberOfbetters = 1;
00056 }
00057
00058
00059
00060
00061 public int[] mejoresParticulas(ArrayList<Prototype> population,double fitness[]){
00062 int number = this.numberOfbetters;
00063 int index[] = new int[number];
00064 int ind= 0;
00065 double mejor = Double.MIN_VALUE;
00066 double acc;
00067
00068 for(int i=0; i< population.size(); i++){
00069 acc =fitness[i];
00070
00071 if(acc > mejor )
00072 {
00073 ind = i;
00074 mejor = acc;
00075 }
00076 }
00077 index[0] = ind;
00078
00079 for (int j=1; j<number; j++){
00080 mejor = Double.MIN_VALUE;
00081 for(int i=0; i< population.size(); i++){
00082 acc = fitness[i];
00083
00084
00085 if(acc > mejor && acc < fitness[index[j-1]])
00086 {
00087 ind = i;
00088 mejor = acc;
00089 }
00090 }
00091 index[j] = ind;
00092
00093 }
00094 return index;
00095 }
00096
00097 public Prototype mutant(ArrayList<Prototype> population, double fitness[], int actual, ArrayList<Prototype> Archivo,int utilArchivo){
00098
00099
00100 Prototype mutant = new Prototype();
00101 Prototype r1,r2,xbest, resta, producto, resta2, producto2, result;
00102
00103
00104
00105 int ran;
00106 do{
00107 ran = RandomGenerator.Randint(0, population.size());
00108 }while(ran == actual);
00109
00110 r1 = population.get(ran);
00111
00112 int number;
00113
00114 do{
00115 number = RandomGenerator.Randint(0, population.size()+ utilArchivo);
00116 }while (number==ran || number == actual );
00117
00118 if(number < population.size()){
00119 r2 = population.get(number);
00120 }else
00121 r2 = Archivo.get(number-population.size());
00122
00123
00124
00125
00126
00127 int indices[] = new int [this.numberOfbetters];
00128
00129 indices = mejoresParticulas(population,fitness);
00130 number = RandomGenerator.Randint(0, indices.length);
00131
00132 xbest = population.get(indices[number]);
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142 switch(this.Strategy){
00143 case 1:
00144 resta = xbest.sub(population.get(actual));
00145 resta2 = r1.sub(r2);
00146
00147 producto = resta.mul(this.ScalingFactor);
00148 producto2 = resta2.mul(this.ScalingFactor);
00149
00150 result = producto.add(producto2);
00151 mutant = population.get(actual).add(result);
00152 break;
00153
00154 }
00155
00156
00157
00158
00159
00160
00161
00162 return mutant;
00163 }
00164
00165
00166
00167 double calc_rastrigin (double x[])
00168 {
00169 int i;
00170 double res;
00171 res = 0.0;
00172 for (i=0; i<this.Dimension; i++)
00173 {
00174 res += (x[i]*x[i] - 10.0*Math.cos(2.0*this.PI*x[i]) + 10.0);
00175 }
00176 return (res-(330));
00177 }
00178
00179
00180
00181
00182
00183 double fitness(Prototype p){
00184
00185 return calc_rastrigin(p.getInputs());
00186
00187 }
00188
00189
00196 public void execute()
00197 {
00198 System.out.print("\nThe algorithm JADE is starting...\n Computing...\n");
00199
00200
00201
00202
00203 ArrayList<Prototype> population = new ArrayList<Prototype>();
00204 Prototype mutation = new Prototype();
00205 Prototype crossover = new Prototype();
00206
00207
00208 double fitness[] = new double[PopulationSize];
00209
00210 double meanCR = 0.5;
00211 double meanF = 0.5;
00212 ArrayList<Prototype> Archivo = new ArrayList<Prototype>();
00213 int utilArchivo = 0;
00214
00215
00216 double SF[] = new double[this.PopulationSize];
00217 double SCR[] = new double[this.PopulationSize];
00218
00219 this.CrossOverRate= new double[this.PopulationSize];
00220 double F[] = new double[this.PopulationSize];
00221
00222
00223
00224
00225
00226
00227 for(int i=0; i< PopulationSize; i++){
00228 Prototype cromosoma = new Prototype(this.Dimension,1);
00229
00230 for(int j=0; j< this.Dimension; j++){
00231 cromosoma.setInput(j, RandomGenerator.Randdouble(-2, 2));
00232 }
00233
00234
00235 fitness[i]= fitness(cromosoma);
00236
00237 population.add(cromosoma);
00238 Archivo.add(cromosoma);
00239 }
00240
00241
00242
00243
00244
00245
00246 double bestFitness=fitness[0];
00247 int bestFitnessIndex=0;
00248
00249 for(int i=1; i< PopulationSize;i++){
00250 if(fitness[i]<bestFitness){
00251 bestFitness = fitness[i];
00252 bestFitnessIndex=i;
00253 }
00254
00255 }
00256
00257 this.Strategy = 1;
00258
00259 for(int iter=0; iter< MaxIter; iter++){
00260
00261 int utilF = 0;
00262 int utilCR = 0;
00263
00264
00265
00266
00267 for(int i=0; i<PopulationSize; i++){
00268
00269
00270 this.CrossOverRate[i] = RandomGenerator.RandGaussian()*0.1 + meanCR;
00271
00272 if(this.CrossOverRate[i]>1) this.CrossOverRate[i] =1;
00273 else if(this.CrossOverRate[i]<0) this.CrossOverRate[i] =0;
00274
00275
00276 double uniforme;
00277 do{
00278 uniforme= RandomGenerator.Randdouble(0,1);
00279 F[i] = 0.1*Math.tan(this.PI*uniforme) + meanF;
00280 }while (F[i] <=0);
00281
00282 if(F[i] >1) F[i] = 1;
00283
00284 if( Double.isNaN(F[i]) ){
00285 F[i] = RandomGenerator.Randdouble(0.1, 1);
00286 }
00287 this.ScalingFactor = F[i];
00288
00289
00290
00291
00292
00293
00294
00295 mutation = new Prototype();
00296
00297
00298 mutation = mutant(population,fitness, i, Archivo, utilArchivo);
00299
00300
00301
00302
00303 crossover= new Prototype(population.get(i));
00304
00305 for(int j=0; j<population.get(i).numberOfInputs(); j++){
00306
00307
00308 double randNumber = RandomGenerator.Randdouble(0, 1);
00309
00310 if(randNumber<this.CrossOverRate[i]){
00311 crossover.setInput(j, mutation.getInput(j));
00312 }
00313
00314
00315 }
00316
00317
00318
00319
00320
00321 double trialVector = fitness(crossover);
00322
00323
00324 if(trialVector < fitness[i]){
00325
00326 Archivo.set(utilArchivo%PopulationSize, population.get(i));
00327 utilArchivo++;
00328 SCR[utilCR%PopulationSize] = this.CrossOverRate[i];
00329 utilCR++;
00330 SF[utilF%PopulationSize] = F[i];
00331 utilF++;
00332
00333 population.set(i, crossover);
00334 fitness[i] = trialVector;
00335 utilArchivo = utilArchivo%PopulationSize;
00336 }
00337
00338
00339 if(fitness[i]<bestFitness){
00340 bestFitness = fitness[i];
00341 bestFitnessIndex=i;
00342
00343
00344 }
00345
00346
00347
00348 }
00349
00350
00351
00352 if(utilArchivo > this.PopulationSize){
00353 utilArchivo = this.PopulationSize;
00354 }
00355
00356 double meanA= 0;
00357 double meanL =0;
00358 double numerator=0, denominator =0;
00359
00360 for(int i=0; i< utilCR; i++){
00361 meanA+= SCR[i];
00362 numerator += SF[i] * SF[i];
00363 denominator += SF[i];
00364 }
00365
00366 if(numerator == 0) numerator =1;
00367 if(denominator ==0) denominator = 1;
00368
00369 meanL = numerator/denominator;
00370
00371 meanCR =(1-c)*meanCR + c * meanA;
00372 meanF = (1-c)*meanF + c * meanL;
00373
00374
00375
00376 }
00377
00378
00379
00380 System.out.println("Best -> "+ fitness[bestFitnessIndex]);
00381 }
00382
00383
00384 public void leerConfiguracion (String ficheroScript) {
00385
00386 String fichero, linea, token;
00387 StringTokenizer lineasFichero, tokens;
00388 byte line[];
00389 int i, j;
00390
00391 fichero = Fichero.leeFichero (ficheroScript);
00392 lineasFichero = new StringTokenizer (fichero,"\n\r");
00393
00394
00395
00396 linea = lineasFichero.nextToken();
00397 tokens = new StringTokenizer (linea, "=");
00398 tokens.nextToken();
00399 semilla = Long.parseLong(tokens.nextToken().substring(1));
00400
00401 linea = lineasFichero.nextToken();
00402 tokens = new StringTokenizer (linea, "=");
00403 tokens.nextToken();
00404 this.PopulationSize = Integer.parseInt(tokens.nextToken().substring(1));
00405
00406
00407 linea = lineasFichero.nextToken();
00408 tokens = new StringTokenizer (linea, "=");
00409 tokens.nextToken();
00410 this.Dimension = Integer.parseInt(tokens.nextToken().substring(1));
00411
00412
00413 linea = lineasFichero.nextToken();
00414 tokens = new StringTokenizer (linea, "=");
00415 tokens.nextToken();
00416 this.MaxIter = Integer.parseInt(tokens.nextToken().substring(1));
00417
00418 linea = lineasFichero.nextToken();
00419 tokens = new StringTokenizer (linea, "=");
00420 tokens.nextToken();
00421 this.p = Double.parseDouble(tokens.nextToken().substring(1));
00422
00423 linea = lineasFichero.nextToken();
00424 tokens = new StringTokenizer (linea, "=");
00425 tokens.nextToken();
00426 this.c = Double.parseDouble(tokens.nextToken().substring(1));
00427
00428
00429 }
00430
00431
00432
00436 public static void main(String[] args)
00437 {
00438
00439 RandomGenerator.setSeed(868469644);
00440
00441
00442 int Poblacion=100, dimension=50, iteraciones=100000;
00443 double p=.05, c=.1;
00444
00445
00446 JADEGenerator differential = new JADEGenerator(Poblacion,dimension,iteraciones, p,c);
00447
00448
00449 if(args.length == 1){
00450 differential.leerConfiguracion(args[0]);
00451 }
00452
00453 differential.execute();
00454
00455
00456 }
00457
00458 }