Ejemplo n.º 1
0
 @Before
 public void setUp() {
   gei = IndividualMaker.makeIndividual();
   PictureCopy instance = new PictureCopy();
   assertEquals(true, instance != null);
 }
  /** Test of perform method, of class SimpleReplacementStrategy. */
  @Test
  public void testPerform() {
    System.out.println("* SimpleReplacementStrategy: perform");
    // population < incomingPopulation
    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);

    // population = incommingPopulation
    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);

    // population > incommingPopulation
    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);
  }