Skip to content

Feature/math 1563 #197

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 59 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
ce94df0
Created the genetics module
avbasak1 Aug 8, 2021
5aed328
Code changes for Math-1563/MATH-1618
avbasak1 Aug 15, 2021
c80e03a
changed the since
avbasak1 Aug 15, 2021
60cc7aa
Changes for introduction of population statistics for
avijitbasak Aug 21, 2021
490de10
MATH-1563/MATH-1618 -- Introduction of ConvergenceListener
avijitbasak Aug 21, 2021
53a7545
MATH-1563/MATH-1618 -- Introduced AbstractListMutation and optimized the
avbasak1 Aug 26, 2021
6f84391
Restructured few classes. Minimized use of PopulationStatisticalSummary
avbasak1 Aug 27, 2021
80cb75f
Removed abstract keyword from the classes
avbasak1 Aug 29, 2021
1b361eb
Introduced static method of chromosome instantiation with random
avbasak1 Aug 29, 2021
4876c32
Restructuring of Elitism
avbasak1 Aug 31, 2021
de6fa0a
Added Licenses
avbasak1 Sep 6, 2021
7675414
modified implementation
avbasak1 Sep 6, 2021
bea5011
Committing the example
avbasak1 Sep 6, 2021
a2c98c1
updated examples packages
avbasak1 Sep 6, 2021
56d86c4
Added Licenses
avbasak1 Sep 6, 2021
99d017f
Created a hierarchy of Crossover operations
avbasak1 Sep 7, 2021
a132dd4
Made isSame method public
avbasak1 Sep 7, 2021
be50d1c
removed duplicate code
avbasak1 Sep 7, 2021
93a04da
Introduced a hierarchy of crossover operators
avbasak1 Sep 7, 2021
e77ac07
Added static method level Generic Type
avbasak1 Sep 7, 2021
ec5d82a
Updated examples
avbasak1 Sep 7, 2021
eb0deb6
updated examples
avbasak1 Sep 7, 2021
712923d
Fixed bug
avbasak1 Sep 7, 2021
f5ea657
updated fitnessCalculator as fitnessFunction
avbasak1 Sep 7, 2021
c384a6f
Added test cases
avbasak1 Sep 7, 2021
5fb37e8
Changes for checkstyle
avbasak1 Sep 9, 2021
9ce171c
Renamed method newFixedLengthChromosome() to newChromosome()
avbasak1 Sep 9, 2021
bba1518
changed javadoc
avbasak1 Sep 9, 2021
d0b061b
changed javadoc
avbasak1 Sep 9, 2021
d4c7836
Fixed pmd comments
avbasak1 Sep 10, 2021
3cd6fcc
exception and logger modified
avbasak1 Sep 12, 2021
ca9a8bb
Re-designed for Decoder.
avbasak1 Sep 20, 2021
13951fc
changes for checkstyle
avbasak1 Sep 20, 2021
b4a3e25
Fixed pmd errors
avbasak1 Sep 20, 2021
db0fa1b
Merge pull request #1 from avijitbasak/decoder
avijitbasak Sep 20, 2021
4e0bd47
changes
avbasak1 Sep 20, 2021
4917d31
Fixed error
avbasak1 Sep 20, 2021
3a19909
Added
avbasak1 Sep 20, 2021
3315939
Merge pull request #2 from avijitbasak/decoder
avijitbasak Sep 20, 2021
732b0d6
changed formatting
avbasak1 Sep 20, 2021
31f1f24
Merge pull request #3 from avijitbasak/decoder
avijitbasak Sep 20, 2021
7d1ad7e
fixed build errors
avbasak1 Sep 21, 2021
46a32c9
Merge pull request #4 from avijitbasak/decoder
avijitbasak Sep 21, 2021
3b742f9
Fixed build errors
avbasak1 Sep 23, 2021
48cfca3
fixed build errors
avbasak1 Sep 23, 2021
5c74860
Merge pull request #6 from avijitbasak/decoder
avijitbasak Sep 23, 2021
79fd001
Fixed build error
avbasak1 Sep 24, 2021
5941120
Modified javadoc
avbasak1 Sep 24, 2021
f2c9b21
Merge pull request #7 from avijitbasak/decoder
avijitbasak Sep 24, 2021
e615725
reverted
avbasak1 Sep 24, 2021
d1a78fa
Formatted the lambda expression
avbasak1 Sep 24, 2021
36c79e9
formatted the comments
avbasak1 Sep 24, 2021
48e4cfb
Fixed minor issues
avbasak1 Sep 24, 2021
22b58d5
Fixed minor bugs
avbasak1 Sep 24, 2021
3d91d92
removed hashcode
avbasak1 Sep 25, 2021
5b32d9a
renamed package and module names
avbasak1 Sep 25, 2021
39db7ca
updated artifact ids
avbasak1 Sep 25, 2021
e084eb6
modified the artifactId
avbasak1 Sep 26, 2021
4b094b2
Fixed few bugs and updated JUnit
avbasak1 Sep 27, 2021
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,44 @@
<!--
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,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;

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

/** 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 Coordinate(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,51 @@
/*
* 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;

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 Coordinate}.
*/
public class Dimension2Decoder extends AbstractListChromosomeDecoder<Integer, Coordinate> {

/**
* decode the binary representation of chromosome to {@link Coordinate}.
* @param chromosome The {@link AbstractListChromosome}
*/
@Override
protected Coordinate decode(AbstractListChromosome<Integer, Coordinate> chromosome) {
final BinaryChromosome<Coordinate> binaryChromosome = (BinaryChromosome<Coordinate>) 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 Coordinate(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;

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<Coordinate> {

/**
* Computes the fitness value based on the decoded chromosome.
* @param coordinate The {@link Coordinate}
* @return the fitness value
*/
@Override
public double compute(Coordinate 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);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* 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;

import org.apache.commons.math4.ga.crossover.OnePointCrossover;
import org.apache.commons.math4.ga.convergencecond.UnchangedBestFitness;
import org.apache.commons.math4.examples.ga.mathfunctions.utils.Constants;
import org.apache.commons.math4.examples.ga.mathfunctions.utils.GraphPlotter;
import org.apache.commons.math4.ga.GeneticAlgorithm;
import org.apache.commons.math4.ga.chromosome.BinaryChromosome;
import org.apache.commons.math4.ga.chromosome.Chromosome;
import org.apache.commons.math4.ga.convergencecond.StoppingCondition;
import org.apache.commons.math4.ga.listener.ConvergenceListenerRegistry;
import org.apache.commons.math4.ga.listener.PopulationStatisticsLogger;
import org.apache.commons.math4.ga.mutation.BinaryMutation;
import org.apache.commons.math4.ga.population.ListPopulation;
import org.apache.commons.math4.ga.population.Population;
import org.apache.commons.math4.ga.selection.TournamentSelection;
import org.apache.commons.math4.ga.utils.ConsoleLogger;

/**
* This class represents an optimizer for a 2-dimensional math function using
* genetic algorithm.
*/
public class Dimension2FunctionOptimizer {

/**
* Optimizes the 2-dimension fitness function.
* @param args arguments
*/
public static void main(String[] args) {
final Population<Coordinate> initPopulation = getInitialPopulation();

final Dimension2FunctionOptimizer optimizer = new Dimension2FunctionOptimizer();

final ConvergenceListenerRegistry<Coordinate> convergenceListenerRegistry = ConvergenceListenerRegistry
.getInstance();
convergenceListenerRegistry
.addConvergenceListener(new PopulationStatisticsLogger<Coordinate>(Constants.ENCODING));
convergenceListenerRegistry
.addConvergenceListener(new GraphPlotter("Convergence Stats", "generation", "fitness"));

optimizer.optimize(initPopulation);
}

/**
* Optimizes the population.
* @param initial The {@link Population}
*/
public void optimize(Population<Coordinate> initial) {

// initialize a new genetic algorithm
final GeneticAlgorithm<Coordinate> ga = new GeneticAlgorithm<>(new OnePointCrossover<Integer, Coordinate>(),
Constants.CROSSOVER_RATE, new BinaryMutation<Coordinate>(), Constants.AVERAGE_MUTATION_RATE,
new TournamentSelection<Coordinate>(Constants.TOURNAMENT_SIZE), Constants.ELITISM_RATE);

// stopping condition
final StoppingCondition<Coordinate> stopCond = new UnchangedBestFitness<>(
Constants.GENERATION_COUNT_WITH_UNCHANGED_BEST_FUTNESS);

// run the algorithm
final Population<Coordinate> finalPopulation = ga.evolve(initial, stopCond);

// best chromosome from the final population
final Chromosome<Coordinate> bestFinal = finalPopulation.getFittestChromosome();
final ConsoleLogger consoleLogger = ConsoleLogger.getInstance(Constants.ENCODING);
consoleLogger.log("*********************************************");
consoleLogger.log("***********Optimization Result***************");

consoleLogger.log(bestFinal.toString());

}

/**
* Generates an initial population.
* @return initial population
*/
private static Population<Coordinate> getInitialPopulation() {
final Population<Coordinate> population = new ListPopulation<>(Constants.POPULATION_SIZE);
final Dimension2FitnessFunction fitnessFunction = new Dimension2FitnessFunction();
final Dimension2Decoder decoder = new Dimension2Decoder();
for (int i = 0; i < Constants.POPULATION_SIZE; i++) {
population.addChromosome(BinaryChromosome.<Coordinate>randomChromosome(Constants.CHROMOSOME_LENGTH,
fitnessFunction, decoder));
}
return population;
}

}
Loading