Skip to content

Commit 5d9797d

Browse files
committed
Modifications for adaptive model
1 parent 4b094b2 commit 5d9797d

File tree

37 files changed

+1511
-122
lines changed

37 files changed

+1511
-122
lines changed

commons-math-examples/examples-ga/examples-ga-math-functions/src/main/java/org/apache/commons/math4/examples/ga/mathfunctions/utils/GraphPlotter.java renamed to commons-math-examples/examples-ga/examples-ga-math-functions/src/main/java/org/apache/commons/math4/examples/ga/mathfunctions/dimension2/Dim2GraphPlotter.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,14 @@
1515
* limitations under the License.
1616
*/
1717

18-
package org.apache.commons.math4.examples.ga.mathfunctions.utils;
18+
package org.apache.commons.math4.examples.ga.mathfunctions.dimension2;
1919

2020
import java.awt.BorderLayout;
2121
import java.util.List;
2222

2323
import javax.swing.JFrame;
2424
import javax.swing.JPanel;
2525

26-
import org.apache.commons.math4.examples.ga.mathfunctions.Coordinate;
2726
import org.apache.commons.math4.ga.listener.ConvergenceListener;
2827
import org.apache.commons.math4.ga.population.Population;
2928
import org.apache.commons.math4.ga.stats.PopulationStatisticalSummary;
@@ -40,7 +39,7 @@
4039
/**
4140
* This class represents the graph plotter during optimization.
4241
*/
43-
public class GraphPlotter extends JFrame implements ConvergenceListener<Coordinate> {
42+
public class Dim2GraphPlotter extends JFrame implements ConvergenceListener<Dimension2Coordinate> {
4443

4544
/**
4645
* Generated serialversionId.
@@ -56,7 +55,7 @@ public class GraphPlotter extends JFrame implements ConvergenceListener<Coordina
5655
* @param xAxisLabel x axis label
5756
* @param yAxisLabel y axis label
5857
*/
59-
public GraphPlotter(String plotSubject, String xAxisLabel, String yAxisLabel) {
58+
public Dim2GraphPlotter(String plotSubject, String xAxisLabel, String yAxisLabel) {
6059
super(plotSubject);
6160

6261
final JPanel chartPanel = createChartPanel(plotSubject, xAxisLabel, yAxisLabel);
@@ -136,9 +135,9 @@ private JPanel createChartPanel(String chartTitle, String xAxisLabel, String yAx
136135
* {@inheritDoc}
137136
*/
138137
@Override
139-
public void notify(int generation, Population<Coordinate> population) {
140-
PopulationStatisticalSummary<Coordinate> populationStatisticalSummary = new PopulationStatisticalSummaryImpl<>(
141-
population);
138+
public void notify(int generation, Population<Dimension2Coordinate> population) {
139+
PopulationStatisticalSummary<Dimension2Coordinate> populationStatisticalSummary =
140+
new PopulationStatisticalSummaryImpl<>(population);
142141
this.addDataPoint("Average", generation, populationStatisticalSummary.getMeanFitness());
143142
this.addDataPoint("Best", generation, populationStatisticalSummary.getMaxFitness());
144143
}

commons-math-examples/examples-ga/examples-ga-math-functions/src/main/java/org/apache/commons/math4/examples/ga/mathfunctions/Coordinate.java renamed to commons-math-examples/examples-ga/examples-ga-math-functions/src/main/java/org/apache/commons/math4/examples/ga/mathfunctions/dimension2/Dimension2Coordinate.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
package org.apache.commons.math4.examples.ga.mathfunctions;
17+
package org.apache.commons.math4.examples.ga.mathfunctions.dimension2;
1818

1919
/**
2020
* This class represents the coordinate of the problem domain i.e. the phenotype of chromosome.
2121
*/
22-
public class Coordinate {
22+
public class Dimension2Coordinate {
2323

2424
/** coordinate of first dimension. **/
2525
private final double x;
@@ -32,7 +32,7 @@ public class Coordinate {
3232
* @param x coordinate of first dimension
3333
* @param y coordinate of second dimension
3434
*/
35-
public Coordinate(double x, double y) {
35+
public Dimension2Coordinate(double x, double y) {
3636
this.x = x;
3737
this.y = y;
3838
}

commons-math-examples/examples-ga/examples-ga-math-functions/src/main/java/org/apache/commons/math4/examples/ga/mathfunctions/Dimension2Decoder.java renamed to commons-math-examples/examples-ga/examples-ga-math-functions/src/main/java/org/apache/commons/math4/examples/ga/mathfunctions/dimension2/Dimension2Decoder.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
package org.apache.commons.math4.examples.ga.mathfunctions;
17+
package org.apache.commons.math4.examples.ga.mathfunctions.dimension2;
1818

1919
import java.util.List;
2020

@@ -24,17 +24,19 @@
2424

2525
/**
2626
* Decoder to convert chromosome's binary genotype to phenotype
27-
* {@link Coordinate}.
27+
* {@link Dimension2Coordinate}.
2828
*/
29-
public class Dimension2Decoder extends AbstractListChromosomeDecoder<Integer, Coordinate> {
29+
public class Dimension2Decoder extends AbstractListChromosomeDecoder<Integer, Dimension2Coordinate> {
3030

3131
/**
32-
* decode the binary representation of chromosome to {@link Coordinate}.
32+
* decode the binary representation of chromosome to
33+
* {@link Dimension2Coordinate}.
3334
* @param chromosome The {@link AbstractListChromosome}
3435
*/
3536
@Override
36-
protected Coordinate decode(AbstractListChromosome<Integer, Coordinate> chromosome) {
37-
final BinaryChromosome<Coordinate> binaryChromosome = (BinaryChromosome<Coordinate>) chromosome;
37+
protected Dimension2Coordinate decode(AbstractListChromosome<Integer, Dimension2Coordinate> chromosome) {
38+
final BinaryChromosome<Dimension2Coordinate> binaryChromosome =
39+
(BinaryChromosome<Dimension2Coordinate>) chromosome;
3840
final List<Integer> alleles = binaryChromosome.getRepresentation();
3941

4042
final StringBuilder allelesStr = new StringBuilder();
@@ -45,7 +47,7 @@ protected Coordinate decode(AbstractListChromosome<Integer, Coordinate> chromoso
4547
final double x = Integer.parseInt(allelesStr.substring(0, 12), 2) / 100.0;
4648
final double y = Integer.parseInt(allelesStr.substring(12, 24), 2) / 100.0;
4749

48-
return new Coordinate(x, y);
50+
return new Dimension2Coordinate(x, y);
4951
}
5052

5153
}

commons-math-examples/examples-ga/examples-ga-math-functions/src/main/java/org/apache/commons/math4/examples/ga/mathfunctions/Dimension2FitnessFunction.java renamed to commons-math-examples/examples-ga/examples-ga-math-functions/src/main/java/org/apache/commons/math4/examples/ga/mathfunctions/dimension2/Dimension2FitnessFunction.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,23 @@
1515
* limitations under the License.
1616
*/
1717

18-
package org.apache.commons.math4.examples.ga.mathfunctions;
18+
package org.apache.commons.math4.examples.ga.mathfunctions.dimension2;
1919

2020
import org.apache.commons.math4.ga.fitness.FitnessFunction;
2121

2222
/**
2323
* This class represents the mathematical fitness function for optimizing a 2
2424
* dimension mathematical function.
2525
*/
26-
public class Dimension2FitnessFunction implements FitnessFunction<Coordinate> {
26+
public class Dimension2FitnessFunction implements FitnessFunction<Dimension2Coordinate> {
2727

2828
/**
2929
* Computes the fitness value based on the decoded chromosome.
30-
* @param coordinate The {@link Coordinate}
30+
* @param coordinate The {@link Dimension2Coordinate}
3131
* @return the fitness value
3232
*/
3333
@Override
34-
public double compute(Coordinate coordinate) {
34+
public double compute(Dimension2Coordinate coordinate) {
3535
return -Math.pow(Math.pow(coordinate.getX(), 2) + Math.pow(coordinate.getY(), 2), .25) *
3636
(Math.pow(Math.sin(50 * Math.pow(Math.pow(coordinate.getX(), 2) + Math.pow(coordinate.getY(), 2), .1)),
3737
2) + 1);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.commons.math4.examples.ga.mathfunctions.dimension2;
19+
20+
import org.apache.commons.math4.examples.ga.mathfunctions.utils.Constants;
21+
import org.apache.commons.math4.ga.AdaptiveGeneticAlgorithm;
22+
import org.apache.commons.math4.ga.chromosome.BinaryChromosome;
23+
import org.apache.commons.math4.ga.chromosome.Chromosome;
24+
import org.apache.commons.math4.ga.convergencecond.StoppingCondition;
25+
import org.apache.commons.math4.ga.convergencecond.UnchangedBestFitness;
26+
import org.apache.commons.math4.ga.crossover.OnePointCrossover;
27+
import org.apache.commons.math4.ga.crossover.rategenerator.ConstantCrossoverRateGenerator;
28+
import org.apache.commons.math4.ga.listener.ConvergenceListenerRegistry;
29+
import org.apache.commons.math4.ga.listener.PopulationStatisticsLogger;
30+
import org.apache.commons.math4.ga.mutation.BinaryMutation;
31+
import org.apache.commons.math4.ga.mutation.rategenerator.AdaptiveLinearMutationRateGenerator;
32+
import org.apache.commons.math4.ga.population.ListPopulation;
33+
import org.apache.commons.math4.ga.population.Population;
34+
import org.apache.commons.math4.ga.selection.TournamentSelection;
35+
import org.apache.commons.math4.ga.utils.ConsoleLogger;
36+
37+
/**
38+
* This class represents an optimizer for a 2-dimensional math function using
39+
* genetic algorithm.
40+
*/
41+
public class Dimension2FunctionAdaptiveOptimizer {
42+
43+
/** number of dimension. **/
44+
private static final int DIMENSION = 2;
45+
46+
/** size of tournament. **/
47+
private static final int TOURNAMENT_SIZE = 3;
48+
49+
/**
50+
* Optimizes the 2-dimension fitness function.
51+
* @param args arguments
52+
*/
53+
public static void main(String[] args) {
54+
final Population<Dimension2Coordinate> initPopulation = getInitialPopulation();
55+
56+
final Dimension2FunctionAdaptiveOptimizer optimizer = new Dimension2FunctionAdaptiveOptimizer();
57+
58+
final ConvergenceListenerRegistry<Dimension2Coordinate> convergenceListenerRegistry =
59+
ConvergenceListenerRegistry.getInstance();
60+
convergenceListenerRegistry
61+
.addConvergenceListener(new PopulationStatisticsLogger<Dimension2Coordinate>(Constants.ENCODING));
62+
convergenceListenerRegistry
63+
.addConvergenceListener(new Dim2GraphPlotter("Adaptive Convergence Stats", "generation", "fitness"));
64+
65+
optimizer.optimize(initPopulation);
66+
}
67+
68+
private void optimize(Population<Dimension2Coordinate> initial) {
69+
70+
// initialize a new genetic algorithm
71+
final AdaptiveGeneticAlgorithm<Dimension2Coordinate> ga = new AdaptiveGeneticAlgorithm<>(
72+
new OnePointCrossover<Integer, Dimension2Coordinate>(), new ConstantCrossoverRateGenerator<>(1),
73+
new BinaryMutation<Dimension2Coordinate>(),
74+
new AdaptiveLinearMutationRateGenerator<>(Constants.AVERAGE_MUTATION_RATE / 2,
75+
Constants.AVERAGE_MUTATION_RATE * 2),
76+
new TournamentSelection<>(TOURNAMENT_SIZE));
77+
78+
// stopping condition
79+
final StoppingCondition<Dimension2Coordinate> stopCond = new UnchangedBestFitness<>(
80+
Constants.GENERATION_COUNT_WITH_UNCHANGED_BEST_FUTNESS);
81+
82+
// run the algorithm
83+
final Population<Dimension2Coordinate> finalPopulation = ga.evolve(initial, stopCond);
84+
85+
// best chromosome from the final population
86+
final Chromosome<Dimension2Coordinate> bestFinal = finalPopulation.getFittestChromosome();
87+
final ConsoleLogger consoleLogger = ConsoleLogger.getInstance(Constants.ENCODING);
88+
consoleLogger.log("*********************************************");
89+
consoleLogger.log("***********Optimization Result***************");
90+
91+
consoleLogger.log(bestFinal.toString());
92+
93+
}
94+
95+
/**
96+
* Generates an initial population.
97+
* @return initial population
98+
*/
99+
private static Population<Dimension2Coordinate> getInitialPopulation() {
100+
final Population<Dimension2Coordinate> population = new ListPopulation<>(
101+
DIMENSION * Constants.POPULATION_SIZE_PER_DIMENSION);
102+
final Dimension2FitnessFunction fitnessFunction = new Dimension2FitnessFunction();
103+
final Dimension2Decoder decoder = new Dimension2Decoder();
104+
for (int i = 0; i < DIMENSION * Constants.POPULATION_SIZE_PER_DIMENSION; i++) {
105+
population.addChromosome(BinaryChromosome.<Dimension2Coordinate>randomChromosome(
106+
DIMENSION * Constants.CHROMOSOME_LENGTH_PER_DIMENSION, fitnessFunction, decoder));
107+
}
108+
return population;
109+
}
110+
111+
}

commons-math-examples/examples-ga/examples-ga-math-functions/src/main/java/org/apache/commons/math4/examples/ga/mathfunctions/Dimension2FunctionOptimizer.java renamed to commons-math-examples/examples-ga/examples-ga-math-functions/src/main/java/org/apache/commons/math4/examples/ga/mathfunctions/dimension2/Dimension2FunctionOptimizer.java

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,11 @@
1515
* limitations under the License.
1616
*/
1717

18-
package org.apache.commons.math4.examples.ga.mathfunctions;
18+
package org.apache.commons.math4.examples.ga.mathfunctions.dimension2;
1919

2020
import org.apache.commons.math4.ga.crossover.OnePointCrossover;
2121
import org.apache.commons.math4.ga.convergencecond.UnchangedBestFitness;
2222
import org.apache.commons.math4.examples.ga.mathfunctions.utils.Constants;
23-
import org.apache.commons.math4.examples.ga.mathfunctions.utils.GraphPlotter;
2423
import org.apache.commons.math4.ga.GeneticAlgorithm;
2524
import org.apache.commons.math4.ga.chromosome.BinaryChromosome;
2625
import org.apache.commons.math4.ga.chromosome.Chromosome;
@@ -39,21 +38,27 @@
3938
*/
4039
public class Dimension2FunctionOptimizer {
4140

41+
/** number of dimension. **/
42+
private static final int DIMENSION = 2;
43+
44+
/** size of tournament. **/
45+
private static final int TOURNAMENT_SIZE = 3;
46+
4247
/**
4348
* Optimizes the 2-dimension fitness function.
4449
* @param args arguments
4550
*/
4651
public static void main(String[] args) {
47-
final Population<Coordinate> initPopulation = getInitialPopulation();
52+
final Population<Dimension2Coordinate> initPopulation = getInitialPopulation();
4853

4954
final Dimension2FunctionOptimizer optimizer = new Dimension2FunctionOptimizer();
5055

51-
final ConvergenceListenerRegistry<Coordinate> convergenceListenerRegistry = ConvergenceListenerRegistry
52-
.getInstance();
56+
final ConvergenceListenerRegistry<Dimension2Coordinate> convergenceListenerRegistry =
57+
ConvergenceListenerRegistry.getInstance();
5358
convergenceListenerRegistry
54-
.addConvergenceListener(new PopulationStatisticsLogger<Coordinate>(Constants.ENCODING));
59+
.addConvergenceListener(new PopulationStatisticsLogger<Dimension2Coordinate>(Constants.ENCODING));
5560
convergenceListenerRegistry
56-
.addConvergenceListener(new GraphPlotter("Convergence Stats", "generation", "fitness"));
61+
.addConvergenceListener(new Dim2GraphPlotter("Convergence Stats", "generation", "fitness"));
5762

5863
optimizer.optimize(initPopulation);
5964
}
@@ -62,22 +67,24 @@ public static void main(String[] args) {
6267
* Optimizes the population.
6368
* @param initial The {@link Population}
6469
*/
65-
public void optimize(Population<Coordinate> initial) {
70+
public void optimize(Population<Dimension2Coordinate> initial) {
6671

6772
// initialize a new genetic algorithm
68-
final GeneticAlgorithm<Coordinate> ga = new GeneticAlgorithm<>(new OnePointCrossover<Integer, Coordinate>(),
69-
Constants.CROSSOVER_RATE, new BinaryMutation<Coordinate>(), Constants.AVERAGE_MUTATION_RATE,
70-
new TournamentSelection<Coordinate>(Constants.TOURNAMENT_SIZE), Constants.ELITISM_RATE);
73+
final GeneticAlgorithm<Dimension2Coordinate> ga = new GeneticAlgorithm<>(
74+
new OnePointCrossover<Integer, Dimension2Coordinate>(), Constants.CROSSOVER_RATE,
75+
new BinaryMutation<Dimension2Coordinate>(), Constants.AVERAGE_MUTATION_RATE,
76+
new TournamentSelection<Dimension2Coordinate>(TOURNAMENT_SIZE),
77+
Constants.ELITISM_RATE);
7178

7279
// stopping condition
73-
final StoppingCondition<Coordinate> stopCond = new UnchangedBestFitness<>(
80+
final StoppingCondition<Dimension2Coordinate> stopCond = new UnchangedBestFitness<>(
7481
Constants.GENERATION_COUNT_WITH_UNCHANGED_BEST_FUTNESS);
7582

7683
// run the algorithm
77-
final Population<Coordinate> finalPopulation = ga.evolve(initial, stopCond);
84+
final Population<Dimension2Coordinate> finalPopulation = ga.evolve(initial, stopCond);
7885

7986
// best chromosome from the final population
80-
final Chromosome<Coordinate> bestFinal = finalPopulation.getFittestChromosome();
87+
final Chromosome<Dimension2Coordinate> bestFinal = finalPopulation.getFittestChromosome();
8188
final ConsoleLogger consoleLogger = ConsoleLogger.getInstance(Constants.ENCODING);
8289
consoleLogger.log("*********************************************");
8390
consoleLogger.log("***********Optimization Result***************");
@@ -90,13 +97,14 @@ public void optimize(Population<Coordinate> initial) {
9097
* Generates an initial population.
9198
* @return initial population
9299
*/
93-
private static Population<Coordinate> getInitialPopulation() {
94-
final Population<Coordinate> population = new ListPopulation<>(Constants.POPULATION_SIZE);
100+
private static Population<Dimension2Coordinate> getInitialPopulation() {
101+
final Population<Dimension2Coordinate> population = new ListPopulation<>(
102+
DIMENSION * Constants.POPULATION_SIZE_PER_DIMENSION);
95103
final Dimension2FitnessFunction fitnessFunction = new Dimension2FitnessFunction();
96104
final Dimension2Decoder decoder = new Dimension2Decoder();
97-
for (int i = 0; i < Constants.POPULATION_SIZE; i++) {
98-
population.addChromosome(BinaryChromosome.<Coordinate>randomChromosome(Constants.CHROMOSOME_LENGTH,
99-
fitnessFunction, decoder));
105+
for (int i = 0; i < DIMENSION * Constants.POPULATION_SIZE_PER_DIMENSION; i++) {
106+
population.addChromosome(BinaryChromosome.<Dimension2Coordinate>randomChromosome(
107+
DIMENSION * Constants.CHROMOSOME_LENGTH_PER_DIMENSION, fitnessFunction, decoder));
100108
}
101109
return population;
102110
}

0 commit comments

Comments
 (0)