Search in sources :

Example 56 with ElapsedTimer

use of utilities.ElapsedTimer in project SimpleAsteroids by ljialin.

the class GeneralGameRunner method playGames.

public GeneralGameRunner playGames(int n) {
    // plays an additional n games without resetting the stats
    ElapsedTimer t = new ElapsedTimer();
    for (int i = 0; i < n; i++) {
        System.out.println("Playing game: " + i);
        playGame();
    }
    if (verbose) {
        System.out.println(scores);
        System.out.println();
        System.out.println("p1 wins:\t " + p1Wins);
        System.out.println("p2 wins:\t " + p2Wins);
        System.out.println("n games:\t " + nGames);
        System.out.println(t);
    }
    System.out.println();
    return this;
}
Also used : ElapsedTimer(utilities.ElapsedTimer)

Example 57 with ElapsedTimer

use of utilities.ElapsedTimer in project SimpleAsteroids by ljialin.

the class GameState method main.

public static void main(String[] args) {
    // set a fixed random seed in order to check the outcome
    Random random = new Random(3);
    GameState.random = random;
    GameState gameState = new GameState().setNPlanets(10);
    gameState.setRandomGrowthRates().setRandomOwnerships();
    int nTicks = 20000000;
    ElapsedTimer timer = new ElapsedTimer();
    System.out.format("Initial score: %.2f\n", gameState.getScore());
    int terminals = 0;
    System.out.println(timer);
    for (int i = 0; i < nTicks; i++) {
        int a1 = random.nextInt(GameState.nActions);
        int a2 = random.nextInt(GameState.nActions);
        // a1 = AsteroidsGameState.doNothing;
        // a2 = AsteroidsGameState.doNothing;
        int[] actions = new int[] { a1, a2 };
        gameState.next(actions);
        // gameState = gameState.copy();
        if (gameState.isTerminal()) {
            terminals++;
        }
    }
    System.out.println(timer);
    System.out.println(Arrays.toString(gameState.planets));
    System.out.println(Arrays.toString(gameState.growthRates));
    System.out.println("Terminal states: " + terminals);
    System.out.println();
    System.out.format("Final score: %.2f\n", gameState.getScore());
}
Also used : Random(java.util.Random) ElapsedTimer(utilities.ElapsedTimer)

Example 58 with ElapsedTimer

use of utilities.ElapsedTimer in project SimpleAsteroids by ljialin.

the class RandomTestAsteroids method main.

public static void main(String[] args) {
    int nTrials = 100;
    SimplePlayerInterface player = new RandomAgent();
    StatSummary ss = new StatSummary("Random Player on Asteroids");
    ElapsedTimer t = new ElapsedTimer();
    for (int i = 0; i < nTrials; i++) {
        ss.add(evaluate(player));
    }
    System.out.println(ss);
    System.out.println();
    System.out.println(t);
}
Also used : StatSummary(utilities.StatSummary) ElapsedTimer(utilities.ElapsedTimer)

Example 59 with ElapsedTimer

use of utilities.ElapsedTimer in project SimpleAsteroids by ljialin.

the class GsonTest method main.

public static void main(String[] args) {
    GsonTest gt = new GsonTest(10000);
    String out = new Gson().toJson(gt);
    ElapsedTimer t = new ElapsedTimer();
    out = new Gson().toJson(gt);
    System.out.println("Length of: " + out.length());
    System.out.println(t);
    String out2 = getString(gt.a);
    System.out.println("Length of: " + out2.length());
    System.out.println(t);
}
Also used : Gson(com.google.gson.Gson) ElapsedTimer(utilities.ElapsedTimer)

Example 60 with ElapsedTimer

use of utilities.ElapsedTimer 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

ElapsedTimer (utilities.ElapsedTimer)63 StatSummary (utilities.StatSummary)27 Random (java.util.Random)12 SimpleRMHC (ga.SimpleRMHC)9 Types (ontology.Types)9 ElapsedCpuTimer (tools.ElapsedCpuTimer)9 EvoAlg (evodef.EvoAlg)7 NTupleBanditEA (ntuple.NTupleBanditEA)7 JEasyFrame (utilities.JEasyFrame)7 ArrayList (java.util.ArrayList)6 AbstractPlayer (core.player.AbstractPlayer)5 Gson (com.google.gson.Gson)4 Agent (controllers.singlePlayer.ea.Agent)4 StateObservationMulti (core.game.StateObservationMulti)4 StateObservation (core.game.StateObservation)3 AbstractMultiPlayer (core.player.AbstractMultiPlayer)3 GameParameters (evogame.GameParameters)3 ConvNTuple (ntuple.ConvNTuple)3 SlidingMeanEDA (ntuple.SlidingMeanEDA)3 SimpleMaxGame (altgame.SimpleMaxGame)2