From ada94fcb0a7e96ec6c3b1107459d6df50c1728e3 Mon Sep 17 00:00:00 2001 From: Fabian Becker Date: Fri, 11 Dec 2015 18:58:16 +0100 Subject: [PATCH] Simple test for ESIndividualDoubleData refs #53 --- .../individuals/ESIndividualDoubleData.java | 16 ++--- .../ESIndividualDoubleDataTest.java | 60 +++++++++++++++++++ 2 files changed, 69 insertions(+), 7 deletions(-) create mode 100644 src/test/java/eva2/optimization/individuals/ESIndividualDoubleDataTest.java diff --git a/src/main/java/eva2/optimization/individuals/ESIndividualDoubleData.java b/src/main/java/eva2/optimization/individuals/ESIndividualDoubleData.java index 04492384..cec0dbf7 100644 --- a/src/main/java/eva2/optimization/individuals/ESIndividualDoubleData.java +++ b/src/main/java/eva2/optimization/individuals/ESIndividualDoubleData.java @@ -13,12 +13,15 @@ import eva2.tools.math.RNG; import eva2.util.annotation.Description; import eva2.util.annotation.Hidden; +import java.util.logging.Logger; + /** * This individual uses a real-valued genotype to code for double values. */ @Description(value = "This is an ES individual suited to optimize double values.") public class ESIndividualDoubleData extends AbstractEAIndividual implements InterfaceESIndividual, InterfaceDataTypeDouble, java.io.Serializable { + private static final Logger LOGGER = Logger.getLogger(ESIndividualDoubleData.class.getName()); private double[] genotype; private double[] phenotype; private double[][] range; @@ -46,7 +49,7 @@ public class ESIndividualDoubleData extends AbstractEAIndividual implements Inte this.genotype[i] = individual.genotype[i]; this.range[i][0] = individual.range[i][0]; this.range[i][1] = individual.range[i][1]; - } + }; // cloning the members of AbstractEAIndividual this.age = individual.age; @@ -108,7 +111,7 @@ public class ESIndividualDoubleData extends AbstractEAIndividual implements Inte /** * This method allows you to request a certain amount of double data * - * @param length The lenght of the double[] that is to be optimized + * @param length The length of the double[] that is to be optimized */ @Override public void setDoubleDataLength(int length) { @@ -154,7 +157,7 @@ public class ESIndividualDoubleData extends AbstractEAIndividual implements Inte @Hidden public void setDoubleRange(double[][] range) { if (range.length != this.range.length) { - System.out.println("Warning: Trying to set a range of length " + range.length + " to a vector of length " + LOGGER.warning("Trying to set a range of length " + range.length + " to a vector of length " + this.range.length + "!\n Use method setDoubleDataLength first! (ESIndividualDoubleData:setDoubleRange)"); } for (int i = 0; ((i < this.range.length) && (i < range.length)); i++) { @@ -209,7 +212,7 @@ public class ESIndividualDoubleData extends AbstractEAIndividual implements Inte /** * This method allows you to set the phenotype double data. To change the genotype, - * use SetDoubleDataLamarckian(). + * use #setDoubleDataLamarckian(). * * @param doubleData The new double data. */ @@ -226,7 +229,6 @@ public class ESIndividualDoubleData extends AbstractEAIndividual implements Inte */ @Override public void setDoubleGenotype(double[] doubleData) { -// this.setDoublePhenotype(doubleData); this.setDoublePhenotype(null); // tag it as invalid this.genotype = new double[doubleData.length]; System.arraycopy(doubleData, 0, this.genotype, 0, doubleData.length); @@ -271,7 +273,7 @@ public class ESIndividualDoubleData extends AbstractEAIndividual implements Inte /** * This method will return a string description of the GAIndividal - * noteably the Genotype. + * notably the Genotype. * * @return A descriptive string */ @@ -390,7 +392,7 @@ public class ESIndividualDoubleData extends AbstractEAIndividual implements Inte } /** - * This method allows the CommonJavaObjectEditorPanel to read the + * This method allows the ObjectEditorPanel to read the * name to the current object. * * @return The name. diff --git a/src/test/java/eva2/optimization/individuals/ESIndividualDoubleDataTest.java b/src/test/java/eva2/optimization/individuals/ESIndividualDoubleDataTest.java new file mode 100644 index 00000000..eefe2944 --- /dev/null +++ b/src/test/java/eva2/optimization/individuals/ESIndividualDoubleDataTest.java @@ -0,0 +1,60 @@ +package eva2.optimization.individuals; + +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class ESIndividualDoubleDataTest { + ESIndividualDoubleData indy; + + @Before + public void setUp() throws Exception { + indy = new ESIndividualDoubleData(); + } + + @Test + public void testEqualGenotypes() throws Exception { + // Genotype of two different types is not equal + assertFalse(indy.equalGenotypes(new ESIndividualBinaryData())); + + // Default init genotype should be equal + assertTrue(indy.equalGenotypes(new ESIndividualDoubleData())); + } + + @Test + public void testSize() throws Exception { + // Default individual has size 1 + assertEquals(1, indy.size()); + + // Returns length of genotype + indy.setDoubleGenotype(new double[]{1.0, 1.0, 1.0, 1.0, 1.0}); + assertEquals(5, indy.size()); + } + + @Test + public void testSetDoubleGenotype() throws Exception { + double[] genotype = {1.0, 1.0, 1.0, 1.0, 1.0}; + + indy.setDoubleGenotype(genotype); + + // Setting the genotype clears the phenotype and matches the genotype + assertArrayEquals(genotype, indy.getDoubleData(), 0.0); + + assertArrayEquals(genotype, indy.getDGenotype(), 0.0); + } + + @Test + public void testGetStringRepresentation() throws Exception { + assertEquals( + "ESIndividual coding double: (Fitness {0.0;}/SelProb{0.0;}) Value: [0.0; ]", + indy.getStringRepresentation() + ); + } + + @Test + public void testEqual() throws Exception { + // Is equal to itself + assertTrue(indy.equals(indy)); + } +} \ No newline at end of file