net.sourceforge.cilib.tuning.FRaceIterationStrategy Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cilib-library Show documentation
Show all versions of cilib-library Show documentation
A library of composable components enabling simpler Computational Intelligence
The newest version!
/** __ __
* _____ _/ /_/ /_ Computational Intelligence Library (CIlib)
* / ___/ / / / __ \ (c) CIRG @ UP
* / /__/ / / / /_/ / http://cilib.net
* \___/_/_/_/_.___/
*/
package net.sourceforge.cilib.tuning;
import static fj.Function.flip;
import fj.*;
import fj.data.List;
import static fj.data.List.*;
import static fj.data.List.iterableList;
import static fj.function.Doubles.add;
import static fj.Ord.*;
import net.sourceforge.cilib.algorithm.population.AbstractIterationStrategy;
import net.sourceforge.cilib.controlparameter.ConstantControlParameter;
import net.sourceforge.cilib.controlparameter.ControlParameter;
import net.sourceforge.cilib.math.Stats;
import net.sourceforge.cilib.math.StatsTests;
import net.sourceforge.cilib.problem.objective.Maximise;
import net.sourceforge.cilib.problem.objective.Minimise;
import net.sourceforge.cilib.problem.objective.Objective;
import net.sourceforge.cilib.problem.solution.OptimisationSolution;
import net.sourceforge.cilib.type.types.container.Vector;
import static net.sourceforge.cilib.util.functions.Fitnesses.getValue;
import static net.sourceforge.cilib.util.functions.Solutions.getFitness;
import net.sourceforge.cilib.util.functions.Utils;
public class FRaceIterationStrategy extends AbstractIterationStrategy {
private List> results;
private ControlParameter minProblems;
private ControlParameter minSolutions;
public FRaceIterationStrategy() {
this.minProblems = ConstantControlParameter.of(4.0);
this.minSolutions = ConstantControlParameter.of(2.0);
this.results = List.>nil();
}
public FRaceIterationStrategy(FRaceIterationStrategy copy) {
this.minProblems = copy.minProblems.getClone();
this.minSolutions = copy.minSolutions.getClone();
this.results = iterableList(copy.results);
}
@Override
public FRaceIterationStrategy getClone() {
return new FRaceIterationStrategy(this);
}
@Override
public void performIteration(final TuningAlgorithm alg) {
final List parameterList = alg.getParameterList();
final TuningProblem tuningProblem = (TuningProblem) alg.getOptimisationProblem();
//TODO: deal with maximisation problems
results = results.snoc(parameterList.map(new F() {
@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> data = results
.map(List.map_().f(getFitness()
.andThen(getValue())
.andThen(negateIfMaximising(tuningProblem.getObjective()))));
P2 friedman = StatsTests.friedman(0.05, data);
if (friedman._1() > friedman._2()) {
final List indexes = StatsTests.postHoc(0.05, friedman._1(), data);
if (indexes.isNotEmpty() && indexes.length() >= minSolutions.getParameter()) {
alg.setParameterList(indexes.map(flip(Utils.index()).f(parameterList)));
results = results.map(new F,List>() {
@Override
public List f(final List a) {
return indexes.map(flip(Utils.index()).f(a));
}
});
} else {
final List> ranks = iterableList(data)
.map(Stats.rank.andThen(Utils.iterableList()));
final List newIndexes = ranks.foldLeft(Utils.pairwise(add), replicate(data.head().length(), 0.0))
.zipIndex()
.sort(p2Ord(doubleOrd, intOrd))
.take((int)minSolutions.getParameter())
.map(P2.__2());
alg.setParameterList(newIndexes.map(flip(Utils.index()).f(parameterList)));
results = results.map(new F,List>() {
@Override
public List f(final List a) {
return newIndexes.map(flip(Utils.index()).f(a));
}
});
}
}
}
List> data = results
.map(List.map_().f(getFitness()
.andThen(getValue())
.andThen(negateIfMaximising(tuningProblem.getObjective()))));
final List> ranks = iterableList(data)
.map(Stats.rank.andThen(Utils.iterableList()));
final List indexes = ranks.foldLeft(Utils.pairwise(add), replicate(data.head().length(), 0.0))
.zipIndex()
.sort(p2Ord(doubleOrd, intOrd))
.map(P2.__2());
alg.setParameterList(indexes.map(flip(Utils.index()).f(alg.getParameterList())));
results = results.map(new F,List>() {
@Override
public List f(final List a) {
return indexes.map(flip(Utils.index()).f(a));
}
});
}
public static F negateIfMaximising(final Objective obj) {
return new F() {
@Override
public Double f(Double a) {
return obj.fold(Function.constant(a), Function.constant(-a));
}
};
}
public void setMinProblems(ControlParameter r) {
this.minProblems = r;
}
public ControlParameter getMinProblems() {
return minProblems;
}
public void setMinSolutions(ControlParameter r) {
this.minSolutions = r;
}
public ControlParameter getMinSolutions() {
return minSolutions;
}
public void resetResults() {
results = List.nil();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy