Search in sources :

Example 1 with Agent

use of controllers.multiPlayer.ea.Agent in project SimpleAsteroids by ljialin.

the class PlanetWarsLinkTest method runTest.

public static double runTest() throws Exception {
    PlanetWarsLinkState state = new PlanetWarsLinkState();
    // state.state.
    GameState.includeBuffersInScore = true;
    PlanetWarView view = null;
    view = new PlanetWarView((GameState) state.state);
    // JEasyFrame frame = new JEasyFrame(view, "Simple Planet Wars");
    // KeyController controller = new KeyController();
    // frame.addKeyListener(controller);
    // now play
    Random random = new Random();
    AbstractMultiPlayer player1, player2;
    GameActionSpaceAdapterMulti.visual = false;
    // DefaultMutator.totalRandomChaosMutation = true;
    // controllers.singlePlayer.sampleOLMCTS.Agent olmcts =
    // new controllers.singlePlayer.sampleOLMCTS.Agent(linkState, timer);
    int idPlayer1 = 0;
    int idPlayer2 = 1;
    ElapsedCpuTimer timer = new ElapsedCpuTimer();
    player2 = new controllers.multiPlayer.discountOLMCTS.Agent(state.copy(), timer, idPlayer2);
    // player2 = new controllers.multiPlayer.sampleOLMCTS.Agent(state.copy(), timer, idPlayer2);
    // try the evolutionary players
    int nResamples = 1;
    EvoAlg evoAlg = new SimpleRMHC(nResamples);
    int nEvals = 133;
    // evoAlg = new SlidingMeanEDA().setHistoryLength(20);
    Agent evoAgent = new controllers.multiPlayer.ea.Agent(state.copy(), timer, evoAlg, idPlayer1, nEvals);
    evoAgent.sequenceLength = 15;
    evoAgent.setUseShiftBuffer(true);
    player1 = evoAgent;
    // player2 = new controllers.multiPlayer.ea.Agent(linkState, timer, evoAlg2, idPlayer2, nEvals);
    // player2 = new controllers.multiPlayer.ea.Agent(linkState, timer, new SimpleRMHC(nResamples), idPlayer2, nEvals);
    // player1 = new controllers.multiPlayer.smlrand.Agent();
    // player2 = new controllers.multiPlayer.smlrand.Agent();
    // player2 = new controllers.multiPlayer.doNothing.Agent(state, timer, 1);
    // EvoAlg evoAlg2 = new SimpleRMHC(2);
    // player1 = new controllers.multiPlayer.ea.Agent(linkState, timer, evoAlg2, idPlayer1, nEvals);
    // player1 =
    // in milliseconds
    int thinkingTime = 50;
    int delay = 200;
    // player = new controllers.singlePlayer.sampleRandom.Agent(stateObs, timer);
    // check that we can play the game
    int nSteps = 200;
    view = null;
    for (int i = 0; i < nSteps; i++) {
        timer = new ElapsedCpuTimer();
        timer.setMaxTimeMillis(thinkingTime);
        Types.ACTIONS action1 = player1.act(state.copy(), timer);
        timer = new ElapsedCpuTimer();
        timer.setMaxTimeMillis(thinkingTime);
        Types.ACTIONS action2 = player2.act(state.copy(), timer);
        // §action2 =
        state.advance(new Types.ACTIONS[] { action1, action2 });
        if (view != null) {
            view.update((GameState) state.state);
            Thread.sleep(delay);
        }
    // System.out.println("Game tick: " + i);
    }
    System.out.println("Game Score: " + state.getGameScore());
    // System.out.println("MCTS Evals: " + TreeNode);
    return state.getGameScore() > 0 ? 1 : 0;
}
Also used : PlanetWarView(planetwar.PlanetWarView) Agent(controllers.multiPlayer.ea.Agent) Types(ontology.Types) AbstractMultiPlayer(core.player.AbstractMultiPlayer) GameState(planetwar.GameState) EvoAlg(evodef.EvoAlg) Random(java.util.Random) SimpleRMHC(ga.SimpleRMHC) ElapsedCpuTimer(tools.ElapsedCpuTimer)

Example 2 with Agent

use of controllers.multiPlayer.ea.Agent in project SimpleAsteroids by ljialin.

the class GeneralGameRunnerTest method main.

public static void main(String[] args) {
    PlanetWarsLinkState linkState = new PlanetWarsLinkState();
    GeneralGameRunner runner = new GeneralGameRunner().setGame(linkState).setLength(200);
    AbstractMultiPlayer player1, player2;
    GameActionSpaceAdapterMulti.visual = false;
    GameState.includeBuffersInScore = false;
    int idPlayer1 = 0;
    int idPlayer2 = 1;
    ElapsedCpuTimer timer = new ElapsedCpuTimer();
    player1 = new controllers.multiPlayer.discountOLMCTS.Agent(linkState.copy(), timer, idPlayer1);
    // try the evolutionary players
    // GameActionSpaceAdapterMulti.visual = true;
    int nResamples = 1;
    EvoAlg evoAlg = new SimpleRMHC(nResamples);
    int nEvals = 200;
    EvoAlg evoAlg2 = new SlidingMeanEDA().setHistoryLength(20);
    Agent evoAgent1 = new controllers.multiPlayer.ea.Agent(linkState.copy(), timer, evoAlg, idPlayer1, nEvals);
    evoAgent1.sequenceLength = 10;
    // player1 = evoAgent1;
    Agent evoAgent2 = new controllers.multiPlayer.ea.Agent(linkState.copy(), timer, evoAlg, idPlayer2, nEvals);
    evoAgent2.sequenceLength = 10;
    // evoAgent2.setUseShiftBuffer(false);
    player2 = evoAgent2;
    // player2 = new controllers.multiPlayer.ea.Agent(linkState, timer, evoAlg2, idPlayer2, nEvals);
    // player2 = new controllers.multiPlayer.ea.Agent(linkState, timer, new SimpleRMHC(nResamples), idPlayer2, nEvals);
    // player1 = new controllers.multiPlayer.smlrand.Agent();
    // player1 = new controllers.multiPlayer.smlrand.Agent();
    // player2 = new controllers.multiPlayer.doNothing.Agent(linkState.copy(), timer, 1);
    runner.setPlayers(player1, player2);
    int nGames = 100;
    runner.playGames(nGames);
    System.out.println(runner.scores);
}
Also used : Agent(controllers.multiPlayer.ea.Agent) AbstractMultiPlayer(core.player.AbstractMultiPlayer) SlidingMeanEDA(ntuple.SlidingMeanEDA) EvoAlg(evodef.EvoAlg) SimpleRMHC(ga.SimpleRMHC) ElapsedCpuTimer(tools.ElapsedCpuTimer)

Example 3 with Agent

use of controllers.multiPlayer.ea.Agent 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

Agent (controllers.multiPlayer.ea.Agent)3 SimpleRMHC (ga.SimpleRMHC)3 ElapsedCpuTimer (tools.ElapsedCpuTimer)3 AbstractMultiPlayer (core.player.AbstractMultiPlayer)2 EvoAlg (evodef.EvoAlg)2 Random (java.util.Random)2 SlidingMeanEDA (ntuple.SlidingMeanEDA)2 Types (ontology.Types)2 StateObservationMulti (core.game.StateObservationMulti)1 SpaceBattleLinkStateTwoPlayer (gvglink.SpaceBattleLinkStateTwoPlayer)1 NTupleBanditEA (ntuple.NTupleBanditEA)1 GameState (planetwar.GameState)1 PlanetWarView (planetwar.PlanetWarView)1 ElapsedTimer (utilities.ElapsedTimer)1 StatSummary (utilities.StatSummary)1