Skip to content

Commit 15fba2a

Browse files
committed
GA ISB done (but not tested)
Signed-off-by: facaraff <[email protected]>
1 parent c0ad029 commit 15fba2a

File tree

8 files changed

+398
-440
lines changed

8 files changed

+398
-440
lines changed

src/algorithms/specialOptions/BIAS/CMAES.java

-30
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,3 @@
1-
/**
2-
Copyright (c) 2019, Fabio Caraffini ([email protected], [email protected])
3-
All rights reserved.
4-
5-
Redistribution and use in source and binary forms, with or without
6-
modification, are permitted provided that the following conditions are met:
7-
8-
1. Redistributions of source code must retain the above copyright notice, this
9-
list of conditions and the following disclaimer.
10-
2. Redistributions in binary form must reproduce the above copyright notice,
11-
this list of conditions and the following disclaimer in the documentation
12-
and/or other materials provided with the distribution.
13-
14-
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15-
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16-
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17-
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
18-
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19-
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20-
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21-
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22-
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23-
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24-
25-
The views and conclusions contained in the software and documentation are those
26-
of the authors and should not be interpreted as representing official policies,
27-
either expressed or implied, of the FreeBSD Project.
28-
*/
29-
30-
311
package algorithms.specialOptions.BIAS;
322

333
import static utils.algorithms.Misc.generateRandomSolution;

src/algorithms/specialOptions/BIAS/DE.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public FTrend execute(Problem problem, int maxEvaluations) throws Exception
9898
int newID = 0;
9999
int bestID = -1;
100100

101-
writeHeader(" pop "+populationSize+" F "+F+" CR "+CR+" alpha "+alpha, problem);
101+
writeHeader(" popSize "+populationSize+" F "+F+" CR "+CR+" alpha "+alpha, problem);
102102

103103
String line = new String();
104104

+175
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
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

Comments
 (0)