SimpleReplacementStrategy extends JoinOperator { public void perform() { if(this.incomingPopulation.size()>this.replacementOperation.getReplacementSize()) { int size = this.incomingPopulation.size()-this.replacementOperation.getReplacementSize(); this.replacementOperation.doOperation(this.incomingPopulation.getAll(), size); } if(this.incomingPopulation.size() == this.population.size()) { this.population.clear(); } else { this.replacementOperation.doOperation(this.population.getAll()); } this.population.addAll(this.incomingPopulation); this.incomingPopulation.clear(); this.increaseAge(this.population.getAll()); } SimpleReplacementStrategy(RandomNumberGenerator rng, Population incPop, ReplacementOperation rO);  SimpleReplacementStrategy(); void perform(); void setOperation(Operation op); Operation getOperation();  }
@Test public void testPerform() { System.out.println("* SimpleReplacementStrategy: perform"); int populationSize = 10; int incomingpopulationSize = 15; List<Individual> operands = new ArrayList<Individual>(); for (int i = incomingpopulationSize; i > 0; i--) { operands.add(IndividualMaker.makeIndividual()); operands.get(incomingpopulationSize-i).getFitness().setDouble(i+populationSize/2); } Population incommingPopulation = new SimplePopulation(); incommingPopulation.addAll(operands); List<Individual> operands2 = new ArrayList<Individual>(); for (int i = populationSize; i > 0; i--) { operands2.add(IndividualMaker.makeIndividual()); operands2.get(populationSize-i).getFitness().setDouble(i); } Population population = new SimplePopulation(); population.addAll(operands2); SimpleReplacementStrategy instance = new SimpleReplacementStrategy(); instance.setOperation(new ReplacementOperation(populationSize)); instance.setPopulation(population); instance.setIncomingPopulation(incommingPopulation); instance.perform(); assertEquals(instance.population.size(), populationSize); assertEquals(instance.incomingPopulation.size(), 0); assertEquals(instance.population.get(0).getAge(),1); Individual[] pA = instance.population.getAll().toArray(new Individual[0]); Individual[] ipA = operands.subList(incomingpopulationSize-populationSize,incomingpopulationSize).toArray(new Individual[0]); assertArrayEquals(pA, ipA); populationSize = 10; incomingpopulationSize = 10; operands = new ArrayList<Individual>(); for (int i = incomingpopulationSize; i > 0; i--) { operands.add(IndividualMaker.makeIndividual()); operands.get(incomingpopulationSize-i).getFitness().setDouble(i+populationSize/2); } incommingPopulation = new SimplePopulation(); incommingPopulation.addAll(operands); operands2 = new ArrayList<Individual>(); for (int i = populationSize; i > 0; i--) { operands2.add(IndividualMaker.makeIndividual()); operands2.get(populationSize-i).getFitness().setDouble(i); } population = new SimplePopulation(); population.addAll(operands2); instance = new SimpleReplacementStrategy(); instance.setOperation(new ReplacementOperation(populationSize)); instance.setPopulation(population); instance.setIncomingPopulation(incommingPopulation); instance.perform(); assertEquals(instance.population.size(), populationSize); assertEquals(instance.incomingPopulation.size(), 0); assertEquals(instance.population.get(0).getAge(),1); pA = instance.population.getAll().toArray(new Individual[0]); ipA = operands.subList(incomingpopulationSize-populationSize,incomingpopulationSize).toArray(new Individual[0]); assertArrayEquals(pA, ipA); populationSize = 15; incomingpopulationSize = 10; operands = new ArrayList<Individual>(); for (int i = incomingpopulationSize; i > 0; i--) { operands.add(IndividualMaker.makeIndividual()); operands.get(incomingpopulationSize-i).getFitness().setDouble(i+populationSize/2); } incommingPopulation = new SimplePopulation(); incommingPopulation.addAll(operands); operands2 = new ArrayList<Individual>(); for (int i = populationSize; i > 0; i--) { operands2.add(IndividualMaker.makeIndividual()); operands2.get(populationSize-i).getFitness().setDouble(i); } population = new SimplePopulation(); population.addAll(operands2); instance = new SimpleReplacementStrategy(); instance.setOperation(new ReplacementOperation(populationSize)); instance.setPopulation(population); instance.setIncomingPopulation(incommingPopulation); instance.perform(); assertEquals(instance.population.size(), incomingpopulationSize); assertEquals(instance.incomingPopulation.size(), 0); assertEquals(instance.population.get(0).getAge(),1); pA = instance.population.getAll().toArray(new Individual[0]); ipA = operands.subList(0,incomingpopulationSize).toArray(new Individual[0]); assertArrayEquals(pA, ipA); }