More population tests

refs #53
This commit is contained in:
Fabian Becker 2015-12-22 16:42:55 +01:00
parent 3133652cd2
commit 6ea7dc46f0
2 changed files with 42 additions and 9 deletions

View File

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

View File

@ -1,12 +1,14 @@
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;