Search in sources :

Example 6 with SlidingMeanEDA

use of ntuple.SlidingMeanEDA 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)

Example 7 with SlidingMeanEDA

use of ntuple.SlidingMeanEDA in project SimpleAsteroids by ljialin.

the class GameRunnerTest method main.

public static void main(String[] args) {
    GameState.includeBuffersInScore = false;
    GameRunner gameRunner = new GameRunner().setLength(200);
    SimplePlayerInterface p1, p2;
    p1 = new RandomAgent();
    // p2 = new DoNothingAgent();
    EvoAlg evoAlg1 = new SimpleRMHC();
    // evoAlg1.mu
    int nEvals1 = 200;
    int seqLength1 = 10;
    int nEvals2 = 400;
    int seqLength2 = 5;
    SlidingMeanEDA evoAlg2 = new SlidingMeanEDA().setHistoryLength(40);
    SimpleGA simpleGA = new SimpleGA().setPopulationSize(20);
    EvoAgent evoAgent1 = new EvoAgent().setEvoAlg(evoAlg1, nEvals1).setSequenceLength(seqLength1);
    evoAgent1.setUseShiftBuffer(true);
    // evoAgent1.mu
    // evoAgent1.u
    // evoAgent1.setOpponent(new RandomAgent()).setUseShiftBuffer(false);
    // evoAgent1.setOpponent(new RandomAgent());
    p1 = evoAgent1;
    SimplePlayerInterface opponentModel;
    opponentModel = new DoNothingAgent();
    opponentModel = new RandomAgent();
    // p2 = new EvoAgent().setEvoAlg(simpleGA, nEvals).setSequenceLength(seqLength).setOpponent(opponentModel);
    // p2 = new EvoAgent().setEvoAlg(evoAlg1, nEvals/2).setSequenceLength(seqLength*2).setOpponent(opponentModel);
    EvoAgent evoAgent2 = new EvoAgent().setEvoAlg(evoAlg1, nEvals2).setSequenceLength(seqLength2).setOpponent(opponentModel);
    evoAgent2.setUseShiftBuffer(true);
    p2 = evoAgent2;
    p2 = getMCTSAgent(new GameState().defaultState(), 1);
    // p2 = new RandomAgent();
    gameRunner.setPlayers(p1, p2);
    // now play a number of games and observe the outcomes
    // verbose is set to true by default so after the games have been played
    // it will report the outcomes
    int nGames = 20;
    gameRunner.playGames(nGames);
    // now access the game logs to plot the scores
    gameRunner.plotGameScores();
// System.out.println(evoAlg2.pVec);
}
Also used : SimpleGA(ga.SimpleGA) SimpleRMHC(ga.SimpleRMHC) SlidingMeanEDA(ntuple.SlidingMeanEDA) EvoAlg(evodef.EvoAlg)

Example 8 with SlidingMeanEDA

use of ntuple.SlidingMeanEDA in project SimpleAsteroids by ljialin.

the class AgentEvaluator method evaluate.

@Override
public double evaluate(int[] solution) {
    // at thias point,
    System.out.println("Params are:");
    System.out.println(searchSpace.report(solution));
    // can also override parameters by setting them directly as follows:
    BattleGameParameters.loss = 0.996;
    BattleGameParameters.thrust = 3;
    // BattleGameParameters.shipSize *= 2;
    // BattleGameParameters.damageRadius *= 2;
    SpaceBattleLinkStateTwoPlayer linkState = new SpaceBattleLinkStateTwoPlayer();
    StateObservationMulti multi = linkState;
    GameActionSpaceAdapterMulti.useHeuristic = false;
    // DefaultMutator.totalRandomChaosMutation = false;
    ElapsedCpuTimer timer = new ElapsedCpuTimer();
    // AbstractMultiPlayer player2;
    int idPlayer1 = 0;
    int idPlayer2 = 1;
    // player2 = new controllers.multiPlayer.discountOLMCTS.Agent(linkState, timer, idPlayer2);
    // try the evolutionary players
    int nResamples = 2;
    EvoAlg evoAlg = new SimpleRMHC(nResamples);
    double kExplore = searchSpace.getExplorationFactor(solution);
    int nNeighbours = 100;
    int nEvals = 100;
    evoAlg = new NTupleBanditEA(kExplore, nNeighbours);
    evoAlg = new SlidingMeanEDA().setHistoryLength(searchSpace.getHistoryLength(solution));
    Agent evoAgent = new controllers.multiPlayer.ea.Agent(linkState, timer, evoAlg, idPlayer1, nEvals);
    evoAgent.setDiscountFactor(searchSpace.getDiscountFactor(solution));
    evoAgent.sequenceLength = searchSpace.getRolloutLength(solution);
    // evoAgent.di
    // EvoAlg evoAlg2 = new CompactSlidingModelGA().setHistoryLength(2);
    EvoAlg evoAlg2 = new SlidingMeanEDA().setHistoryLength(2);
    Agent player2 = new controllers.multiPlayer.ea.Agent(linkState, timer, evoAlg2, idPlayer2, nEvals);
    player2.sequenceLength = 5;
    // player2 = new controllers.multiPlayer.ea.Agent(linkState, timer, new SimpleRMHC(nResamples), idPlayer2, nEvals);
    // player1  = new controllers.multiPlayer.smlrand.Agent();
    // EvoAlg evoAlg2 = new SimpleRMHC(2);
    // player1 = new controllers.multiPlayer.ea.Agent(linkState, timer, evoAlg2, idPlayer1, nEvals);
    // in milliseconds
    int thinkingTime = 10;
    int delay = 10;
    // player = new controllers.singlePlayer.sampleRandom.Agent(stateObs, timer);
    // check that we can play the game
    Random random = new Random();
    int nSteps = 500;
    ElapsedTimer t = new ElapsedTimer();
    StatSummary sst1 = new StatSummary("Player 1 Elapsed Time");
    StatSummary sst2 = new StatSummary("Player 2 Elapsed Time");
    StatSummary ssTicks1 = new StatSummary("Player 1 nTicks");
    StatSummary ssTicks2 = new StatSummary("Player 2 nTicks");
    for (int i = 0; i < nSteps && !linkState.isGameOver(); i++) {
        linkState.state = linkState.state.copyState();
        timer = new ElapsedCpuTimer();
        timer.setMaxTimeMillis(thinkingTime);
        ElapsedTimer t1 = new ElapsedTimer();
        // keep track of the number of game ticks used by each algorithm
        int ticks;
        ticks = SpaceBattleLinkStateTwoPlayer.nTicks;
        Types.ACTIONS action1 = evoAgent.act(multi.copy(), timer);
        sst1.add(t1.elapsed());
        ticks = SpaceBattleLinkStateTwoPlayer.nTicks - ticks;
        ssTicks1.add(ticks);
        // System.out.println("Player 1 Ticks = " + ticks);
        ElapsedTimer t2 = new ElapsedTimer();
        ticks = SpaceBattleLinkStateTwoPlayer.nTicks;
        Types.ACTIONS action2 = player2.act(multi.copy(), timer);
        sst2.add(t2.elapsed());
        ticks = SpaceBattleLinkStateTwoPlayer.nTicks - ticks;
        ssTicks2.add(ticks);
        // System.out.println("Player 2 Ticks = " + ticks);
        multi.advance(new Types.ACTIONS[] { action1, action2 });
    }
    System.out.println(multi.getGameScore());
    System.out.println(multi.isGameOver());
    // System.out.println(SingleTreeNode.rollOutScores);
    System.out.println(sst1);
    System.out.println(sst2);
    System.out.println(ssTicks1);
    System.out.println(ssTicks2);
    double score = multi.getGameScore(0);
    System.out.println("Game score: " + score);
    if (score > 0)
        return 1;
    if (score < 0)
        return -1;
    return 0;
}
Also used : Agent(controllers.multiPlayer.ea.Agent) Types(ontology.Types) SlidingMeanEDA(ntuple.SlidingMeanEDA) NTupleBanditEA(ntuple.NTupleBanditEA) StatSummary(utilities.StatSummary) SimpleRMHC(ga.SimpleRMHC) Random(java.util.Random) StateObservationMulti(core.game.StateObservationMulti) ElapsedTimer(utilities.ElapsedTimer) ElapsedCpuTimer(tools.ElapsedCpuTimer) SpaceBattleLinkStateTwoPlayer(gvglink.SpaceBattleLinkStateTwoPlayer)

Aggregations

SlidingMeanEDA (ntuple.SlidingMeanEDA)8 EvoAlg (evodef.EvoAlg)6 SimpleRMHC (ga.SimpleRMHC)6 Random (java.util.Random)4 NTupleBanditEA (ntuple.NTupleBanditEA)4 ElapsedCpuTimer (tools.ElapsedCpuTimer)4 Types (ontology.Types)3 ElapsedTimer (utilities.ElapsedTimer)3 Agent (controllers.multiPlayer.ea.Agent)2 StateObservationMulti (core.game.StateObservationMulti)2 AbstractMultiPlayer (core.player.AbstractMultiPlayer)2 StatSummary (utilities.StatSummary)2 AsteroidsGameState (asteroids.AsteroidsGameState)1 BattleView (battle.BattleView)1 Agent (controllers.singlePlayer.ea.Agent)1 StateObservation (core.game.StateObservation)1 AbstractPlayer (core.player.AbstractPlayer)1 AnnotatedFitnessSpace (evodef.AnnotatedFitnessSpace)1 DefaultMutator (evodef.DefaultMutator)1 SimpleGA (ga.SimpleGA)1