Commit 6ea7dc46 authored by Fabian Becker's avatar Fabian Becker

More population tests

refs #53
parent 3133652c
......@@ -883,7 +883,7 @@ public class Population extends ArrayList<AbstractEAIndividual> implements Popul
}
/**
* Resets the fitnes to the maximum possible value for the given individual.
* Resets the fitness to the maximum possible value for the given individual.
*
* @param indy an individual whose fitness will be reset
*/
......@@ -2091,7 +2091,7 @@ public class Population extends ArrayList<AbstractEAIndividual> implements Popul
*/
public double[] getCenter() {
if (size() == 0) {
EVAERROR.errorMsgOnce("Invalid pop size in DistractingPopulation:getCenter!");
return null;
}
double[] centerPos = AbstractEAIndividual.getDoublePosition(getEAIndividual(0));
for (int i = 1; i < size(); i++) {
......@@ -2123,7 +2123,7 @@ public class Population extends ArrayList<AbstractEAIndividual> implements Popul
*/
public double[] getCenterWeighted(double[] weights) {
if (size() == 0 || (weights.length > size()) || (weights.length == 0)) {
EVAERROR.errorMsgOnce("Invalid pop size in DistractingPopulation:getCenterWeighted!");
return null;
}
double[] centerPos = AbstractEAIndividual.getDoublePosition(getEAIndividual(0));
Mathematics.svMult(weights[0], centerPos, centerPos);
......
package eva2.optimization.population;
import com.sun.org.apache.bcel.internal.generic.POP;
import eva2.optimization.individuals.AbstractEAIndividual;
import eva2.optimization.individuals.ESIndividualDoubleData;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
public class PopulationTest {
Population emptyPopulation;
......@@ -21,11 +23,6 @@ public class PopulationTest {
}
@Test
public void testPutData() throws Exception {
}
@Test
public void testGetData() throws Exception {
// Simple data
......@@ -314,12 +311,22 @@ public class PopulationTest {
@Test
public void testGetCenter() throws Exception {
assertNull(emptyPopulation.getCenter());
for(int i = 0; i < 10; i++) {
ESIndividualDoubleData indy = mock(ESIndividualDoubleData.class);
when(indy.getDGenotype()).thenReturn(new double[]{i, i, i, i, i});
emptyPopulation.add(indy);
}
assertArrayEquals(new double[]{
4.5, 4.5, 4.5, 4.5, 4.5
}, emptyPopulation.getCenter(), 0.0);
}
@Test
public void testGetCenterIndy() throws Exception {
}
@Test
......@@ -334,7 +341,15 @@ public class PopulationTest {
@Test
public void testGetFitSum() throws Exception {
for(int i = 0; i < 10; i++) {
ESIndividualDoubleData indy = mock(ESIndividualDoubleData.class);
when(indy.getFitness(0)).thenReturn((double)i);
when(indy.getFitness(1)).thenReturn((double)i*2);
emptyPopulation.add(indy);
}
assertEquals(45.0, emptyPopulation.getFitSum(0), 0.0);
assertEquals(90.0, emptyPopulation.getFitSum(1), 0.0);
}
@Test
......@@ -354,7 +369,12 @@ public class PopulationTest {
@Test
public void testGetFreeSlots() throws Exception {
assertEquals(10, emptyPopulation.getFreeSlots());
ESIndividualDoubleData indy = mock(ESIndividualDoubleData.class);
emptyPopulation.add(indy);
assertEquals(9, emptyPopulation.getFreeSlots());
}
@Test
......@@ -367,6 +387,19 @@ public class PopulationTest {
}
@Test
public void testPutDataAllIndies() throws Exception {
ESIndividualDoubleData indy1 = mock(ESIndividualDoubleData.class);
ESIndividualDoubleData indy2 = mock(ESIndividualDoubleData.class);
emptyPopulation.add(indy1);
emptyPopulation.add(indy2);
emptyPopulation.putDataAllIndies("test", 12);
verify(indy1).putData("test", 12);
verify(indy2).putData("test", 12);
}
@Test
public void testFilterByFitness() throws Exception {
ESIndividualDoubleData indy1, indy2, indy3;
......
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