PopulationUtils { public static Population concatenate(Population[] populations, PopulationFactory populationFactory){ if (populations == null){ throw new OptimizationException("input Population[] cannot be null"); } return concatenate(populations, populationFactory, populations.length); }  static Population concatenate(Population[] populations, PopulationFactory populationFactory); static Population concatenate(Population[] populations, PopulationFactory populationFactory, int maxIndexExclusive); static Population[] appendPopulation(Population[] populations, Population populationToAppend, boolean appendAtTheEnd); static int sumSizes(Population[] populations); static int sumSizes(Population[] populations, int maxIndexExclusive);  }
@Test public void testConcatenate1() throws Exception { SimpleIndividual[] ind1 = new SimpleIndividual[] {new SimpleIndividual(1, 1.0, null), new SimpleIndividual(2, 3.0, null), new SimpleIndividual(5, 3.0, null)}; SimpleIndividual[] ind2 = new SimpleIndividual[] {new SimpleIndividual(10, 0.0, null), new SimpleIndividual(2, 2.0, null)}; Individual[] indOut = PopulationUtils.concatenate(new Population[]{new SimplePopulation(ind1), new SimplePopulation(ind2), new SimplePopulation(ind1)}, new SimplePopulationFactory()).getIndividuals(); assertTrue(indOut[0] == ind1[0]); assertTrue(indOut[1] == ind1[1]); assertTrue(indOut[2] == ind1[2]); assertTrue(indOut[3] == ind2[0]); assertTrue(indOut[4] == ind2[1]); assertTrue(indOut[5] == ind1[0]); assertTrue(indOut[6] == ind1[1]); assertTrue(indOut[7] == ind1[2]); assertTrue(indOut.length == ind1.length + ind1.length + ind2.length); }
@Test public void testConcatenate2() throws Exception { SimpleIndividual[] ind1 = new SimpleIndividual[] {new SimpleIndividual(1, 1.0, null), new SimpleIndividual(2, 3.0, null), new SimpleIndividual(5, 3.0, null)}; SimpleIndividual[] ind2 = new SimpleIndividual[] {new SimpleIndividual(10, 0.0, null), new SimpleIndividual(2, 2.0, null)}; Individual[] indOut = PopulationUtils.concatenate(new Population[]{new SimplePopulation(ind1), new SimplePopulation(ind2), new SimplePopulation(ind1)}, new SimplePopulationFactory(), 2).getIndividuals(); assertTrue(indOut[0] == ind1[0]); assertTrue(indOut[1] == ind1[1]); assertTrue(indOut[2] == ind1[2]); assertTrue(indOut[3] == ind2[0]); assertTrue(indOut[4] == ind2[1]); assertTrue(indOut.length == ind1.length + ind2.length); }
@Test public void testConcatenate3() throws Exception { SimpleIndividual[] ind1 = new SimpleIndividual[] {new SimpleIndividual(1, 1.0, null), new SimpleIndividual(2, 3.0, null), new SimpleIndividual(5, 3.0, null)}; SimplePopulation pop1 = new SimplePopulation(ind1); Individual[] indOut = PopulationUtils.concatenate(new Population[]{pop1}, new SimplePopulationFactory()).getIndividuals(); assertTrue(indOut[0] == ind1[0]); assertTrue(indOut[1] == ind1[1]); assertTrue(indOut[2] == ind1[2]); assertNotSame(pop1.getIndividuals(), indOut); assertNotSame(ind1, indOut); assertTrue(indOut.length == ind1.length); }
@Test public void testConcatenate4() throws Exception { SimplePopulation pop1 = new SimplePopulation(); Individual[] indOut = PopulationUtils.concatenate(new Population[]{pop1}, new SimplePopulationFactory()).getIndividuals(); assertTrue(pop1.getIndividuals() == indOut || indOut.length == 0); }
@Test public void testConcatenate5() throws Exception { SimpleIndividual[] ind1 = new SimpleIndividual[] {new SimpleIndividual(1, 1.0, null), new SimpleIndividual(2, 3.0, null), new SimpleIndividual(5, 3.0, null)}; SimplePopulation pop1 = new SimplePopulation(ind1); Individual[] indOut = PopulationUtils.concatenate(new Population[]{new SimplePopulation(), pop1}, new SimplePopulationFactory()).getIndividuals(); assertTrue(indOut[0] == ind1[0]); assertTrue(indOut[1] == ind1[1]); assertTrue(indOut[2] == ind1[2]); assertNotSame(pop1.getIndividuals(), indOut); assertNotSame(ind1, indOut); assertTrue(indOut.length == ind1.length); }