Search in sources :

Example 21 with JEasyFrame

use of utilities.JEasyFrame in project SimpleAsteroids by ljialin.

the class EvolveMarioLevelTest method getAndShowLevel.

public static int[][] getAndShowLevel(boolean show) throws Exception {
    String inputFile = "data/mario/levels/mario-2-1.txt";
    System.out.println("Reading: " + inputFile);
    int[][] level = flip(readLevel(new Scanner(new FileInputStream(inputFile))));
    level = border(level);
    if (show) {
        LevelView levelView = new LevelView(level).setColorMap(tileColors).setCellSize(10);
        new JEasyFrame(levelView, inputFile);
    }
    return level;
}
Also used : Scanner(java.util.Scanner) LevelView(ntuple.LevelView) JEasyFrame(utilities.JEasyFrame) FileInputStream(java.io.FileInputStream)

Example 22 with JEasyFrame

use of utilities.JEasyFrame in project SimpleAsteroids by ljialin.

the class EvolveMarioLevelTest method plotData.

private void plotData(ArrayList<Double> data) {
    LineChart lineChart = LineChart.easyPlot(data);
    int mid = (data.size() - 1) / 2;
    int end = data.size() - 2;
    System.out.println("Endpoint: " + end);
    lineChart.xAxis = new LineChartAxis(new double[] { 0, mid, end });
    StatSummary ss = new StatSummary().add(data);
    lineChart.yAxis = new LineChartAxis(new double[] { ss.min(), ss.max() });
    lineChart.title = "Evolution of Fitness";
    lineChart.setXLabel("Iterations").setYLabel("Fitness");
    new JEasyFrame(lineChart, "KL-Based PCG");
}
Also used : LineChartAxis(plot.LineChartAxis) StatSummary(utilities.StatSummary) JEasyFrame(utilities.JEasyFrame) LineChart(plot.LineChart)

Example 23 with JEasyFrame

use of utilities.JEasyFrame in project SimpleAsteroids by ljialin.

the class DiffGameViewTest method main.

public static void main(String[] args) throws Exception {
    // stop the game ending
    DiffGame.maxTick = 1000000;
    DiffGame dg = new DiffGame();
    AbstractMultiPlayer player1, player2;
    ElapsedCpuTimer timer = new ElapsedCpuTimer();
    player1 = new controllers.multiPlayer.sampleRandom.Agent(dg, timer, 0);
    player2 = new controllers.multiPlayer.sampleRandom.Agent(dg, timer, 1);
    int idPlayer1 = 0;
    int idPlayer2 = 1;
    // try the evolutionary players
    int nResamples = 3;
    EvoAlg evoAlg = new SimpleRMHC(nResamples);
    EvoAlg evoAlg2 = new SimpleRMHC(nResamples);
    double kExplore = 2;
    int nNeighbours = 50;
    int nEvals = 15000;
    // evoAlg = new NTupleBanditEA(kExplore, nNeighbours);
    controllers.multiPlayer.ea.Agent agentEAShift = new controllers.multiPlayer.ea.Agent(dg, timer, evoAlg, idPlayer1, nEvals);
    agentEAShift.useShiftBuffer = true;
    agentEAShift.sequenceLength = 10;
    player1 = agentEAShift;
    // player2 = new controllers.multiPlayer.discountOLMCTS.Agent(dg, timer, idPlayer2);
    // player2 = new controllers.multiPlayer.sampleOLMCTS.Agent(dg, timer, idPlayer2);
    controllers.multiPlayer.ea.Agent agentEANoShift = new controllers.multiPlayer.ea.Agent(dg, timer, evoAlg2, idPlayer1, nEvals);
    agentEANoShift.useShiftBuffer = true;
    agentEANoShift.sequenceLength = 10;
    player1 = agentEANoShift;
    int nTicks = 250;
    int delay = 100;
    int updateTick = 1;
    DiffGameView view = new DiffGameView(dg);
    JEasyFrame frame = new JEasyFrame(view, "Diff Game");
    KeyPlayer keyPlayer = new KeyPlayer(view);
    frame.addKeyListener(keyPlayer.controller);
    // player1 = keyPlayer;
    player2 = new ConstantPlayer(2);
    Types.ACTIONS[] actions = new Types.ACTIONS[2];
    for (int i = 0; i < nTicks; i++) {
        // update the chosen actions every so often
        if (i % updateTick == 0) {
            actions[0] = player1.act(dg.copy(), timer);
            actions[1] = player2.act(dg.copy(), timer);
        }
        // advance anyway
        dg.advance(actions);
        view.update(dg);
        Thread.sleep(delay);
    }
    System.out.println(dg.getGameScore());
}
Also used : AbstractMultiPlayer(core.player.AbstractMultiPlayer) EvoAlg(evodef.EvoAlg) SimpleRMHC(ga.SimpleRMHC) JEasyFrame(utilities.JEasyFrame) ElapsedCpuTimer(tools.ElapsedCpuTimer)

Example 24 with JEasyFrame

use of utilities.JEasyFrame in project SimpleAsteroids by ljialin.

the class GameActionSpaceAdapterMulti method evaluate.

@Override
public double evaluate(int[] actions) {
    // take a copy of the current game state and accumulate the score as we go along
    // System.out.println("Checking action length: " + actions.length + " : " + sequenceLength);
    // System.out.println("PLayer id: " + playerID);
    StateObservationMulti obs = stateObservation.copy();
    // note the score now - for normalisation reasons
    // we wish to track the change in score, not the absolute score
    double initScore = obs.getGameScore(playerID);
    double discount = 1.0;
    double denom = 0;
    double discountedTot = 0;
    double total = 0;
    // need to do the visual stuff here ...
    LinePlot linePlot = null;
    if (visual) {
        if (lineChart == null) {
            lineChart = new LineChart().setBG(Color.gray);
            lineChart.xAxis = new LineChartAxis(new double[] { 0, sequenceLength / 2, sequenceLength });
            lineChart.yAxis = new LineChartAxis(new double[] { -50, -25, 0, 25, 50 });
            lineChart.plotBG = Color.white;
            lineChart.setYLabel("Score");
            lineChart.setXLabel("Rollout depth");
            frame = new JEasyFrame(lineChart, "Score versus depth");
        }
        float grey = (nEvals % 100) / 150.0f;
        // add in a zero for the first element of the plot, since there
        // will be zero difference before any action has been taken
        linePlot = new LinePlot().setColor(new Color(grey, grey, grey));
    // linePlot = new LinePlot().setColor(Color.red);
    }
    // deltas.add(0);
    for (int i = 0; i < actions.length; i++) {
        // Note here that we need to look at the advance method which takes multiple players
        // hence an array of actions
        // the idea is that we'll pad out the
        int myAction = actions[i];
        int opAction = random.nextInt(obs.getAvailableActions(opponentID).size());
        // opAction = AsteroidsGameState.doNothing;
        Types.ACTIONS[] acts = new Types.ACTIONS[2];
        acts[playerID] = gvgaiActions[myAction];
        acts[opponentID] = gvgaiActions[opAction];
        for (int k = 0; k < actionRepeat; k++) {
            obs.advance(acts);
        }
        discountedTot += discount * (obs.getGameScore(playerID) - initScore);
        if (useHeuristic && obs instanceof SpaceBattleLinkStateTwoPlayer) {
            SpaceBattleLinkStateTwoPlayer state = (SpaceBattleLinkStateTwoPlayer) obs;
            discountedTot += state.getHeuristicScore();
        }
        denom += discount;
        discount *= discountFactor;
        if (linePlot != null) {
            // linePlot.add(discountedTot);
            double delta = obs.getGameScore((playerID)) - initScore;
            linePlot.add(delta);
            deltas.add(delta);
        }
    }
    if (visual) {
        linePlots.add(linePlot);
    }
    nEvals++;
    double delta;
    if (useDiscountFactor) {
        delta = discountedTot / denom;
    } else {
        delta = obs.getGameScore(playerID) - initScore;
    }
    delta += noiseLevel * random.nextGaussian();
    logger.log(delta, actions, false);
    return delta;
}
Also used : LineChartAxis(plot.LineChartAxis) LinePlot(plot.LinePlot) JEasyFrame(utilities.JEasyFrame) StateObservationMulti(core.game.StateObservationMulti) LineChart(plot.LineChart) SpaceBattleLinkStateTwoPlayer(gvglink.SpaceBattleLinkStateTwoPlayer)

Example 25 with JEasyFrame

use of utilities.JEasyFrame in project SimpleAsteroids by ljialin.

the class FeatureView method main.

public static void main(String[] args) throws Exception {
    SimpleBattleState state = new SimpleBattleState();
    FeatureView fv = new FeatureView().setBattleState(state);
    new JEasyFrame(fv, "Feature view");
    while (true) {
        Thread.sleep(50);
        asteroids.Action action = new Action(0, 1, false);
        state.ships[0].update(action);
        AgentCentric ac = new AgentCentric(state.ships[0].s, state.ships[0].d);
        Vector2d tmp = ac.transform(state.ships[1].s);
        int r = (int) tmp.mag();
        int theta = (int) (180 * tmp.theta() / Math.PI);
        System.out.println(r + "\t " + theta);
        fv.repaint();
    }
}
Also used : Action(asteroids.Action) JEasyFrame(utilities.JEasyFrame) Vector2d(math.Vector2d) SimpleBattleState(battle.SimpleBattleState) Action(asteroids.Action)

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