ntbea.NTupleBanditEA Maven / Gradle / Ivy
package ntbea;
import evodef.*;
import org.jetbrains.annotations.NotNull;
import utilities.StatSummary;
import java.util.Arrays;
/**
* Created by sml on 09/01/2017.
*/
public class NTupleBanditEA implements EvoAlg {
// public NTupleSystem banditLandscapeModel;
protected BanditLandscapeModel banditLandscapeModel;
// the exploration rate normally called K or C - called kExplore here for clarity
public double kExplore = 100.0; // 1.0; // Math.sqrt(0.4);
// the number of neighbours to explore around the current point each time
// they are only explored IN THE FITNESS LANDSCAPE MODEL, not by sampling the fitness function
int nNeighbours = 50;
static double defaultEpsilon = 0.5;
double epsilon = defaultEpsilon;
public int nSamples = 1;
public void setSamplingRate(int n) {
nSamples = n;
}
public NTupleBanditEA(BanditLandscapeModel model, double kExplore, int nNeighbours) {
banditLandscapeModel = model;
this.kExplore = kExplore;
this.nNeighbours = nNeighbours;
}
StatSummary fitness(SolutionEvaluator evaluator, int[] sol) {
StatSummary ss = new StatSummary();
for (int i = 0; i < nSamples; i++) {
double fitness = evaluator.evaluate(sol);
ss.add(fitness);
}
return ss;
}
public int[] seed;
SolutionEvaluator evaluator;
public boolean logBestYet = false;
@NotNull
@Override
public double[] runTrial(SolutionEvaluator evaluator, int nEvals) {
this.evaluator = evaluator;
// set up some convenient references
SearchSpace searchSpace = evaluator.searchSpace();
DefaultMutator mutator = new DefaultMutator(searchSpace);
// banditLandscapeModel.setSearchSpace(searchSpace);
nNeighbours = (int) Math.min(nNeighbours, SearchSpaceUtil.size(searchSpace) / 4);
if (nNeighbours < 5) nNeighbours = 5;
// System.out.println("Set neighbours to: " + nNeighbours);
if (banditLandscapeModel == null) {
// System.out.println("NTupleBanditEA.runTrial: Creating new landscape model");
banditLandscapeModel = new NTupleSystem(searchSpace);
}
banditLandscapeModel.setEpsilon(epsilon);
// then each time around the loop try the following
// create a neighbourhood set of points and pick the best one that combines it's exploitation and evaluation scores
int[] p;
if (seed == null) {
p = SearchSpaceUtil.randomPoint(searchSpace);
} else {
p = seed;
}
// banditLandscapeModel.printDetailedReport();
for(int i=0; i