Skip to content

Commit

Permalink
Developed the new genetic algorithm module following the JIRA MATH-1563.
Browse files Browse the repository at this point in the history
  • Loading branch information
avbasak1 committed Nov 1, 2021
1 parent 142dcaa commit 93fe6a2
Show file tree
Hide file tree
Showing 148 changed files with 10,287 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?xml version="1.0"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.commons</groupId>
<artifactId>examples-ga</artifactId>
<version>4.0-SNAPSHOT</version>
</parent>
<artifactId>examples-ga-math-functions</artifactId>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>

<!-- OSGi -->
<commons.osgi.symbolicName>org.apache.commons.math4.examples.ga.mathfunctions</commons.osgi.symbolicName>
<commons.osgi.export>org.apache.commons.math4.examples.ga.mathfunctions</commons.osgi.export>
<!-- Java 9+ -->
<commons.automatic.module.name>org.apache.commons.math4.examples.ga.mathfunctions</commons.automatic.module.name>
<!-- Workaround to avoid duplicating config files. -->
<math.parent.dir>${basedir}/../../..</math.parent.dir>

<uberjar.name>examples-ga-mathfunctions</uberjar.name>
<project.mainClass>org.apache.commons.math4.examples.ga.mathfunctions.Dimension2FunctionOptimizer</project.mainClass>
</properties>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.commons.math4.examples.ga.mathfunctions.dimension2;

import java.awt.BorderLayout;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;

import org.apache.commons.math4.ga.internal.stats.PopulationStatisticalSummaryImpl;
import org.apache.commons.math4.ga.listener.ConvergenceListener;
import org.apache.commons.math4.ga.population.Population;
import org.apache.commons.math4.ga.stats.PopulationStatisticalSummary;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

/**
* This class represents the graph plotter during optimization.
*/
public class Dim2GraphPlotter extends JFrame implements ConvergenceListener<Dimension2Coordinate> {

/**
* Generated serialversionId.
*/
private static final long serialVersionUID = -5683904006424006584L;

/** collection of 2-D series. **/
private final XYSeriesCollection dataset = new XYSeriesCollection();

/**
* constructor.
* @param plotSubject subject of plot
* @param xAxisLabel x axis label
* @param yAxisLabel y axis label
*/
public Dim2GraphPlotter(String plotSubject, String xAxisLabel, String yAxisLabel) {
super(plotSubject);

final JPanel chartPanel = createChartPanel(plotSubject, xAxisLabel, yAxisLabel);
add(chartPanel, BorderLayout.CENTER);

setSize(640, 480);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);

setVisible(true);
}

/**
* Adds data point to graph.
* @param graphName name of graph
* @param generation generation, to be plotted along x axis
* @param value value, to be plotted along y axis
*/
private void addDataPoint(String graphName, int generation, double value) {
XYSeries series = null;

if (!containsGraph(graphName)) {
series = new XYSeries(graphName);
dataset.addSeries(series);
} else {
series = dataset.getSeries(graphName);
}
series.add(generation, value);

setVisible(true);
}

/**
* Checks if the graph with the given name already exists.
* @param graphName name of the graph
* @return true/false
*/
@SuppressWarnings("unchecked")
private boolean containsGraph(String graphName) {
final List<XYSeries> seriesList = dataset.getSeries();
if (seriesList == null || seriesList.isEmpty()) {
return false;
}
for (XYSeries series : seriesList) {
if (series.getKey().compareTo(graphName) == 0) {
return true;
}
}
return false;
}

/**
* Creates chart panel.
* @param chartTitle chart title
* @param xAxisLabel x axis label
* @param yAxisLabel y axis label
* @return panel
*/
private JPanel createChartPanel(String chartTitle, String xAxisLabel, String yAxisLabel) {

final boolean showLegend = true;
final boolean createURL = false;
final boolean createTooltip = false;

final JFreeChart chart = ChartFactory.createXYLineChart(chartTitle, xAxisLabel, yAxisLabel, dataset,
PlotOrientation.VERTICAL, showLegend, createTooltip, createURL);
final XYPlot plot = chart.getXYPlot();
final XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();

plot.setRenderer(renderer);

return new ChartPanel(chart);

}

/**
* {@inheritDoc}
*/
@Override
public void notify(int generation, Population<Dimension2Coordinate> population) {
PopulationStatisticalSummary<Dimension2Coordinate> populationStatisticalSummary =
new PopulationStatisticalSummaryImpl<>(population);
this.addDataPoint("Average", generation, populationStatisticalSummary.getMeanFitness());
this.addDataPoint("Best", generation, populationStatisticalSummary.getMaxFitness());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math4.examples.ga.mathfunctions.dimension2;

/**
* This class represents the coordinate of the problem domain i.e. the phenotype of chromosome.
*/
public class Dimension2Coordinate {

/** coordinate of first dimension. **/
private final double x;

/** coordinate of second dimension. **/
private final double y;

/**
* constructor.
* @param x coordinate of first dimension
* @param y coordinate of second dimension
*/
public Dimension2Coordinate(double x, double y) {
this.x = x;
this.y = y;
}

/**
* returns the coordinate of first dimension.
* @return coordinate of first dimension
*/
public double getX() {
return x;
}

/**
* returns the coordinate of second dimension.
* @return coordinate of second dimension
*/
public double getY() {
return y;
}

/**
* Returns a string representation of coordinate.
*/
@Override
public String toString() {
return "Coordinate [x=" + x + ", y=" + y + "]";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math4.examples.ga.mathfunctions.dimension2;

import java.util.List;

import org.apache.commons.math4.ga.chromosome.AbstractListChromosome;
import org.apache.commons.math4.ga.chromosome.BinaryChromosome;
import org.apache.commons.math4.ga.decoder.AbstractListChromosomeDecoder;

/**
* Decoder to convert chromosome's binary genotype to phenotype
* {@link Dimension2Coordinate}.
*/
public class Dimension2Decoder extends AbstractListChromosomeDecoder<Integer, Dimension2Coordinate> {

/**
* decode the binary representation of chromosome to
* {@link Dimension2Coordinate}.
* @param chromosome The {@link AbstractListChromosome}
*/
@Override
protected Dimension2Coordinate decode(AbstractListChromosome<Integer, Dimension2Coordinate> chromosome) {
final BinaryChromosome<Dimension2Coordinate> binaryChromosome =
(BinaryChromosome<Dimension2Coordinate>) chromosome;
final List<Integer> alleles = binaryChromosome.getRepresentation();

final StringBuilder allelesStr = new StringBuilder();
for (Integer allele : alleles) {
allelesStr.append(Integer.toBinaryString(allele));
}

final double x = Integer.parseInt(allelesStr.substring(0, 12), 2) / 100.0;
final double y = Integer.parseInt(allelesStr.substring(12, 24), 2) / 100.0;

return new Dimension2Coordinate(x, y);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.commons.math4.examples.ga.mathfunctions.dimension2;

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

/**
* This class represents the mathematical fitness function for optimizing a 2
* dimension mathematical function.
*/
public class Dimension2FitnessFunction implements FitnessFunction<Dimension2Coordinate> {

/**
* Computes the fitness value based on the decoded chromosome.
* @param coordinate The {@link Dimension2Coordinate}
* @return the fitness value
*/
@Override
public double compute(Dimension2Coordinate coordinate) {
return -Math.pow(Math.pow(coordinate.getX(), 2) + Math.pow(coordinate.getY(), 2), .25) *
(Math.pow(Math.sin(50 * Math.pow(Math.pow(coordinate.getX(), 2) + Math.pow(coordinate.getY(), 2), .1)),
2) + 1);
}

}

0 comments on commit 93fe6a2

Please sign in to comment.