StagnationRestart implements IndividualSetListener, OptimizerIterationListener { @Override public void iterationComplete(int iteration) { this.iteration = iteration; for (Individual in0 : population) { for (Individual in1 : archive) { if (in0.getObjectives().dominates(in1.getObjectives())) { lastUpdate = iteration; } } } archive.update(population); if (iteration - lastUpdate > diff) { lastUpdate = iteration; archive.clear(); population.clear(); } } @Inject  StagnationRestart(Population population,
			@Constant(value = "maximalNumberStagnatingGenerations", namespace = StagnationRestart.class) int diff); @Override void iterationComplete(int iteration); @Override void individualAdded(IndividualSet collection, Individual individual); @Override void individualRemoved(IndividualSet collection, Individual individual);  }
@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()); }