Search in sources :

Example 6 with DefaultMutator

use of evodef.DefaultMutator in project SimpleAsteroids by ljialin.

the class SimpleRMHC method runTrial.

/**
 * @param evaluator
 * @param maxEvals
 * @return: the solution coded as an array of int
 */
@Override
public int[] runTrial(SolutionEvaluator evaluator, int maxEvals) {
    init(evaluator);
    StatSummary fitBest = fitness(evaluator, bestYet, new StatSummary());
    // create a mutator if it has not already been made
    if (mutator == null)
        mutator = new DefaultMutator(searchSpace);
    else
        mutator.setSearchSpace(searchSpace);
    while (evaluator.nEvals() < maxEvals && !evaluator.optimalFound()) {
        // System.out.println("nEvals: " + evaluator.nEvals());
        int[] mut = mutator.randMut(bestYet);
        // int[] mut = randMutAll(bestYet);
        // int[] mut = randAll(bestYet);
        int oneBits = 0;
        for (int x : bestYet) oneBits += x;
        if (oneBits == 10 && TestFHT.foundOpt == false) {
            TestFHT.foundOpt = true;
        // System.out.println("Stumbled on opt: " + Arrays.toString(a));
        }
        // keep track of how much we want to mutate this
        int prevEvals = evaluator.nEvals();
        StatSummary fitMut = fitness(evaluator, mut, new StatSummary());
        if (accumulateBestYetStats) {
            fitBest = fitness(evaluator, bestYet, fitBest);
        } else {
            if (resampleParent) {
                fitBest = fitness(evaluator, bestYet, new StatSummary());
            }
        }
        // System.out.println(fitBest.mean() + " : " + fitMut.mean());
        if (fitMut.mean() >= fitBest.mean()) {
            // System.out.println("Updating best");
            bestYet = mut;
            fitBest = fitMut;
            evaluator.logger().keepBest(mut, fitMut.mean());
            if (noisy) {
                Double opt = evaluator.optimalIfKnown();
                if (opt != null) {
                    if (fitMut.mean() >= opt + epsilon) {
                        return bestYet;
                    }
                }
            }
        }
        int evalDiff = evaluator.nEvals() - prevEvals;
        for (int i = 0; i < evalDiff; i++) {
            evaluator.logger().logBestYest(bestYet);
        }
    }
    // System.out.println("Sampling rate: " + nSamples);
    return bestYet;
}
Also used : StatSummary(utilities.StatSummary) DefaultMutator(evodef.DefaultMutator)

Example 7 with DefaultMutator

use of evodef.DefaultMutator in project SimpleAsteroids by ljialin.

the class EvoAgentSearchSpace method evaluate.

@Override
public double evaluate(int[] x) {
    // create a problem to evaluate this one on ...
    // this should really be set externally, but just doing it this way for now
    GameRunner gameRunner = new GameRunner();
    // search space will need to be set before use
    DefaultMutator mutator = new DefaultMutator(null);
    mutator.pointProb = pointMutationRate[x[pointMutationRateIndex]];
    mutator.flipAtLeastOneValue = flipAtLeastOneBit[x[flipAtLeastOneBitIndex]];
    mutator.totalRandomChaosMutation = false;
    SimpleRMHC simpleRMHC = new SimpleRMHC();
    simpleRMHC.setSamplingRate(nResamples[x[nResamplesIndex]]);
    simpleRMHC.setMutator(mutator);
    EvoAgent evoAgent = new EvoAgent().setEvoAlg(simpleRMHC, getNEvals(x));
    evoAgent.setUseShiftBuffer(useShiftBuffer[x[useShiftBufferIndex]]);
    evoAgent.setSequenceLength(seqLength[x[seqLengthIndex]]);
    EvoAlg evoAlgOpponent = new SimpleRMHC();
    // set up some defaults for opponent
    // previously 400 and 5 respectively
    int nOpponentEvals = 400;
    int opponentSeqLength = 5;
    EvoAgent evoOpponent = new EvoAgent().setEvoAlg(evoAlgOpponent, nOpponentEvals).setSequenceLength(opponentSeqLength);
    // setting to false provides a much weaker opponent
    evoOpponent.setUseShiftBuffer(true);
    // now run a game and return the result
    gameRunner.verbose = false;
    gameRunner.reset();
    gameRunner.setPlayers(evoAgent, evoOpponent);
    gameRunner.playGame();
    double fitness = gameRunner.scores.mean();
    double value = 0;
    if (fitness > 0)
        value = 1;
    if (fitness < 0)
        value = -1;
    logger.log(value, x, false);
    return value;
}
Also used : SimpleRMHC(ga.SimpleRMHC) DefaultMutator(evodef.DefaultMutator)

Example 8 with DefaultMutator

use of evodef.DefaultMutator in project SimpleAsteroids by ljialin.

the class EvoAgentSearchSpaceAsteroids method evaluate.

@Override
public double evaluate(int[] x) {
    // create a problem to evaluate this one on ...
    // this should really be set externally, but just doing it this way for now
    AsteroidsGameState gameState = new AsteroidsGameState().setParams(params).initForwardModel();
    // search space will need to be set before use
    DefaultMutator mutator = new DefaultMutator(null);
    mutator.pointProb = pointMutationRate[x[pointMutationRateIndex]];
    mutator.flipAtLeastOneValue = flipAtLeastOneBit[x[flipAtLeastOneBitIndex]];
    // setting to true may give best performance
    mutator.totalRandomChaosMutation = false;
    SimpleRMHC simpleRMHC = new SimpleRMHC();
    simpleRMHC.setSamplingRate(nResamples[x[nResamplesIndex]]);
    simpleRMHC.setMutator(mutator);
    EvoAlg sliding = new SlidingMeanEDA();
    EvoAgent evoAgent = new EvoAgent().setEvoAlg(simpleRMHC, getNEvals(x));
    // EvoAgent evoAgent = new EvoAgent().setEvoAlg(sliding, getNEvals(x));
    evoAgent.setUseShiftBuffer(useShiftBuffer[x[useShiftBufferIndex]]);
    evoAgent.setSequenceLength(seqLength[x[seqLengthIndex]]);
    for (int i = 0; i < maxTick; i++) {
        int action = evoAgent.getAction(gameState, 0);
        gameState.next(new int[] { action });
    }
    double fitness = gameState.getScore();
    logger.log(fitness, x, false);
    return fitness;
}
Also used : SimpleRMHC(ga.SimpleRMHC) DefaultMutator(evodef.DefaultMutator) SlidingMeanEDA(ntuple.SlidingMeanEDA) AsteroidsGameState(asteroids.AsteroidsGameState)

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