Exemplo n.º 1
0
  @Override
  public void performIteration(NichingAlgorithm alg) {
    Preconditions.checkState(
        alg.getOptimisationProblem() instanceof DeratingOptimisationProblem,
        "DeratingNichePSO can only be used with DeratingOptimisationProblem.");
    DeratingOptimisationProblem problem =
        (DeratingOptimisationProblem) alg.getOptimisationProblem();

    List<PopulationBasedAlgorithm> subswarms =
        List.<PopulationBasedAlgorithm>iterableList(alg.getPopulations());
    subswarms =
        onMainSwarm(Algorithms.<PopulationBasedAlgorithm>initialise())
            .andThen(phase1(alg))
            .andThen(onSubswarms(clearDeratingSolutions(problem)))
            .andThen(phase2(alg))
            .andThen(joinAndMerge(alg, subswarms))
            .f(
                NichingSwarms.of(
                    alg.getMainSwarm(), Collections.<PopulationBasedAlgorithm>emptyList()))
            ._2();

    problem.clearSolutions();
    problem.addSolutions(
        subswarms
            .map(Solutions.getPosition().o(Algorithms.<PopulationBasedAlgorithm>getBestSolution()))
            .toCollection());
    alg.setPopulations(Lists.newLinkedList(subswarms.toCollection()));
    alg.getMainSwarm().setOptimisationProblem(problem);
    // dont need to set the main swarm because it gets reinitialised
  }
  @Test
  public void testCreation() {
    Particle p1 =
        NichingFunctionsTest.createParticle(new MinimisationFitness(3.0), Vector.of(0.0, 1.0));
    Particle p2 =
        NichingFunctionsTest.createParticle(new MinimisationFitness(2.0), Vector.of(1.0, 1.0));
    Particle p3 =
        NichingFunctionsTest.createParticle(new MinimisationFitness(1.0), Vector.of(2.0, 2.0));

    PSO pso = new PSO();
    pso.getTopology().addAll(Arrays.asList(p1, p2, p3));

    ClosestNeighbourNicheCreationStrategy creator = new ClosestNeighbourNicheCreationStrategy();
    creator.setSwarmBehavior(new ParticleBehavior());
    NichingSwarms swarms =
        creator.f(NichingSwarms.of(pso, List.<PopulationBasedAlgorithm>nil()), p1);

    Assert.assertEquals(1, swarms._1().getTopology().size());
    Assert.assertEquals(
        Vector.of(2.0, 2.0), swarms._1().getTopology().get(0).getCandidateSolution());
    Assert.assertEquals(2, swarms._2().head().getTopology().size());
    Assert.assertEquals(
        Vector.of(0.0, 1.0), swarms._2().head().getTopology().get(0).getCandidateSolution());
    Assert.assertEquals(
        Vector.of(1.0, 1.0), swarms._2().head().getTopology().get(1).getCandidateSolution());
  }