use of utilities.ElapsedTimer 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.ElapsedTimer 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.ElapsedTimer in project SimpleAsteroids by ljialin.
the class TestPlotter method main.
public static void main(String[] args) {
int nDims = 5;
int mValues = 4;
double noiseLevel = 1.0;
double kExplore = 10;
boolean useTrap = true;
// EvalMaxM is like Noisy OneMax but generalised to M values
// instead of binary
EvalMaxM problem = new EvalMaxM(nDims, mValues, noiseLevel).setTrap(useTrap);
NTupleBanditEA banditEA = new NTupleBanditEA().setKExplore(kExplore);
// set a particlar NTuple System as the model
// if this is not set, then it will use a default model
NTupleSystem model = new NTupleSystem();
// set up a non-standard tuple pattern
model.use1Tuple = true;
model.use2Tuple = true;
model.useNTuple = false;
banditEA.setModel(model);
ElapsedTimer timer = new ElapsedTimer();
int nEvals = 200;
int[] solution = banditEA.runTrial(problem, nEvals);
System.out.println(Arrays.toString(solution));
System.out.println(timer);
new Plotter().setModel(model).defaultPlot().plot1Tuples();
System.out.println(timer);
}
use of utilities.ElapsedTimer 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;
}
use of utilities.ElapsedTimer in project SimpleAsteroids by ljialin.
the class ConvNTuple method main.
public static void main(String[] args) {
int w = 20, h = 20;
int filterWidth = 6, filterHeight = 6;
ConvNTuple convNTuple = new ConvNTuple().setImageDimensions(w, h);
convNTuple.setFilterDimensions(filterWidth, filterHeight);
convNTuple.setMValues(2).setStride(2);
convNTuple.makeIndices();
System.out.println("Address space size: " + convNTuple.addressSpaceSize());
// System.out.println("Mean of empty summary: " + new StatSummary().mean());
// now put some random data in to it
ElapsedTimer timer = new ElapsedTimer();
int nPoints = 100;
int nDims = w * h;
Random random = new Random();
for (int i = 0; i < nPoints; i++) {
double p = random.nextDouble();
// now make a random array with this P(x==1)
int[] x = new int[nDims];
int tot = 0;
for (int j = 0; j < nDims; j++) {
int z = random.nextDouble() < p ? 1 : 0;
x[j] = z;
tot += z;
}
convNTuple.addPoint(x, tot);
}
// now iterate over all the values in there
System.out.println("Training finished: ");
System.out.println(timer);
convNTuple.report();
System.out.println(timer);
// now make some random points
}
Aggregations