use of utilities.ElapsedTimer in project SimpleAsteroids by ljialin.
the class EvoNTupleTest method runTrials.
public static StatSummary runTrials(EvoAlg ea, SolutionEvaluator evaluator, int nTrials, int nFitnessEvals) {
// record the time stats
StatSummary ss = new StatSummary();
for (int i = 0; i < nTrials; i++) {
ElapsedTimer t = new ElapsedTimer();
evaluator.reset();
ea.runTrial(evaluator, nFitnessEvals);
System.out.println(t);
evaluator.logger().report();
System.out.println();
ss.add(evaluator.logger().ss.max());
}
return ss;
}
use of utilities.ElapsedTimer in project SimpleAsteroids by ljialin.
the class NTupleSystemTest method main.
// create a way to make N-Tuple systems ...
public static void main(String[] args) {
// NTupleSystem nts = new NTupleSystem(new TenSpace(nDims, 2));
int m = 2;
FitnessSpace evalTrue = new EvalNoisyWinRate(nDims, m);
FitnessSpace evalNoisy = new EvalNoisyWinRate(nDims, m, 1.0);
NTupleSystem nts = new NTupleSystem();
nts.setSearchSpace(evalNoisy);
// nts.addTuples();
// nts.printSummaryReport();
// nts = new NTupleSystem(BattleGameSearchSpace.getSearchSpace());
// nts.addTuples();
// now add some random data
ElapsedTimer t = new ElapsedTimer();
double[] hist = new double[50];
int nReps = 1;
int nPoints = 1000;
nPoints = (int) SearchSpaceUtil.size(nts.searchSpace);
nPoints *= 100;
for (int i = 0; i < nPoints; i++) {
int[] p = SearchSpaceUtil.randomPoint(nts.searchSpace);
for (int j = 0; j < nReps; j++) {
double value = evalNoisy.evaluate(p);
// index of histogram calculation assumes that
// value is in range 0 .. 1
int ix = (int) (value * (hist.length - 1));
// System.out.println(ix) + ;
// hist[ix]++;
nts.addPoint(p, value);
}
}
// BarChart.display(hist, "Distribution");
System.out.format("Added %d points\n", nPoints);
nts.printSummaryReport();
System.out.println(t);
System.out.println("Now testing ...");
System.out.println(t);
// nPoints = 1000;
StatSummary ss = new StatSummary();
Ranker<Integer> trueRank = new Ranker<>();
Ranker<Integer> estRank = new Ranker<>();
// ensure we sample all the points in the search space when testing
nPoints = (int) SearchSpaceUtil.size(nts.searchSpace);
// the rank correlation was originally computed in-ine in the code,
// but this method has now been superceded by a separate utility class
// whose use is also demonstrated
RankCorrelation rc = new RankCorrelation();
for (int i = 0; i < nPoints; i++) {
int[] p = SearchSpaceUtil.randomPoint(nts.searchSpace);
p = SearchSpaceUtil.nthPoint(nts.searchSpace, i);
// make the value depend on a few of the indices rather than
// the actual values, just for a simple test
// System.out.println("Probing: " + Arrays.toString(p));
// Double value = nts.get(p);
Double value = nts.getMeanEstimate(p);
if (value != null) {
double trueVal = evalTrue.evaluate(p);
double diff = Math.abs(trueVal - value);
rc.add(i, value, trueVal);
ss.add(diff);
// System.out.format("%d\t %.34f\t %.4f\n", i, value, trueVal);
trueRank.add(trueVal, i);
estRank.add(value, i);
// System.out.println();
}
// System.out.println(ss);
// nts.addPoint(p, value);
// System.out.println("Returned value = " + value);
// System.out.println();
}
System.out.println();
System.out.println(ss);
System.out.println(t);
// now show the ranks
double sumSquaredDiff = 0;
for (int i = 0; i < nPoints; i++) {
// System.out.println(i + "\t " + trueRank.getRank(i) + "\t " + estRank.getRank(i));
int[] x = SearchSpaceUtil.nthPoint(nts.searchSpace, i);
// System.out.println(Arrays.toString(nts.getExplorationVector(x)) + " : " + nts.getExplorationEstimate(x));
// System.out.println();
sumSquaredDiff += sqr(trueRank.getRank(i) - estRank.getRank(i));
}
// System.out.println("diffSum = " + sumSquaredDiff);
double spearmanCoefficient = 1 - sumSquaredDiff * 6 / (nPoints * nPoints * nPoints - nPoints);
System.out.format("Spearman correlation = %.4f\n", spearmanCoefficient);
System.out.println("And the RankCorrelation utility class test: ");
double rankCorrelation = rc.rankCorrelation();
System.out.println("RC = " + rankCorrelation);
// nts.printDetailedReport();
}
use of utilities.ElapsedTimer in project SimpleAsteroids by ljialin.
the class EvolveMarioLevelTest method main.
public static void main(String[] args) throws Exception {
int[][] level = getAndShowLevel(true);
int nTrials = 1;
SimpleRMHC simpleRMHC = new SimpleRMHC();
DefaultMutator mutator = new DefaultMutator(null);
mutator.flipAtLeastOneValue = true;
mutator.pointProb = 2;
mutator.setSwap(true);
simpleRMHC.setMutator(mutator);
// EvoAlg evoAlg = simpleRMHC;
// evoAlg = new SlidingMeanEDA().setHistoryLength(30);
// evoAlg = new CompactSlidingGA();
int nEvals = 20000;
StatSummary results = new StatSummary();
EvolveMarioLevelTest evolver = new EvolveMarioLevelTest();
for (int i = 0; i < nTrials; i++) {
ElapsedTimer timer = new ElapsedTimer();
results.add(evolver.runTrial(simpleRMHC, nEvals, level));
System.out.println(timer);
}
}
use of utilities.ElapsedTimer in project SimpleAsteroids by ljialin.
the class EvolvePatternTest method getTrainedConvNTuple.
public static ConvNTuple getTrainedConvNTuple() {
int w = 4, h = 4;
int filterWidth = 2, filterHeight = 2;
ConvNTuple convNTuple = new ConvNTuple().setImageDimensions(w, h);
convNTuple.setFilterDimensions(filterWidth, filterHeight);
convNTuple.setMValues(3).setStride(1);
convNTuple.makeWrapAroundIndices();
convNTuple.reset();
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();
// 'x' is the Red Maze example explained here:
// https://adamsmith.as/papers/wfc_is_constraint_solving_in_the_wild.pdf
int[] x = { 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 2, 1, 0, 1, 1, 1 };
convNTuple.addPoint(x, 1);
// now iterate over all the values in there
System.out.println("Training finished: ");
System.out.println(timer);
// convNTuple.report();
// System.out.println(timer);
// now reset the indices to the true image size, but do not use wrap around
convNTuple.setImageDimensions(imageWidth, imageHeight);
convNTuple.makeWrapAroundIndices();
return convNTuple;
}
use of utilities.ElapsedTimer in project SimpleAsteroids by ljialin.
the class HashSpeedTest method main.
public static void main(String[] args) {
ElapsedTimer timer = new ElapsedTimer();
System.out.println(timer);
}
Aggregations