@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 testLift() { final Promise<Integer> p1 = promise(3), p2 = promise(4); final Promise<Integer> p3 = lift(addInts).f(p1, p2); assertEquals(7, (int) p3.claim()); final List<Integer> list = list(1, 2, 3, 4); final List<Promise<Integer>> pList = list.map(Promises.<Integer>promise()); Promise<Integer> p4 = pList.foldLeft(lift(addInts), promise(0)); assertEquals(10, (int) p4.claim()); assertEquals( 10, (int) Promise.foldRight(Strategies.sequential, curry(addInts), 0).f(list).claim()); }
public void performIteration(final TuningAlgorithm alg) { final List<Vector> parameterList = alg.getParameterList(); // TODO: deal with maximisation problems results = results.snoc( parameterList.map( new F<Vector, OptimisationSolution>() { @Override public OptimisationSolution f(Vector a) { return new OptimisationSolution(a, alg.evaluate(a)); } })); // (+1 because iterations start at 0) if (alg.getIterations() + 1 >= minProblems.getParameter() && parameterList.length() != 1) { List<List<Double>> data = results.map( List.<OptimisationSolution, Double>map_().f(getFitness().andThen(getValue()))); P2<Double, Double> friedman = StatsTests.friedman(0.05, data); if (friedman._1() > friedman._2()) { final List<Integer> indexes = StatsTests.postHoc(0.05, friedman._1(), data); alg.setParameterList(indexes.map(flip(Utils.<Vector>index()).f(parameterList))); results = results.map( new F<List<OptimisationSolution>, List<OptimisationSolution>>() { @Override public List<OptimisationSolution> f(final List<OptimisationSolution> a) { return indexes.map(flip(Utils.<OptimisationSolution>index()).f(a)); } }); } } }
/** @param pso The {@link PSO} to have an iteration applied. */ @Override public void performIteration(final PSO pso) { final fj.data.List<Particle> topology = pso.getTopology(); this.calculateAbsoluteAverages(pso); this.updateInertia(pso); final F<Particle, Particle> first = new F<Particle, Particle>() { @Override public Particle f(Particle current) { WeightedInertiaVelocityProvider wp = (WeightedInertiaVelocityProvider) current.getVelocityProvider(); // put wp.setInertiaWeight(inertiaWeight); current.updateVelocity(); current.updatePosition(); boundaryConstraint.enforce(current); return current; } }; final F<Particle, Particle> second = new F<Particle, Particle>() { public Particle f(Particle current) { current.calculateFitness(); for (Particle other : pso.getNeighbourhood().f(topology, current)) { if (current .getSocialFitness() .compareTo(other.getNeighbourhoodBest().getSocialFitness()) > 0) { other.setNeighbourhoodBest(current); } } return current; } }; pso.setTopology(topology.map(first).map(second)); }
public static void main(final String[] args) { final List<Integer> a = list(1, 2, 3); final List<Integer> b = a.map(add.f(42)); listShow(intShow).println(b); // [43,44,45] }