Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from FedorSmirnov89/master
Adjustment of Stagnation Restart
- Loading branch information
Showing
3 changed files
with
126 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
opendse-optimization/src/test/java/net/sf/opendse/optimization/StagnationRestartTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package net.sf.opendse.optimization; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
import org.junit.Test; | ||
import org.opt4j.core.Individual; | ||
import org.opt4j.core.Objectives; | ||
import org.opt4j.core.optimizer.Population; | ||
|
||
public class StagnationRestartTest { | ||
|
||
class MockIndividual extends Individual { | ||
public MockIndividual(boolean dominated) { | ||
super(); | ||
setObjectives(dominated ? new MockDominatedObjectives() : new MockDominantObjectives()); | ||
} | ||
} | ||
|
||
class MockPopulation extends Population { | ||
|
||
} | ||
|
||
class MockDominatedObjectives extends Objectives { | ||
@Override | ||
public boolean dominates(Objectives opponent) { | ||
return false; | ||
} | ||
} | ||
|
||
class MockDominantObjectives extends Objectives { | ||
@Override | ||
public boolean dominates(Objectives opponent) { | ||
return true; | ||
} | ||
} | ||
|
||
@Test | ||
public void testNoStagnation() { | ||
Population population = new Population(); | ||
Individual indi1 = new MockIndividual(false); | ||
Individual indi2 = new MockIndividual(false); | ||
population.add(indi1); | ||
population.add(indi2); | ||
StagnationRestart restart = new StagnationRestart(population, 20); | ||
restart.archive.add(indi1); | ||
restart.archive.add(indi2); | ||
assertEquals(0, restart.lastUpdate); | ||
restart.iterationComplete(21); | ||
assertEquals(21, restart.lastUpdate); | ||
assertFalse(population.isEmpty()); | ||
assertFalse(restart.archive.isEmpty()); | ||
} | ||
|
||
@Test | ||
public void testStagnation() { | ||
Population population = new Population(); | ||
Individual indi1 = new MockIndividual(true); | ||
Individual indi2 = new MockIndividual(true); | ||
population.add(indi1); | ||
population.add(indi2); | ||
StagnationRestart restart = new StagnationRestart(population, 20); | ||
restart.archive.add(indi1); | ||
restart.archive.add(indi2); | ||
assertEquals(0, restart.lastUpdate); | ||
restart.iterationComplete(5); | ||
assertEquals(0, restart.lastUpdate); | ||
assertFalse(population.isEmpty()); | ||
assertFalse(restart.archive.isEmpty()); | ||
restart.iterationComplete(21); | ||
assertEquals(21, restart.lastUpdate); | ||
assertTrue(population.isEmpty()); | ||
assertTrue(restart.archive.isEmpty()); | ||
} | ||
|
||
} |