def testAugmented(): from core import VGDLParser from pybrain.rl.experiments.episodic import EpisodicExperiment from mdpmap import MDPconverter from agents import PolicyDrivenAgent zelda_level2 = """ wwwwwwwwwwwww wA wwk1ww w ww ww 1 w ww wwww+w wwwww1ww www wwwww 0 Gww wwwwwwwwwwwww """ from examples.gridphysics.mazes.rigidzelda import rigidzelda_game g = VGDLParser().parseGame(rigidzelda_game) g.buildLevel(zelda_level2) env = GameEnvironment(g, visualize=False, recordingEnabled=True, actionDelay=150) C = MDPconverter(g, env=env, verbose=True) Ts, R, _ = C.convert() print C.states print Ts[0] print R env.reset() agent = PolicyDrivenAgent.buildOptimal(env) env.visualize = True env.reset() task = GameTask(env) exper = EpisodicExperiment(task, agent) exper.doEpisodes(1)
def testRecordingToGif(human=False): from pybrain.rl.experiments.episodic import EpisodicExperiment from core import VGDLParser from examples.gridphysics.mazes import polarmaze_game, maze_level_2 from agents import PolicyDrivenAgent, InteractiveAgent from tools import makeGifVideo game_str, map_str = polarmaze_game, maze_level_2 g = VGDLParser().parseGame(game_str) g.buildLevel(map_str) env = GameEnvironment(g, visualize=human, recordingEnabled=True, actionDelay=200) task = GameTask(env) if human: agent = InteractiveAgent() else: agent = PolicyDrivenAgent.buildOptimal(env) exper = EpisodicExperiment(task, agent) res = exper.doEpisodes(1) print res actions = [a for _, a, _ in env._allEvents] print actions makeGifVideo(env, actions, initstate=env._initstate)
def playMultipleEpisodes(self, numEpisodes): tally, finalEventList, totalStatesEncountered = [], [], [] gameObject = None for i in range(numEpisodes): print "Starting episode", i self.pickNewLevel(index=i) gameObject, won, eventList, statesEncountered = self.playEpisode( gameObject, finalEventList) finalEventList.extend(eventList) VGDLParser.playGame(self.gameString, self.levelString, statesEncountered, persist_movie=True, make_images=True, make_movie=False, movie_dir="videos/" + self.gameName, padding=10) totalStatesEncountered.append(statesEncountered) tally.append(won) print "Episode ended. Won:", won print "Have won", sum(tally), "out of", len(tally), "episodes" print "Won", sum(tally), "out of ", len(tally), "episodes." makeVideo(movie_dir="videos/" + self.gameName) # empty image directory # shutil.rmtree("images/tmp") # os.makedirs("images/tmp") return
def setup(name, mode): if mode == "vgdl": # Read in level and game file information level = get_file(name, "level") game = get_file(name, "game") # Start game g = VGDLParser().parseGame(game) g.buildLevel(level) rle = RLEnvironment(game, level, observationType='global', visualize=True) # Set up RLE rle.actionDelay = 200 rle.recordingEnabled = True rle.reset() rle._game._drawAll() dims = rle.outdim environment = rle # TODO elif mode == "ale": return else: return # Return environment return environment, dims
def testRollout(actions=[0, 0, 2, 2, 0, 3] * 20): from examples.gridphysics.mazes import polarmaze_game, maze_level_1 from core import VGDLParser game_str, map_str = polarmaze_game, maze_level_1 g = VGDLParser().parseGame(game_str) g.buildLevel(map_str) env = GameEnvironment(g, visualize=True, actionDelay=100) env.rollOut(actions)
def testRolloutVideo(actions=[0, 0, 2, 2, 0, 3] * 2): from examples.gridphysics.mazes import polarmaze_game, maze_level_1 from core import VGDLParser from tools import makeGifVideo game_str, map_str = polarmaze_game, maze_level_1 g = VGDLParser().parseGame(game_str) g.buildLevel(map_str) makeGifVideo(GameEnvironment(g, visualize=True), actions)
def _createVGDLGame(gameSpec, levelSpec): import uuid from core import VGDLParser # parse, run and play. game = VGDLParser().parseGame(gameSpec) game.buildLevel(levelSpec) game.uiud = uuid.uuid4() return game
def testStochMaze(): from core import VGDLParser from examples.gridphysics.mazes.stochastic import stoch_game, stoch_level g = VGDLParser().parseGame(stoch_game) g.buildLevel(stoch_level) C = MDPconverter(g, verbose=True) Ts, R, fMap = C.convert() print C.states print R for T in Ts: print T print fMap
def test2(): from examples.gridphysics.mazes import polarmaze_game, maze_level_1 from core import VGDLParser game_str, map_str = polarmaze_game, maze_level_1 g = VGDLParser().parseGame(game_str) g.buildLevel(map_str) actions = [1, 0, 0, 3, 0, 2, 0, 2, 0, 0, 0] env = GameEnvironment(g, visualize=True, actionDelay=100) env.rollOut(actions) env.reset() senv = SubjectiveGame(g, actionDelay=1500) senv.rollOut(actions)
def testMaze(): from core import VGDLParser from examples.gridphysics.mazes import polarmaze_game, maze_level_1 game_str, map_str = polarmaze_game, maze_level_1 g = VGDLParser().parseGame(game_str) g.buildLevel(map_str) C = MDPconverter(g, verbose=True) Ts, R, fMap = C.convert() print C.states print R for T in Ts: print T print fMap
def testPolicyAgent(): from pybrain.rl.experiments.episodic import EpisodicExperiment from core import VGDLParser from examples.gridphysics.mazes import polarmaze_game, maze_level_2 from agents import PolicyDrivenAgent game_str, map_str = polarmaze_game, maze_level_2 g = VGDLParser().parseGame(game_str) g.buildLevel(map_str) env = GameEnvironment(g, visualize=False, actionDelay=100) task = GameTask(env) agent = PolicyDrivenAgent.buildOptimal(env) env.visualize = True env.reset() exper = EpisodicExperiment(task, agent) res = exper.doEpisodes(2) print res
def test3(): from examples.gridphysics.mazes import polarmaze_game from examples.gridphysics.mazes.simple import maze_level_1b from core import VGDLParser from pybrain.rl.experiments.episodic import EpisodicExperiment from interfaces import GameTask from agents import InteractiveAgent, UserTiredException game_str, map_str = polarmaze_game, maze_level_1b g = VGDLParser().parseGame(game_str) g.buildLevel(map_str) senv = SubjectiveGame(g, actionDelay=100, recordingEnabled=True) #senv = GameEnvironment(g, actionDelay=100, recordingEnabled=True, visualize=True) task = GameTask(senv) iagent = InteractiveAgent() exper = EpisodicExperiment(task, iagent) try: exper.doEpisodes(1) except UserTiredException: pass print senv._allEvents
def test3(): from examples.gridphysics.mazes import polarmaze_game from examples.gridphysics.mazes.simple import maze_level_1b from core import VGDLParser from pybrain.rl.experiments.episodic import EpisodicExperiment from interfaces import GameTask from agents import InteractiveAgent, UserTiredException game_str, map_str = polarmaze_game, maze_level_1b g = VGDLParser().parseGame(game_str) g.buildLevel(map_str) senv = SubjectiveGame(g, actionDelay=100, recordingEnabled=True) # senv = GameEnvironment(g, actionDelay=100, recordingEnabled=True, visualize=True) task = GameTask(senv) iagent = InteractiveAgent() exper = EpisodicExperiment(task, iagent) try: exper.doEpisodes(1) except UserTiredException: pass print senv._allEvents
def testInteractions(): from random import randint from pybrain.rl.experiments.episodic import EpisodicExperiment from core import VGDLParser from examples.gridphysics.mazes import polarmaze_game, maze_level_1 from pybrain.rl.agents.agent import Agent class DummyAgent(Agent): total = 4 def getAction(self): res = randint(0, self.total - 1) return res game_str, map_str = polarmaze_game, maze_level_1 g = VGDLParser().parseGame(game_str) g.buildLevel(map_str) env = GameEnvironment(g, visualize=True, actionDelay=100) task = GameTask(env) agent = DummyAgent() exper = EpisodicExperiment(task, agent) res = exper.doEpisodes(2) print res
def makeImages(gameName, gameString, levelString, statesEncountered, param_ID): VGDLParser.playGame(gameString, levelString, statesEncountered, \ persist_movie=True, make_images=True, make_movie=False, movie_dir="videos/"+gameName, gameName = gameName, parameter_string=param_ID, padding=10)
# gameFilename = "examples.gridphysics.demo_multigoal_and" ##takes forever if you have many boxes and don't use 2BFS (with metabolic penalty) ## Continuous physics games can't work right now. RLE is discretized, getSensors() relies on this, and a lot of the induction/planning ## architecture depends on that. Will take some work to do this well. Best plan is to shrink the grid squares and increase speeds/strengths of ## objects. # gameFilename = "examples.continuousphysics.mario" # gameFilename = "examples.gridphysics.boulderdash" #Game is buggy. # gameFilename = "examples.gridphysics.butterflies" gameString, levelString = defInputGame(gameFilename, randomize=True) rleCreateFunc = lambda: createRLInputGame(gameFilename) rle = rleCreateFunc() p = IW(rle, gameString, levelString, gameFilename, k=2) VGDLParser.playGame(gameString, levelString, p.statesEncountered, persist_movie=True, make_images=True, make_movie=False, movie_dir="videos/"+gameFilename, padding=10) # embed() t1 = time.time() last, visited, rejected = BFS3(rle, p) print time.time()-t1 print len(visited), len(rejected) embed() #