Search in sources :

Example 16 with JEasyFrame

use of utilities.JEasyFrame in project SimpleAsteroids by ljialin.

the class AsteroidsSimpleTest method runOnce.

public static double runOnce() throws Exception {
    AsteroidsLinkState stateObs = new AsteroidsLinkState();
    ElapsedCpuTimer timer = new ElapsedCpuTimer();
    AbstractPlayer player = new controllers.singlePlayer.discountOLMCTS.Agent(stateObs, timer);
    int depth = 100;
    int ticks = 2000;
    SingleTreeNode.DEFAULT_ROLLOUT_DEPTH = depth;
    SingleTreeNode.scoreDiscountFactor = 0.999;
    SingleTreeNode.useScoreDiscount = true;
    SingleTreeNode.DEFAULT_ROLLOUT_DEPTH = depth;
    controllers.singlePlayer.discountOLMCTS.Agent.MCTS_ITERATIONS = ticks / depth;
    // in milliseconds
    int thinkingTime = 10;
    int delay = 20;
    int nSteps = 1000;
    ElapsedTimer t = new ElapsedTimer();
    View view = new View(stateObs.state);
    // set view to null to run fast with no visuals
    view = null;
    JEasyFrame frame;
    if (view != null) {
        frame = new JEasyFrame(view, "Asteroids");
    }
    for (int i = 0; i < nSteps && !stateObs.isGameOver(); i++) {
        timer = new ElapsedCpuTimer();
        timer.setMaxTimeMillis(thinkingTime);
        Types.ACTIONS action = player.act(stateObs.copy(), timer);
        stateObs.advance(action);
        if (view != null) {
            view.repaint();
            Thread.sleep(delay);
        }
    }
    System.out.println(stateObs.getGameScore());
    System.out.println(stateObs.isGameOver());
    System.out.println(t);
    System.out.println("Agent of type: " + player.getClass().getSimpleName());
    return stateObs.getGameScore();
}
Also used : Types(ontology.Types) JEasyFrame(utilities.JEasyFrame) AbstractPlayer(core.player.AbstractPlayer) ElapsedTimer(utilities.ElapsedTimer) ElapsedCpuTimer(tools.ElapsedCpuTimer) View(asteroids.View)

Example 17 with JEasyFrame

use of utilities.JEasyFrame in project SimpleAsteroids by ljialin.

the class AsteroidsTest method runOnce.

public static double runOnce() throws Exception {
    // make an agent to test
    // AsteroidsLinkState.defaultStartLevel = 1;
    AsteroidsLinkState stateObs = new AsteroidsLinkState();
    System.out.println(stateObs.getGameScore());
    System.out.println(stateObs.copy().getGameScore());
    ElapsedCpuTimer timer = new ElapsedCpuTimer();
    AbstractPlayer player;
    controllers.singlePlayer.sampleOLMCTS.Agent olmcts = new controllers.singlePlayer.sampleOLMCTS.Agent(stateObs, timer);
    controllers.singlePlayer.discountOLMCTS.Agent discountOlmcts = new controllers.singlePlayer.discountOLMCTS.Agent(stateObs, timer);
    controllers.singlePlayer.nestedMC.Agent nestedMC = new controllers.singlePlayer.nestedMC.Agent(stateObs, timer);
    int depth = 100;
    int ticks = 2000;
    controllers.singlePlayer.discountOLMCTS.SingleTreeNode.DEFAULT_ROLLOUT_DEPTH = depth;
    SingleTreeNode.scoreDiscountFactor = 0.999;
    SingleTreeNode.useScoreDiscount = true;
    controllers.singlePlayer.discountOLMCTS.SingleTreeNode.DEFAULT_ROLLOUT_DEPTH = depth;
    controllers.singlePlayer.discountOLMCTS.Agent.MCTS_ITERATIONS = ticks / depth;
    // player = olmcts;
    player = discountOlmcts;
    int nResamples = 1;
    EvoAlg evoAlg = new SimpleRMHC(nResamples);
    int nEvals = 20;
    double kExplore = 10;
    int nNeighbours = 100;
    // evoAlg = new NTupleBanditEA(kExplore, nNeighbours);
    // evoAlg = new SlidingMeanEDA();
    DefaultMutator.totalRandomChaosMutation = true;
    Agent.useShiftBuffer = true;
    controllers.singlePlayer.ea.Agent.SEQUENCE_LENGTH = ticks / nEvals;
    player = new controllers.singlePlayer.ea.Agent(stateObs, timer, evoAlg, nEvals);
    // player = new controllers.singlePlayer.ea.Agent(stateObs, timer);
    // nestedMC.maxRolloutLength = 5;
    // nestedMC.nestDepth = 5;
    // player = nestedMC;
    // in milliseconds
    int thinkingTime = 10;
    int delay = 20;
    // player = new controllers.singlePlayer.sampleRandom.Agent(stateObs, timer);
    // check that we can play the game
    Random random = new Random();
    // this is how many steps we'll take in the actual game ...
    int nSteps = 1000;
    ElapsedTimer t = new ElapsedTimer();
    View view = new View(stateObs.state);
    // set view to null to run fast with no visuals
    view = null;
    JEasyFrame frame;
    if (view != null) {
        frame = new JEasyFrame(view, "Asteroids");
    }
    for (int i = 0; i < nSteps && !stateObs.isGameOver(); i++) {
        timer = new ElapsedCpuTimer();
        timer.setMaxTimeMillis(thinkingTime);
        Types.ACTIONS action = player.act(stateObs.copy(), timer);
        // System.out.println("Selected: " + action); //  + "\t " + action.ordinal());
        stateObs.advance(action);
        // System.out.println(stateObs.getGameScore());
        if (view != null) {
            view.repaint();
            Thread.sleep(delay);
        }
    }
    System.out.println(stateObs.getGameScore());
    System.out.println(stateObs.isGameOver());
    System.out.println(t);
    System.out.println("Agent of type: " + player.getClass().getSimpleName());
    return stateObs.getGameScore();
}
Also used : Agent(controllers.singlePlayer.ea.Agent) Types(ontology.Types) Agent(controllers.singlePlayer.ea.Agent) View(asteroids.View) EvoAlg(evodef.EvoAlg) SimpleRMHC(ga.SimpleRMHC) Random(java.util.Random) JEasyFrame(utilities.JEasyFrame) AbstractPlayer(core.player.AbstractPlayer) ElapsedTimer(utilities.ElapsedTimer) ElapsedCpuTimer(tools.ElapsedCpuTimer)

Example 18 with JEasyFrame

use of utilities.JEasyFrame 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 19 with JEasyFrame

use of utilities.JEasyFrame in project SimpleAsteroids by ljialin.

the class HyperParamTuneRunner method plotConvergence.

private void plotConvergence(EvolutionLogger logger, int[] solution) {
    LineChart lineChart = new LineChart().setBG(Color.gray);
    lineChart.plotBG = Color.white;
    // now the plots we will add are as follows
    ArrayList<Double> bestMatches = new ArrayList<>();
    ArrayList<Double> bestCumulative = new ArrayList<>();
    ArrayList<Double> sampleMatches = new ArrayList<>();
    ArrayList<Double> sampleCumulative = new ArrayList<>();
    StatSummary cumul = new StatSummary();
    for (int[] best : logger.bestYetSolutions) {
        double x = match(best, solution);
        bestMatches.add(x);
        cumul.add(x);
        bestCumulative.add(cumul.mean());
    }
    StatSummary sample = new StatSummary();
    for (int[] sol : logger.solutions) {
        double x = match(sol, solution);
        sampleMatches.add(x);
        sample.add(x);
        sampleCumulative.add(sample.mean());
    }
    // LinePlot matchPlot = new LinePlot().
    LineGroup lgMatch = new LineGroup().setName("Best Match").setColor(Color.red).add(bestMatches);
    LineGroup lgCumul = new LineGroup().setName("Best Cumul").setColor(Color.black).add(bestCumulative);
    lineChart.addLineGroup(lgMatch);
    lineChart.addLineGroup(lgCumul);
    LineGroup lgSampleMatch = new LineGroup().setName("Sample Match").setColor(Color.green).add(sampleMatches);
    LineGroup lgSampleCumul = new LineGroup().setName("Sample Cumul").setColor(Color.magenta).add(sampleCumulative);
    lineChart.addLineGroup(lgSampleMatch);
    lineChart.addLineGroup(lgSampleCumul);
    lineChart.setXLabel("Iteration");
    lineChart.setYLabel("Candidate == solution");
    lineChart.yAxis = new LineChartAxis(new double[] { 0, 1 });
    lineChart.xAxis = new LineChartAxis(new double[] { 0, bestMatches.size() / 2, bestMatches.size() });
    new JEasyFrame(lineChart, "NTBEA Best Guess Convergence");
}
Also used : StatSummary(utilities.StatSummary) LineChartAxis(plot.LineChartAxis) JEasyFrame(utilities.JEasyFrame) ArrayList(java.util.ArrayList) LineGroup(plot.LineGroup) LineChart(plot.LineChart)

Example 20 with JEasyFrame

use of utilities.JEasyFrame in project SimpleAsteroids by ljialin.

the class MarioReader method showLevels.

public static void showLevels() throws Exception {
    // String inputFile = "data/mario/example.txt";
    String inputDirectory = "data/mario/levels/";
    String outputFile = "data/mario/example.json";
    // need to iterate over all the files in a directory
    File file = new File(inputDirectory);
    String[] fileList = file.list();
    for (String inputFile : fileList) {
        try {
            System.out.println("Reading: " + inputFile);
            int[][] level = readLevel(new Scanner(new FileInputStream(inputDirectory + inputFile)));
            LevelView levelView = new LevelView(flip(level)).setColorMap(tileColors).setCellSize(10);
            new JEasyFrame(levelView, inputFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Also used : Scanner(java.util.Scanner) LevelView(ntuple.LevelView) JEasyFrame(utilities.JEasyFrame) File(java.io.File) FileInputStream(java.io.FileInputStream)

Aggregations

JEasyFrame (utilities.JEasyFrame)30 Random (java.util.Random)9 StatSummary (utilities.StatSummary)8 LineChart (plot.LineChart)7 ElapsedTimer (utilities.ElapsedTimer)7 LineChartAxis (plot.LineChartAxis)6 EvoAlg (evodef.EvoAlg)5 SimpleRMHC (ga.SimpleRMHC)5 ElapsedCpuTimer (tools.ElapsedCpuTimer)5 Types (ontology.Types)4 AbstractPlayer (core.player.AbstractPlayer)3 ArrayList (java.util.ArrayList)3 Scanner (java.util.Scanner)3 LevelView (ntuple.LevelView)3 NTupleBanditEA (ntuple.NTupleBanditEA)3 LineGroup (plot.LineGroup)3 LinePlot (plot.LinePlot)3 View (asteroids.View)2 BattleView (battle.BattleView)2 Agent (controllers.singlePlayer.ea.Agent)2