Search in sources :

Example 51 with StatSummary

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

Example 52 with StatSummary

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

Example 53 with StatSummary

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

Example 54 with StatSummary

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

Example 55 with StatSummary

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;
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) StatSummary(utilities.StatSummary) ShortestPathTest(evomaze.ShortestPathTest) ElapsedTimer(utilities.ElapsedTimer) PrintWriter(java.io.PrintWriter)

Aggregations

StatSummary (utilities.StatSummary)88 ElapsedTimer (utilities.ElapsedTimer)27 ArrayList (java.util.ArrayList)10 JEasyFrame (utilities.JEasyFrame)8 SimpleRMHC (ga.SimpleRMHC)7 NTupleBanditEA (ntuple.NTupleBanditEA)6 EvoAlg (evodef.EvoAlg)4 Random (java.util.Random)4 LineChart (plot.LineChart)4 LineChartAxis (plot.LineChartAxis)4 StateObservationMulti (core.game.StateObservationMulti)3 DefaultMutator (evodef.DefaultMutator)3 TreeSet (java.util.TreeSet)3 Types (ontology.Types)3 LineGroup (plot.LineGroup)3 ElapsedCpuTimer (tools.ElapsedCpuTimer)3 Gson (com.google.gson.Gson)2 GsonBuilder (com.google.gson.GsonBuilder)2 AbstractMultiPlayer (core.player.AbstractMultiPlayer)2 ShortestPathTest (evomaze.ShortestPathTest)2