Search in sources :

Example 1 with StateObservationMulti

use of core.game.StateObservationMulti in project SimpleAsteroids by ljialin.

the class GameTickTest method main.

public static void main(String[] args) {
    ElapsedTimer timer = new ElapsedTimer();
    int gameTickMillis = 40;
    int nTicks = 500;
    int nTrials = 10000;
    Random random = new Random();
    for (int i = 0; i < nTrials; i++) {
        StateObservationMulti game = new SpaceBattleLinkStateTwoPlayer();
        // SimpleBattleState game = new SimpleBattleState();
        controllers.multiPlayer.sampleRandom.Agent randomAgent = new controllers.multiPlayer.sampleRandom.Agent(game, null, 0);
        for (int j = 0; j < nTicks; j++) {
            Types.ACTIONS a1 = randomAgent.act(game, null);
            Types.ACTIONS a2 = randomAgent.act(game, null);
            Types.ACTIONS[] actions = new Types.ACTIONS[] { a1, a2 };
            game.advance(actions);
        // int[] actions = new int[]{random.nextInt(game.nActions()), random.nextInt(game.nActions())};
        // game.next(actions);
        }
    // System.out.println(i + "\t " + game.getGameTick());
    }
    int totalTicks = nTicks * nTrials;
    System.out.format("Made %d ticks\n", totalTicks);
    System.out.format("Ticks per milli-second: %.1f\n\n", totalTicks / (double) timer.elapsed());
    // System.out.format("Game tick time = %d ms\n", (int) gameTick);
    System.out.format("Updates per game tick:   %d\n\n", (int) (gameTickMillis * totalTicks / (double) timer.elapsed()));
    System.out.println(timer);
}
Also used : Types(ontology.Types) Random(java.util.Random) ElapsedTimer(utilities.ElapsedTimer) StateObservationMulti(core.game.StateObservationMulti) SpaceBattleLinkStateTwoPlayer(gvglink.SpaceBattleLinkStateTwoPlayer)

Example 2 with StateObservationMulti

use of core.game.StateObservationMulti in project SimpleAsteroids by ljialin.

the class GeneralGameRunner method playGame.

public GeneralGameRunner playGame() {
    StateObservationMulti currentGame = gameState.copy();
    currentGame.setNewSeed(random.nextInt(100000));
    // currentGame.setup()
    // int[] actions = new int[2];
    Types.ACTIONS[] actions = new Types.ACTIONS[2];
    for (int i = 0; i < nSteps; i++) {
        ElapsedCpuTimer timer = new ElapsedCpuTimer();
        actions[0] = p1.act(currentGame.copy(), timer);
        actions[1] = p2.act(currentGame.copy(), timer);
        currentGame.advance(actions);
    }
    scores.add(currentGame.getGameScore());
    if (currentGame.getGameScore() > 0)
        p1Wins++;
    if (currentGame.getGameScore() < 0)
        p2Wins++;
    nGames++;
    return this;
}
Also used : StateObservationMulti(core.game.StateObservationMulti) ElapsedCpuTimer(tools.ElapsedCpuTimer)

Example 3 with StateObservationMulti

use of core.game.StateObservationMulti in project SimpleAsteroids by ljialin.

the class SingleTreeNode method mctsSearch.

public void mctsSearch(ElapsedCpuTimer elapsedTimer) {
    double avgTimeTaken = 0;
    double acumTimeTaken = 0;
    long remaining = elapsedTimer.remainingTimeMillis();
    int numIters = 0;
    int remainingLimit = 5;
    // while(remaining > 2*avgTimeTaken && remaining > remainingLimit){
    int nIterations = 1000;
    while (numIters < nIterations) {
        StateObservationMulti state = rootState.copy();
        ElapsedCpuTimer elapsedTimerIteration = new ElapsedCpuTimer();
        SingleTreeNode selected = treePolicy(state);
        double delta = selected.rollOut(state);
        backUp(selected, delta);
        numIters++;
        acumTimeTaken += (elapsedTimerIteration.elapsedMillis());
        // System.out.println(elapsedTimerIteration.elapsedMillis() + " --> " + acumTimeTaken + " (" + remaining + ")");
        avgTimeTaken = acumTimeTaken / numIters;
        remaining = elapsedTimer.remainingTimeMillis();
        nIts++;
    }
// System.out.println("Total iterations = " + nIts);
}
Also used : StateObservationMulti(core.game.StateObservationMulti) ElapsedCpuTimer(tools.ElapsedCpuTimer)

Example 4 with StateObservationMulti

use of core.game.StateObservationMulti in project SimpleAsteroids by ljialin.

the class SpaceBattleLinkTestTwoPlayer method runTrial.

public static double runTrial(boolean runVisible) {
    // make an agent to test
    // StateObservation stateObs = new SimpleMaxGame();
    // BattleGameSearchSpace.inject(BattleGameSearchSpace.getRandomPoint());
    // SampleEvolvedParams.solutions[1][2] = 5;
    // SampleEvolvedParams.solutions[5][4] = 0;
    // BattleGameSearchSpace.inject(SampleEvolvedParams.solutions[2]);
    // BattleGameSearchSpace.inject(SampleEvolvedParams.solutions[2]);
    // BattleGameSearchSpace.inject(SampleEvolvedParams.solutions[1]);
    System.out.println("Params are:");
    System.out.println(BattleGameParameters.params);
    // 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;
    // // supercl
    // StateObservation stateObs = linkState;
    ElapsedCpuTimer timer = new ElapsedCpuTimer();
    AbstractMultiPlayer player1, player2;
    // controllers.singlePlayer.sampleOLMCTS.Agent olmcts =
    // new controllers.singlePlayer.sampleOLMCTS.Agent(linkState, timer);
    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 = 10;
    int nNeighbours = 100;
    int nEvals = 500;
    evoAlg = new NTupleBanditEA(kExplore, nNeighbours);
    // evoAlg = new CompactSlidingModelGA().setHistoryLength(20);
    evoAlg = new SlidingMeanEDA().setHistoryLength(20);
    EvoAlg evoAlg2 = new CompactSlidingModelGA().setHistoryLength(2);
    player1 = new controllers.multiPlayer.ea.Agent(linkState, timer, evoAlg, idPlayer1, nEvals);
    // 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();
    // EvoAlg evoAlg2 = new SimpleRMHC(2);
    // player1 = new controllers.multiPlayer.ea.Agent(linkState, timer, evoAlg2, idPlayer1, nEvals);
    // in milliseconds
    int thinkingTime = 50;
    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();
    BattleView view = new BattleView(linkState.state.copyState());
    // set view to null to run fast with no visuals
    if (!runVisible)
        view = null;
    if (view != null) {
        new JEasyFrame(view, "Simple Battle Game");
    }
    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 = player1.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 });
        if (view != null) {
            view.game = linkState.state.copyState();
            view.setRolls(0, evoAlg);
            view.setRolls(1, evoAlg2);
            view.repaint();
            try {
                Thread.sleep(delay);
            } catch (Exception e) {
            }
        }
    // System.out.println(multi.getGameScore());
    }
    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);
    return multi.getGameScore(0);
}
Also used : Types(ontology.Types) BattleView(battle.BattleView) AbstractMultiPlayer(core.player.AbstractMultiPlayer) SlidingMeanEDA(ntuple.SlidingMeanEDA) NTupleBanditEA(ntuple.NTupleBanditEA) EvoAlg(evodef.EvoAlg) StatSummary(utilities.StatSummary) SimpleRMHC(ga.SimpleRMHC) Random(java.util.Random) JEasyFrame(utilities.JEasyFrame) CompactSlidingModelGA(ntuple.CompactSlidingModelGA) StateObservationMulti(core.game.StateObservationMulti) ElapsedTimer(utilities.ElapsedTimer) ElapsedCpuTimer(tools.ElapsedCpuTimer)

Example 5 with StateObservationMulti

use of core.game.StateObservationMulti in project SimpleAsteroids by ljialin.

the class TestDiffGame method runTrial.

public static double runTrial(boolean runVisible) {
    // make an agent to test
    DiffGame.nValues = 21;
    DiffGame.minscore = 0;
    StateObservationMulti stateObs = new DiffGame();
    DefaultMutator.totalRandomChaosMutation = false;
    ElapsedCpuTimer timer = new ElapsedCpuTimer();
    AbstractMultiPlayer player1, player2;
    int idPlayer1 = 0;
    int idPlayer2 = 1;
    // try the evolutionary players
    int nResamples = 10;
    EvoAlg evoAlg = new SimpleRMHC(nResamples);
    EvoAlg evoAlg2 = new SimpleRMHC(nResamples);
    double kExplore = 2;
    int nNeighbours = 50;
    int nEvals = 1500;
    // evoAlg = new NTupleBanditEA(kExplore, nNeighbours);
    controllers.multiPlayer.ea.Agent agentEAShift = new controllers.multiPlayer.ea.Agent(stateObs, timer, evoAlg, idPlayer1, nEvals);
    agentEAShift.useShiftBuffer = true;
    agentEAShift.sequenceLength = 5;
    player1 = agentEAShift;
    controllers.multiPlayer.ea.Agent agentEANoShift = new controllers.multiPlayer.ea.Agent(stateObs, timer, evoAlg2, idPlayer2, nEvals);
    agentEANoShift.useShiftBuffer = true;
    agentEANoShift.sequenceLength = 25;
    player2 = agentEANoShift;
    // player2 = new controllers.multiPlayer.discountOLMCTS.Agent(stateObs, timer, idPlayer2);
    // player2 = new controllers.multiPlayer.doNothing.Agent(stateObs, timer, idPlayer2);
    // player2 = new controllers.multiPlayer.sampleRandom.Agent(stateObs, timer, idPlayer2);
    // player1  = new controllers.multiPlayer.smlrand.Agent();
    // EvoAlg evoAlg2 = new SimpleRMHC(1);
    // player1 = new controllers.multiPlayer.ea.Agent(linkState, timer, evoAlg2, idPlayer1, nEvals / 5);
    // in milliseconds
    int thinkingTime = 50;
    int delay = 10;
    // player = new controllers.singlePlayer.sampleRandom.Agent(stateObs, timer);
    // check that we can play the game
    Random random = new Random();
    int nSteps = 50;
    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 && !stateObs.isGameOver(); i++) {
        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 = DiffGame.nTicks;
        Types.ACTIONS action1 = player1.act(stateObs.copy(), timer);
        sst1.add(t1.elapsed());
        ticks = DiffGame.nTicks - ticks;
        ssTicks1.add(ticks);
        System.out.println("Player 1 Ticks = " + ticks);
        ElapsedTimer t2 = new ElapsedTimer();
        ticks = DiffGame.nTicks;
        Types.ACTIONS action2 = player2.act(stateObs.copy(), timer);
        sst2.add(t2.elapsed());
        ticks = DiffGame.nTicks - ticks;
        ssTicks2.add(ticks);
        stateObs.advance(new Types.ACTIONS[] { action1, action2 });
        // System.out.println(multi.getGameScore());
        System.out.println(stateObs);
    }
    System.out.println(stateObs.getGameScore());
    System.out.println(stateObs.isGameOver());
    // System.out.println(SingleTreeNode.rollOutScores);
    System.out.println(sst1);
    System.out.println(sst2);
    System.out.println(ssTicks1);
    System.out.println(ssTicks2);
    return stateObs.getGameScore(0);
}
Also used : Types(ontology.Types) AbstractMultiPlayer(core.player.AbstractMultiPlayer) EvoAlg(evodef.EvoAlg) StatSummary(utilities.StatSummary) SimpleRMHC(ga.SimpleRMHC) Random(java.util.Random) StateObservationMulti(core.game.StateObservationMulti) ElapsedTimer(utilities.ElapsedTimer) DiffGame(numbergame.DiffGame) ElapsedCpuTimer(tools.ElapsedCpuTimer)

Aggregations

StateObservationMulti (core.game.StateObservationMulti)8 ElapsedCpuTimer (tools.ElapsedCpuTimer)5 Random (java.util.Random)4 Types (ontology.Types)4 ElapsedTimer (utilities.ElapsedTimer)4 SimpleRMHC (ga.SimpleRMHC)3 SpaceBattleLinkStateTwoPlayer (gvglink.SpaceBattleLinkStateTwoPlayer)3 StatSummary (utilities.StatSummary)3 AbstractMultiPlayer (core.player.AbstractMultiPlayer)2 EvoAlg (evodef.EvoAlg)2 NTupleBanditEA (ntuple.NTupleBanditEA)2 SlidingMeanEDA (ntuple.SlidingMeanEDA)2 JEasyFrame (utilities.JEasyFrame)2 BattleView (battle.BattleView)1 Agent (controllers.multiPlayer.ea.Agent)1 CompactSlidingModelGA (ntuple.CompactSlidingModelGA)1 DiffGame (numbergame.DiffGame)1 LineChart (plot.LineChart)1 LineChartAxis (plot.LineChartAxis)1 LinePlot (plot.LinePlot)1