From 1a9e32ab8f63ab40517a9e3837ca8c5dce52f20d Mon Sep 17 00:00:00 2001 From: Thomas Neidhart Date: Sun, 16 Sep 2012 16:05:57 +0000 Subject: [PATCH] [MATH-854] fill throws clause for genetics package. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1385297 13f79535-47bb-0310-9956-ffa450edef68 --- .../genetics/AbstractListChromosome.java | 5 +++-- .../math3/genetics/BinaryChromosome.java | 10 +++------ .../math3/genetics/BinaryMutation.java | 2 +- .../math3/genetics/CrossoverPolicy.java | 5 ++++- .../math3/genetics/CycleCrossover.java | 11 ++++++++-- .../genetics/ElitisticListPopulation.java | 22 ++++++++++++++----- .../math3/genetics/FixedElapsedTime.java | 4 ++-- .../math3/genetics/FixedGenerationCount.java | 2 +- .../math3/genetics/GeneticAlgorithm.java | 2 +- .../math3/genetics/ListPopulation.java | 18 +++++++++------ .../math3/genetics/MutationPolicy.java | 5 ++++- .../math3/genetics/NPointCrossover.java | 10 ++++++--- .../math3/genetics/OnePointCrossover.java | 6 +++-- .../math3/genetics/OrderedCrossover.java | 12 ++++++++-- .../commons/math3/genetics/Population.java | 8 ++++--- .../commons/math3/genetics/RandomKey.java | 17 ++++++++------ .../math3/genetics/RandomKeyMutation.java | 2 +- .../math3/genetics/SelectionPolicy.java | 5 ++++- .../math3/genetics/TournamentSelection.java | 5 +++-- .../math3/genetics/UniformCrossover.java | 12 +++++++--- 20 files changed, 109 insertions(+), 54 deletions(-) diff --git a/src/main/java/org/apache/commons/math3/genetics/AbstractListChromosome.java b/src/main/java/org/apache/commons/math3/genetics/AbstractListChromosome.java index ecd484822..4faa27db5 100644 --- a/src/main/java/org/apache/commons/math3/genetics/AbstractListChromosome.java +++ b/src/main/java/org/apache/commons/math3/genetics/AbstractListChromosome.java @@ -38,7 +38,7 @@ public abstract class AbstractListChromosome extends Chromosome { * @param representation inner representation of the chromosome * @throws InvalidRepresentationException iff the representation can not represent a valid chromosome */ - public AbstractListChromosome(final List representation) { + public AbstractListChromosome(final List representation) throws InvalidRepresentationException { checkValidity(representation); this.representation = Collections.unmodifiableList(new ArrayList (representation)); } @@ -46,8 +46,9 @@ public abstract class AbstractListChromosome extends Chromosome { /** * Constructor. * @param representation inner representation of the chromosome + * @throws InvalidRepresentationException iff the representation can not represent a valid chromosome */ - public AbstractListChromosome(final T[] representation) { + public AbstractListChromosome(final T[] representation) throws InvalidRepresentationException { this(Arrays.asList(representation)); } diff --git a/src/main/java/org/apache/commons/math3/genetics/BinaryChromosome.java b/src/main/java/org/apache/commons/math3/genetics/BinaryChromosome.java index 99393f987..19574f09b 100644 --- a/src/main/java/org/apache/commons/math3/genetics/BinaryChromosome.java +++ b/src/main/java/org/apache/commons/math3/genetics/BinaryChromosome.java @@ -34,7 +34,7 @@ public abstract class BinaryChromosome extends AbstractListChromosome { * @param representation list of {0,1} values representing the chromosome * @throws InvalidRepresentationException iff the representation can not represent a valid chromosome */ - public BinaryChromosome(List representation) { + public BinaryChromosome(List representation) throws InvalidRepresentationException { super(representation); } @@ -43,7 +43,7 @@ public abstract class BinaryChromosome extends AbstractListChromosome { * @param representation array of {0,1} values representing the chromosome * @throws InvalidRepresentationException iff the representation can not represent a valid chromosome */ - public BinaryChromosome(Integer[] representation) { + public BinaryChromosome(Integer[] representation) throws InvalidRepresentationException { super(representation); } @@ -51,8 +51,7 @@ public abstract class BinaryChromosome extends AbstractListChromosome { * {@inheritDoc} */ @Override - protected void checkValidity(List chromosomeRepresentation) - throws InvalidRepresentationException { + protected void checkValidity(List chromosomeRepresentation) throws InvalidRepresentationException { for (int i : chromosomeRepresentation) { if (i < 0 || i >1) { throw new InvalidRepresentationException(LocalizedFormats.INVALID_BINARY_DIGIT, @@ -75,9 +74,6 @@ public abstract class BinaryChromosome extends AbstractListChromosome { return rList; } - /** - * {@inheritDoc} - */ @Override protected boolean isSame(Chromosome another) { // type check diff --git a/src/main/java/org/apache/commons/math3/genetics/BinaryMutation.java b/src/main/java/org/apache/commons/math3/genetics/BinaryMutation.java index 242a965fd..7fd806003 100644 --- a/src/main/java/org/apache/commons/math3/genetics/BinaryMutation.java +++ b/src/main/java/org/apache/commons/math3/genetics/BinaryMutation.java @@ -37,7 +37,7 @@ public class BinaryMutation implements MutationPolicy { * @return the mutated chromosome. * @throws MathIllegalArgumentException if original is not an instance of {@link BinaryChromosome}. */ - public Chromosome mutate(Chromosome original) { + public Chromosome mutate(Chromosome original) throws MathIllegalArgumentException { if (!(original instanceof BinaryChromosome)) { throw new MathIllegalArgumentException(LocalizedFormats.INVALID_BINARY_CHROMOSOME); } diff --git a/src/main/java/org/apache/commons/math3/genetics/CrossoverPolicy.java b/src/main/java/org/apache/commons/math3/genetics/CrossoverPolicy.java index ddd05a364..bb0d62fbc 100644 --- a/src/main/java/org/apache/commons/math3/genetics/CrossoverPolicy.java +++ b/src/main/java/org/apache/commons/math3/genetics/CrossoverPolicy.java @@ -16,6 +16,8 @@ */ package org.apache.commons.math3.genetics; +import org.apache.commons.math3.exception.MathIllegalArgumentException; + /** * Policy used to create a pair of new chromosomes by performing a crossover * operation on a source pair of chromosomes. @@ -31,6 +33,7 @@ public interface CrossoverPolicy { * @param first the first chromosome. * @param second the second chromosome. * @return the pair of new chromosomes that resulted from the crossover. + * @throws MathIllegalArgumentException if the given chromosomes are not compatible with this {@link CrossoverPolicy} */ - ChromosomePair crossover(Chromosome first, Chromosome second); + ChromosomePair crossover(Chromosome first, Chromosome second) throws MathIllegalArgumentException; } diff --git a/src/main/java/org/apache/commons/math3/genetics/CycleCrossover.java b/src/main/java/org/apache/commons/math3/genetics/CycleCrossover.java index 1ff29f993..bd3599044 100644 --- a/src/main/java/org/apache/commons/math3/genetics/CycleCrossover.java +++ b/src/main/java/org/apache/commons/math3/genetics/CycleCrossover.java @@ -95,9 +95,14 @@ public class CycleCrossover implements CrossoverPolicy { /** * {@inheritDoc} + * + * @throws MathIllegalArgumentException if the chromosomes are not an instance of {@link AbstractListChromosome} + * @throws DimensionMismatchException if the length of the two chromosomes is different */ @SuppressWarnings("unchecked") - public ChromosomePair crossover(final Chromosome first, final Chromosome second) { + public ChromosomePair crossover(final Chromosome first, final Chromosome second) + throws DimensionMismatchException, MathIllegalArgumentException { + if (!(first instanceof AbstractListChromosome && second instanceof AbstractListChromosome)) { throw new MathIllegalArgumentException(LocalizedFormats.INVALID_FIXED_LENGTH_CHROMOSOME); } @@ -112,7 +117,9 @@ public class CycleCrossover implements CrossoverPolicy { * @return the pair of new chromosomes that resulted from the crossover * @throws DimensionMismatchException if the length of the two chromosomes is different */ - protected ChromosomePair mate(final AbstractListChromosome first, final AbstractListChromosome second) { + protected ChromosomePair mate(final AbstractListChromosome first, final AbstractListChromosome second) + throws DimensionMismatchException { + final int length = first.getLength(); if (length != second.getLength()) { throw new DimensionMismatchException(second.getLength(), length); diff --git a/src/main/java/org/apache/commons/math3/genetics/ElitisticListPopulation.java b/src/main/java/org/apache/commons/math3/genetics/ElitisticListPopulation.java index 1c3dacdab..bc8ab1017 100644 --- a/src/main/java/org/apache/commons/math3/genetics/ElitisticListPopulation.java +++ b/src/main/java/org/apache/commons/math3/genetics/ElitisticListPopulation.java @@ -19,6 +19,9 @@ package org.apache.commons.math3.genetics; import java.util.Collections; import java.util.List; +import org.apache.commons.math3.exception.NotPositiveException; +import org.apache.commons.math3.exception.NullArgumentException; +import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.util.FastMath; @@ -41,13 +44,18 @@ public class ElitisticListPopulation extends ListPopulation { * @param chromosomes list of chromosomes in the population * @param populationLimit maximal size of the population * @param elitismRate how many best chromosomes will be directly transferred to the next generation [in %] + * @throws NullArgumentException if the list of chromosomes is {@code null} + * @throws NotPositiveException if the population limit is not a positive number (< 1) + * @throws NumberIsTooLargeException if the list of chromosomes exceeds the population limit * @throws OutOfRangeException if the elitism rate is outside the [0, 1] range */ - public ElitisticListPopulation(final List chromosomes, - final int populationLimit, - final double elitismRate) { + public ElitisticListPopulation(final List chromosomes, final int populationLimit, + final double elitismRate) + throws NullArgumentException, NotPositiveException, NumberIsTooLargeException, OutOfRangeException { + super(chromosomes, populationLimit); setElitismRate(elitismRate); + } /** @@ -55,11 +63,15 @@ public class ElitisticListPopulation extends ListPopulation { * * @param populationLimit maximal size of the population * @param elitismRate how many best chromosomes will be directly transferred to the next generation [in %] + * @throws NotPositiveException if the population limit is not a positive number (< 1) * @throws OutOfRangeException if the elitism rate is outside the [0, 1] range */ - public ElitisticListPopulation(final int populationLimit, final double elitismRate) { + public ElitisticListPopulation(final int populationLimit, final double elitismRate) + throws NotPositiveException, OutOfRangeException { + super(populationLimit); setElitismRate(elitismRate); + } /** @@ -90,7 +102,7 @@ public class ElitisticListPopulation extends ListPopulation { * @param elitismRate how many best chromosomes will be directly transferred to the next generation [in %] * @throws OutOfRangeException if the elitism rate is outside the [0, 1] range */ - public void setElitismRate(final double elitismRate) { + public void setElitismRate(final double elitismRate) throws OutOfRangeException { if (elitismRate < 0 || elitismRate > 1) { throw new OutOfRangeException(LocalizedFormats.ELITISM_RATE, elitismRate, 0, 1); } diff --git a/src/main/java/org/apache/commons/math3/genetics/FixedElapsedTime.java b/src/main/java/org/apache/commons/math3/genetics/FixedElapsedTime.java index f636ab6b2..9c699b300 100644 --- a/src/main/java/org/apache/commons/math3/genetics/FixedElapsedTime.java +++ b/src/main/java/org/apache/commons/math3/genetics/FixedElapsedTime.java @@ -43,7 +43,7 @@ public class FixedElapsedTime implements StoppingCondition { * @param maxTime maximum number of seconds generations are allowed to evolve * @throws NumberIsTooSmallException if the provided time is < 0 */ - public FixedElapsedTime(final long maxTime) { + public FixedElapsedTime(final long maxTime) throws NumberIsTooSmallException { this(maxTime, TimeUnit.SECONDS); } @@ -54,7 +54,7 @@ public class FixedElapsedTime implements StoppingCondition { * @param unit {@link TimeUnit} of the maxTime argument * @throws NumberIsTooSmallException if the provided time is < 0 */ - public FixedElapsedTime(final long maxTime, final TimeUnit unit) { + public FixedElapsedTime(final long maxTime, final TimeUnit unit) throws NumberIsTooSmallException { if (maxTime < 0) { throw new NumberIsTooSmallException(maxTime, 0, true); } diff --git a/src/main/java/org/apache/commons/math3/genetics/FixedGenerationCount.java b/src/main/java/org/apache/commons/math3/genetics/FixedGenerationCount.java index f746f13bd..bcf26bdc5 100644 --- a/src/main/java/org/apache/commons/math3/genetics/FixedGenerationCount.java +++ b/src/main/java/org/apache/commons/math3/genetics/FixedGenerationCount.java @@ -39,7 +39,7 @@ public class FixedGenerationCount implements StoppingCondition { * @param maxGenerations number of generations to evolve * @throws NumberIsTooSmallException if the number of generations is < 1 */ - public FixedGenerationCount(final int maxGenerations) { + public FixedGenerationCount(final int maxGenerations) throws NumberIsTooSmallException { if (maxGenerations <= 0) { throw new NumberIsTooSmallException(maxGenerations, 1, true); } diff --git a/src/main/java/org/apache/commons/math3/genetics/GeneticAlgorithm.java b/src/main/java/org/apache/commons/math3/genetics/GeneticAlgorithm.java index 475739421..445ed5825 100644 --- a/src/main/java/org/apache/commons/math3/genetics/GeneticAlgorithm.java +++ b/src/main/java/org/apache/commons/math3/genetics/GeneticAlgorithm.java @@ -69,7 +69,7 @@ public class GeneticAlgorithm { final double crossoverRate, final MutationPolicy mutationPolicy, final double mutationRate, - final SelectionPolicy selectionPolicy) { + final SelectionPolicy selectionPolicy) throws OutOfRangeException { if (crossoverRate < 0 || crossoverRate > 1) { throw new OutOfRangeException(LocalizedFormats.CROSSOVER_RATE, diff --git a/src/main/java/org/apache/commons/math3/genetics/ListPopulation.java b/src/main/java/org/apache/commons/math3/genetics/ListPopulation.java index 95cace0c9..48eb12082 100644 --- a/src/main/java/org/apache/commons/math3/genetics/ListPopulation.java +++ b/src/main/java/org/apache/commons/math3/genetics/ListPopulation.java @@ -48,7 +48,7 @@ public abstract class ListPopulation implements Population { * @param populationLimit maximal size of the population * @throws NotPositiveException if the population limit is not a positive number (< 1) */ - public ListPopulation(final int populationLimit) { + public ListPopulation(final int populationLimit) throws NotPositiveException { this(Collections. emptyList(), populationLimit); } @@ -63,7 +63,9 @@ public abstract class ListPopulation implements Population { * @throws NotPositiveException if the population limit is not a positive number (< 1) * @throws NumberIsTooLargeException if the list of chromosomes exceeds the population limit */ - public ListPopulation(final List chromosomes, final int populationLimit) { + public ListPopulation(final List chromosomes, final int populationLimit) + throws NullArgumentException, NotPositiveException, NumberIsTooLargeException { + if (chromosomes == null) { throw new NullArgumentException(); } @@ -91,7 +93,9 @@ public abstract class ListPopulation implements Population { * @deprecated use {@link #addChromosomes(Collection)} instead */ @Deprecated - public void setChromosomes(final List chromosomes) { + public void setChromosomes(final List chromosomes) + throws NullArgumentException, NumberIsTooLargeException { + if (chromosomes == null) { throw new NullArgumentException(); } @@ -107,9 +111,9 @@ public abstract class ListPopulation implements Population { * Add a {@link Collection} of chromosomes to this {@link Population}. * @param chromosomeColl a {@link Collection} of chromosomes * @throws NumberIsTooLargeException if the population would exceed the population limit when - * adding this chromosome + * adding this chromosome */ - public void addChromosomes(final Collection chromosomeColl) { + public void addChromosomes(final Collection chromosomeColl) throws NumberIsTooLargeException { if (chromosomes.size() + chromosomeColl.size() > populationLimit) { throw new NumberIsTooLargeException(LocalizedFormats.LIST_OF_CHROMOSOMES_BIGGER_THAN_POPULATION_SIZE, chromosomes.size(), populationLimit, false); @@ -140,7 +144,7 @@ public abstract class ListPopulation implements Population { * @throws NumberIsTooLargeException if the population would exceed the {@code populationLimit} after * adding this chromosome */ - public void addChromosome(final Chromosome chromosome) { + public void addChromosome(final Chromosome chromosome) throws NumberIsTooLargeException { if (chromosomes.size() >= populationLimit) { throw new NumberIsTooLargeException(LocalizedFormats.LIST_OF_CHROMOSOMES_BIGGER_THAN_POPULATION_SIZE, chromosomes.size(), populationLimit, false); @@ -179,7 +183,7 @@ public abstract class ListPopulation implements Population { * @throws NumberIsTooSmallException if the new population size is smaller than the current number * of chromosomes in the population */ - public void setPopulationLimit(final int populationLimit) { + public void setPopulationLimit(final int populationLimit) throws NotPositiveException, NumberIsTooSmallException { if (populationLimit <= 0) { throw new NotPositiveException(LocalizedFormats.POPULATION_LIMIT_NOT_POSITIVE, populationLimit); } diff --git a/src/main/java/org/apache/commons/math3/genetics/MutationPolicy.java b/src/main/java/org/apache/commons/math3/genetics/MutationPolicy.java index 374d8a024..9a4520e97 100644 --- a/src/main/java/org/apache/commons/math3/genetics/MutationPolicy.java +++ b/src/main/java/org/apache/commons/math3/genetics/MutationPolicy.java @@ -16,6 +16,8 @@ */ package org.apache.commons.math3.genetics; +import org.apache.commons.math3.exception.MathIllegalArgumentException; + /** * Algorithm used to mutate a chromosome. * @@ -28,6 +30,7 @@ public interface MutationPolicy { * Mutate the given chromosome. * @param original the original chromosome. * @return the mutated chromosome. + * @throws MathIllegalArgumentException if the given chromosome is not compatible with this {@link MutationPolicy} */ - Chromosome mutate(Chromosome original); + Chromosome mutate(Chromosome original) throws MathIllegalArgumentException; } diff --git a/src/main/java/org/apache/commons/math3/genetics/NPointCrossover.java b/src/main/java/org/apache/commons/math3/genetics/NPointCrossover.java index 6f7b645ae..8243eceff 100644 --- a/src/main/java/org/apache/commons/math3/genetics/NPointCrossover.java +++ b/src/main/java/org/apache/commons/math3/genetics/NPointCrossover.java @@ -64,7 +64,7 @@ public class NPointCrossover implements CrossoverPolicy { * @param crossoverPoints the number of crossover points * @throws NotStrictlyPositiveException if the number of {@code crossoverPoints} is not strictly positive */ - public NPointCrossover(final int crossoverPoints) { + public NPointCrossover(final int crossoverPoints) throws NotStrictlyPositiveException { if (crossoverPoints <= 0) { throw new NotStrictlyPositiveException(crossoverPoints); } @@ -105,7 +105,9 @@ public class NPointCrossover implements CrossoverPolicy { * @throws DimensionMismatchException if the length of the two chromosomes is different */ @SuppressWarnings("unchecked") // OK because of instanceof checks - public ChromosomePair crossover(final Chromosome first, final Chromosome second) { + public ChromosomePair crossover(final Chromosome first, final Chromosome second) + throws DimensionMismatchException, MathIllegalArgumentException { + if (!(first instanceof AbstractListChromosome && second instanceof AbstractListChromosome)) { throw new MathIllegalArgumentException(LocalizedFormats.INVALID_FIXED_LENGTH_CHROMOSOME); } @@ -122,7 +124,9 @@ public class NPointCrossover implements CrossoverPolicy { * @throws NumberIsTooLargeException if the number of crossoverPoints is too large for the actual chromosomes */ private ChromosomePair mate(final AbstractListChromosome first, - final AbstractListChromosome second) { + final AbstractListChromosome second) + throws DimensionMismatchException, NumberIsTooLargeException { + final int length = first.getLength(); if (length != second.getLength()) { throw new DimensionMismatchException(second.getLength(), length); diff --git a/src/main/java/org/apache/commons/math3/genetics/OnePointCrossover.java b/src/main/java/org/apache/commons/math3/genetics/OnePointCrossover.java index 25d968f6a..0366cadbf 100644 --- a/src/main/java/org/apache/commons/math3/genetics/OnePointCrossover.java +++ b/src/main/java/org/apache/commons/math3/genetics/OnePointCrossover.java @@ -76,7 +76,9 @@ public class OnePointCrossover implements CrossoverPolicy { * @throws DimensionMismatchException if the length of the two chromosomes is different */ @SuppressWarnings("unchecked") // OK because of instanceof checks - public ChromosomePair crossover(final Chromosome first, final Chromosome second) { + public ChromosomePair crossover(final Chromosome first, final Chromosome second) + throws DimensionMismatchException, MathIllegalArgumentException { + if (! (first instanceof AbstractListChromosome && second instanceof AbstractListChromosome)) { throw new MathIllegalArgumentException(LocalizedFormats.INVALID_FIXED_LENGTH_CHROMOSOME); } @@ -93,7 +95,7 @@ public class OnePointCrossover implements CrossoverPolicy { * @throws DimensionMismatchException if the length of the two chromosomes is different */ private ChromosomePair crossover(final AbstractListChromosome first, - final AbstractListChromosome second) { + final AbstractListChromosome second) throws DimensionMismatchException { final int length = first.getLength(); if (length != second.getLength()) { throw new DimensionMismatchException(second.getLength(), length); diff --git a/src/main/java/org/apache/commons/math3/genetics/OrderedCrossover.java b/src/main/java/org/apache/commons/math3/genetics/OrderedCrossover.java index fb8cd84e9..304dc27a8 100644 --- a/src/main/java/org/apache/commons/math3/genetics/OrderedCrossover.java +++ b/src/main/java/org/apache/commons/math3/genetics/OrderedCrossover.java @@ -62,9 +62,15 @@ public class OrderedCrossover implements CrossoverPolicy { /** * {@inheritDoc} + * + * @throws MathIllegalArgumentException iff one of the chromosomes is + * not an instance of {@link AbstractListChromosome} + * @throws DimensionMismatchException if the length of the two chromosomes is different */ @SuppressWarnings("unchecked") - public ChromosomePair crossover(final Chromosome first, final Chromosome second) { + public ChromosomePair crossover(final Chromosome first, final Chromosome second) + throws DimensionMismatchException, MathIllegalArgumentException { + if (!(first instanceof AbstractListChromosome && second instanceof AbstractListChromosome)) { throw new MathIllegalArgumentException(LocalizedFormats.INVALID_FIXED_LENGTH_CHROMOSOME); } @@ -79,7 +85,9 @@ public class OrderedCrossover implements CrossoverPolicy { * @return the pair of new chromosomes that resulted from the crossover * @throws DimensionMismatchException if the length of the two chromosomes is different */ - protected ChromosomePair mate(final AbstractListChromosome first, final AbstractListChromosome second) { + protected ChromosomePair mate(final AbstractListChromosome first, final AbstractListChromosome second) + throws DimensionMismatchException { + final int length = first.getLength(); if (length != second.getLength()) { throw new DimensionMismatchException(second.getLength(), length); diff --git a/src/main/java/org/apache/commons/math3/genetics/Population.java b/src/main/java/org/apache/commons/math3/genetics/Population.java index 41b8a91f1..1db3e665a 100644 --- a/src/main/java/org/apache/commons/math3/genetics/Population.java +++ b/src/main/java/org/apache/commons/math3/genetics/Population.java @@ -16,6 +16,8 @@ */ package org.apache.commons.math3.genetics; +import org.apache.commons.math3.exception.NumberIsTooLargeException; + /** * A collection of chromosomes that facilitates generational evolution. @@ -45,10 +47,10 @@ public interface Population extends Iterable { /** * Add the given chromosome to the population. * @param chromosome the chromosome to add. - * @throws org.apache.commons.math3.exception.NumberIsTooLargeException if the population would exceed - * the population limit when adding this chromosome + * @throws NumberIsTooLargeException if the population would exceed the population limit when adding + * this chromosome */ - void addChromosome(Chromosome chromosome); + void addChromosome(Chromosome chromosome) throws NumberIsTooLargeException; /** * Access the fittest chromosome in this population. diff --git a/src/main/java/org/apache/commons/math3/genetics/RandomKey.java b/src/main/java/org/apache/commons/math3/genetics/RandomKey.java index 0ec33c05b..75ae2bed4 100644 --- a/src/main/java/org/apache/commons/math3/genetics/RandomKey.java +++ b/src/main/java/org/apache/commons/math3/genetics/RandomKey.java @@ -68,10 +68,9 @@ public abstract class RandomKey extends AbstractListChromosome implem * Constructor. * * @param representation list of [0,1] values representing the permutation - * @throws InvalidRepresentationException iff the representation can not represent - * a valid chromosome + * @throws InvalidRepresentationException iff the representation can not represent a valid chromosome */ - public RandomKey(final List representation) { + public RandomKey(final List representation) throws InvalidRepresentationException { super(representation); // store the sorted representation List sortedRepr = new ArrayList (getRepresentation()); @@ -87,8 +86,9 @@ public abstract class RandomKey extends AbstractListChromosome implem * Constructor. * * @param representation array of [0,1] values representing the permutation + * @throws InvalidRepresentationException iff the representation can not represent a valid chromosome */ - public RandomKey(final Double[] representation) { + public RandomKey(final Double[] representation) throws InvalidRepresentationException { this(Arrays.asList(representation)); } @@ -112,7 +112,9 @@ public abstract class RandomKey extends AbstractListChromosome implem * representation or sortedRepr lists are not equal */ private static List decodeGeneric(final List sequence, List representation, - final List sortedRepr) { + final List sortedRepr) + throws DimensionMismatchException { + int l = sequence.size(); // the size of the three lists must be equal @@ -230,7 +232,7 @@ public abstract class RandomKey extends AbstractListChromosome implem */ public static List comparatorPermutation(final List data, final Comparator comparator) { - List sortedData = new ArrayList (data); + List sortedData = new ArrayList(data); Collections.sort(sortedData, comparator); return inducedPermutation(data, sortedData); @@ -254,7 +256,8 @@ public abstract class RandomKey extends AbstractListChromosome implem * originalData lists contain different data */ public static List inducedPermutation(final List originalData, - final List permutedData) { + final List permutedData) + throws DimensionMismatchException, MathIllegalArgumentException { if (originalData.size() != permutedData.size()) { throw new DimensionMismatchException(permutedData.size(), originalData.size()); diff --git a/src/main/java/org/apache/commons/math3/genetics/RandomKeyMutation.java b/src/main/java/org/apache/commons/math3/genetics/RandomKeyMutation.java index 3f93772f6..eef8cbaa3 100644 --- a/src/main/java/org/apache/commons/math3/genetics/RandomKeyMutation.java +++ b/src/main/java/org/apache/commons/math3/genetics/RandomKeyMutation.java @@ -36,7 +36,7 @@ public class RandomKeyMutation implements MutationPolicy { * * @throws MathIllegalArgumentException if original is not a {@link RandomKey} instance */ - public Chromosome mutate(final Chromosome original) { + public Chromosome mutate(final Chromosome original) throws MathIllegalArgumentException { if (!(original instanceof RandomKey)) { throw new MathIllegalArgumentException(LocalizedFormats.RANDOMKEY_MUTATION_WRONG_CLASS, original.getClass().getSimpleName()); diff --git a/src/main/java/org/apache/commons/math3/genetics/SelectionPolicy.java b/src/main/java/org/apache/commons/math3/genetics/SelectionPolicy.java index ef1d8cc5a..ab83280e8 100644 --- a/src/main/java/org/apache/commons/math3/genetics/SelectionPolicy.java +++ b/src/main/java/org/apache/commons/math3/genetics/SelectionPolicy.java @@ -16,6 +16,8 @@ */ package org.apache.commons.math3.genetics; +import org.apache.commons.math3.exception.MathIllegalArgumentException; + /** * Algorithm used to select a chromosome pair from a population. * @@ -27,6 +29,7 @@ public interface SelectionPolicy { * Select two chromosomes from the population. * @param population the population from which the chromosomes are choosen. * @return the selected chromosomes. + * @throws MathIllegalArgumentException if the population is not compatible with this {@link SelectionPolicy} */ - ChromosomePair select(Population population); + ChromosomePair select(Population population) throws MathIllegalArgumentException; } diff --git a/src/main/java/org/apache/commons/math3/genetics/TournamentSelection.java b/src/main/java/org/apache/commons/math3/genetics/TournamentSelection.java index a56e273f4..7c35084d8 100644 --- a/src/main/java/org/apache/commons/math3/genetics/TournamentSelection.java +++ b/src/main/java/org/apache/commons/math3/genetics/TournamentSelection.java @@ -53,8 +53,9 @@ public class TournamentSelection implements SelectionPolicy { * * @param population the population from which the chromosomes are chosen. * @return the selected chromosomes. + * @throws MathIllegalArgumentException if the tournament arity is bigger than the population size */ - public ChromosomePair select(final Population population) { + public ChromosomePair select(final Population population) throws MathIllegalArgumentException { return new ChromosomePair(tournament((ListPopulation) population), tournament((ListPopulation) population)); } @@ -67,7 +68,7 @@ public class TournamentSelection implements SelectionPolicy { * @return the selected chromosome. * @throws MathIllegalArgumentException if the tournament arity is bigger than the population size */ - private Chromosome tournament(final ListPopulation population) { + private Chromosome tournament(final ListPopulation population) throws MathIllegalArgumentException { if (population.getPopulationSize() < this.arity) { throw new MathIllegalArgumentException(LocalizedFormats.TOO_LARGE_TOURNAMENT_ARITY, arity, population.getPopulationSize()); diff --git a/src/main/java/org/apache/commons/math3/genetics/UniformCrossover.java b/src/main/java/org/apache/commons/math3/genetics/UniformCrossover.java index 2bd9ca4c7..b135f91d8 100644 --- a/src/main/java/org/apache/commons/math3/genetics/UniformCrossover.java +++ b/src/main/java/org/apache/commons/math3/genetics/UniformCrossover.java @@ -60,7 +60,7 @@ public class UniformCrossover implements CrossoverPolicy { * @param ratio the mixing ratio * @throws OutOfRangeException if the mixing ratio is outside the [0, 1] range */ - public UniformCrossover(final double ratio) { + public UniformCrossover(final double ratio) throws OutOfRangeException { if (ratio < 0.0d || ratio > 1.0d) { throw new OutOfRangeException(LocalizedFormats.CROSSOVER_RATE, ratio, 0.0d, 1.0d); } @@ -78,9 +78,15 @@ public class UniformCrossover implements CrossoverPolicy { /** * {@inheritDoc} + * + * @throws MathIllegalArgumentException iff one of the chromosomes is + * not an instance of {@link AbstractListChromosome} + * @throws DimensionMismatchException if the length of the two chromosomes is different */ @SuppressWarnings("unchecked") - public ChromosomePair crossover(final Chromosome first, final Chromosome second) { + public ChromosomePair crossover(final Chromosome first, final Chromosome second) + throws DimensionMismatchException, MathIllegalArgumentException { + if (!(first instanceof AbstractListChromosome && second instanceof AbstractListChromosome)) { throw new MathIllegalArgumentException(LocalizedFormats.INVALID_FIXED_LENGTH_CHROMOSOME); } @@ -96,7 +102,7 @@ public class UniformCrossover implements CrossoverPolicy { * @throws DimensionMismatchException if the length of the two chromosomes is different */ private ChromosomePair mate(final AbstractListChromosome first, - final AbstractListChromosome second) { + final AbstractListChromosome second) throws DimensionMismatchException { final int length = first.getLength(); if (length != second.getLength()) { throw new DimensionMismatchException(second.getLength(), length);