This is an automated email from the ASF dual-hosted git repository. erans pushed a commit to branch feature__MATH-1563__genetic_algorithm in repository https://gitbox.apache.org/repos/asf/commons-math.git
commit 27858914eecb82e6669e06daf906cbe9cdd06bec Author: avbasak1 <avbas...@in.ibm.com> AuthorDate: Mon Jan 24 17:07:29 2022 +0530 Converted protected method to private and fixed minor bugs. --- .../commons/math4/ga/AdaptiveGeneticAlgorithm.java | 2 +- .../apache/commons/math4/ga/GeneticAlgorithm.java | 2 +- .../math4/ga/chromosome/BinaryChromosome.java | 10 ++--- .../AbstractListChromosomeCrossoverPolicy.java | 2 +- .../commons/math4/ga/crossover/CycleCrossover.java | 2 +- .../math4/ga/crossover/OrderedCrossover.java | 1 - .../math4/ga/crossover/UniformCrossover.java | 1 - .../ga/decoder/AbstractListChromosomeDecoder.java | 3 +- .../AbstractListChromosomeMutationPolicy.java | 18 ++++++--- .../commons/math4/ga/mutation/BinaryMutation.java | 14 ++----- .../math4/ga/mutation/IntegralValuedMutation.java | 29 ++++++++++++-- .../math4/ga/mutation/RealValuedMutation.java | 19 +++++++++- .../math4/ga/population/ListPopulation.java | 11 +----- .../math4/ga/chromosome/BinaryChromosomeTest.java | 9 +++++ .../math4/ga/dummy/DummyListChromosomeDecoder.java | 6 --- .../AbstractListChromosomeMutationPolicyTest.java | 44 ---------------------- .../math4/ga/mutation/BinaryMutationTest.java | 4 +- .../ga/mutation/IntegralValuedMutationTest.java | 5 ++- .../math4/ga/mutation/RealValuedMutationTest.java | 31 +++++++++++++-- 19 files changed, 111 insertions(+), 102 deletions(-) diff --git a/commons-math-ga/src/main/java/org/apache/commons/math4/ga/AdaptiveGeneticAlgorithm.java b/commons-math-ga/src/main/java/org/apache/commons/math4/ga/AdaptiveGeneticAlgorithm.java index 07e8913..bf1c800 100644 --- a/commons-math-ga/src/main/java/org/apache/commons/math4/ga/AdaptiveGeneticAlgorithm.java +++ b/commons-math-ga/src/main/java/org/apache/commons/math4/ga/AdaptiveGeneticAlgorithm.java @@ -41,7 +41,7 @@ import org.slf4j.LoggerFactory; public class AdaptiveGeneticAlgorithm<P> extends AbstractGeneticAlgorithm<P> { /** instance of logger. **/ - private static final Logger LOGGER = LoggerFactory.getLogger(AbstractGeneticAlgorithm.class); + private static final Logger LOGGER = LoggerFactory.getLogger(AdaptiveGeneticAlgorithm.class); /** The crossover rate generator. **/ private final CrossoverRateGenerator<P> crossoverRateGenerator; diff --git a/commons-math-ga/src/main/java/org/apache/commons/math4/ga/GeneticAlgorithm.java b/commons-math-ga/src/main/java/org/apache/commons/math4/ga/GeneticAlgorithm.java index 94e320d..0a8cc77 100644 --- a/commons-math-ga/src/main/java/org/apache/commons/math4/ga/GeneticAlgorithm.java +++ b/commons-math-ga/src/main/java/org/apache/commons/math4/ga/GeneticAlgorithm.java @@ -35,7 +35,7 @@ import org.slf4j.LoggerFactory; public class GeneticAlgorithm<P> extends AbstractGeneticAlgorithm<P> { /** instance of logger. **/ - private static final Logger LOGGER = LoggerFactory.getLogger(AbstractGeneticAlgorithm.class); + private static final Logger LOGGER = LoggerFactory.getLogger(GeneticAlgorithm.class); /** crossover rate string. **/ private static final String CROSSOVER_RATE = "CROSSOVER_RATE"; diff --git a/commons-math-ga/src/main/java/org/apache/commons/math4/ga/chromosome/BinaryChromosome.java b/commons-math-ga/src/main/java/org/apache/commons/math4/ga/chromosome/BinaryChromosome.java index a9ef839..13e0bf1 100644 --- a/commons-math-ga/src/main/java/org/apache/commons/math4/ga/chromosome/BinaryChromosome.java +++ b/commons-math-ga/src/main/java/org/apache/commons/math4/ga/chromosome/BinaryChromosome.java @@ -17,7 +17,6 @@ package org.apache.commons.math4.ga.chromosome; import java.util.List; - import java.util.Objects; import org.apache.commons.math4.ga.decoder.Decoder; @@ -125,7 +124,7 @@ public class BinaryChromosome<P> extends AbstractChromosome<P> { * Checks the input chromosome length against predefined maximum length. * @param chromosomeLength input chromsome length */ - protected void checkMaximumLength(long chromosomeLength) { + private void checkMaximumLength(long chromosomeLength) { if (chromosomeLength > MAX_LENGTH) { throw new GeneticException(GeneticException.ILLEGAL_ARGUMENT, "length exceeded the max length " + MAX_LENGTH); @@ -206,8 +205,9 @@ public class BinaryChromosome<P> extends AbstractChromosome<P> { throw new GeneticException(GeneticException.ILLEGAL_ARGUMENT, "start " + start + " is greater than end " + end); } - if (end - start > Integer.MAX_VALUE) { - throw new GeneticException(GeneticException.LENGTH_TOO_LARGE, end - start); + if (end > length) { + throw new GeneticException(GeneticException.ILLEGAL_ARGUMENT, + "end " + end + " is greater than length " + length); } final int offset = (int) (length % Long.SIZE == 0 ? 0 : Long.SIZE - length % Long.SIZE); final long offsettedStart = offset + start; @@ -250,7 +250,7 @@ public class BinaryChromosome<P> extends AbstractChromosome<P> { */ private String getAlleleBlockString(final int alleleBlockIndex) { return prepareZeroPrefix(representation[alleleBlockIndex] == 0 ? Long.SIZE - 1 : - Long.numberOfLeadingZeros(representation[alleleBlockIndex])) + + Long.numberOfLeadingZeros(representation[alleleBlockIndex])) + Long.toUnsignedString(representation[alleleBlockIndex], 2); } diff --git a/commons-math-ga/src/main/java/org/apache/commons/math4/ga/crossover/AbstractListChromosomeCrossoverPolicy.java b/commons-math-ga/src/main/java/org/apache/commons/math4/ga/crossover/AbstractListChromosomeCrossoverPolicy.java index 2184310..8b773a5 100644 --- a/commons-math-ga/src/main/java/org/apache/commons/math4/ga/crossover/AbstractListChromosomeCrossoverPolicy.java +++ b/commons-math-ga/src/main/java/org/apache/commons/math4/ga/crossover/AbstractListChromosomeCrossoverPolicy.java @@ -51,7 +51,7 @@ public abstract class AbstractListChromosomeCrossoverPolicy<T, P> extends Abstra * @param second second chromosome */ @SuppressWarnings("unchecked") - protected void checkValidity(final Chromosome<P> first, final Chromosome<P> second) { + private void checkValidity(final Chromosome<P> first, final Chromosome<P> second) { if (!(first instanceof AbstractListChromosome<?, ?> && second instanceof AbstractListChromosome<?, ?>)) { throw new GeneticException(GeneticException.INVALID_FIXED_LENGTH_CHROMOSOME); } diff --git a/commons-math-ga/src/main/java/org/apache/commons/math4/ga/crossover/CycleCrossover.java b/commons-math-ga/src/main/java/org/apache/commons/math4/ga/crossover/CycleCrossover.java index 1d0da03..ff4bf34 100644 --- a/commons-math-ga/src/main/java/org/apache/commons/math4/ga/crossover/CycleCrossover.java +++ b/commons-math-ga/src/main/java/org/apache/commons/math4/ga/crossover/CycleCrossover.java @@ -17,7 +17,6 @@ package org.apache.commons.math4.ga.crossover; import java.util.ArrayList; - import java.util.HashSet; import java.util.List; import java.util.Set; @@ -46,6 +45,7 @@ import org.apache.commons.math4.ga.utils.RandomProviderManager; * ... * * Example (zero-start cycle): + * * <pre> * p1 = (8 4 7 3 6 2 5 1 9 0) X c1 = (8 1 2 3 4 5 6 7 9 0) * p2 = (0 1 2 3 4 5 6 7 8 9) X c2 = (0 4 7 3 6 2 5 1 8 9) diff --git a/commons-math-ga/src/main/java/org/apache/commons/math4/ga/crossover/OrderedCrossover.java b/commons-math-ga/src/main/java/org/apache/commons/math4/ga/crossover/OrderedCrossover.java index 24608f9..c53180f 100644 --- a/commons-math-ga/src/main/java/org/apache/commons/math4/ga/crossover/OrderedCrossover.java +++ b/commons-math-ga/src/main/java/org/apache/commons/math4/ga/crossover/OrderedCrossover.java @@ -17,7 +17,6 @@ package org.apache.commons.math4.ga.crossover; import java.util.ArrayList; - import java.util.Collections; import java.util.HashSet; import java.util.List; diff --git a/commons-math-ga/src/main/java/org/apache/commons/math4/ga/crossover/UniformCrossover.java b/commons-math-ga/src/main/java/org/apache/commons/math4/ga/crossover/UniformCrossover.java index 23292f7..df5321a 100644 --- a/commons-math-ga/src/main/java/org/apache/commons/math4/ga/crossover/UniformCrossover.java +++ b/commons-math-ga/src/main/java/org/apache/commons/math4/ga/crossover/UniformCrossover.java @@ -17,7 +17,6 @@ package org.apache.commons.math4.ga.crossover; import java.util.ArrayList; - import java.util.List; import org.apache.commons.math4.ga.chromosome.AbstractListChromosome; diff --git a/commons-math-ga/src/main/java/org/apache/commons/math4/ga/decoder/AbstractListChromosomeDecoder.java b/commons-math-ga/src/main/java/org/apache/commons/math4/ga/decoder/AbstractListChromosomeDecoder.java index 96a1920..fccc851 100644 --- a/commons-math-ga/src/main/java/org/apache/commons/math4/ga/decoder/AbstractListChromosomeDecoder.java +++ b/commons-math-ga/src/main/java/org/apache/commons/math4/ga/decoder/AbstractListChromosomeDecoder.java @@ -34,6 +34,7 @@ public abstract class AbstractListChromosomeDecoder<T, P> implements Decoder<P> @SuppressWarnings("unchecked") @Override public P decode(Chromosome<P> chromosome) { + //check for validity checkValidity(chromosome); return decode((AbstractListChromosome<T, P>) chromosome); @@ -43,7 +44,7 @@ public abstract class AbstractListChromosomeDecoder<T, P> implements Decoder<P> * Checks validity of {@link Chromosome}. * @param chromosome the {@link Chromosome} */ - protected void checkValidity(Chromosome<P> chromosome) { + private void checkValidity(Chromosome<P> chromosome) { if (!AbstractListChromosome.class.isAssignableFrom(chromosome.getClass())) { throw new GeneticException(GeneticException.ILLEGAL_ARGUMENT, chromosome.getClass().getSimpleName()); } diff --git a/commons-math-ga/src/main/java/org/apache/commons/math4/ga/mutation/AbstractListChromosomeMutationPolicy.java b/commons-math-ga/src/main/java/org/apache/commons/math4/ga/mutation/AbstractListChromosomeMutationPolicy.java index 3e41fc2..f2ace6a 100644 --- a/commons-math-ga/src/main/java/org/apache/commons/math4/ga/mutation/AbstractListChromosomeMutationPolicy.java +++ b/commons-math-ga/src/main/java/org/apache/commons/math4/ga/mutation/AbstractListChromosomeMutationPolicy.java @@ -24,6 +24,7 @@ import java.util.Set; import org.apache.commons.math4.ga.chromosome.AbstractListChromosome; import org.apache.commons.math4.ga.chromosome.Chromosome; +import org.apache.commons.math4.ga.internal.exception.GeneticException; import org.apache.commons.math4.ga.utils.RandomProviderManager; import org.apache.commons.rng.UniformRandomProvider; @@ -43,8 +44,9 @@ public abstract class AbstractListChromosomeMutationPolicy<T, P> implements Muta * @return the mutated chromosome. */ @Override - public Chromosome<P> mutate(Chromosome<P> original, double mutationRate) { - // check for validity. + public AbstractListChromosome<T, P> mutate(Chromosome<P> original, double mutationRate) { + + // check for validity checkValidity(original); @SuppressWarnings("unchecked") @@ -60,10 +62,14 @@ public abstract class AbstractListChromosomeMutationPolicy<T, P> implements Muta } /** - * Checks input chromosome validity. - * @param original chromosome to be mutated + * This method validates input chromosome. + * @param original chromosome */ - protected abstract void checkValidity(Chromosome<P> original); + private void checkValidity(Chromosome<P> original) { + if (!AbstractListChromosome.class.isAssignableFrom(original.getClass())) { + throw new GeneticException(GeneticException.ILLEGAL_ARGUMENT, original.getClass().getSimpleName()); + } + } /** * Selects and returns mutable gene indexes based on mutation rate. @@ -71,7 +77,7 @@ public abstract class AbstractListChromosomeMutationPolicy<T, P> implements Muta * @param mutationRate mutation rate of the allele/gene * @return mutable gene indexes */ - protected Set<Integer> getMutableGeneIndexes(int length, double mutationRate) { + private Set<Integer> getMutableGeneIndexes(int length, double mutationRate) { // calculate the total mutation rate of all the alleles i.e. chromosome. final double chromosomeMutationRate = mutationRate * length; diff --git a/commons-math-ga/src/main/java/org/apache/commons/math4/ga/mutation/BinaryMutation.java b/commons-math-ga/src/main/java/org/apache/commons/math4/ga/mutation/BinaryMutation.java index 2470f38..d6c3e0e 100644 --- a/commons-math-ga/src/main/java/org/apache/commons/math4/ga/mutation/BinaryMutation.java +++ b/commons-math-ga/src/main/java/org/apache/commons/math4/ga/mutation/BinaryMutation.java @@ -38,7 +38,7 @@ public class BinaryMutation<P> implements MutationPolicy<P> { * {@inheritDoc} */ @Override - public Chromosome<P> mutate(Chromosome<P> original, double mutationRate) { + public BinaryChromosome<P> mutate(Chromosome<P> original, double mutationRate) { // check for validity. checkValidity(original); final BinaryChromosome<P> chromosome = (BinaryChromosome<P>) original; @@ -57,14 +57,6 @@ public class BinaryMutation<P> implements MutationPolicy<P> { } newRep[alleleBlockIndex] = newRep[alleleBlockIndex] ^ mask; } -// for (int alleleBlockIndex : mutableGeneIndexMap.keySet()) { -// long mask = 0; -// final Set<Integer> alleleElementIndexes = mutableGeneIndexMap.get(alleleBlockIndex); -// for (int index : alleleElementIndexes) { -// mask += index == 0 ? Long.MIN_VALUE : Math.pow(2, Long.SIZE - 1 - index); -// } -// newRep[alleleBlockIndex] = newRep[alleleBlockIndex] ^ mask; -// } return chromosome.newChromosome(newRep, chromosome.getLength()); } @@ -73,7 +65,7 @@ public class BinaryMutation<P> implements MutationPolicy<P> { * Checks input chromosome validity. * @param original chromosome to be mutated */ - protected void checkValidity(Chromosome<P> original) { + private void checkValidity(Chromosome<P> original) { if (!BinaryChromosome.class.isAssignableFrom(original.getClass())) { throw new GeneticException(GeneticException.ILLEGAL_ARGUMENT, original.getClass().getSimpleName()); } @@ -85,7 +77,7 @@ public class BinaryMutation<P> implements MutationPolicy<P> { * @param mutationRate mutation rate of the allele/gene * @return mutable gene indexes */ - protected Map<Integer, Set<Integer>> getMutableGeneIndexes(long length, double mutationRate) { + private Map<Integer, Set<Integer>> getMutableGeneIndexes(long length, double mutationRate) { // calculate the total mutation rate of all the alleles i.e. chromosome. final double chromosomeMutationRate = mutationRate * length; diff --git a/commons-math-ga/src/main/java/org/apache/commons/math4/ga/mutation/IntegralValuedMutation.java b/commons-math-ga/src/main/java/org/apache/commons/math4/ga/mutation/IntegralValuedMutation.java index 2ff7f0f..64fe6cf 100644 --- a/commons-math-ga/src/main/java/org/apache/commons/math4/ga/mutation/IntegralValuedMutation.java +++ b/commons-math-ga/src/main/java/org/apache/commons/math4/ga/mutation/IntegralValuedMutation.java @@ -36,14 +36,19 @@ public class IntegralValuedMutation<P> extends AbstractListChromosomeMutationPol /** * @param min minimum value of allele - * @param max maximum value of allele + * @param max maximum(exclusive) value of allele */ public IntegralValuedMutation(final int min, final int max) { this.min = min; this.max = max; - if (min >= max) { + + // To perform mutation for an IntegralValuedChromosome the minimum difference + // between + // max and min should be 2. + if ((max - min) < 2) { throw new GeneticException(GeneticException.TOO_LARGE, min, max); } + } /** @@ -66,7 +71,18 @@ public class IntegralValuedMutation<P> extends AbstractListChromosomeMutationPol * {@inheritDoc} */ @Override - protected void checkValidity(Chromosome<P> original) { + public IntegralValuedChromosome<P> mutate(Chromosome<P> original, double mutationRate) { + // check for validity + checkValidity(original); + + return (IntegralValuedChromosome<P>) super.mutate(original, mutationRate); + } + + /** + * This method validates input chromosome. + * @param original chromosome + */ + private void checkValidity(Chromosome<P> original) { if (!IntegralValuedChromosome.class.isAssignableFrom(original.getClass())) { throw new GeneticException(GeneticException.ILLEGAL_ARGUMENT, original.getClass().getSimpleName()); } @@ -82,7 +98,12 @@ public class IntegralValuedMutation<P> extends AbstractListChromosomeMutationPol */ @Override protected Integer mutateGene(Integer originalValue) { - return min + RandomProviderManager.getRandomProvider().nextInt(max - min); + Integer mutatedValue = 0; + do { + mutatedValue = min + RandomProviderManager.getRandomProvider().nextInt(max - min); + } while (mutatedValue.equals(originalValue)); + + return mutatedValue; } } diff --git a/commons-math-ga/src/main/java/org/apache/commons/math4/ga/mutation/RealValuedMutation.java b/commons-math-ga/src/main/java/org/apache/commons/math4/ga/mutation/RealValuedMutation.java index 3dcbdcf..d5ea01f 100644 --- a/commons-math-ga/src/main/java/org/apache/commons/math4/ga/mutation/RealValuedMutation.java +++ b/commons-math-ga/src/main/java/org/apache/commons/math4/ga/mutation/RealValuedMutation.java @@ -75,7 +75,17 @@ public class RealValuedMutation<P> extends AbstractListChromosomeMutationPolicy< * {@inheritDoc} */ @Override - protected void checkValidity(Chromosome<P> original) { + public RealValuedChromosome<P> mutate(Chromosome<P> original, double mutationRate) { + // check for validity. + checkValidity(original); + return (RealValuedChromosome<P>) super.mutate(original, mutationRate); + } + + /** + * This method validates the input chromosome. + * @param original chromosome + */ + private void checkValidity(Chromosome<P> original) { if (!RealValuedChromosome.class.isAssignableFrom(original.getClass())) { throw new GeneticException(GeneticException.ILLEGAL_ARGUMENT, original.getClass().getSimpleName()); } @@ -91,7 +101,12 @@ public class RealValuedMutation<P> extends AbstractListChromosomeMutationPolicy< */ @Override protected Double mutateGene(Double originalValue) { - return min + RandomProviderManager.getRandomProvider().nextDouble() * (max - min); + Double mutatedValue = 0.0; + do { + mutatedValue = min + RandomProviderManager.getRandomProvider().nextDouble() * (max - min); + } while (mutatedValue.equals(originalValue)); + + return mutatedValue; } } diff --git a/commons-math-ga/src/main/java/org/apache/commons/math4/ga/population/ListPopulation.java b/commons-math-ga/src/main/java/org/apache/commons/math4/ga/population/ListPopulation.java index 1622d9c..405edcc 100644 --- a/commons-math-ga/src/main/java/org/apache/commons/math4/ga/population/ListPopulation.java +++ b/commons-math-ga/src/main/java/org/apache/commons/math4/ga/population/ListPopulation.java @@ -99,15 +99,6 @@ public class ListPopulation<P> implements Population<P> { } /** - * Access the list of chromosomes. - * @return the list of chromosomes - * @since 3.1 - */ - protected List<Chromosome<P>> getChromosomeList() { - return chromosomes; - } - - /** * Add the given chromosome to the population. * @param chromosome the chromosome to add. */ @@ -195,7 +186,7 @@ public class ListPopulation<P> implements Population<P> { */ @Override public Population<P> nextGeneration(final double elitismRate) { - final List<Chromosome<P>> oldChromosomes = getChromosomeList(); + final List<Chromosome<P>> oldChromosomes = this.chromosomes; if ((int) (oldChromosomes.size() * elitismRate) == 0) { // if no of elite chromosome is 0 crete and return an empty population instance. diff --git a/commons-math-ga/src/test/java/org/apache/commons/math4/ga/chromosome/BinaryChromosomeTest.java b/commons-math-ga/src/test/java/org/apache/commons/math4/ga/chromosome/BinaryChromosomeTest.java index ef6669a..a092ae9 100644 --- a/commons-math-ga/src/test/java/org/apache/commons/math4/ga/chromosome/BinaryChromosomeTest.java +++ b/commons-math-ga/src/test/java/org/apache/commons/math4/ga/chromosome/BinaryChromosomeTest.java @@ -19,6 +19,7 @@ package org.apache.commons.math4.ga.chromosome; import org.apache.commons.math4.ga.dummy.DummyListChromosomeDecoder; import org.apache.commons.math4.ga.internal.exception.GeneticException; import org.apache.commons.math4.ga.utils.ChromosomeRepresentationUtils; +import org.junit.Assert; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -89,4 +90,12 @@ public class BinaryChromosomeTest { } } + @Test + public void testStringRepresentationWithRange() { + int length = 100; + String representationStr = ChromosomeRepresentationUtils.randomStringRepresentation(new char[] {'0', '1'}, + length); + BinaryChromosome<String> chromosome = new BinaryChromosome<>(representationStr, c -> 0, c -> "0"); + Assert.assertThrows(GeneticException.class, () -> chromosome.getStringRepresentation(10, length + 1)); + } } diff --git a/commons-math-ga/src/test/java/org/apache/commons/math4/ga/dummy/DummyListChromosomeDecoder.java b/commons-math-ga/src/test/java/org/apache/commons/math4/ga/dummy/DummyListChromosomeDecoder.java index 541cd26..969e69c 100644 --- a/commons-math-ga/src/test/java/org/apache/commons/math4/ga/dummy/DummyListChromosomeDecoder.java +++ b/commons-math-ga/src/test/java/org/apache/commons/math4/ga/dummy/DummyListChromosomeDecoder.java @@ -17,7 +17,6 @@ package org.apache.commons.math4.ga.dummy; import org.apache.commons.math4.ga.chromosome.AbstractListChromosome; -import org.apache.commons.math4.ga.chromosome.Chromosome; import org.apache.commons.math4.ga.decoder.AbstractListChromosomeDecoder; public class DummyListChromosomeDecoder<T> extends AbstractListChromosomeDecoder<T, String> { @@ -33,9 +32,4 @@ public class DummyListChromosomeDecoder<T> extends AbstractListChromosomeDecoder return value; } - @Override - protected void checkValidity(Chromosome<String> chromosome) { - // No op - } - } diff --git a/commons-math-ga/src/test/java/org/apache/commons/math4/ga/mutation/AbstractListChromosomeMutationPolicyTest.java b/commons-math-ga/src/test/java/org/apache/commons/math4/ga/mutation/AbstractListChromosomeMutationPolicyTest.java deleted file mode 100644 index 17c481a..0000000 --- a/commons-math-ga/src/test/java/org/apache/commons/math4/ga/mutation/AbstractListChromosomeMutationPolicyTest.java +++ /dev/null @@ -1,44 +0,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. - */ -package org.apache.commons.math4.ga.mutation; - -import org.apache.commons.math4.ga.chromosome.Chromosome; -import org.apache.commons.math4.ga.utils.RandomProviderManager; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -public class AbstractListChromosomeMutationPolicyTest { - - @Test - public void testGetMutableGeneIndexes() { - AbstractListChromosomeMutationPolicy<Integer, String> chromosomeMutationPolicy = new AbstractListChromosomeMutationPolicy<Integer, String>() { - - @Override - protected Integer mutateGene(Integer originalValue) { - return RandomProviderManager.getRandomProvider().nextInt(2); - } - - @Override - protected void checkValidity(Chromosome<String> original) { - // No Op - } - }; - Assertions.assertEquals(1, chromosomeMutationPolicy.getMutableGeneIndexes(10, .1).size()); - chromosomeMutationPolicy.getMutableGeneIndexes(10, .001); - } - -} diff --git a/commons-math-ga/src/test/java/org/apache/commons/math4/ga/mutation/BinaryMutationTest.java b/commons-math-ga/src/test/java/org/apache/commons/math4/ga/mutation/BinaryMutationTest.java index d2cc2bb..5cc7127 100644 --- a/commons-math-ga/src/test/java/org/apache/commons/math4/ga/mutation/BinaryMutationTest.java +++ b/commons-math-ga/src/test/java/org/apache/commons/math4/ga/mutation/BinaryMutationTest.java @@ -30,9 +30,9 @@ public class BinaryMutationTest { public void testCheckValidity() { BinaryMutation<String> mutation = new BinaryMutation<>(); Assertions.assertThrows(GeneticException.class, () -> { - mutation.checkValidity(new IntegralValuedChromosome<String>( + mutation.mutate(new IntegralValuedChromosome<String>( ChromosomeRepresentationUtils.randomIntegralRepresentation(10, 0, 2), c -> 0, - new DummyListChromosomeDecoder<>("0"), 0, 2)); + new DummyListChromosomeDecoder<>("0"), 0, 2), .1); }); } diff --git a/commons-math-ga/src/test/java/org/apache/commons/math4/ga/mutation/IntegralValuedMutationTest.java b/commons-math-ga/src/test/java/org/apache/commons/math4/ga/mutation/IntegralValuedMutationTest.java index 0d6135f..19f4f36 100644 --- a/commons-math-ga/src/test/java/org/apache/commons/math4/ga/mutation/IntegralValuedMutationTest.java +++ b/commons-math-ga/src/test/java/org/apache/commons/math4/ga/mutation/IntegralValuedMutationTest.java @@ -37,7 +37,7 @@ public class IntegralValuedMutationTest { new DummyListChromosomeDecoder<>("0")); IntegralValuedMutation<String> mutation = new IntegralValuedMutation<>(min - 10, max); Assertions.assertThrows(GeneticException.class, () -> { - mutation.checkValidity(chromosome); + mutation.mutate(chromosome, .1); }); } @@ -50,7 +50,7 @@ public class IntegralValuedMutationTest { new DummyListChromosomeDecoder<>("0"), min, max); IntegralValuedMutation<String> mutation = new IntegralValuedMutation<>(min - 10, max); Assertions.assertThrows(GeneticException.class, () -> { - mutation.checkValidity(chromosome); + mutation.mutate(chromosome, .1); }); } @@ -79,6 +79,7 @@ public class IntegralValuedMutationTest { int origValue = min + RandomProviderManager.getRandomProvider().nextInt(max - min); int mutatedValued = mutation.mutateGene(origValue); Assertions.assertTrue(min <= mutatedValued && mutatedValued < max); + Assertions.assertNotEquals(origValue, mutatedValued); } } diff --git a/commons-math-ga/src/test/java/org/apache/commons/math4/ga/mutation/RealValuedMutationTest.java b/commons-math-ga/src/test/java/org/apache/commons/math4/ga/mutation/RealValuedMutationTest.java index 015801b..c3ee9ff 100644 --- a/commons-math-ga/src/test/java/org/apache/commons/math4/ga/mutation/RealValuedMutationTest.java +++ b/commons-math-ga/src/test/java/org/apache/commons/math4/ga/mutation/RealValuedMutationTest.java @@ -16,6 +16,8 @@ */ package org.apache.commons.math4.ga.mutation; +import java.util.List; + import org.apache.commons.math4.ga.chromosome.Chromosome; import org.apache.commons.math4.ga.chromosome.IntegralValuedChromosome; import org.apache.commons.math4.ga.chromosome.RealValuedChromosome; @@ -37,7 +39,7 @@ public class RealValuedMutationTest { new DummyListChromosomeDecoder<>("0"), min, max); RealValuedMutation<String> mutation = new RealValuedMutation<>(min - 10, max); Assertions.assertThrows(GeneticException.class, () -> { - mutation.checkValidity(chromosome); + mutation.mutate(chromosome, .1); }); } @@ -50,12 +52,34 @@ public class RealValuedMutationTest { new DummyListChromosomeDecoder<>("0"), min, max); RealValuedMutation<String> mutation = new RealValuedMutation<>(min - 10, max); Assertions.assertThrows(GeneticException.class, () -> { - mutation.checkValidity(chromosome); + mutation.mutate(chromosome, .1); }); } @Test - public void testIntegralValuedMutation() { + public void testGetMutableGeneIndexes() { + RealValuedMutation<String> mutationPolicy = new RealValuedMutation<>(0, 10); + List<Double> representation = ChromosomeRepresentationUtils.randomDoubleRepresentation(10, 0, 10); + RealValuedChromosome<String> chromosome = new RealValuedChromosome<>(representation, c -> 0, + new DummyListChromosomeDecoder<>("0"), 0, 10); + RealValuedChromosome<String> mutatedChromosome = mutationPolicy.mutate(chromosome, .2); + Assertions.assertEquals(2, calculatedMutatedAlleleCount(representation, mutatedChromosome.getRepresentation())); + mutatedChromosome = mutationPolicy.mutate(chromosome, .1); + Assertions.assertEquals(1, calculatedMutatedAlleleCount(representation, mutatedChromosome.getRepresentation())); + } + + private int calculatedMutatedAlleleCount(List<Double> representation, List<Double> mutatedRepresentation) { + int mutationCount = 0; + for (int i = 0; i < representation.size(); i++) { + if (representation.get(i) != mutatedRepresentation.get(i)) { + mutationCount++; + } + } + return mutationCount; + } + + @Test + public void testRealValuedMutation() { Assertions.assertThrows(GeneticException.class, () -> { new RealValuedMutation<>(10, 5); }); @@ -79,6 +103,7 @@ public class RealValuedMutationTest { double origValue = min + (max - min) * RandomProviderManager.getRandomProvider().nextDouble(); double mutatedValue = mutation.mutateGene(origValue); Assertions.assertTrue(min <= mutatedValue && mutatedValue < max); + Assertions.assertNotEquals(origValue, mutatedValue); } }