Simple test for ESIndividualDoubleData

refs #53
This commit is contained in:
Fabian Becker 2015-12-11 18:58:16 +01:00
parent 9d0b14becb
commit ada94fcb0a
2 changed files with 69 additions and 7 deletions

View File

@ -13,12 +13,15 @@ import eva2.tools.math.RNG;
import eva2.util.annotation.Description; import eva2.util.annotation.Description;
import eva2.util.annotation.Hidden; import eva2.util.annotation.Hidden;
import java.util.logging.Logger;
/** /**
* This individual uses a real-valued genotype to code for double values. * This individual uses a real-valued genotype to code for double values.
*/ */
@Description(value = "This is an ES individual suited to optimize 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 { 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[] genotype;
private double[] phenotype; private double[] phenotype;
private double[][] range; private double[][] range;
@ -46,7 +49,7 @@ public class ESIndividualDoubleData extends AbstractEAIndividual implements Inte
this.genotype[i] = individual.genotype[i]; this.genotype[i] = individual.genotype[i];
this.range[i][0] = individual.range[i][0]; this.range[i][0] = individual.range[i][0];
this.range[i][1] = individual.range[i][1]; this.range[i][1] = individual.range[i][1];
} };
// cloning the members of AbstractEAIndividual // cloning the members of AbstractEAIndividual
this.age = individual.age; 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 * 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 @Override
public void setDoubleDataLength(int length) { public void setDoubleDataLength(int length) {
@ -154,7 +157,7 @@ public class ESIndividualDoubleData extends AbstractEAIndividual implements Inte
@Hidden @Hidden
public void setDoubleRange(double[][] range) { public void setDoubleRange(double[][] range) {
if (range.length != this.range.length) { 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)"); + this.range.length + "!\n Use method setDoubleDataLength first! (ESIndividualDoubleData:setDoubleRange)");
} }
for (int i = 0; ((i < this.range.length) && (i < range.length)); i++) { 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, * This method allows you to set the phenotype double data. To change the genotype,
* use SetDoubleDataLamarckian(). * use #setDoubleDataLamarckian().
* *
* @param doubleData The new double data. * @param doubleData The new double data.
*/ */
@ -226,7 +229,6 @@ public class ESIndividualDoubleData extends AbstractEAIndividual implements Inte
*/ */
@Override @Override
public void setDoubleGenotype(double[] doubleData) { public void setDoubleGenotype(double[] doubleData) {
// this.setDoublePhenotype(doubleData);
this.setDoublePhenotype(null); // tag it as invalid this.setDoublePhenotype(null); // tag it as invalid
this.genotype = new double[doubleData.length]; this.genotype = new double[doubleData.length];
System.arraycopy(doubleData, 0, this.genotype, 0, 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 * This method will return a string description of the GAIndividal
* noteably the Genotype. * notably the Genotype.
* *
* @return A descriptive string * @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. * name to the current object.
* *
* @return The name. * @return The name.

View File

@ -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));
}
}