Commit 1acc3650 authored by Fabian Becker's avatar Fabian Becker

Add tests for PhenotypeMetric

refs #53
parent b2c0ae94
......@@ -13,15 +13,11 @@ import java.util.BitSet;
@Description("This is a genotype based distance metric suited for binary data. The hamming distance is computed and normalized by chromosome length.")
public class GenotypeMetricBitSet implements InterfaceDistanceMetric, java.io.Serializable {
public GenotypeMetricBitSet() {
}
public GenotypeMetricBitSet(GenotypeMetricBitSet a) {
}
public GenotypeMetricBitSet() {}
@Override
public Object clone() {
return new GenotypeMetricBitSet(this);
return new GenotypeMetricBitSet();
}
/**
......
......@@ -16,15 +16,11 @@ public class PhenotypeMetric implements InterfaceDistanceMetric, java.io.Seriali
private static PhenotypeMetric pMetric = null;
private static GenotypeMetricBitSet bitMetric = null;
public PhenotypeMetric() {
}
public PhenotypeMetric(PhenotypeMetric a) {
}
public PhenotypeMetric() {}
@Override
public Object clone() {
return new PhenotypeMetric(this);
return new PhenotypeMetric();
}
private static int min(int a, int b, int c) {
......@@ -110,7 +106,6 @@ public class PhenotypeMetric implements InterfaceDistanceMetric, java.io.Seriali
r2 = ((InterfaceDataTypeInteger) indy2).getIntRange();
for (int i = 0; (i < d1.length) && (i < d2.length); i++) {
tmpResult += Math.pow(((d1[i] - r1[i][0]) / ((double) (r1[i][1] - r1[i][0]))) - ((d2[i] - r2[i][0]) / ((double) (r2[i][1] - r2[i][0]))), 2);
//tmpResult += Math.abs(d1[i] - d2[i])/((double)(r1[i][1]-r1[i][0]));
}
result += Math.sqrt(tmpResult);
}
......
package eva2.optimization.operator.distancemetric;
import eva2.optimization.individuals.AbstractEAIndividual;
import eva2.optimization.individuals.ESIndividualDoubleData;
import eva2.optimization.individuals.ESIndividualIntegerData;
import eva2.optimization.individuals.InterfaceDataTypeDouble;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class PhenotypeMetricTest {
private PhenotypeMetric metric;
@Before
public void setUp() throws Exception {
metric = new PhenotypeMetric();
}
@Test
public void testDistance() throws Exception {
// DOUBLE
ESIndividualDoubleData dindy1, dindy2;
dindy1 = mock(ESIndividualDoubleData.class);
when(dindy1.getDoubleData()).thenReturn(new double[]{1.0, 1.0, 1.0, 1.0, 1.0});
when(dindy1.getDoubleRange()).thenReturn(new double[][]{
{0.0, 10.0},{0.0, 10.0},{0.0, 10.0},{0.0, 10.0},{0.0, 10.0}
});
dindy2 = mock(ESIndividualDoubleData.class);
when(dindy2.getDoubleData()).thenReturn(new double[]{2.0, 2.0, 2.0, 2.0, 2.0});
when(dindy2.getDoubleRange()).thenReturn(new double[][]{
{0.0, 10.0},{0.0, 10.0},{0.0, 10.0},{0.0, 10.0},{0.0, 10.0}
});
// Should be zero for distance to itself
assertEquals(0.0, metric.distance(dindy1, dindy1), 0.0);
assertEquals(0.2236, metric.distance(dindy1, dindy2), 1E-4);
// INTEGER
ESIndividualIntegerData iindy1, iindy2;
iindy1 = mock(ESIndividualIntegerData.class);
when(iindy1.getIntegerData()).thenReturn(new int[]{1, 1, 1, 1, 1});
when(iindy1.getIntRange()).thenReturn(new int[][]{
{0, 10},{0, 10},{0, 10},{0, 10},{0, 10}
});
iindy2 = mock(ESIndividualIntegerData.class);
when(iindy2.getIntegerData()).thenReturn(new int[]{3, 3, 3, 3, 3});
when(iindy2.getIntRange()).thenReturn(new int[][]{
{0, 10},{0, 10},{0, 10},{0, 10},{0, 10}
});
// Should be zero for distance to itself
assertEquals(0.0, metric.distance(iindy1, iindy1), 0.0);
assertEquals(0.4472, metric.distance(iindy1, iindy2), 1E-4);
}
@Test
public void testNorm() throws Exception {
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment