|
| 1 | +package algorithms.specialOptions.BIAS; |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | +import static utils.algorithms.operators.ISBOp.generateRandomSolution; |
| 6 | +import static utils.algorithms.operators.ISBOp.GAmutations; |
| 7 | +import static utils.algorithms.operators.ISBOp.GAParentSelections; |
| 8 | +import static utils.algorithms.operators.ISBOp.GACrossovers; |
| 9 | +import utils.algorithms.Counter; |
| 10 | +import static utils.MatLab.indexMin; |
| 11 | +import static utils.MatLab.indexMax; |
| 12 | +import interfaces.AlgorithmBias; |
| 13 | +import interfaces.Problem; |
| 14 | +import static utils.RunAndStore.FTrend; |
| 15 | + |
| 16 | +public class GA extends AlgorithmBias |
| 17 | +{ |
| 18 | + private char selectionStrategy = 't'; // r --> fitness proportional roulette wheel t-->stochastic tournament |
| 19 | + private char crossoverStrategy = 'a'; //d-->discrete a-->full arithmetic |
| 20 | + private char mutationStrategy = 'g'; // c --> Cauchy g-->Gaussian |
| 21 | + |
| 22 | + @Override |
| 23 | + public FTrend execute(Problem problem, int maxEvaluations) throws Exception |
| 24 | + { |
| 25 | + int populationSize = getParameter("p0").intValue(); |
| 26 | + int nt = getParameter("p1").intValue(); |
| 27 | + double CR = getParameter("p2").doubleValue(); |
| 28 | + double d = getParameter("p3").doubleValue(); //fixed to 0.25 in Kononova 2015 |
| 29 | + double md = getParameter("p4").doubleValue(); //fixed to ; 0.01 in Kononova 2015 |
| 30 | + |
| 31 | + FTrend FT = new FTrend(); |
| 32 | + int problemDimension = problem.getDimension(); |
| 33 | + double[][] bounds = problem.getBounds(); |
| 34 | + |
| 35 | + int[] ids = new int[populationSize]; |
| 36 | + double[][] population = new double[populationSize][problemDimension]; |
| 37 | + double[] fitnesses = new double[populationSize]; |
| 38 | + |
| 39 | + String FullName = getFullName("GA"+this.mutationStrategy+this.crossoverStrategy+this.selectionStrategy+this.correction,problem); |
| 40 | + Counter PRNGCounter = new Counter(0); |
| 41 | + createFile(FullName); |
| 42 | + |
| 43 | + int newID = 0; |
| 44 | + String line = new String(); |
| 45 | + |
| 46 | + |
| 47 | + if(this.selectionStrategy == 't') |
| 48 | + line+=" popSize "+populationSize+" nt "+nt; |
| 49 | + else if(this.selectionStrategy == 'r') |
| 50 | + line+=" popSize "+populationSize; |
| 51 | + else |
| 52 | + System.out.println("Unrecognised selection stratgy!"); |
| 53 | + |
| 54 | + |
| 55 | + if(this.crossoverStrategy == 'a') |
| 56 | + line+=" d "+d; |
| 57 | + else if(this.crossoverStrategy == 'd') |
| 58 | + line+=" CR "+CR; |
| 59 | + else |
| 60 | + System.out.println("Unrecognised crossover stratgy!"); |
| 61 | + |
| 62 | + |
| 63 | + if(this.mutationStrategy == 'c') |
| 64 | + line+=" md "+md; |
| 65 | + else if(this.mutationStrategy == 'g') |
| 66 | + line+=" md "+md; |
| 67 | + else |
| 68 | + System.out.println("Unrecognised mutation stratgy!"); |
| 69 | + |
| 70 | + |
| 71 | + writeHeader(line, problem); |
| 72 | + line = new String(); |
| 73 | + |
| 74 | + double[] best = new double[problemDimension]; |
| 75 | + double fBest = Double.NaN; |
| 76 | + |
| 77 | + |
| 78 | + int i = 0; |
| 79 | + int parent1 = 0; |
| 80 | + int parent2 = 0; |
| 81 | + int worst = 0; |
| 82 | + |
| 83 | + // evaluate initial population |
| 84 | + for (int j = 0; j < populationSize; j++) |
| 85 | + { |
| 86 | + |
| 87 | + double[] tmp = generateRandomSolution(bounds, problemDimension, PRNGCounter); |
| 88 | + for (int n = 0; n < problemDimension; n++) |
| 89 | + population[j][n] = tmp[n]; |
| 90 | + fitnesses[j] = problem.f(population[j]); |
| 91 | + |
| 92 | + i++; |
| 93 | + newID++; |
| 94 | + ids[j] = newID; |
| 95 | + line =""+newID+" -1 "+"-1 "+formatter(fitnesses[j])+" "+i+" -1"; |
| 96 | + for(int n = 0; n < problemDimension; n++) |
| 97 | + line+=" "+formatter(population[j][n]); |
| 98 | + line+="\n"; |
| 99 | + bw.write(line); |
| 100 | + line = null; |
| 101 | + line = new String(); |
| 102 | + |
| 103 | + if (j == 0 || fitnesses[j] < fBest) |
| 104 | + { |
| 105 | + fBest = fitnesses[j]; |
| 106 | + for (int n = 0; n < problemDimension; n++) |
| 107 | + best[n] = population[j][n]; |
| 108 | + FT.add(i, fBest); |
| 109 | + } |
| 110 | + |
| 111 | + } |
| 112 | + |
| 113 | + // iterate |
| 114 | + while (i < maxEvaluations) |
| 115 | + { |
| 116 | + |
| 117 | + parent1 = GAParentSelections( selectionStrategy, fitnesses, nt, PRNGCounter); |
| 118 | + parent2 = GAParentSelections( selectionStrategy, fitnesses, nt,PRNGCounter); |
| 119 | + double[] child = GACrossovers(population[parent1], population[parent2], CR, d, crossoverStrategy, PRNGCounter); |
| 120 | + |
| 121 | + |
| 122 | + child = GAmutations(child, mutationStrategy, md, bounds, PRNGCounter); |
| 123 | + |
| 124 | + child = correct(child,population[parent1],bounds); |
| 125 | + double fChild = problem.f(child); |
| 126 | + i++; |
| 127 | + |
| 128 | + worst = indexMax(fitnesses); |
| 129 | + |
| 130 | + if(fChild<fitnesses[worst]) |
| 131 | + { |
| 132 | + newID++; |
| 133 | + int indexWorst = ids[worst]; |
| 134 | + int indexParent1 = ids[parent1]; |
| 135 | + int indexParent2 = ids[parent2]; |
| 136 | + ids[worst] = newID; |
| 137 | + for(int n=0; n<problemDimension; n++) |
| 138 | + population[worst][n] = child[n]; |
| 139 | + fitnesses[worst] = fChild; |
| 140 | + |
| 141 | + |
| 142 | + line =""+newID+" "+indexParent1+" "+indexParent2+" "+formatter(fChild)+" "+i+" "+indexWorst; |
| 143 | + for(int n = 0; n < problemDimension; n++) |
| 144 | + line+=" "+formatter(child[n]); |
| 145 | + line+="\n"; |
| 146 | + bw.write(line); |
| 147 | + line = null; |
| 148 | + line = new String(); |
| 149 | + } |
| 150 | + |
| 151 | + |
| 152 | + } |
| 153 | + |
| 154 | + int ib = indexMin(fitnesses); |
| 155 | + |
| 156 | + finalBest = population[ib]; |
| 157 | + fBest = fitnesses[ib]; |
| 158 | + FT.add(i, fBest); |
| 159 | + |
| 160 | + closeAll(); |
| 161 | + writeStats(FullName, (double) this.numberOfCorrections/maxEvaluations, PRNGCounter.getCounter(), "correctionsGA"); |
| 162 | + |
| 163 | + return FT; |
| 164 | + } |
| 165 | + |
| 166 | + |
| 167 | + protected void setSelectionStrategy(char ss) {this.selectionStrategy = ss;} // r --> fitness proportional roulette wheel t-->stochastic tournament |
| 168 | + protected void setCrossoverStrategy(char cs) {this.crossoverStrategy = cs;} //d-->discrete a-->full arithmetic |
| 169 | + protected void setMutationStrategy(char ms) {this.mutationStrategy = ms;} // c --> Cauchy g-->Gaussian |
| 170 | + |
| 171 | + public GA() {} |
| 172 | + public GA(char ss,char cs, char ms){super(); setSelectionStrategy(ss); setCrossoverStrategy(cs); setMutationStrategy(ms);} |
| 173 | + |
| 174 | + |
| 175 | +} |
0 commit comments