Skip to content

Feature/math 1563 adaptive #199

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?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>
<slf4jVersion>1.7.32</slf4jVersion>
</properties>

<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>${slf4jVersion}</version>
</dependency>
</dependencies>
</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,49 @@
/*
* 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.chromosome.BinaryChromosome;
import org.apache.commons.math4.ga.chromosome.Chromosome;
import org.apache.commons.math4.ga.decoder.Decoder;

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

/**
* Decode the binary representation of chromosome to
* {@link Dimension2Coordinate}.
* @param chromosome The {@link Chromosome}
*/
@Override
public Dimension2Coordinate decode(Chromosome<Dimension2Coordinate> chromosome) {
final BinaryChromosome<Dimension2Coordinate> binaryChromosome =
(BinaryChromosome<Dimension2Coordinate>) chromosome;
final long alleles = binaryChromosome.getRepresentation()[0];

long mask1 = ~(Long.MAX_VALUE << 12);
long mask2 = ~(Long.MAX_VALUE << 24) ^ mask1;

final double x = (alleles & mask1) / 100d;
final double y = ((alleles & mask2) >> 12) / 100d;

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);
}

}
Loading