use of utilities.StatSummary in project SimpleAsteroids by ljialin.
the class NTupleBanditEA method runTrial.
@Override
public int[] runTrial(SolutionEvaluator evaluator, int nEvals) {
this.evaluator = evaluator;
// set up some convenient references
SearchSpace searchSpace = evaluator.searchSpace();
EvolutionLogger logger = evaluator.logger();
DefaultMutator mutator = new DefaultMutator(searchSpace);
banditLandscapeModel.setSearchSpace(searchSpace);
nNeighbours = (int) Math.min(nNeighbours, SearchSpaceUtil.size(searchSpace) / 4);
System.out.println("Set neighbours to: " + nNeighbours);
if (banditLandscapeModel == null) {
System.out.println("NTupleBanditEA.runTrial: Creating new landscape model");
banditLandscapeModel = new NTupleSystem().setSearchSpace(searchSpace);
}
// create an NTuple fitness landscape model if needed
if (resetModelEachRun) {
System.out.println("NTupleBanditEA.runTrial: resetting landscape model");
banditLandscapeModel.reset();
}
// 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
StatSummary ss = new StatSummary();
int[] p;
if (seed == null) {
p = SearchSpaceUtil.randomPoint(searchSpace);
} else {
p = seed;
}
while (evaluator.nEvals() < nEvals) {
// each time around the loop we make one fitness evaluation of p
// and add this NEW information to the memory
int prevEvals = evaluator.nEvals();
// the new version enables resampling
double fitness;
if (nSamples == 1) {
fitness = evaluator.evaluate(p);
} else {
fitness = fitness(evaluator, p).mean();
}
if (reportFrequency > 0 && evaluator.nEvals() % reportFrequency == 0) {
System.out.format("Iteration: %d\t %.1f\n", evaluator.nEvals(), fitness);
System.out.println(evaluator.logger().ss);
System.out.println();
// System.out.println(p.length);
// System.out.println(p);
}
ElapsedTimer t = new ElapsedTimer();
banditLandscapeModel.addPoint(p, fitness);
// ss.add(t.elapsed());
// System.out.println(ss);
// System.out.println("N Neighbours: " + nNeighbours);
EvaluateChoices evc = new EvaluateChoices(banditLandscapeModel, kExplore);
while (evc.n() < nNeighbours) {
int[] pp = mutator.randMut(p);
evc.add(pp);
}
// evc.report();
// now set the next point to explore
p = evc.picker.getBest();
// logger.keepBest(picker.getBest(), picker.getBestScore());
int diffEvals = evaluator.nEvals() - prevEvals;
if (logBestYet) {
int[] bestYet = banditLandscapeModel.getBestOfSampled();
for (int i = 0; i < diffEvals; i++) {
evaluator.logger().logBestYest(bestYet);
}
}
// System.out.println("Best solution: " + Arrays.toString(evc.picker.getBest()) + "\t: " + evc.picker.getBestScore());
}
// System.out.println("Time for calling addPoint: ");
// System.out.println(ss);
// int[] solution = banditLandscapeModel.getBestSolution();
int[] solution = banditLandscapeModel.getBestOfSampled();
// int[] solution = banditLandscapeModel.getBestOfSampledPlusNeighbours(neighboursWhenFindingBest);
logger.keepBest(solution, evaluator.evaluate(solution));
return solution;
}
use of utilities.StatSummary in project SimpleAsteroids by ljialin.
the class StatSummarySpeed method main.
// create a number of stat summary objects
// creates more than 1 million in half a second ...
public static void main(String[] args) {
int n = (int) 1e6;
StatSummary[] ssa = new StatSummary[n];
ElapsedTimer t = new ElapsedTimer();
for (int i = 0; i < n; i++) {
ssa[i] = new StatSummary();
ssa[i].add(i);
}
System.out.println("Created: " + n);
System.out.println(t);
}
use of utilities.StatSummary in project SimpleAsteroids by ljialin.
the class CompactSlidingGA method fitness.
static StatSummary fitness(SolutionEvaluator evaluator, int[] sol, int nSamples) {
StatSummary ss = new StatSummary();
for (int i = 0; i < nSamples; i++) {
double fitness = evaluator.evaluate(sol);
ss.add(fitness);
}
return ss;
}
use of utilities.StatSummary in project SimpleAsteroids by ljialin.
the class CompactSlidingModelGA method fitness.
static StatSummary fitness(SolutionEvaluator evaluator, int[] sol, int nSamples) {
StatSummary ss = new StatSummary();
for (int i = 0; i < nSamples; i++) {
double fitness = evaluator.evaluate(sol);
ss.add(fitness);
}
return ss;
}
use of utilities.StatSummary in project SimpleAsteroids by ljialin.
the class ConvMazeTest method runTrials.
public StatSummary runTrials(int nDimenions, int nTrials) throws Exception {
StatSummary ss = new StatSummary();
ArrayList<int[][]> examples = new ArrayList<>();
// System.out.println(examples);
rankCorrelation = new RankCorrelation();
ElapsedTimer timer = new ElapsedTimer();
for (int i = 0; i < nTrials; i++) {
evaluator = new ShortestPathTest();
System.out.println("N DIMS = " + evaluator.searchSpace().nDims());
ElapsedTimer t = new ElapsedTimer();
NTupleBanditEA nTupleBanditEA = new NTupleBanditEA().setKExplore(nDimenions);
nTupleBanditEA.setKExplore(10).setNeighbours(50);
convNTuple.reset();
nTupleBanditEA.setModel(convNTuple);
// nTupleBanditEA.s
int[] solution = nTupleBanditEA.runTrial(evaluator, nEvals);
System.out.println("Solution array: " + Arrays.toString(solution));
System.out.println(t);
double fitness = evaluator.evaluate(solution);
ss.add(fitness);
System.out.println("Checking fitness: " + fitness);
examples.add(toSquareArray(solution));
System.out.println("Rank correlation check:");
// rankCorrelation.rankCorrelation();
convNTuple.report(evaluator);
MazeView.showMaze(solution, "" + fitness);
System.out.println(timer);
}
if (createFiles) {
System.out.println("Created mazes");
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String out = gson.toJson(examples);
System.out.println("Created JSON String");
// System.out.println(out);
String outputFile = "data/mazes.json";
PrintWriter writer = new PrintWriter(outputFile);
writer.print(out);
writer.close();
System.out.println("Wrote file with " + examples.size() + " examples");
}
return ss;
}
Aggregations