use of utilities.StatSummary in project SimpleAsteroids by ljialin.
the class NTuple method printNonEmpty.
public void printNonEmpty() {
if (ntArray != null) {
for (int i = 0; i < ntArray.length; i++) {
if (ntArray[i] != null) {
StatSummary ss = ntArray[i];
// System.out.println(i + "\t " + ss.n() + "\t " + ss.mean());
System.out.format("%d\t %.2f\t %d\n", i, ss.mean(), ss.n());
}
}
} else {
for (Double key : ntMap.keySet()) {
StatSummary ss = ntMap.get(key);
System.out.println(key + "\t " + ss.n() + "\t " + ss.mean());
}
}
}
use of utilities.StatSummary in project SimpleAsteroids by ljialin.
the class NTuple method printNonEmpty.
public void printNonEmpty(Param[] params) {
for (int i : tuple) {
System.out.println(params[i].getName() + "\t ");
}
System.out.println();
if (ntArray != null) {
for (int i = 0; i < ntArray.length; i++) {
if (ntArray[i] != null) {
StatSummary ss = ntArray[i];
// System.out.println(i + "\t " + ss.n() + "\t " + ss.mean());
int[] ind = getIndices(i);
System.out.println(Arrays.toString(ind));
System.out.println(paramString(params, ind));
System.out.format("%d\t %.2f\t %.3f\t %d\n", i, ss.mean(), ss.stdErr(), ss.n());
System.out.println();
}
}
} else {
for (Double key : ntMap.keySet()) {
StatSummary ss = ntMap.get(key);
System.out.println(key + "\t " + ss.n() + "\t " + ss.mean());
}
}
}
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);
nNeighbours = (int) Math.min(nNeighbours, SearchSpaceUtil.size(searchSpace) / 4);
System.out.println("Set neighbours to: " + nNeighbours);
// force creation
banditLandscapeModel = null;
// create an NTuple fitness landscape model
if (banditLandscapeModel == null) {
System.out.println("Creating new model");
banditLandscapeModel = new NTupleSystem().setSearchSpace(searchSpace);
}
// banditLandscapeModel.addTuples();
// 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();
if (view != null) {
view.repaint();
try {
Thread.sleep(400);
} catch (Exception e) {
}
}
// double fitness = evaluator.evaluate(p);
// 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;
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 NTupleView2D method paintComponent.
public void paintComponent(Graphics gx) {
setupNTuples();
Graphics2D g = (Graphics2D) gx;
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g.setColor(bg);
g.fillRect(0, 0, getWidth(), getHeight());
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
int[] p = new int[] { i, j };
StatSummary ss = xyn.getStats(p);
String cellString = String.format("[%d,%d]", i, j);
String trueVal = null;
if (solutionEvaluator != null) {
trueVal = String.format("(%.3f)", solutionEvaluator.evaluate(p));
}
drawCell(g, ss, i, j, cellString, trueVal);
}
}
for (int i = 0; i < m; i++) {
// show the 1-tuple ones
// note that each of these only samples the
// non-zero dimension: we could set the other value to anything
int[] px = new int[] { i, 0 };
int[] py = new int[] { 0, i };
String cellStringX = String.format("[%d,*]", i);
String cellStringY = String.format("[*,%d]", i);
// calculate the trueval for each one
StatSummary xTrue = new StatSummary();
StatSummary yTrue = new StatSummary();
for (int j = 0; j < m; j++) {
xTrue.add(solutionEvaluator.evaluate(new int[] { i, j }));
yTrue.add(solutionEvaluator.evaluate(new int[] { j, i }));
}
String xs = null, ys = null;
if (solutionEvaluator != null) {
xs = String.format("(%.3f)", xTrue.mean());
ys = String.format("(%.3f)", yTrue.mean());
}
drawCell(g, xn.getStats(px), i, m, cellStringX, xs);
drawCell(g, yn.getStats(py), m, i, cellStringY, ys);
// draw the borders:
g.setColor(Color.red);
int linwWidth = 6;
g.fillRect(0, m * cellSize - linwWidth / 2, getWidth(), linwWidth);
g.fillRect(m * cellSize - linwWidth / 2, 0, linwWidth, getHeight());
}
}
use of utilities.StatSummary in project SimpleAsteroids by ljialin.
the class SlidingMeanEDA 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;
}
Aggregations