Search in sources :

Example 1 with SlidingMeanEDA

use of ntuple.SlidingMeanEDA in project SimpleAsteroids by ljialin.

the class Test method main.

public static void main(String[] args) {
    // pre-load some classes
    EvoAlg alg = new SlidingMeanEDA();
    // Available controllers:
    String sampleRandomController = "controllers.singlePlayer.sampleRandom.Agent";
    String doNothingController = "controllers.singlePlayer.doNothing.Agent";
    String sampleOneStepController = "controllers.singlePlayer.sampleonesteplookahead.Agent";
    String sampleMCTSController = "controllers.singlePlayer.sampleMCTS.Agent";
    String sampleFlatMCTSController = "controllers.singlePlayer.sampleFlatMCTS.Agent";
    String sampleOLMCTSController = "controllers.singlePlayer.sampleOLMCTS.Agent";
    String sampleGAController = "controllers.singlePlayer.sampleGA.Agent";
    String sampleOLETSController = "controllers.singlePlayer.olets.Agent";
    String repeatOLETS = "controllers.singlePlayer.repeatOLETS.Agent";
    String slidingEA = "controllers.singlePlayer.ea.Agent";
    // Available Generators
    String randomLevelGenerator = "levelGenerators.randomLevelGenerator.LevelGenerator";
    String geneticGenerator = "levelGenerators.geneticLevelGenerator.LevelGenerator";
    String constructiveLevelGenerator = "levelGenerators.constructiveLevelGenerator.LevelGenerator";
    // Available games:
    String gamesPath = "examples/gridphysics/";
    String[] games = new String[] {};
    String generateLevelPath = "examples/gridphysics/";
    // All public games
    games = new String[] { // 0-4
    "aliens", // 0-4
    "angelsdemons", // 0-4
    "assemblyline", // 0-4
    "avoidgeorge", // 0-4
    "bait", // 5-9
    "beltmanager", // 5-9
    "blacksmoke", // 5-9
    "boloadventures", // 5-9
    "bomber", // 5-9
    "bomberman", // 10-14
    "boulderchase", // 10-14
    "boulderdash", // 10-14
    "brainman", // 10-14
    "butterflies", // 10-14
    "cakybaky", // 15-19
    "camelRace", // 15-19
    "catapults", // 15-19
    "chainreaction", // 15-19
    "chase", // 15-19
    "chipschallenge", // 20-24
    "clusters", // 20-24
    "colourescape", // 20-24
    "chopper", // 20-24
    "cookmepasta", // 20-24
    "cops", // 25-29
    "crossfire", // 25-29
    "defem", // 25-29
    "defender", // 25-29
    "digdug", // 25-29
    "dungeon", // 30-34
    "eighthpassenger", // 30-34
    "eggomania", // 30-34
    "enemycitadel", // 30-34
    "escape", // 30-34
    "factorymanager", // 35-39
    "firecaster", // 35-39
    "fireman", // 35-39
    "firestorms", // 35-39
    "freeway", // 35-39
    "frogs", // 40-44
    "garbagecollector", // 40-44
    "gymkhana", // 40-44
    "hungrybirds", // 40-44
    "iceandfire", // 40-44
    "ikaruga", // 45-49
    "infection", // 45-49
    "intersection", // 45-49
    "islands", // 45-49
    "jaws", // 45-49
    "killbillVol1", // 50-54
    "labyrinth", // 50-54
    "labyrinthdual", // 50-54
    "lasers", // 50-54
    "lasers2", // 50-54
    "lemmings", // 55-59
    "missilecommand", // 55-59
    "modality", // 55-59
    "overload", // 55-59
    "pacman", // 55-59
    "painter", // 60-64
    "pokemon", // 60-64
    "plants", // 60-64
    "plaqueattack", // 60-64
    "portals", // 60-64
    "racebet", // 65-69
    "raceBet2", // 65-69
    "realportals", // 65-69
    "realsokoban", // 65-69
    "rivers", // 65-69
    "roadfighter", // 70-74
    "roguelike", // 70-74
    "run", // 70-74
    "seaquest", // 70-74
    "sheriff", // 70-74
    "shipwreck", // 75-79
    "sokoban", // 75-79
    "solarfox", // 75-79
    "superman", // 75-79
    "surround", // 75-79
    "survivezombies", // 80-84
    "tercio", // 80-84
    "thecitadel", // 80-84
    "thesnowman", // 80-84
    "waitforbreakfast", // 80-84
    "watergame", // 85-89
    "waves", // 85-89
    "whackamole", // 85-89
    "wildgunman", // 85-89
    "witnessprotection", // 85-89
    "wrapsokoban", "zelda", // 90, 91
    "zenpuzzle" };
    // Other settings
    boolean visuals = true;
    int seed = new Random().nextInt();
    // Game and level to play
    // int gameIdx = 0;  // aliens
    // int gameIdx = 28; // dig-dug
    // 
    int gameIdx = 72;
    // level names from 0 to 4 (game_lvlN.txt).
    int levelIdx = 0;
    String game = gamesPath + games[gameIdx] + ".txt";
    String level1 = gamesPath + games[gameIdx] + "_lvl" + levelIdx + ".txt";
    String recordLevelFile = generateLevelPath + games[gameIdx] + "_glvl.txt";
    // "actions_" + games[gameIdx] + "_lvl" + levelIdx + "_" + seed + ".txt"; //where to record the actions executed. null if not to save.
    String recordActionsFile = null;
    // 1. This starts a game, in a level, played by a human.
    // ArcadeMachine.playOneGame(game, level1, recordActionsFile, seed);
    core.competition.CompetitionParameters.TIMER_TYPE = ElapsedCpuTimer.TimerType.WALL_TIME;
    // TIME_CONSTRAINED = false;
    CompetitionParameters.LEVEL_ACTION_TIME = 10000000;
    // TIME_CONSTRAINED = false;
    CompetitionParameters.LEVEL_ACTION_TIME_DISQ = 10000000;
    // 2. This plays a game in a level by the controller.
    ArcadeMachine.runOneGame(game, level1, visuals, slidingEA, recordActionsFile, seed, 0);
    System.out.println("\nRunning next game\n");
    ArcadeMachine.runOneGame(game, level1, visuals, sampleMCTSController, recordActionsFile, seed, 0);
    System.out.println(Agent.nanoTimer);
    System.out.println(Agent.milliTimer);
    System.out.println(Agent.diffTimer);
// ArcadeMachine.runOneGame(game, level1, visuals, sampleRandomController, recordActionsFile, seed, 0);
// 3. This replays a game from an action file previously recorded
// String readActionsFile = recordActionsFile;
// ArcadeMachine.replayGame(game, level1, visuals, readActionsFile);
// 4. This plays a single game, in N levels, M times :
// String level2 = gamesPath + games[gameIdx] + "_lvl" + 1 +".txt";
// int M = 10;
// for(int i=0; i<games.length; i++){
// game = gamesPath + games[i] + ".txt";
// level1 = gamesPath + games[i] + "_lvl" + levelIdx +".txt";
// ArcadeMachine.runGames(game, new String[]{level1}, M, sampleMCTSController, null);
// }
// 5. This starts a game, in a generated level created by a specific level generator
// if(ArcadeMachine.generateOneLevel(game, randomLevelGenerator, recordLevelFile)){
// ArcadeMachine.playOneGeneratedLevel(game, recordActionsFile, recordLevelFile, seed);
// }
// 6. This plays N games, in the first L levels, M times each. Actions to file optional (set saveActions to true).
// int N = 82, L = 5, M = 1;
// boolean saveActions = false;
// String[] levels = new String[L];
// String[] actionFiles = new String[L*M];
// for(int i = 0; i < N; ++i)
// {
// int actionIdx = 0;
// game = gamesPath + games[i] + ".txt";
// for(int j = 0; j < L; ++j){
// levels[j] = gamesPath + games[i] + "_lvl" + j +".txt";
// if(saveActions) for(int k = 0; k < M; ++k)
// actionFiles[actionIdx++] = "actions_game_" + i + "_level_" + j + "_" + k + ".txt";
// }
// ArcadeMachine.runGames(game, levels, M, sampleMCTSController, saveActions? actionFiles:null);
// }
}
Also used : Random(java.util.Random) SlidingMeanEDA(ntuple.SlidingMeanEDA) EvoAlg(evodef.EvoAlg)

Example 2 with SlidingMeanEDA

use of ntuple.SlidingMeanEDA in project SimpleAsteroids by ljialin.

the class TestHyperParamAsteroids method main.

public static void main(String[] args) {
    AnnotatedFitnessSpace testAsteroids = new EvoAgentSearchSpaceAsteroids();
    EvoAlg[] evoAlgs = { new NTupleBanditEA().setKExplore(10000), // new CompactSlidingGA(),
    new SlidingMeanEDA() };
    int nChecks = 50;
    int nEvals = 50;
    int nTrials = 2;
    for (EvoAlg evoAlg : evoAlgs) {
        HyperParamTuneRunner runner = new HyperParamTuneRunner();
        runner.nChecks = nChecks;
        runner.nTrials = nTrials;
        runner.nEvals = nEvals;
        runner.runTrials(evoAlg, testAsteroids);
    }
}
Also used : AnnotatedFitnessSpace(evodef.AnnotatedFitnessSpace) EvoAgentSearchSpaceAsteroids(planetwar.EvoAgentSearchSpaceAsteroids) SlidingMeanEDA(ntuple.SlidingMeanEDA) NTupleBanditEA(ntuple.NTupleBanditEA) EvoAlg(evodef.EvoAlg)

Example 3 with SlidingMeanEDA

use of ntuple.SlidingMeanEDA in project SimpleAsteroids by ljialin.

the class SimpleGridTest method runOnce.

public static double runOnce() {
    // make an agent to test
    StateObservation gridGame = new GridModel();
    System.out.println(gridGame.getGameScore());
    System.out.println(gridGame.copy().getGameScore());
    // System.exit(0);
    ElapsedCpuTimer timer = new ElapsedCpuTimer();
    AbstractPlayer player;
    controllers.singlePlayer.sampleOLMCTS.Agent olmcts = new controllers.singlePlayer.sampleOLMCTS.Agent(gridGame, timer);
    controllers.singlePlayer.discountOLMCTS.Agent discountOlmcts = new controllers.singlePlayer.discountOLMCTS.Agent(gridGame, timer);
    controllers.singlePlayer.nestedMC.Agent nestedMC = new controllers.singlePlayer.nestedMC.Agent(gridGame, timer);
    player = olmcts;
    // player = discountOlmcts;
    // for the following we can pass the Evolutionary algorithm to use
    int nResamples = 2;
    EvoAlg evoAlg = new SimpleRMHC(nResamples);
    int nEvals = 2000;
    double kExplore = 10;
    int nNeighbours = 100;
    evoAlg = new NTupleBanditEA(kExplore, nNeighbours);
    evoAlg = new SlidingMeanEDA();
    // DefaultMutator.totalRandomChaosMutation = false;
    Agent.useShiftBuffer = true;
    Agent.SEQUENCE_LENGTH = 30;
    player = new Agent(gridGame, timer, evoAlg, nEvals);
    nestedMC.maxRolloutLength = 30;
    nestedMC.nestDepth = 3;
    // player = nestedMC;
    // in milliseconds
    int thinkingTime = 50;
    int delay = 30;
    // 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 = 30;
    ElapsedTimer t = new ElapsedTimer();
    for (int i = 0; i < nSteps && !gridGame.isGameOver(); i++) {
        timer = new ElapsedCpuTimer();
        timer.setMaxTimeMillis(thinkingTime);
        Types.ACTIONS action = player.act(gridGame.copy(), timer);
        System.out.println();
        // + "\t " + action.ordinal());
        System.out.println("Selected: " + action);
        gridGame.advance(action);
        System.out.println("Game state: " + gridGame);
        System.out.println();
    }
    System.out.println(gridGame.getGameScore());
    return gridGame.getGameScore();
}
Also used : Agent(controllers.singlePlayer.ea.Agent) Types(ontology.Types) GridModel(rl.grid.GridModel) SlidingMeanEDA(ntuple.SlidingMeanEDA) NTupleBanditEA(ntuple.NTupleBanditEA) EvoAlg(evodef.EvoAlg) StateObservation(core.game.StateObservation) SimpleRMHC(ga.SimpleRMHC) Random(java.util.Random) AbstractPlayer(core.player.AbstractPlayer) ElapsedTimer(utilities.ElapsedTimer) ElapsedCpuTimer(tools.ElapsedCpuTimer)

Example 4 with SlidingMeanEDA

use of ntuple.SlidingMeanEDA 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 SlidingMeanEDA

use of ntuple.SlidingMeanEDA 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)

Aggregations

SlidingMeanEDA (ntuple.SlidingMeanEDA)8 EvoAlg (evodef.EvoAlg)6 SimpleRMHC (ga.SimpleRMHC)6 Random (java.util.Random)4 NTupleBanditEA (ntuple.NTupleBanditEA)4 ElapsedCpuTimer (tools.ElapsedCpuTimer)4 Types (ontology.Types)3 ElapsedTimer (utilities.ElapsedTimer)3 Agent (controllers.multiPlayer.ea.Agent)2 StateObservationMulti (core.game.StateObservationMulti)2 AbstractMultiPlayer (core.player.AbstractMultiPlayer)2 StatSummary (utilities.StatSummary)2 AsteroidsGameState (asteroids.AsteroidsGameState)1 BattleView (battle.BattleView)1 Agent (controllers.singlePlayer.ea.Agent)1 StateObservation (core.game.StateObservation)1 AbstractPlayer (core.player.AbstractPlayer)1 AnnotatedFitnessSpace (evodef.AnnotatedFitnessSpace)1 DefaultMutator (evodef.DefaultMutator)1 SimpleGA (ga.SimpleGA)1