Skip to content

Commit 4b094b2

Browse files
committed
Fixed few bugs and updated JUnit
1 parent e084eb6 commit 4b094b2

33 files changed

Lines changed: 683 additions & 97 deletions

commons-math-ga/src/main/java/org/apache/commons/math4/ga/AbstractGeneticAlgorithm.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public abstract class AbstractGeneticAlgorithm<P> {
4747
*/
4848
private int generationsEvolved;
4949

50-
/** The elitism rate haveing default value of .25. */
50+
/** The elitism rate having default value of .25. */
5151
private double elitismRate = .25;
5252

5353
/**

commons-math-ga/src/main/java/org/apache/commons/math4/ga/GeneticAlgorithm.java

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,7 @@ public GeneticAlgorithm(final CrossoverPolicy<P> crossoverPolicy, final double c
5252
final SelectionPolicy<P> selectionPolicy) {
5353
super(crossoverPolicy, mutationPolicy, selectionPolicy);
5454

55-
if (crossoverRate < 0 || crossoverRate > 1) {
56-
throw new GeneticException(GeneticException.OUT_OF_RANGE, crossoverRate, Constants.CROSSOVER_RATE, 0, 1);
57-
}
58-
if (mutationRate < 0 || mutationRate > 1) {
59-
throw new GeneticException(GeneticException.OUT_OF_RANGE, mutationRate, Constants.MUTATION_RATE, 0, 1);
60-
}
55+
checkValidity(crossoverRate, mutationRate);
6156
this.crossoverRate = crossoverRate;
6257
this.mutationRate = mutationRate;
6358
}
@@ -76,32 +71,38 @@ public GeneticAlgorithm(final CrossoverPolicy<P> crossoverPolicy, final double c
7671
final double elitismRate) {
7772
super(crossoverPolicy, mutationPolicy, selectionPolicy, elitismRate);
7873

79-
if (crossoverRate < 0 || crossoverRate > 1) {
80-
throw new GeneticException(GeneticException.OUT_OF_RANGE, crossoverRate, Constants.CROSSOVER_RATE, 0, 1);
81-
}
82-
if (mutationRate < 0 || mutationRate > 1) {
83-
throw new GeneticException(GeneticException.OUT_OF_RANGE, mutationRate, Constants.MUTATION_RATE, 0, 1);
84-
}
74+
checkValidity(crossoverRate, mutationRate);
8575
this.crossoverRate = crossoverRate;
8676
this.mutationRate = mutationRate;
8777
}
8878

79+
private void checkValidity(final double crossoverRateInput, final double inputMutationRate) {
80+
if (crossoverRateInput < 0 || crossoverRateInput > 1) {
81+
throw new GeneticException(GeneticException.OUT_OF_RANGE, crossoverRateInput, Constants.CROSSOVER_RATE, 0,
82+
1);
83+
}
84+
if (inputMutationRate < 0 || inputMutationRate > 1) {
85+
throw new GeneticException(GeneticException.OUT_OF_RANGE, inputMutationRate, Constants.MUTATION_RATE, 0, 1);
86+
}
87+
}
88+
8989
/**
9090
* Evolve the given population into the next generation.
9191
* <ol>
92-
* <li>Get nextGeneration population to fill from <code>current</code>
93-
* generation, using its nextGeneration method</li>
94-
* <li>Loop until new generation is filled:
95-
* <ul><li>Apply configured SelectionPolicy to select a pair of parents
96-
* from <code>current</code></li>
97-
* <li>With probability = {@link #getCrossoverRate()}, apply
98-
* configured {@link CrossoverPolicy} to parents</li>
99-
* <li>With probability = {@link #getMutationRate()}, apply
100-
* configured {@link MutationPolicy} to each of the offspring</li>
101-
* <li>Add offspring individually to nextGeneration,
102-
* space permitting</li>
103-
* </ul></li>
104-
* <li>Return nextGeneration</li>
92+
* <li>Get nextGeneration population to fill from <code>current</code>
93+
* generation, using its nextGeneration method</li>
94+
* <li>Loop until new generation is filled:
95+
* <ul>
96+
* <li>Apply configured SelectionPolicy to select a pair of parents from
97+
* <code>current</code></li>
98+
* <li>With probability = {@link #getCrossoverRate()}, apply configured
99+
* {@link CrossoverPolicy} to parents</li>
100+
* <li>With probability = {@link #getMutationRate()}, apply configured
101+
* {@link MutationPolicy} to each of the offspring</li>
102+
* <li>Add offspring individually to nextGeneration, space permitting</li>
103+
* </ul>
104+
* </li>
105+
* <li>Return nextGeneration</li>
105106
* </ol>
106107
*
107108
* @param current the current population.

commons-math-ga/src/main/java/org/apache/commons/math4/ga/chromosome/IntegralValuedChromosome.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.apache.commons.math4.ga.exception.GeneticException;
2323
import org.apache.commons.math4.ga.fitness.FitnessFunction;
2424
import org.apache.commons.math4.ga.utils.ChromosomeRepresentationUtils;
25+
import org.apache.commons.math4.ga.utils.ValidationUtils;
2526

2627
/**
2728
* Chromosome represented by a list of integral values. The acceptable integral
@@ -89,9 +90,7 @@ public int getMax() {
8990
* Asserts that <code>representation</code> can represent a valid chromosome.
9091
*/
9192
private void checkValidity() {
92-
if (min > max) {
93-
throw new GeneticException(GeneticException.TOO_LARGE, min, max);
94-
}
93+
ValidationUtils.checkForMinMax(min, max);
9594
for (int i : getRepresentation()) {
9695
if (i < min || i >= max) {
9796
throw new GeneticException(GeneticException.ILLEGAL_ARGUMENT, i);

commons-math-ga/src/main/java/org/apache/commons/math4/ga/chromosome/RealValuedChromosome.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.apache.commons.math4.ga.exception.GeneticException;
2424
import org.apache.commons.math4.ga.fitness.FitnessFunction;
2525
import org.apache.commons.math4.ga.utils.ChromosomeRepresentationUtils;
26+
import org.apache.commons.math4.ga.utils.ValidationUtils;
2627

2728
/**
2829
* DoubleEncodedChromosome is used for representing chromosome encoded as
@@ -114,9 +115,7 @@ public double getMax() {
114115
* Asserts that <code>representation</code> can represent a valid chromosome.
115116
*/
116117
private void checkValidity() {
117-
if (min > max) {
118-
throw new GeneticException(GeneticException.TOO_LARGE, min, max);
119-
}
118+
ValidationUtils.checkForMinMax(min, max);
120119
for (double i : getRepresentation()) {
121120
if (i < min || i >= max) {
122121
throw new GeneticException(GeneticException.ILLEGAL_ARGUMENT, i);

commons-math-ga/src/main/java/org/apache/commons/math4/ga/decoder/RandomKeyDecoder.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,19 @@ protected List<U> decode(AbstractListChromosome<Double, List<U>> chromosome) {
5353
final List<Double> sortedRepresentation = new ArrayList<>(representation);
5454
Collections.sort(sortedRepresentation);
5555

56-
final int l = baseSequence.size();
56+
final int sequenceLength = baseSequence.size();
5757

5858
// the size of the three lists must be equal
59-
if (representation.size() != l) {
60-
throw new GeneticException(GeneticException.SIZE_MISMATCH, representation.size(), l);
59+
if (representation.size() != sequenceLength) {
60+
throw new GeneticException(GeneticException.SIZE_MISMATCH, representation.size(), sequenceLength);
6161
}
6262

6363
// do not modify the original representation
6464
final List<Double> representationCopy = new ArrayList<>(representation);
6565

6666
// now find the indices in the original repr and use them for permuting
67-
final List<U> res = new ArrayList<>(l);
68-
for (int i = 0; i < l; i++) {
67+
final List<U> res = new ArrayList<>(sequenceLength);
68+
for (int i = 0; i < sequenceLength; i++) {
6969
final int index = representationCopy.indexOf(sortedRepresentation.get(i));
7070
res.add(baseSequence.get(index));
7171
representationCopy.set(index, null);

commons-math-ga/src/main/java/org/apache/commons/math4/ga/listener/ConvergenceListenerRegistry.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.List;
2222

2323
import org.apache.commons.math4.ga.population.Population;
24+
import org.apache.commons.math4.ga.utils.ValidationUtils;
2425

2526
/**
2627
* This class is the default implementation of ConvergenceListenerRegistry. It
@@ -72,10 +73,9 @@ public void notifyAll(int generation, Population<P> population) {
7273
* @param convergenceListeners list of {@link ConvergenceListener}
7374
*/
7475
public void addConvergenceListeners(List<ConvergenceListener<P>> convergenceListeners) {
75-
if (convergenceListeners != null) {
76-
for (ConvergenceListener<P> convergenceListener : convergenceListeners) {
77-
this.listeners.add(convergenceListener);
78-
}
76+
ValidationUtils.checkForNull("Null convergenceListeners", convergenceListeners);
77+
for (ConvergenceListener<P> convergenceListener : convergenceListeners) {
78+
addConvergenceListener(convergenceListener);
7979
}
8080
}
8181

commons-math-ga/src/main/java/org/apache/commons/math4/ga/mutation/BinaryMutation.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,18 @@
2525
* @param <P> phenotype of chromosome
2626
* @since 4.0
2727
*/
28-
public class BinaryMutation<P> extends AbstractListChromosomeMutationPolicy<Integer, P> {
28+
public class BinaryMutation<P> extends IntegralValuedMutation<P> {
29+
30+
public BinaryMutation() {
31+
super(0, 2);
32+
}
2933

3034
/**
3135
* {@inheritDoc}
3236
*/
3337
@Override
3438
protected void checkValidity(Chromosome<P> original) {
39+
super.checkValidity(original);
3540
if (!BinaryChromosome.class.isAssignableFrom(original.getClass())) {
3641
throw new GeneticException(GeneticException.ILLEGAL_ARGUMENT, original.getClass().getSimpleName());
3742
}

commons-math-ga/src/main/java/org/apache/commons/math4/ga/mutation/IntegralValuedMutation.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.apache.commons.math4.ga.chromosome.IntegralValuedChromosome;
2121
import org.apache.commons.math4.ga.exception.GeneticException;
2222
import org.apache.commons.math4.ga.utils.RandomGenerator;
23+
import org.apache.commons.math4.ga.utils.ValidationUtils;
2324

2425
/**
2526
* Mutation for {@link IntegralValuedChromosome}. Randomly changes few genes.
@@ -42,9 +43,7 @@ public class IntegralValuedMutation<P> extends AbstractListChromosomeMutationPol
4243
public IntegralValuedMutation(final int min, final int max) {
4344
this.min = min;
4445
this.max = max;
45-
if (min > max) {
46-
throw new GeneticException(GeneticException.TOO_LARGE, min, max);
47-
}
46+
ValidationUtils.checkForMinMax(min, max);
4847
}
4948

5049
/**
@@ -83,7 +82,7 @@ protected void checkValidity(Chromosome<P> original) {
8382
*/
8483
@Override
8584
protected Integer mutateGene(Integer originalValue) {
86-
return RandomGenerator.getRandomGenerator().nextInt();
85+
return min + RandomGenerator.getRandomGenerator().nextInt(max - min);
8786
}
8887

8988
}

commons-math-ga/src/main/java/org/apache/commons/math4/ga/mutation/RealValuedMutation.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.apache.commons.math4.ga.chromosome.RealValuedChromosome;
2121
import org.apache.commons.math4.ga.exception.GeneticException;
2222
import org.apache.commons.math4.ga.utils.RandomGenerator;
23+
import org.apache.commons.math4.ga.utils.ValidationUtils;
2324

2425
/**
2526
* This class mutates real-valued chromosome.
@@ -50,10 +51,7 @@ public RealValuedMutation() {
5051
public RealValuedMutation(double min, double max) {
5152
this.min = min;
5253
this.max = max;
53-
54-
if (min > max) {
55-
throw new GeneticException(GeneticException.TOO_LARGE, min, max);
56-
}
54+
ValidationUtils.checkForMinMax(min, max);
5755
}
5856

5957
/**

commons-math-ga/src/main/java/org/apache/commons/math4/ga/population/ListPopulation.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.apache.commons.math4.ga.chromosome.Chromosome;
2626
import org.apache.commons.math4.ga.exception.GeneticException;
2727
import org.apache.commons.math4.ga.utils.Constants;
28+
import org.apache.commons.math4.ga.utils.ValidationUtils;
2829

2930
/**
3031
* Population of chromosomes represented by a {@link List}.
@@ -60,9 +61,8 @@ public ListPopulation(final int populationLimit) {
6061
*/
6162
public ListPopulation(final List<Chromosome<P>> chromosomes, final int populationLimit) {
6263

63-
if (chromosomes == null) {
64-
throw new GeneticException(GeneticException.NULL_ARGUMENT, "chromosomes");
65-
}
64+
ValidationUtils.checkForNull("chromosomes", chromosomes);
65+
6666
if (populationLimit <= 0) {
6767
throw new GeneticException(GeneticException.NOT_STRICTLY_POSITIVE, populationLimit);
6868
}
@@ -195,7 +195,7 @@ public Iterator<Chromosome<P>> iterator() {
195195
public Population<P> nextGeneration(final double elitismRate) {
196196
final List<Chromosome<P>> oldChromosomes = getChromosomeList();
197197

198-
if (oldChromosomes.size() * elitismRate == 0) {
198+
if ((int) (oldChromosomes.size() * elitismRate) == 0) {
199199
// if no of elite chromosome is 0 crete and return an empty population instance.
200200
return new ListPopulation<>(getPopulationLimit());
201201
} else {

0 commit comments

Comments
 (0)