From 58133c861282c9107e9b62ca41bf97b93af4c8b7 Mon Sep 17 00:00:00 2001 From: Thomas Neidhart Date: Tue, 24 Jan 2012 10:17:13 +0000 Subject: [PATCH] Fixed localized exception messages according to review of Gilles JIRA: MATH-575 git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1235197 13f79535-47bb-0310-9956-ffa450edef68 --- .../math/exception/util/LocalizedFormats.java | 13 +++----- .../genetics/ElitisticListPopulation.java | 2 +- .../math/genetics/FixedGenerationCount.java | 4 +-- .../math/genetics/GeneticAlgorithm.java | 4 +-- .../math/genetics/OnePointCrossover.java | 10 +++---- .../commons/math/genetics/RandomKey.java | 30 +++++++++++-------- 6 files changed, 30 insertions(+), 33 deletions(-) diff --git a/src/main/java/org/apache/commons/math/exception/util/LocalizedFormats.java b/src/main/java/org/apache/commons/math/exception/util/LocalizedFormats.java index f6985f096..2a5436752 100644 --- a/src/main/java/org/apache/commons/math/exception/util/LocalizedFormats.java +++ b/src/main/java/org/apache/commons/math/exception/util/LocalizedFormats.java @@ -78,6 +78,7 @@ public enum LocalizedFormats implements Localizable { CONTRACTION_CRITERIA_SMALLER_THAN_ONE("contraction criteria smaller than one ({0}). This would lead to a never ending loop of expansion and contraction as an internal storage array length equal to the number of elements would satisfy the contraction criteria."), CONVERGENCE_FAILED("convergence failed"), /* keep */ CROSSING_BOUNDARY_LOOPS("some outline boundary loops cross each other"), + CROSSOVER_RATE("crossover rate ({0})"), CUMULATIVE_PROBABILITY_RETURNED_NAN("Cumulative probability function returned NaN for argument {0} p = {1}"), DIFFERENT_ROWS_LENGTHS("some rows have length {0} while others have length {1}"), DIFFERENT_ORIG_AND_PERMUTED_DATA("original and permuted data must contain the same elements"), @@ -88,6 +89,7 @@ public enum LocalizedFormats implements Localizable { DISCRETE_CUMULATIVE_PROBABILITY_RETURNED_NAN("Discrete cumulative probability function returned NaN for argument {0}"), DISTRIBUTION_NOT_LOADED("distribution not loaded"), DUPLICATED_ABSCISSA("Abscissa {0} is duplicated at both indices {1} and {2}"), + ELITISM_RATE("elitism rate ({0})"), EMPTY_CLUSTER_IN_K_MEANS("empty cluster in k-means"), EMPTY_POLYNOMIALS_COEFFICIENTS_ARRAY("empty polynomials coefficients array"), /* keep */ EMPTY_SELECTED_COLUMN_INDEX_ARRAY("empty selected column index array"), @@ -136,8 +138,7 @@ public enum LocalizedFormats implements Localizable { INVALID_BINARY_DIGIT("invalid binary digit: {0}"), INVALID_BINARY_CHROMOSOME("binary mutation works on BinaryChromosome only"), INVALID_BRACKETING_PARAMETERS("invalid bracketing parameters: lower bound={0}, initial={1}, upper bound={2}"), - INVALID_CROSSOVER_CHROMOSOME_LENGTH("both chromosomes must have same lengths: ({0}) ({1})"), - INVALID_FIXED_LENGTH_CHROMOSOME("one-point crossover works only on instances of AbstractListChromosome"), + INVALID_FIXED_LENGTH_CHROMOSOME("one-point crossover only works with fixed-length chromosomes"), INVALID_INTERVAL_INITIAL_VALUE_PARAMETERS("invalid interval, initial value parameters: lower={0}, initial={1}, upper={2}"), INVALID_ITERATIONS_LIMITS("invalid iteration limits: min={0}, max={1}"), INVALID_MAX_ITERATIONS("bad value for maximum iterations number: {0}"), @@ -158,6 +159,7 @@ public enum LocalizedFormats implements Localizable { MAX_ITERATIONS_EXCEEDED("maximal number of iterations ({0}) exceeded"), MINIMAL_STEPSIZE_REACHED_DURING_INTEGRATION("minimal step size ({1,number,0.00E00}) reached, integration needs {0,number,0.00E00}"), MISMATCHED_LOESS_ABSCISSA_ORDINATE_ARRAYS("Loess expects the abscissa and ordinate arrays to be of the same size, but got {0} abscissae and {1} ordinatae"), + MUTATION_RATE("mutation rate ({0})"), NAN_ELEMENT_AT_INDEX("element {0} is NaN"), NAN_VALUE_CONVERSION("cannot convert NaN value"), NEGATIVE_BRIGHTNESS_EXPONENT("brightness exponent should be positive or null, but got {0}"), @@ -274,9 +276,6 @@ public enum LocalizedFormats implements Localizable { OUT_OF_BOUND_SIGNIFICANCE_LEVEL("out of bounds significance level {0}, must be between {1} and {2}"), SIGNIFICANCE_LEVEL("significance level ({0})"), /* keep */ OUT_OF_ORDER_ABSCISSA_ARRAY("the abscissae array must be sorted in a strictly increasing order, but the {0}-th element is {1} whereas {2}-th is {3}"), - OUT_OF_RANGE_CROSSOVER_RATE("crossover rate ({0}) out of [{1}, {2}] range"), - OUT_OF_RANGE_ELITISM_RATE("elitism rate ({0}) out of [{1}, {2}] range"), - OUT_OF_RANGE_MUTATION_RATE("mutation rate ({0}) out of [{1}, {2}] range"), OUT_OF_RANGE_ROOT_OF_UNITY_INDEX("out of range root of unity index {0} (must be in [{1};{2}])"), OUT_OF_RANGE("out of range"), /* keep */ OUT_OF_RANGE_SIMPLE("{0} out of [{1}, {2}] range"), /* keep */ @@ -315,7 +314,6 @@ public enum LocalizedFormats implements Localizable { TOO_MUCH_CANCELLATION("too much cancellation in a denominator"), TOO_MANY_REGRESSORS("too many regressors ({0}) specified, only {1} in the model"), TOO_SMALL_COST_RELATIVE_TOLERANCE("cost relative tolerance is too small ({0}), no further reduction in the sum of squares is possible"), - TOO_SMALL_GENERATION_COUNT("number of generations ({0}) must be >= {1}"), TOO_SMALL_INTEGRATION_INTERVAL("too small integration interval: length = {0}"), TOO_SMALL_ORTHOGONALITY_TOLERANCE("orthogonality tolerance is too small ({0}), solution is orthogonal to the jacobian"), TOO_SMALL_PARAMETERS_RELATIVE_TOLERANCE("parameters relative tolerance is too small ({0}), no further improvement in the approximate solution is possible"), @@ -349,9 +347,6 @@ public enum LocalizedFormats implements Localizable { WEIGHT_AT_LEAST_ONE_NON_ZERO("weigth array must contain at least one non-zero value"), WRONG_BLOCK_LENGTH("wrong array shape (block length = {0}, expected {1})"), WRONG_NUMBER_OF_POINTS("{0} points are required, got only {1}"), - WRONG_SEQUENCE_LENGTH_RANDOMKEY("length of sequence for decoding ({0}) has to be equal to the RandomKey length ({1})"), - WRONG_REPR_AND_SREPR_SIZE("size of representation and sorted representation must be equals: {0} != {1}"), - WRONG_ORIG_AND_PERMUTED_SIZE("original and permuted data must have same length: {0} != {1}"), NUMBER_OF_POINTS("number of points ({0})"), /* keep */ ZERO_DENOMINATOR("denominator must be different from 0"), ZERO_DENOMINATOR_IN_FRACTION("zero denominator in fraction {0}/{1}"), diff --git a/src/main/java/org/apache/commons/math/genetics/ElitisticListPopulation.java b/src/main/java/org/apache/commons/math/genetics/ElitisticListPopulation.java index a636f9f5a..ff687dc2d 100644 --- a/src/main/java/org/apache/commons/math/genetics/ElitisticListPopulation.java +++ b/src/main/java/org/apache/commons/math/genetics/ElitisticListPopulation.java @@ -94,7 +94,7 @@ public class ElitisticListPopulation extends ListPopulation { */ public void setElitismRate(final double elitismRate) { if (elitismRate < 0 || elitismRate > 1) { - throw new OutOfRangeException(LocalizedFormats.OUT_OF_RANGE_ELITISM_RATE, elitismRate, 0, 1); + throw new OutOfRangeException(LocalizedFormats.ELITISM_RATE, elitismRate, 0, 1); } this.elitismRate = elitismRate; } diff --git a/src/main/java/org/apache/commons/math/genetics/FixedGenerationCount.java b/src/main/java/org/apache/commons/math/genetics/FixedGenerationCount.java index 90b17a063..4bcb22ac5 100644 --- a/src/main/java/org/apache/commons/math/genetics/FixedGenerationCount.java +++ b/src/main/java/org/apache/commons/math/genetics/FixedGenerationCount.java @@ -17,7 +17,6 @@ package org.apache.commons.math.genetics; import org.apache.commons.math.exception.NumberIsTooSmallException; -import org.apache.commons.math.exception.util.LocalizedFormats; /** * Stops after a fixed number of generations. Each time @@ -44,8 +43,7 @@ public class FixedGenerationCount implements StoppingCondition { */ public FixedGenerationCount(final int maxGenerations) { if (maxGenerations <= 0) { - throw new NumberIsTooSmallException(LocalizedFormats.TOO_SMALL_GENERATION_COUNT, - maxGenerations, 1, true); + throw new NumberIsTooSmallException(maxGenerations, 1, true); } this.maxGenerations = maxGenerations; } diff --git a/src/main/java/org/apache/commons/math/genetics/GeneticAlgorithm.java b/src/main/java/org/apache/commons/math/genetics/GeneticAlgorithm.java index 8e23ed00f..41701ceff 100644 --- a/src/main/java/org/apache/commons/math/genetics/GeneticAlgorithm.java +++ b/src/main/java/org/apache/commons/math/genetics/GeneticAlgorithm.java @@ -72,11 +72,11 @@ public class GeneticAlgorithm { final SelectionPolicy selectionPolicy) { if (crossoverRate < 0 || crossoverRate > 1) { - throw new OutOfRangeException(LocalizedFormats.OUT_OF_RANGE_CROSSOVER_RATE, + throw new OutOfRangeException(LocalizedFormats.CROSSOVER_RATE, crossoverRate, 0, 1); } if (mutationRate < 0 || mutationRate > 1) { - throw new OutOfRangeException(LocalizedFormats.OUT_OF_RANGE_MUTATION_RATE, + throw new OutOfRangeException(LocalizedFormats.MUTATION_RATE, mutationRate, 0, 1); } this.crossoverPolicy = crossoverPolicy; diff --git a/src/main/java/org/apache/commons/math/genetics/OnePointCrossover.java b/src/main/java/org/apache/commons/math/genetics/OnePointCrossover.java index 933f887d3..096057205 100644 --- a/src/main/java/org/apache/commons/math/genetics/OnePointCrossover.java +++ b/src/main/java/org/apache/commons/math/genetics/OnePointCrossover.java @@ -19,6 +19,7 @@ package org.apache.commons.math.genetics; import java.util.ArrayList; import java.util.List; +import org.apache.commons.math.exception.DimensionMismatchException; import org.apache.commons.math.exception.MathIllegalArgumentException; import org.apache.commons.math.exception.util.LocalizedFormats; @@ -69,8 +70,8 @@ public class OnePointCrossover implements CrossoverPolicy { * @param second second parent (p2) * @return pair of two children (c1,c2) * @throws MathIllegalArgumentException iff one of the chromosomes is - * not an instance of {@link AbstractListChromosome} or the length - * of the two chromosomes is not equal + * not an instance of {@link AbstractListChromosome} + * @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) { @@ -87,14 +88,13 @@ public class OnePointCrossover implements 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 length of the two chromosomes is different + * @throws DimensionMismatchException if the length of the two chromosomes is different */ private ChromosomePair crossover(final AbstractListChromosome first, final AbstractListChromosome second) { int length = first.getLength(); if (length != second.getLength()) { - throw new MathIllegalArgumentException(LocalizedFormats.INVALID_CROSSOVER_CHROMOSOME_LENGTH, - length, second.getLength()); + throw new DimensionMismatchException(second.getLength(), length); } // array representations of the parents diff --git a/src/main/java/org/apache/commons/math/genetics/RandomKey.java b/src/main/java/org/apache/commons/math/genetics/RandomKey.java index 203cd8cad..9f5d840a6 100644 --- a/src/main/java/org/apache/commons/math/genetics/RandomKey.java +++ b/src/main/java/org/apache/commons/math/genetics/RandomKey.java @@ -22,6 +22,7 @@ import java.util.Collections; import java.util.Comparator; import java.util.List; +import org.apache.commons.math.exception.DimensionMismatchException; import org.apache.commons.math.exception.MathIllegalArgumentException; import org.apache.commons.math.exception.util.LocalizedFormats; @@ -118,21 +119,23 @@ public abstract class RandomKey extends AbstractListChromosome implem * @param representation representation of the permutation ([0,1] vector) * @param sortedRepr sorted representation * @return list with the sequence values permuted according to the representation + * @throws DimensionMismatchException iff the length of the sequence, + * representation or sortedRepr lists are not equal */ private static List decodeGeneric(final List sequence, List representation, final List sortedRepr) { int l = sequence.size(); + // the size of the three lists must be equal if (representation.size() != l) { - throw new MathIllegalArgumentException(LocalizedFormats.WRONG_SEQUENCE_LENGTH_RANDOMKEY, - l, representation.size()); + throw new DimensionMismatchException(representation.size(), l); } - if (representation.size() != sortedRepr.size()) { - throw new MathIllegalArgumentException(LocalizedFormats.WRONG_REPR_AND_SREPR_SIZE, - representation.size(), sortedRepr.size()); + if (sortedRepr.size() != l) { + throw new DimensionMismatchException(sortedRepr.size(), l); } - List reprCopy = new ArrayList (representation);// do not modify the orig. representation + // do not modify the original representation + List reprCopy = new ArrayList (representation); // now find the indices in the original repr and use them for permuting List res = new ArrayList (l); @@ -186,7 +189,8 @@ public abstract class RandomKey extends AbstractListChromosome implem for (double val : chromosomeRepresentation) { if (val < 0 || val > 1) { - throw new InvalidRepresentationException(LocalizedFormats.OUT_OF_RANGE_SIMPLE, val, 0, 1); + throw new InvalidRepresentationException(LocalizedFormats.OUT_OF_RANGE_SIMPLE, + val, 0, 1); } } } @@ -255,16 +259,16 @@ public abstract class RandomKey extends AbstractListChromosome implem * @param permutedData the data, somehow permuted * @return representation of a permutation corresponding to the permutation * originalData -> permutedData - * @throws IllegalArgumentException iff the permutedData and - * originalData contains different data + * @throws DimensionMismatchException iff the length of originalData + * and permutedData lists are not equal + * @throws MathIllegalArgumentException iff the permutedData and + * originalData lists contain different data */ public static List inducedPermutation(final List originalData, - final List permutedData) - throws IllegalArgumentException { + final List permutedData) { if (originalData.size() != permutedData.size()) { - throw new MathIllegalArgumentException(LocalizedFormats.WRONG_ORIG_AND_PERMUTED_SIZE, - originalData.size(), permutedData.size()); + throw new DimensionMismatchException(permutedData.size(), originalData.size()); } int l = originalData.size();