Search in sources :

Example 1 with DefaultMutator

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

Example 2 with DefaultMutator

use of evodef.DefaultMutator in project SimpleAsteroids by ljialin.

the class SimpleGA method runTrial.

@Override
public int[] runTrial(SolutionEvaluator evaluator, int nEvals) {
    this.evaluator = evaluator;
    mutator = new DefaultMutator(evaluator.searchSpace());
    initPop();
    while (evaluator.nEvals() < nEvals) {
        int prevEvals = evaluator.nEvals();
        // evaluate them
        evalPop();
        // breed them
        pop = breed();
        int diffEvals = evaluator.nEvals() - prevEvals;
        for (int i = 0; i < diffEvals; i++) {
            evaluator.logger().logBestYest(pop.get(0).p);
        }
    }
    // a final evaluation
    evalPop();
    finalFitness = pop.get(0).score;
    return pop.get(0).p;
}
Also used : DefaultMutator(evodef.DefaultMutator)

Example 3 with DefaultMutator

use of evodef.DefaultMutator in project SimpleAsteroids by ljialin.

the class Game method getEvoAgent.

Controller getEvoAgent() {
    // 
    // todo Add in the code t make this
    int nResamples = 1;
    DefaultMutator mutator = new DefaultMutator(null);
    // setting to true may give best performance
    mutator.totalRandomChaosMutation = true;
    SimpleRMHC simpleRMHC = new SimpleRMHC();
    simpleRMHC.setSamplingRate(nResamples);
    simpleRMHC.setMutator(mutator);
    EvoAlg evoAlg = simpleRMHC;
    // evoAlg = new SlidingMeanEDA();
    int nEvals = 20;
    int seqLength = 100;
    EvoAgent evoAgent = new EvoAgent().setEvoAlg(evoAlg, nEvals).setSequenceLength(seqLength);
    evoAgent.setUseShiftBuffer(true);
    evoAgent.setVisual();
    return new EvoAgentAdapter().setAgent(evoAgent);
}
Also used : SimpleRMHC(ga.SimpleRMHC) DefaultMutator(evodef.DefaultMutator) EvoAgent(planetwar.EvoAgent) EvoAlg(evodef.EvoAlg)

Example 4 with DefaultMutator

use of evodef.DefaultMutator 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 5 with DefaultMutator

use of evodef.DefaultMutator in project SimpleAsteroids by ljialin.

the class ProblemInstance method useVecsAroundRandomPoint.

public ProblemInstance useVecsAroundRandomPoint() {
    scoredVecs = new ArrayList<>();
    int[] px = SearchSpaceUtil.randomPoint(evaluator.searchSpace());
    DefaultMutator mutator = new DefaultMutator(evaluator.searchSpace());
    // 1.0; // 3 / n;
    mutator.pointProb = 0.0;
    // mutator
    DefaultMutator.flipAtLeastOneValueDefault = true;
    DefaultMutator.totalRandomChaosMutation = false;
    for (int i = 0; i < k; i++) {
        // int[] p = SearchSpaceUtil.randomPoint(searchSpace);
        int[] p = mutator.randMut(px);
        double score = evaluator.evaluate(p);
        scoredVecs.add(new ScoredVec(p, score));
    // System.out.println(Arrays.toString(p) + " : " + String.format("%.2f\t %.0f", score, evaluator.trueFitness(p)));
    }
    return this;
}
Also used : DefaultMutator(evodef.DefaultMutator) ScoredVec(ntuple.ScoredVec)

Aggregations

DefaultMutator (evodef.DefaultMutator)8 SimpleRMHC (ga.SimpleRMHC)4 StatSummary (utilities.StatSummary)3 ElapsedTimer (utilities.ElapsedTimer)2 AsteroidsGameState (asteroids.AsteroidsGameState)1 EvoAlg (evodef.EvoAlg)1 ScoredVec (ntuple.ScoredVec)1 SlidingMeanEDA (ntuple.SlidingMeanEDA)1 EvoAgent (planetwar.EvoAgent)1