00001
00002
00003
00004
00005
00006
00007
00008
00009 package org.sci2s.eamhco;
00010
00011
00012 import java.util.*;
00013
00014 import java.util.StringTokenizer;
00015
00016
00017
00018
00023 public class DEGLGenerator{
00024
00025
00026
00027
00028
00029 private int PopulationSize;
00030 private int Dimension;
00031 private int MaxIter;
00032 private double ScalingFactor;
00033 private double CrossOverRate;
00034 private double WeightFactor;
00035 private double WeightFactorAdap[];
00036 private int neighboors;
00037 private String WeightScheme;
00038
00039 private double PI = 3.14159265358;
00040 private long semilla;
00041
00042
00043 public DEGLGenerator(int poblacion, int dimension, int iteraciones, double F, double CR, double WeightFactor, int neighbors, String Wscheme)
00044 {
00045 this.PopulationSize = poblacion;
00046 this.Dimension = dimension;
00047 this.MaxIter = iteraciones;
00048 this.ScalingFactor = F;
00049 this.CrossOverRate = CR;
00050 this.WeightFactor = WeightFactor;
00051 this.neighboors = neighbors;
00052 this.WeightScheme = Wscheme;
00053
00054 }
00055
00056
00057
00058
00059
00060
00061 public Prototype mutant(ArrayList<Prototype> population, int actual, int bestFitnessIndex, int bestNeighboor){
00062
00063
00064 Prototype mutant = new Prototype();
00065 Prototype xbest,nbest, p,q, r1,r2, resta1,resta2, producto1,producto2, suma, Local, Global;
00066
00067
00068
00069 nbest = population.get(bestNeighboor);
00070
00071 int prandom, qrandom;
00072 int inferior =( actual-this.neighboors)%this.PopulationSize;
00073 int superior = (actual+this.neighboors)%this.PopulationSize;
00074 if (inferior <0 ) inferior *=-1;
00075 if (superior <0) superior*= -1;
00076 do{
00077 prandom = RandomGenerator.Randint(inferior,superior);
00078 qrandom = RandomGenerator.Randint(inferior,superior);
00079 }while (prandom != actual && qrandom!= prandom && qrandom!=actual);
00080
00081 p = population.get(prandom);
00082 q = population.get(qrandom);
00083
00084 resta1 = nbest.sub(population.get(actual));
00085 resta2 = p.sub(q);
00086
00087 producto1 = resta1.mul(this.ScalingFactor);
00088 producto2 = resta2.mul(this.ScalingFactor);
00089
00090 suma = producto1.add(producto2);
00091
00092 Local = population.get(actual).add(suma);
00093
00094
00095
00096 xbest = population.get(bestFitnessIndex);
00097
00098 int ran1,ran2;
00099
00100 do{
00101 ran1 = RandomGenerator.Randint(0, this.PopulationSize);
00102 ran2 = RandomGenerator.Randint(0, this.PopulationSize);
00103 }while (ran1 != actual && ran2!= ran1 && ran2!= actual);
00104
00105 r1 = population.get(ran1);
00106 r2 = population.get(ran2);
00107
00108 resta1 = r1.sub(r2);
00109 resta2 = xbest.sub(population.get(actual));
00110
00111 producto1 = resta1.mul(this.ScalingFactor);
00112 producto2 = resta2.mul(this.ScalingFactor);
00113
00114 suma = producto1.add(producto2);
00115
00116 Global = population.get(actual).add(suma);
00117
00118
00119
00120
00121 if(this.WeightScheme.equals("Adaptive")){
00122 producto1 = Global.mul(this.WeightFactorAdap[actual]);
00123 producto2 = Local.mul(1-this.WeightFactorAdap[actual]);
00124
00125 }else{
00126 producto1 = Global.mul(this.WeightFactor);
00127 producto2 = Local.mul(1-this.WeightFactor);
00128 }
00129 mutant = producto1.add(producto2);
00130
00131
00132
00133
00134
00135 return mutant;
00136 }
00137
00138
00139
00140
00141
00142 double calc_rastrigin (double x[])
00143 {
00144 int i;
00145 double res;
00146 res = 0.0;
00147 for (i=0; i<this.Dimension; i++)
00148 {
00149 res += (x[i]*x[i] - 10.0*Math.cos(2.0*this.PI*x[i]) + 10.0);
00150 }
00151 return (res-(330));
00152 }
00153
00154
00155
00156
00157
00158 double fitness(Prototype p){
00159
00160 return calc_rastrigin(p.getInputs());
00161
00162 }
00163
00169 public void execute()
00170 {
00171 System.out.print("\nThe algorithm DEGL is starting...\n Computing...\n");
00172
00173
00174 ArrayList<Prototype> population = new ArrayList<Prototype>();
00175 Prototype mutation = new Prototype();
00176 Prototype crossover = new Prototype();
00177
00178
00179 double fitness[] = new double[PopulationSize];
00180 double fitness_bestPopulation[] = new double[PopulationSize];
00181 Prototype bestParticle = new Prototype();
00182
00183
00184
00185 for(int i=0; i< PopulationSize; i++){
00186 Prototype cromosoma = new Prototype(this.Dimension,1);
00187
00188 for(int j=0; j< this.Dimension; j++){
00189 cromosoma.setInput(j, RandomGenerator.Randdouble(-200, 200));
00190 }
00191
00192
00193 fitness[i]= fitness(cromosoma);
00194
00195 population.add(cromosoma);
00196 }
00197
00198
00199
00200
00201
00202
00203 double bestFitness=fitness[0];
00204 int bestFitnessIndex=0;
00205
00206 for(int i=1; i< PopulationSize;i++){
00207 if(fitness[i]<bestFitness){
00208 bestFitness = fitness[i];
00209 bestFitnessIndex=i;
00210 }
00211
00212 }
00213
00214
00215
00216 if(this.WeightScheme.equals("Adaptive")){
00217 WeightFactorAdap = new double[this.PopulationSize];
00218
00219 for(int i=0; i<PopulationSize; i++){
00220 WeightFactorAdap[i] = RandomGenerator.Randdouble(0,1);
00221 }
00222 }
00223
00224
00225
00226 for(int iter=0; iter< MaxIter; iter++){
00227
00228
00229
00230
00231 for(int i=0; i<PopulationSize; i++){
00232
00233
00234
00235
00236
00237 mutation = new Prototype();
00238
00239
00240 int bestNeighboor=0;
00241 double bestFitnessNeighboor = Double.MIN_VALUE;
00242
00243
00244 for(int j=i-this.neighboors; j< i+this.neighboors; j++){
00245 int pos = (i+j)% this.PopulationSize;
00246 if (pos <0) pos *=-1;
00247
00248 if(fitness[pos]> bestFitnessNeighboor){
00249 bestFitnessNeighboor = fitness[pos];
00250 bestNeighboor = pos;
00251 }
00252 }
00253
00254 mutation = mutant(population, i,bestFitnessIndex, bestNeighboor);
00255
00256
00257
00258
00259 crossover = new Prototype(population.get(i));
00260
00261 for(int j=0; j< (population.get(i)).numberOfInputs(); j++){
00262
00263
00264 double randNumber = RandomGenerator.Randdouble(0, 1);
00265
00266 if(randNumber< this.CrossOverRate){
00267 crossover.setInput(j, mutation.getInput(j));
00268 }
00269 }
00270
00271
00272
00273
00274
00275
00276 double trialVector = fitness(crossover);
00277
00278
00279 if(trialVector < fitness[i]){
00280
00281 population.set(i, crossover);
00282 fitness[i] = trialVector;
00283 }
00284
00285
00286 if(fitness[i]<bestFitness){
00287 bestFitness = fitness[i];
00288 bestFitnessIndex=i;
00289
00290
00291 }
00292
00293
00294
00295 }
00296
00297 if(this.WeightScheme.equals("Linear")){
00298 this.WeightFactor = iter/this.MaxIter *1.;
00299 }else if(this.WeightScheme.equals("Exponential")){
00300 this.WeightFactor = Math.exp(iter/this.MaxIter*1.0 * Math.log1p(2))-1;
00301 }else if(this.WeightScheme.equals("Random")){
00302 this.WeightFactor = RandomGenerator.Randdouble(0, 1);
00303 }else if(this.WeightScheme.equals("Adaptive")){
00304
00305 for(int i=0; i<this.PopulationSize; i++){
00306 double prod = this.ScalingFactor*(this.WeightFactorAdap[bestFitnessIndex]-this.WeightFactorAdap[i]);
00307
00308 int ran1,ran2;
00309
00310 do{
00311 ran1 = RandomGenerator.Randint(0, this.PopulationSize);
00312 ran2 = RandomGenerator.Randint(0, this.PopulationSize);
00313 }while (ran1 != i && ran2!= ran1 && ran2!= i);
00314
00315 double prod2 = this.ScalingFactor*(this.WeightFactorAdap[ran1]-this.WeightFactorAdap[ran2]);
00316 this.WeightFactorAdap[i] += prod + prod2;
00317
00318 if(this.WeightFactorAdap[i] > 0.95) this.WeightFactorAdap[i] =0.95;
00319 else if(this.WeightFactorAdap[i] < 0.05) this.WeightFactorAdap[i] =0.05;
00320 }
00321 }
00322
00323
00324
00325
00326 }
00327
00328
00329 System.out.println("Best -> "+ fitness[bestFitnessIndex]);
00330 }
00331
00332 public void leerConfiguracion (String ficheroScript) {
00333
00334 String fichero, linea, token;
00335 StringTokenizer lineasFichero, tokens;
00336 byte line[];
00337 int i, j;
00338
00339 fichero = Fichero.leeFichero (ficheroScript);
00340 lineasFichero = new StringTokenizer (fichero,"\n\r");
00341
00342
00343
00344 linea = lineasFichero.nextToken();
00345 tokens = new StringTokenizer (linea, "=");
00346 tokens.nextToken();
00347 semilla = Long.parseLong(tokens.nextToken().substring(1));
00348
00349 linea = lineasFichero.nextToken();
00350 tokens = new StringTokenizer (linea, "=");
00351 tokens.nextToken();
00352 this.PopulationSize = Integer.parseInt(tokens.nextToken().substring(1));
00353
00354
00355 linea = lineasFichero.nextToken();
00356 tokens = new StringTokenizer (linea, "=");
00357 tokens.nextToken();
00358 this.Dimension = Integer.parseInt(tokens.nextToken().substring(1));
00359
00360
00361 linea = lineasFichero.nextToken();
00362 tokens = new StringTokenizer (linea, "=");
00363 tokens.nextToken();
00364 this.MaxIter = Integer.parseInt(tokens.nextToken().substring(1));
00365
00366 linea = lineasFichero.nextToken();
00367 tokens = new StringTokenizer (linea, "=");
00368 tokens.nextToken();
00369 this.ScalingFactor = Double.parseDouble(tokens.nextToken().substring(1));
00370
00371 linea = lineasFichero.nextToken();
00372 tokens = new StringTokenizer (linea, "=");
00373 tokens.nextToken();
00374 this.CrossOverRate = Double.parseDouble(tokens.nextToken().substring(1));
00375
00376 linea = lineasFichero.nextToken();
00377 tokens = new StringTokenizer (linea, "=");
00378 tokens.nextToken();
00379 this.WeightFactor = Double.parseDouble(tokens.nextToken().substring(1));
00380
00381 linea = lineasFichero.nextToken();
00382 tokens = new StringTokenizer (linea, "=");
00383 tokens.nextToken();
00384 this.neighboors = Integer.parseInt(tokens.nextToken().substring(1));
00385
00386
00387 linea = lineasFichero.nextToken();
00388 tokens = new StringTokenizer (linea, "=");
00389 tokens.nextToken();
00390 this.WeightScheme = tokens.nextToken().substring(1);
00391
00392
00393 }
00394
00395
00396
00400 public static void main(String[] args)
00401 {
00402
00403 RandomGenerator.setSeed(818469644);
00404
00405
00406 int Poblacion=100, dimension=10, iteraciones=100000, neighbors = 2;
00407 double F=.9, CR=.5, WeightFactor =0.0;
00408 String Wscheme = "Exponential";
00409
00410 DEGLGenerator differential = new DEGLGenerator(Poblacion,dimension,iteraciones, F,CR, WeightFactor, neighbors, Wscheme);
00411
00412 if(args.length == 1){
00413 differential.leerConfiguracion(args[0]);
00414 }
00415
00416 differential.execute();
00417
00418
00419 }
00420
00421 }