Search in sources :

Example 46 with ElapsedTimer

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;
}
Also used : StatSummary(utilities.StatSummary) ElapsedTimer(utilities.ElapsedTimer)

Example 47 with ElapsedTimer

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();
}
Also used : FitnessSpace(evodef.FitnessSpace) Ranker(utilities.Ranker) StatSummary(utilities.StatSummary) EvalNoisyWinRate(evodef.EvalNoisyWinRate) ElapsedTimer(utilities.ElapsedTimer)

Example 48 with ElapsedTimer

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);
    }
}
Also used : StatSummary(utilities.StatSummary) SimpleRMHC(ga.SimpleRMHC) DefaultMutator(evodef.DefaultMutator) ElapsedTimer(utilities.ElapsedTimer)

Example 49 with ElapsedTimer

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;
}
Also used : ElapsedTimer(utilities.ElapsedTimer) ConvNTuple(ntuple.ConvNTuple)

Example 50 with ElapsedTimer

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);
}
Also used : ElapsedTimer(utilities.ElapsedTimer)

Aggregations

ElapsedTimer (utilities.ElapsedTimer)63 StatSummary (utilities.StatSummary)27 Random (java.util.Random)12 SimpleRMHC (ga.SimpleRMHC)9 Types (ontology.Types)9 ElapsedCpuTimer (tools.ElapsedCpuTimer)9 EvoAlg (evodef.EvoAlg)7 NTupleBanditEA (ntuple.NTupleBanditEA)7 JEasyFrame (utilities.JEasyFrame)7 ArrayList (java.util.ArrayList)6 AbstractPlayer (core.player.AbstractPlayer)5 Gson (com.google.gson.Gson)4 Agent (controllers.singlePlayer.ea.Agent)4 StateObservationMulti (core.game.StateObservationMulti)4 StateObservation (core.game.StateObservation)3 AbstractMultiPlayer (core.player.AbstractMultiPlayer)3 GameParameters (evogame.GameParameters)3 ConvNTuple (ntuple.ConvNTuple)3 SlidingMeanEDA (ntuple.SlidingMeanEDA)3 SimpleMaxGame (altgame.SimpleMaxGame)2