Search in sources :

Example 1 with EvoAlg

use of evodef.EvoAlg in project SimpleAsteroids by ljialin.

the class SimpleMaxNTest method runOnce.

public static double runOnce() {
    // make an agent to test
    StateObservation noiseFree = new SimpleMaxGame();
    // new NoisyMaxGame();
    StateObservation stateObs = new SimpleMaxGame();
    System.out.println(stateObs.getGameScore());
    System.out.println(stateObs.copy().getGameScore());
    // System.exit(0);
    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);
    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 = 1000;
    double kExplore = 10;
    int nNeighbours = 100;
    evoAlg = new NTupleBanditEA(kExplore, nNeighbours);
    // DefaultMutator.totalRandomChaosMutation = true;
    Agent.useShiftBuffer = false;
    controllers.singlePlayer.ea.Agent.SEQUENCE_LENGTH = 100;
    player = new controllers.singlePlayer.ea.Agent(stateObs, timer, evoAlg, nEvals);
    nestedMC.maxRolloutLength = 5;
    nestedMC.nestDepth = 5;
    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 = 10;
    ElapsedTimer t = new ElapsedTimer();
    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);
        noiseFree.advance(action);
    // System.out.println(stateObs.getGameScore());
    }
    System.out.println(stateObs.getGameScore());
    System.out.println(noiseFree.getGameScore());
    System.out.println(stateObs.isGameOver());
    System.out.println(t);
    return noiseFree.getGameScore();
}
Also used : Agent(controllers.singlePlayer.ea.Agent) Types(ontology.Types) NTupleBanditEA(ntuple.NTupleBanditEA) Agent(controllers.singlePlayer.ea.Agent) EvoAlg(evodef.EvoAlg) StateObservation(core.game.StateObservation) SimpleRMHC(ga.SimpleRMHC) Random(java.util.Random) SimpleMaxGame(altgame.SimpleMaxGame) AbstractPlayer(core.player.AbstractPlayer) ElapsedTimer(utilities.ElapsedTimer) ElapsedCpuTimer(tools.ElapsedCpuTimer)

Example 2 with EvoAlg

use of evodef.EvoAlg in project SimpleAsteroids by ljialin.

the class SpaceBattleLinkTest 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;
    // BattleGameSearchSpace.inject(SampleEvolvedParams.solutions[1]);
    // BattleGameSearchSpace.inject(SampleEvolvedParams.solutions[2]);
    BattleGameSearchSpace.inject(SampleEvolvedParams.solutions[1]);
    System.out.println("Params are:");
    System.out.println(BattleGameParameters.params);
    // can also overide parameters by setting them directly as follows:
    // BattleGameParameters.loss = 1.1;
    SpaceBattleLinkState linkState = new SpaceBattleLinkState();
    // set some parameters for the experiment
    GameActionSpaceAdapter.useHeuristic = false;
    Agent.useShiftBuffer = true;
    // DefaultMutator.totalRandomChaosMutation = false;
    // // supercl
    // StateObservation stateObs = linkState;
    ElapsedCpuTimer timer = new ElapsedCpuTimer();
    AbstractPlayer player;
    // controllers.singlePlayer.sampleOLMCTS.Agent olmcts =
    // new controllers.singlePlayer.sampleOLMCTS.Agent(linkState, timer);
    player = new controllers.singlePlayer.discountOLMCTS.Agent(linkState, timer);
    // try the evolutionary players
    int nResamples = 2;
    EvoAlg evoAlg = new SimpleRMHC(nResamples);
    double kExplore = 10;
    int nNeighbours = 100;
    int nEvals = 200;
    evoAlg = new NTupleBanditEA(kExplore, nNeighbours);
    // player = new controllers.singlePlayer.ea.Agent(linkState, timer, evoAlg, nEvals);
    controllers.singlePlayer.nestedMC.Agent nestedMC = new controllers.singlePlayer.nestedMC.Agent(linkState, timer);
    nestedMC.maxRolloutLength = 10;
    nestedMC.nestDepth = 2;
    player = nestedMC;
    // 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);
    // set view to null to run fast with no visuals
    if (!runVisible)
        view = null;
    if (view != null) {
        new JEasyFrame(view, "Simple Battle Game");
    }
    boolean verbose = false;
    for (int i = 0; i < nSteps && !linkState.isGameOver(); i++) {
        ArrayList<Types.ACTIONS> actions = linkState.getAvailableActions();
        timer = new ElapsedCpuTimer();
        timer.setMaxTimeMillis(thinkingTime);
        Types.ACTIONS action = player.act(linkState.copy(), timer);
        // action = actions.get(random.nextInt(actions.size()));
        if (verbose)
            // + "\t " + action.ordinal());
            System.out.println(i + "\t Selected: " + action);
        linkState.advance(action);
        if (view != null) {
            view.repaint();
            try {
                Thread.sleep(delay);
            } catch (Exception e) {
            }
        }
        if (verbose)
            System.out.println(linkState.getGameScore());
    }
    System.out.println("Game score: " + linkState.getGameScore());
    return linkState.getGameScore();
}
Also used : Types(ontology.Types) NTupleBanditEA(ntuple.NTupleBanditEA) EvoAlg(evodef.EvoAlg) StateObservation(core.game.StateObservation) Random(java.util.Random) JEasyFrame(utilities.JEasyFrame) SimpleMaxGame(altgame.SimpleMaxGame) AbstractPlayer(core.player.AbstractPlayer) ElapsedTimer(utilities.ElapsedTimer) ElapsedCpuTimer(tools.ElapsedCpuTimer) Agent(controllers.singlePlayer.ea.Agent) BattleView(battle.BattleView) SimpleRMHC(ga.SimpleRMHC)

Example 3 with EvoAlg

use of evodef.EvoAlg 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 4 with EvoAlg

use of evodef.EvoAlg 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 5 with EvoAlg

use of evodef.EvoAlg in project SimpleAsteroids by ljialin.

the class TestHyperParamPlanetWars method main.

public static void main(String[] args) {
    int nEvals = 288;
    if (args.length == 1) {
        nEvals = Integer.parseInt(args[0]);
    }
    System.out.println("Optimization budget: " + nEvals);
    NTupleBanditEA ntbea = new NTupleBanditEA().setKExplore(1);
    GameState.includeBuffersInScore = true;
    EvoAgentSearchSpace.tickBudget = 2000;
    EvoAlg[] evoAlgs = { // new SimpleRMHC(5),
    ntbea };
    int nChecks = 100;
    int nTrials = 100;
    ElapsedTimer timer = new ElapsedTimer();
    for (EvoAlg evoAlg : evoAlgs) {
        // LineChart lineChart = new LineChart();
        // lineChart.yAxis = new LineChartAxis(new double[]{-2, -1, 0, 1, 2});
        // lineChart.setYLabel("Fitness");
        HyperParamTuneRunner runner = new HyperParamTuneRunner();
        // runner.verbose = true;
        // runner.setLineChart(lineChart);
        runner.nChecks = nChecks;
        runner.nTrials = nTrials;
        runner.nEvals = nEvals;
        runner.plotChecks = 0;
        AnnotatedFitnessSpace testPlanetWars = new EvoAgentSearchSpace();
        System.out.println("Testing: " + evoAlg);
        runner.runTrials(evoAlg, testPlanetWars);
        System.out.println("Finished testing: " + evoAlg);
    // note, this is a bit of a hack: it only reports the final solution
    // System.out.println(new EvoAgentSearchSpace().report(runner.solution));
    }
    // System.out.println(ntbea.getModel().s);
    System.out.println("Time for all experiments: " + timer);
}
Also used : AnnotatedFitnessSpace(evodef.AnnotatedFitnessSpace) ElapsedTimer(utilities.ElapsedTimer) NTupleBanditEA(ntuple.NTupleBanditEA) EvoAlg(evodef.EvoAlg) EvoAgentSearchSpace(planetwar.EvoAgentSearchSpace)

Aggregations

EvoAlg (evodef.EvoAlg)23 SimpleRMHC (ga.SimpleRMHC)17 Random (java.util.Random)9 NTupleBanditEA (ntuple.NTupleBanditEA)9 ElapsedCpuTimer (tools.ElapsedCpuTimer)9 Types (ontology.Types)7 ElapsedTimer (utilities.ElapsedTimer)7 SlidingMeanEDA (ntuple.SlidingMeanEDA)6 AbstractMultiPlayer (core.player.AbstractMultiPlayer)5 JEasyFrame (utilities.JEasyFrame)5 Agent (controllers.singlePlayer.ea.Agent)4 AbstractPlayer (core.player.AbstractPlayer)4 StatSummary (utilities.StatSummary)4 StateObservation (core.game.StateObservation)3 SimpleMaxGame (altgame.SimpleMaxGame)2 BattleView (battle.BattleView)2 Agent (controllers.multiPlayer.ea.Agent)2 StateObservationMulti (core.game.StateObservationMulti)2 AnnotatedFitnessSpace (evodef.AnnotatedFitnessSpace)2 CompactSlidingGA (ntuple.CompactSlidingGA)2