Skip to content

Commit cca144b

Browse files
Christoph BodensteinChristoph Bodenstein
authored andcommitted
fixed genetic algo. refactored some typos. correct count of children are generated now.
1 parent a137a89 commit cca144b

File tree

4 files changed

+105
-36
lines changed

4 files changed

+105
-36
lines changed

TimeNETOptimizationEnvironment/src/toe/optimization/OptimizerGenetic.java

Lines changed: 46 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public class OptimizerGenetic extends OptimizerPopulationBased implements Runnab
2424
private double mutationChance = support.getOptimizerPreferences().getPref_GeneticMutationChance(); // chance of genes to Mutate
2525
private final boolean mutateTopMeasure = support.getOptimizerPreferences().getPref_GeneticMutateTopSolution();
2626
private final typeOfGeneticCrossover crossOverStrategy = typeOfGeneticCrossover.OnePoint;
27+
private final int numberOfCrossovers = support.getOptimizerPreferences().getPref_GeneticNumberOfCrossings();
2728
private final int SBX_n = 2;
2829
private final double MPC_cr = 0.5;
2930

@@ -74,7 +75,7 @@ public void setMaxNumberOfOptiCycles(int newMaxNumberOfOtpiCycles) {
7475
*
7576
* @return Chance of mutation
7677
*/
77-
public double getMutatuionChance() {
78+
public double getMutationChance() {
7879
return this.mutationChance;
7980
}
8081

@@ -124,9 +125,9 @@ public void run() {
124125
}
125126

126127
//modification phase -----------------------------------------------------------------------
127-
population = crossPopulation(population, populationSize); //doubles population
128+
population = crossPopulation(population, numberOfCrossovers);
128129
support.log("New Population size after Crossing is: " + population.size());
129-
population = mutatePopulation(population, mutationChance); //
130+
population = mutatePopulation(population, mutationChance);
130131
support.log("New Population size after mutation is: " + population.size());
131132

132133
//simulation phase -----------------------------------------------------------------------
@@ -149,7 +150,9 @@ public void run() {
149150
printPopulationDistances();
150151

151152
++optiCycleCounter;
152-
if(topMeasure.getDistanceToTargetValue()<=0)break;
153+
if (topMeasure.getDistanceToTargetValue() <= 0) {
154+
break;
155+
}
153156
}
154157
optimized = true;
155158
}
@@ -160,7 +163,7 @@ public void run() {
160163
*
161164
* @param father the father for the genetic parameter mix process
162165
* @param mother the mother for the genetic parameter mix process
163-
* @return the resulting list of children
166+
* @return the resulting list of 2 children
164167
*/
165168
private ArrayList<SimulationType> onePointCrossOver(SimulationType father, SimulationType mother) {
166169
if (father == null || mother == null) {
@@ -210,7 +213,7 @@ private ArrayList<SimulationType> onePointCrossOver(SimulationType father, Simul
210213
*
211214
* @param father the father for the genetic parameter mix process
212215
* @param mother the mother for the genetic parameter mix process
213-
* @return the resulting list of children
216+
* @return the resulting list of 2 children
214217
*/
215218
private ArrayList<SimulationType> SBXCrossOver(SimulationType father, SimulationType mother) {
216219
if (father == null || mother == null) {
@@ -271,7 +274,7 @@ private ArrayList<SimulationType> SBXCrossOver(SimulationType father, Simulation
271274
*
272275
* @param father the father for the genetic parameter mix process
273276
* @param mother the mother for the genetic parameter mix process
274-
* @return the resulting list of children
277+
* @return the resulting list of 3 children
275278
*/
276279
private ArrayList<SimulationType> MPCCrossOver(SimulationType parent1, SimulationType parent2, SimulationType parent3) {
277280
if (parent1 == null || parent2 == null || parent3 == null) {
@@ -332,32 +335,47 @@ private ArrayList<SimulationType> MPCCrossOver(SimulationType parent1, Simulatio
332335
return childList;
333336
}
334337

335-
private ArrayList< ArrayList<SimulationType>> crossPopulation(ArrayList< ArrayList<SimulationType>> population, int numNewChildren) {
338+
/**
339+
* @param numOfCrossing how often a random pair of parents create new
340+
* children SBX and OPC -> 2 children per crossing MPC -> always 3 children
341+
* and only one crossing
342+
*/
343+
private ArrayList< ArrayList<SimulationType>> crossPopulation(ArrayList< ArrayList<SimulationType>> population, int numOfCrossing) {
336344
ArrayList<SimulationType> children = new ArrayList<>();
337-
if (this.crossOverStrategy == typeOfGeneticCrossover.OnePoint) {
338-
for (int i = 0; i < numNewChildren; ++i) {
339-
int indexOfFather = randomGenerator.nextInt(populationSize);
340-
int indexOfMother = randomGenerator.nextInt(populationSize);
341345

342-
children = onePointCrossOver(population.get(indexOfFather).get(0), population.get(indexOfMother).get(0));
343-
}
344-
} else if (this.crossOverStrategy == typeOfGeneticCrossover.SBX) {
345-
for (int i = 0; i < numNewChildren; ++i) {
346-
int indexOfFather = randomGenerator.nextInt(populationSize);
347-
int indexOfMother = randomGenerator.nextInt(populationSize);
348-
349-
children = SBXCrossOver(population.get(indexOfFather).get(0), population.get(indexOfMother).get(0));
350-
}
351-
} else if (this.crossOverStrategy == typeOfGeneticCrossover.MPC) {
352-
for (int i = 0; i < population.size(); i += 3) {
353-
if (randomGenerator.nextDouble() <= MPC_cr && population.size() > i + 2) {
354-
children = MPCCrossOver(population.get(i).get(0), population.get(i + 1).get(0), population.get(i + 2).get(0));
346+
switch (this.crossOverStrategy) {
347+
default: //will use OnePoint as default because no break here
348+
case OnePoint:
349+
for (int i = 0; i < numOfCrossing; ++i) {
350+
int indexOfFather = randomGenerator.nextInt(populationSize);
351+
int indexOfMother = randomGenerator.nextInt(populationSize);
352+
ArrayList<SimulationType> tmpList = onePointCrossOver(population.get(indexOfFather).get(0), population.get(indexOfMother).get(0));
353+
for (int c = 0; c < tmpList.size(); c++) {
354+
children.add(tmpList.get(c));
355+
}
355356
}
356-
}
357-
} else {
358-
//TODO default handling
357+
break;
358+
case SBX:
359+
for (int i = 0; i < numOfCrossing; ++i) {
360+
int indexOfFather = randomGenerator.nextInt(populationSize);
361+
int indexOfMother = randomGenerator.nextInt(populationSize);
362+
363+
ArrayList<SimulationType> tmpList = SBXCrossOver(population.get(indexOfFather).get(0), population.get(indexOfMother).get(0));
364+
for (int c = 0; c < tmpList.size(); c++) {
365+
children.add(tmpList.get(c));
366+
}
367+
}
368+
break;
369+
case MPC:
370+
for (int i = 0; i < population.size(); i += 3) {
371+
if (randomGenerator.nextDouble() <= MPC_cr && population.size() > i + 2) {
372+
children = MPCCrossOver(population.get(i).get(0), population.get(i + 1).get(0), population.get(i + 2).get(0));
373+
}
374+
}
375+
break;
359376
}
360377

378+
support.log("Number of new children: " + children.size());
361379
for (SimulationType child : children) {
362380
ArrayList<SimulationType> childList = new ArrayList<>();
363381
childList.add(child);

TimeNETOptimizationEnvironment/src/toe/optimization/OptimizerPreferences.form

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -984,13 +984,15 @@
984984
</Group>
985985
<EmptySpace min="-2" pref="46" max="-2" attributes="0"/>
986986
<Group type="103" groupAlignment="0" attributes="0">
987+
<Component id="jLabel31" min="-2" max="-2" attributes="0"/>
987988
<Component id="jLabel27" min="-2" max="-2" attributes="0"/>
988989
<Component id="jLabel30" min="-2" max="-2" attributes="0"/>
989990
</Group>
990-
<EmptySpace type="unrelated" max="-2" attributes="0"/>
991+
<EmptySpace type="separate" max="-2" attributes="0"/>
991992
<Group type="103" groupAlignment="0" max="-2" attributes="0">
992993
<Component id="jSpinnerGeneticMaxOptiRunsWithoutImprovement" pref="65" max="32767" attributes="0"/>
993994
<Component id="jComboBoxGeneticTypeOfGeneticCrossing" max="32767" attributes="0"/>
995+
<Component id="jSpinnerGeneticMaxNumberOfCrossings" alignment="0" pref="65" max="32767" attributes="0"/>
994996
</Group>
995997
<EmptySpace min="-2" pref="492" max="-2" attributes="0"/>
996998
</Group>
@@ -1014,8 +1016,12 @@
10141016
<Component id="jLabel30" alignment="3" min="-2" max="-2" attributes="0"/>
10151017
</Group>
10161018
<EmptySpace type="unrelated" max="-2" attributes="0"/>
1017-
<Component id="jCheckBoxGeneticMutateTopSolution" min="-2" max="-2" attributes="0"/>
1018-
<EmptySpace pref="232" max="32767" attributes="0"/>
1019+
<Group type="103" groupAlignment="3" attributes="0">
1020+
<Component id="jCheckBoxGeneticMutateTopSolution" alignment="3" min="-2" max="-2" attributes="0"/>
1021+
<Component id="jSpinnerGeneticMaxNumberOfCrossings" alignment="3" min="-2" max="-2" attributes="0"/>
1022+
<Component id="jLabel31" alignment="0" min="-2" max="-2" attributes="0"/>
1023+
</Group>
1024+
<EmptySpace pref="260" max="32767" attributes="0"/>
10191025
</Group>
10201026
</Group>
10211027
</DimensionLayout>
@@ -1087,6 +1093,18 @@
10871093
<Property name="text" type="java.lang.String" value="Kind of crossing"/>
10881094
</Properties>
10891095
</Component>
1096+
<Component class="javax.swing.JSpinner" name="jSpinnerGeneticMaxNumberOfCrossings">
1097+
<Properties>
1098+
<Property name="model" type="javax.swing.SpinnerModel" editor="org.netbeans.modules.form.editors2.SpinnerModelEditor">
1099+
<SpinnerModel initial="1" minimum="1" numberType="java.lang.Integer" stepSize="1" type="number"/>
1100+
</Property>
1101+
</Properties>
1102+
</Component>
1103+
<Component class="javax.swing.JLabel" name="jLabel31">
1104+
<Properties>
1105+
<Property name="text" type="java.lang.String" value="Number of crossings per generation"/>
1106+
</Properties>
1107+
</Component>
10901108
</SubComponents>
10911109
</Container>
10921110
<Container class="javax.swing.JPanel" name="jPanel4">

TimeNETOptimizationEnvironment/src/toe/optimization/OptimizerPreferences.java

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public final class OptimizerPreferences extends javax.swing.JFrame {
5858
private boolean pref_GeneticMutateTopSolution;
5959
private int pref_GeneticMaximumOptirunsWithoutSolution;
6060
private typeOfGeneticCrossover pref_GeneticTypeOfCrossover;
61+
private int pref_GeneticNumberOfCrossings;
6162

6263
//parameters for CSS Optimization
6364
private int pref_CSS_PopulationSize;
@@ -208,6 +209,8 @@ private void initComponents() {
208209
jLabel27 = new javax.swing.JLabel();
209210
jComboBoxGeneticTypeOfGeneticCrossing = new javax.swing.JComboBox();
210211
jLabel30 = new javax.swing.JLabel();
212+
jSpinnerGeneticMaxNumberOfCrossings = new javax.swing.JSpinner();
213+
jLabel31 = new javax.swing.JLabel();
211214
jPanel4 = new javax.swing.JPanel();
212215
jSpinnerCSSPopulationSize = new javax.swing.JSpinner();
213216
jLabelCSSPopulationSize = new javax.swing.JLabel();
@@ -706,6 +709,10 @@ public void actionPerformed(java.awt.event.ActionEvent evt) {
706709

707710
jLabel30.setText("Kind of crossing");
708711

712+
jSpinnerGeneticMaxNumberOfCrossings.setModel(new javax.swing.SpinnerNumberModel(Integer.valueOf(1), Integer.valueOf(1), null, Integer.valueOf(1)));
713+
714+
jLabel31.setText("Number of crossings per generation");
715+
709716
javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
710717
jPanel3.setLayout(jPanel3Layout);
711718
jPanel3Layout.setHorizontalGroup(
@@ -727,12 +734,14 @@ public void actionPerformed(java.awt.event.ActionEvent evt) {
727734
.addComponent(jSpinnerGeneticMutationChance))))
728735
.addGap(46, 46, 46)
729736
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
737+
.addComponent(jLabel31)
730738
.addComponent(jLabel27)
731739
.addComponent(jLabel30))
732-
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
740+
.addGap(18, 18, 18)
733741
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
734742
.addComponent(jSpinnerGeneticMaxOptiRunsWithoutImprovement, javax.swing.GroupLayout.DEFAULT_SIZE, 65, Short.MAX_VALUE)
735-
.addComponent(jComboBoxGeneticTypeOfGeneticCrossing, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
743+
.addComponent(jComboBoxGeneticTypeOfGeneticCrossing, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
744+
.addComponent(jSpinnerGeneticMaxNumberOfCrossings, javax.swing.GroupLayout.DEFAULT_SIZE, 65, Short.MAX_VALUE))
736745
.addGap(492, 492, 492))
737746
);
738747
jPanel3Layout.setVerticalGroup(
@@ -751,8 +760,11 @@ public void actionPerformed(java.awt.event.ActionEvent evt) {
751760
.addComponent(jComboBoxGeneticTypeOfGeneticCrossing, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
752761
.addComponent(jLabel30))
753762
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
754-
.addComponent(jCheckBoxGeneticMutateTopSolution)
755-
.addContainerGap(232, Short.MAX_VALUE))
763+
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
764+
.addComponent(jCheckBoxGeneticMutateTopSolution)
765+
.addComponent(jSpinnerGeneticMaxNumberOfCrossings, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
766+
.addComponent(jLabel31))
767+
.addContainerGap(260, Short.MAX_VALUE))
756768
);
757769

758770
jTabbedPane1.addTab("Genetic", jPanel3);
@@ -1289,6 +1301,7 @@ private void jComboBoxGeneticTypeOfGeneticCrossingActionPerformed(java.awt.event
12891301
private javax.swing.JLabel jLabel29;
12901302
private javax.swing.JLabel jLabel3;
12911303
private javax.swing.JLabel jLabel30;
1304+
private javax.swing.JLabel jLabel31;
12921305
private javax.swing.JLabel jLabel4;
12931306
private javax.swing.JLabel jLabel5;
12941307
private javax.swing.JLabel jLabel6;
@@ -1334,6 +1347,7 @@ private void jComboBoxGeneticTypeOfGeneticCrossingActionPerformed(java.awt.event
13341347
private javax.swing.JSpinner jSpinnerConfidenceIntervallStart;
13351348
private javax.swing.JSpinner jSpinnerEpsilon;
13361349
private javax.swing.JSpinner jSpinnerEpsilon1;
1350+
private javax.swing.JSpinner jSpinnerGeneticMaxNumberOfCrossings;
13371351
private javax.swing.JSpinner jSpinnerGeneticMaxOptiRunsWithoutImprovement;
13381352
private javax.swing.JSpinner jSpinnerGeneticMutationChance;
13391353
private javax.swing.JSpinner jSpinnerGeneticPopulationSize;
@@ -1438,7 +1452,8 @@ public void loadPreferences() {
14381452
support.log("Loaded pref_GeneticMutationChance is " + this.getPref_GeneticMutationChance());
14391453
this.setPref_GeneticMutateTopSolution(Boolean.valueOf(auto.getProperty("pref_GeneticMutateTopSolution", Boolean.toString(support.DEFAULT_GENETIC_MUTATE_TOP_SOLUTION))));
14401454
support.log("Loaded pref_GeneticMutateTopSolution is " + this.getPref_GeneticMutateTopSolution());
1441-
1455+
this.setPref_GeneticNumberOfCrossings(support.loadIntFromProperties("pref_GeneticNumberOfCrossings", support.DEFAULT_GENETIC_NUMBEROFCROSSINGS, auto));
1456+
14421457
this.setPref_GeneticMaximumOptirunsWithoutSolution(support.loadIntFromProperties("pref_GeneticMaxOptiRunsWithoutSolution", support.DEFAULT_GENETIC_MAXWRONGOPTIRUNS, auto));
14431458
support.log("Loaded pref_GeneticMaxOptiRunsWithoutSolution is "+ this.getPref_GeneticMaximumOptirunsWithoutSolution());
14441459
this.setPref_GeneticTypeOfCrossover(typeOfGeneticCrossover.valueOf(auto.getProperty("pref_GeneticTypeOfCrossover", support.DEFAULT_GENETIC_CROSSOVER.toString())));
@@ -1517,6 +1532,7 @@ public void savePreferences() {
15171532
auto.setProperty("pref_GeneticMutateTopSolution", Boolean.toString(this.getPref_GeneticMutateTopSolution()));
15181533
auto.setProperty("pref_GeneticMaxOptiRunsWithoutSolution", Integer.toString(this.getPref_GeneticMaximumOptirunsWithoutSolution()));
15191534
auto.setProperty("pref_GeneticTypeOfCrossover", this.getPref_GeneticTypeOfCrossover().toString());
1535+
auto.setProperty("pref_GeneticNumberOfCrossings", Integer.toString(this.getPref_GeneticNumberOfCrossings()));
15201536

15211537
//setting parameters for CSS optimization
15221538
auto.setProperty("pref_CSS_PopulationSize", Integer.toString(this.getPref_CSS_PopulationSize()));
@@ -2252,4 +2268,20 @@ public void setPref_GeneticTypeOfCrossover(typeOfGeneticCrossover pref_GeneticTy
22522268
this.jComboBoxGeneticTypeOfGeneticCrossing.setSelectedItem(pref_GeneticTypeOfCrossover);
22532269
this.pref_GeneticTypeOfCrossover = pref_GeneticTypeOfCrossover;
22542270
}
2271+
2272+
/**
2273+
* @return the pref_GeneticNumberOfCrossings
2274+
*/
2275+
public int getPref_GeneticNumberOfCrossings() {
2276+
pref_GeneticNumberOfCrossings=(Integer)jSpinnerGeneticMaxNumberOfCrossings.getValue();
2277+
return pref_GeneticNumberOfCrossings;
2278+
}
2279+
2280+
/**
2281+
* @param pref_GeneticNumberOfCrossings the pref_GeneticNumberOfCrossings to set
2282+
*/
2283+
public void setPref_GeneticNumberOfCrossings(int pref_GeneticNumberOfCrossings) {
2284+
jSpinnerGeneticMaxNumberOfCrossings.setValue(pref_GeneticNumberOfCrossings);
2285+
this.pref_GeneticNumberOfCrossings = pref_GeneticNumberOfCrossings;
2286+
}
22552287
}

TimeNETOptimizationEnvironment/src/toe/support.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ public class support {
7373
public static final boolean DEFAULT_GENETIC_MUTATE_TOP_SOLUTION = false;
7474
public static final int DEFAULT_GENETIC_MAXWRONGOPTIRUNS=5;
7575
public static final typeOfGeneticCrossover DEFAULT_GENETIC_CROSSOVER=typeOfGeneticCrossover.SBX;
76+
public static final int DEFAULT_GENETIC_NUMBEROFCROSSINGS=2;
7677

7778
//default values for CSS Optimization
7879
public static final int DEFAULT_CSS_POPULATION_SIZE = 10;

0 commit comments

Comments
 (0)