[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
This commit is contained in:
parent
ef8beb8b2b
commit
1a9e32ab8f
|
@ -38,7 +38,7 @@ public abstract class AbstractListChromosome<T> extends Chromosome {
|
|||
* @param representation inner representation of the chromosome
|
||||
* @throws InvalidRepresentationException iff the <code>representation</code> can not represent a valid chromosome
|
||||
*/
|
||||
public AbstractListChromosome(final List<T> representation) {
|
||||
public AbstractListChromosome(final List<T> representation) throws InvalidRepresentationException {
|
||||
checkValidity(representation);
|
||||
this.representation = Collections.unmodifiableList(new ArrayList<T> (representation));
|
||||
}
|
||||
|
@ -46,8 +46,9 @@ public abstract class AbstractListChromosome<T> extends Chromosome {
|
|||
/**
|
||||
* Constructor.
|
||||
* @param representation inner representation of the chromosome
|
||||
* @throws InvalidRepresentationException iff the <code>representation</code> can not represent a valid chromosome
|
||||
*/
|
||||
public AbstractListChromosome(final T[] representation) {
|
||||
public AbstractListChromosome(final T[] representation) throws InvalidRepresentationException {
|
||||
this(Arrays.asList(representation));
|
||||
}
|
||||
|
||||
|
|
|
@ -34,7 +34,7 @@ public abstract class BinaryChromosome extends AbstractListChromosome<Integer> {
|
|||
* @param representation list of {0,1} values representing the chromosome
|
||||
* @throws InvalidRepresentationException iff the <code>representation</code> can not represent a valid chromosome
|
||||
*/
|
||||
public BinaryChromosome(List<Integer> representation) {
|
||||
public BinaryChromosome(List<Integer> representation) throws InvalidRepresentationException {
|
||||
super(representation);
|
||||
}
|
||||
|
||||
|
@ -43,7 +43,7 @@ public abstract class BinaryChromosome extends AbstractListChromosome<Integer> {
|
|||
* @param representation array of {0,1} values representing the chromosome
|
||||
* @throws InvalidRepresentationException iff the <code>representation</code> 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<Integer> {
|
|||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
protected void checkValidity(List<Integer> chromosomeRepresentation)
|
||||
throws InvalidRepresentationException {
|
||||
protected void checkValidity(List<Integer> 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<Integer> {
|
|||
return rList;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
protected boolean isSame(Chromosome another) {
|
||||
// type check
|
||||
|
|
|
@ -37,7 +37,7 @@ public class BinaryMutation implements MutationPolicy {
|
|||
* @return the mutated chromosome.
|
||||
* @throws MathIllegalArgumentException if <code>original</code> 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);
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -95,9 +95,14 @@ public class CycleCrossover<T> 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<T> 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<T> first, final AbstractListChromosome<T> second) {
|
||||
protected ChromosomePair mate(final AbstractListChromosome<T> first, final AbstractListChromosome<T> second)
|
||||
throws DimensionMismatchException {
|
||||
|
||||
final int length = first.getLength();
|
||||
if (length != second.getLength()) {
|
||||
throw new DimensionMismatchException(second.getLength(), length);
|
||||
|
|
|
@ -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<Chromosome> chromosomes,
|
||||
final int populationLimit,
|
||||
final double elitismRate) {
|
||||
public ElitisticListPopulation(final List<Chromosome> 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);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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.<Chromosome> 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<Chromosome> chromosomes, final int populationLimit) {
|
||||
public ListPopulation(final List<Chromosome> 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<Chromosome> chromosomes) {
|
||||
public void setChromosomes(final List<Chromosome> 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<Chromosome> chromosomeColl) {
|
||||
public void addChromosomes(final Collection<Chromosome> 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);
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -64,7 +64,7 @@ public class NPointCrossover<T> 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<T> 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<T> implements CrossoverPolicy {
|
|||
* @throws NumberIsTooLargeException if the number of crossoverPoints is too large for the actual chromosomes
|
||||
*/
|
||||
private ChromosomePair mate(final AbstractListChromosome<T> first,
|
||||
final AbstractListChromosome<T> second) {
|
||||
final AbstractListChromosome<T> second)
|
||||
throws DimensionMismatchException, NumberIsTooLargeException {
|
||||
|
||||
final int length = first.getLength();
|
||||
if (length != second.getLength()) {
|
||||
throw new DimensionMismatchException(second.getLength(), length);
|
||||
|
|
|
@ -76,7 +76,9 @@ public class OnePointCrossover<T> 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<T> implements CrossoverPolicy {
|
|||
* @throws DimensionMismatchException if the length of the two chromosomes is different
|
||||
*/
|
||||
private ChromosomePair crossover(final AbstractListChromosome<T> first,
|
||||
final AbstractListChromosome<T> second) {
|
||||
final AbstractListChromosome<T> second) throws DimensionMismatchException {
|
||||
final int length = first.getLength();
|
||||
if (length != second.getLength()) {
|
||||
throw new DimensionMismatchException(second.getLength(), length);
|
||||
|
|
|
@ -62,9 +62,15 @@ public class OrderedCrossover<T> 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<T> 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<T> first, final AbstractListChromosome<T> second) {
|
||||
protected ChromosomePair mate(final AbstractListChromosome<T> first, final AbstractListChromosome<T> second)
|
||||
throws DimensionMismatchException {
|
||||
|
||||
final int length = first.getLength();
|
||||
if (length != second.getLength()) {
|
||||
throw new DimensionMismatchException(second.getLength(), length);
|
||||
|
|
|
@ -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<Chromosome> {
|
|||
/**
|
||||
* 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.
|
||||
|
|
|
@ -68,10 +68,9 @@ public abstract class RandomKey<T> extends AbstractListChromosome<Double> implem
|
|||
* Constructor.
|
||||
*
|
||||
* @param representation list of [0,1] values representing the permutation
|
||||
* @throws InvalidRepresentationException iff the <code>representation</code> can not represent
|
||||
* a valid chromosome
|
||||
* @throws InvalidRepresentationException iff the <code>representation</code> can not represent a valid chromosome
|
||||
*/
|
||||
public RandomKey(final List<Double> representation) {
|
||||
public RandomKey(final List<Double> representation) throws InvalidRepresentationException {
|
||||
super(representation);
|
||||
// store the sorted representation
|
||||
List<Double> sortedRepr = new ArrayList<Double> (getRepresentation());
|
||||
|
@ -87,8 +86,9 @@ public abstract class RandomKey<T> extends AbstractListChromosome<Double> implem
|
|||
* Constructor.
|
||||
*
|
||||
* @param representation array of [0,1] values representing the permutation
|
||||
* @throws InvalidRepresentationException iff the <code>representation</code> 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<T> extends AbstractListChromosome<Double> implem
|
|||
* <code>representation</code> or <code>sortedRepr</code> lists are not equal
|
||||
*/
|
||||
private static <S> List<S> decodeGeneric(final List<S> sequence, List<Double> representation,
|
||||
final List<Double> sortedRepr) {
|
||||
final List<Double> sortedRepr)
|
||||
throws DimensionMismatchException {
|
||||
|
||||
int l = sequence.size();
|
||||
|
||||
// the size of the three lists must be equal
|
||||
|
@ -230,7 +232,7 @@ public abstract class RandomKey<T> extends AbstractListChromosome<Double> implem
|
|||
*/
|
||||
public static <S> List<Double> comparatorPermutation(final List<S> data,
|
||||
final Comparator<S> comparator) {
|
||||
List<S> sortedData = new ArrayList<S> (data);
|
||||
List<S> sortedData = new ArrayList<S>(data);
|
||||
Collections.sort(sortedData, comparator);
|
||||
|
||||
return inducedPermutation(data, sortedData);
|
||||
|
@ -254,7 +256,8 @@ public abstract class RandomKey<T> extends AbstractListChromosome<Double> implem
|
|||
* <code>originalData</code> lists contain different data
|
||||
*/
|
||||
public static <S> List<Double> inducedPermutation(final List<S> originalData,
|
||||
final List<S> permutedData) {
|
||||
final List<S> permutedData)
|
||||
throws DimensionMismatchException, MathIllegalArgumentException {
|
||||
|
||||
if (originalData.size() != permutedData.size()) {
|
||||
throw new DimensionMismatchException(permutedData.size(), originalData.size());
|
||||
|
|
|
@ -36,7 +36,7 @@ public class RandomKeyMutation implements MutationPolicy {
|
|||
*
|
||||
* @throws MathIllegalArgumentException if <code>original</code> 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());
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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());
|
||||
|
|
|
@ -60,7 +60,7 @@ public class UniformCrossover<T> 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<T> 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<T> implements CrossoverPolicy {
|
|||
* @throws DimensionMismatchException if the length of the two chromosomes is different
|
||||
*/
|
||||
private ChromosomePair mate(final AbstractListChromosome<T> first,
|
||||
final AbstractListChromosome<T> second) {
|
||||
final AbstractListChromosome<T> second) throws DimensionMismatchException {
|
||||
final int length = first.getLength();
|
||||
if (length != second.getLength()) {
|
||||
throw new DimensionMismatchException(second.getLength(), length);
|
||||
|
|
Loading…
Reference in New Issue