Skip to content

Commit 93fe6a2

Browse files
committed
Developed the new genetic algorithm module following the JIRA MATH-1563.
1 parent 142dcaa commit 93fe6a2

File tree

148 files changed

+10287
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

148 files changed

+10287
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
Licensed to the Apache Software Foundation (ASF) under one or more
4+
contributor license agreements. See the NOTICE file distributed with
5+
this work for additional information regarding copyright ownership.
6+
The ASF licenses this file to You under the Apache License, Version 2.0
7+
(the "License"); you may not use this file except in compliance with
8+
the License. You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
-->
18+
<project xmlns="http://maven.apache.org/POM/4.0.0"
19+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
20+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
21+
<modelVersion>4.0.0</modelVersion>
22+
<parent>
23+
<groupId>org.apache.commons</groupId>
24+
<artifactId>examples-ga</artifactId>
25+
<version>4.0-SNAPSHOT</version>
26+
</parent>
27+
<artifactId>examples-ga-math-functions</artifactId>
28+
29+
<properties>
30+
<maven.compiler.source>1.8</maven.compiler.source>
31+
<maven.compiler.target>1.8</maven.compiler.target>
32+
33+
<!-- OSGi -->
34+
<commons.osgi.symbolicName>org.apache.commons.math4.examples.ga.mathfunctions</commons.osgi.symbolicName>
35+
<commons.osgi.export>org.apache.commons.math4.examples.ga.mathfunctions</commons.osgi.export>
36+
<!-- Java 9+ -->
37+
<commons.automatic.module.name>org.apache.commons.math4.examples.ga.mathfunctions</commons.automatic.module.name>
38+
<!-- Workaround to avoid duplicating config files. -->
39+
<math.parent.dir>${basedir}/../../..</math.parent.dir>
40+
41+
<uberjar.name>examples-ga-mathfunctions</uberjar.name>
42+
<project.mainClass>org.apache.commons.math4.examples.ga.mathfunctions.Dimension2FunctionOptimizer</project.mainClass>
43+
</properties>
44+
45+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
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 java.awt.BorderLayout;
21+
import java.util.List;
22+
23+
import javax.swing.JFrame;
24+
import javax.swing.JPanel;
25+
26+
import org.apache.commons.math4.ga.internal.stats.PopulationStatisticalSummaryImpl;
27+
import org.apache.commons.math4.ga.listener.ConvergenceListener;
28+
import org.apache.commons.math4.ga.population.Population;
29+
import org.apache.commons.math4.ga.stats.PopulationStatisticalSummary;
30+
import org.jfree.chart.ChartFactory;
31+
import org.jfree.chart.ChartPanel;
32+
import org.jfree.chart.JFreeChart;
33+
import org.jfree.chart.plot.PlotOrientation;
34+
import org.jfree.chart.plot.XYPlot;
35+
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
36+
import org.jfree.data.xy.XYSeries;
37+
import org.jfree.data.xy.XYSeriesCollection;
38+
39+
/**
40+
* This class represents the graph plotter during optimization.
41+
*/
42+
public class Dim2GraphPlotter extends JFrame implements ConvergenceListener<Dimension2Coordinate> {
43+
44+
/**
45+
* Generated serialversionId.
46+
*/
47+
private static final long serialVersionUID = -5683904006424006584L;
48+
49+
/** collection of 2-D series. **/
50+
private final XYSeriesCollection dataset = new XYSeriesCollection();
51+
52+
/**
53+
* constructor.
54+
* @param plotSubject subject of plot
55+
* @param xAxisLabel x axis label
56+
* @param yAxisLabel y axis label
57+
*/
58+
public Dim2GraphPlotter(String plotSubject, String xAxisLabel, String yAxisLabel) {
59+
super(plotSubject);
60+
61+
final JPanel chartPanel = createChartPanel(plotSubject, xAxisLabel, yAxisLabel);
62+
add(chartPanel, BorderLayout.CENTER);
63+
64+
setSize(640, 480);
65+
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
66+
setLocationRelativeTo(null);
67+
68+
setVisible(true);
69+
}
70+
71+
/**
72+
* Adds data point to graph.
73+
* @param graphName name of graph
74+
* @param generation generation, to be plotted along x axis
75+
* @param value value, to be plotted along y axis
76+
*/
77+
private void addDataPoint(String graphName, int generation, double value) {
78+
XYSeries series = null;
79+
80+
if (!containsGraph(graphName)) {
81+
series = new XYSeries(graphName);
82+
dataset.addSeries(series);
83+
} else {
84+
series = dataset.getSeries(graphName);
85+
}
86+
series.add(generation, value);
87+
88+
setVisible(true);
89+
}
90+
91+
/**
92+
* Checks if the graph with the given name already exists.
93+
* @param graphName name of the graph
94+
* @return true/false
95+
*/
96+
@SuppressWarnings("unchecked")
97+
private boolean containsGraph(String graphName) {
98+
final List<XYSeries> seriesList = dataset.getSeries();
99+
if (seriesList == null || seriesList.isEmpty()) {
100+
return false;
101+
}
102+
for (XYSeries series : seriesList) {
103+
if (series.getKey().compareTo(graphName) == 0) {
104+
return true;
105+
}
106+
}
107+
return false;
108+
}
109+
110+
/**
111+
* Creates chart panel.
112+
* @param chartTitle chart title
113+
* @param xAxisLabel x axis label
114+
* @param yAxisLabel y axis label
115+
* @return panel
116+
*/
117+
private JPanel createChartPanel(String chartTitle, String xAxisLabel, String yAxisLabel) {
118+
119+
final boolean showLegend = true;
120+
final boolean createURL = false;
121+
final boolean createTooltip = false;
122+
123+
final JFreeChart chart = ChartFactory.createXYLineChart(chartTitle, xAxisLabel, yAxisLabel, dataset,
124+
PlotOrientation.VERTICAL, showLegend, createTooltip, createURL);
125+
final XYPlot plot = chart.getXYPlot();
126+
final XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
127+
128+
plot.setRenderer(renderer);
129+
130+
return new ChartPanel(chart);
131+
132+
}
133+
134+
/**
135+
* {@inheritDoc}
136+
*/
137+
@Override
138+
public void notify(int generation, Population<Dimension2Coordinate> population) {
139+
PopulationStatisticalSummary<Dimension2Coordinate> populationStatisticalSummary =
140+
new PopulationStatisticalSummaryImpl<>(population);
141+
this.addDataPoint("Average", generation, populationStatisticalSummary.getMeanFitness());
142+
this.addDataPoint("Best", generation, populationStatisticalSummary.getMaxFitness());
143+
}
144+
145+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
package org.apache.commons.math4.examples.ga.mathfunctions.dimension2;
18+
19+
/**
20+
* This class represents the coordinate of the problem domain i.e. the phenotype of chromosome.
21+
*/
22+
public class Dimension2Coordinate {
23+
24+
/** coordinate of first dimension. **/
25+
private final double x;
26+
27+
/** coordinate of second dimension. **/
28+
private final double y;
29+
30+
/**
31+
* constructor.
32+
* @param x coordinate of first dimension
33+
* @param y coordinate of second dimension
34+
*/
35+
public Dimension2Coordinate(double x, double y) {
36+
this.x = x;
37+
this.y = y;
38+
}
39+
40+
/**
41+
* returns the coordinate of first dimension.
42+
* @return coordinate of first dimension
43+
*/
44+
public double getX() {
45+
return x;
46+
}
47+
48+
/**
49+
* returns the coordinate of second dimension.
50+
* @return coordinate of second dimension
51+
*/
52+
public double getY() {
53+
return y;
54+
}
55+
56+
/**
57+
* Returns a string representation of coordinate.
58+
*/
59+
@Override
60+
public String toString() {
61+
return "Coordinate [x=" + x + ", y=" + y + "]";
62+
}
63+
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
package org.apache.commons.math4.examples.ga.mathfunctions.dimension2;
18+
19+
import java.util.List;
20+
21+
import org.apache.commons.math4.ga.chromosome.AbstractListChromosome;
22+
import org.apache.commons.math4.ga.chromosome.BinaryChromosome;
23+
import org.apache.commons.math4.ga.decoder.AbstractListChromosomeDecoder;
24+
25+
/**
26+
* Decoder to convert chromosome's binary genotype to phenotype
27+
* {@link Dimension2Coordinate}.
28+
*/
29+
public class Dimension2Decoder extends AbstractListChromosomeDecoder<Integer, Dimension2Coordinate> {
30+
31+
/**
32+
* decode the binary representation of chromosome to
33+
* {@link Dimension2Coordinate}.
34+
* @param chromosome The {@link AbstractListChromosome}
35+
*/
36+
@Override
37+
protected Dimension2Coordinate decode(AbstractListChromosome<Integer, Dimension2Coordinate> chromosome) {
38+
final BinaryChromosome<Dimension2Coordinate> binaryChromosome =
39+
(BinaryChromosome<Dimension2Coordinate>) chromosome;
40+
final List<Integer> alleles = binaryChromosome.getRepresentation();
41+
42+
final StringBuilder allelesStr = new StringBuilder();
43+
for (Integer allele : alleles) {
44+
allelesStr.append(Integer.toBinaryString(allele));
45+
}
46+
47+
final double x = Integer.parseInt(allelesStr.substring(0, 12), 2) / 100.0;
48+
final double y = Integer.parseInt(allelesStr.substring(12, 24), 2) / 100.0;
49+
50+
return new Dimension2Coordinate(x, y);
51+
}
52+
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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.ga.fitness.FitnessFunction;
21+
22+
/**
23+
* This class represents the mathematical fitness function for optimizing a 2
24+
* dimension mathematical function.
25+
*/
26+
public class Dimension2FitnessFunction implements FitnessFunction<Dimension2Coordinate> {
27+
28+
/**
29+
* Computes the fitness value based on the decoded chromosome.
30+
* @param coordinate The {@link Dimension2Coordinate}
31+
* @return the fitness value
32+
*/
33+
@Override
34+
public double compute(Dimension2Coordinate coordinate) {
35+
return -Math.pow(Math.pow(coordinate.getX(), 2) + Math.pow(coordinate.getY(), 2), .25) *
36+
(Math.pow(Math.sin(50 * Math.pow(Math.pow(coordinate.getX(), 2) + Math.pow(coordinate.getY(), 2), .1)),
37+
2) + 1);
38+
}
39+
40+
}

0 commit comments

Comments
 (0)