def __init__(self, game, controller=None, **kwargs): GameEnvironment.__init__(self, game, visualize=True, **kwargs) self.controller = controller self.screen = SubjectiveSceen() self.screen._initScreen() self.reset() self.state = self.getState() self.eventHistory = list()
def performAction(self, ACTION=None, action=None): if ACTION is None: self.setState(self.state) GameEnvironment.performAction(self, action) if action is not None: self._drawState() pygame.time.wait(self.actionDelay) self.eventHistory.append(action) self.state = self.getState() else: GameEnvironment.performAction(self, ACTION)
def test2(): from examples.gridphysics.mazes import polarmaze_game, maze_level_1 from vgdl.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 test6(): """ Now with memory!""" from numpy import ndarray from examples.gridphysics.mazes import polarmaze_game from pybrain.optimization import SNES g = VGDLParser().parseGame(polarmaze_game) g.buildLevel(cheese_maze) game_env = GameEnvironment(g) net = buildNet(game_env.outdim, 10, 4, temperature=0.1, recurrent=True) algo = SNES(lambda x: someEpisodes(game_env, x, avgOver=6, maxSteps=30, exploretoo=False), net, verbose=True, desiredEvaluation=0.85) print algo.batchSize rows, cols = 2,3 episodesPerStep = 5 for i in range(rows*cols): pylab.subplot(rows, cols, i+1) algo.learn(episodesPerStep) if isinstance(algo.bestEvaluable, ndarray): net._setParameters(algo.bestEvaluable) else: net = algo.bestEvaluable plotBackground(game_env) plotTrajectories(game_env, net) pylab.title(str((i+1)*episodesPerStep)) if algo.desiredEvaluation <= algo.bestEvaluation: break print pylab.show()
def test4(): from numpy import ndarray from examples.gridphysics.mazes import polarmaze_game from pybrain.optimization import SNES, WeightGuessing g = VGDLParser().parseGame(polarmaze_game) g.buildLevel(labyrinth2) game_env = GameEnvironment(g) net = buildNet(game_env.outdim, 5, 4, temperature=0.1, recurrent=False) algo = SNES(lambda x: someEpisodes(game_env, x, avgOver=3), net, verbose=True, desiredEvaluation=0.75) #algo = WeightGuessing(lambda x: someEpisodes(game_env, x), net, verbose=True, desiredEvaluation=0.78) rows, cols = 2,2 episodesPerStep = 4 for i in range(rows*cols): pylab.subplot(rows, cols, i+1) algo.learn(episodesPerStep) if isinstance(algo.bestEvaluable, ndarray): net._setParameters(algo.bestEvaluable) else: net = algo.bestEvaluable plotBackground(game_env) plotTrajectories(game_env, net) pylab.title(str((i+1)*episodesPerStep)) if algo.desiredEvaluation <= algo.bestEvaluation: break print pylab.show()
def test3(): from examples.gridphysics.mazes.simple import consistent_corridor from examples.gridphysics.mazes import polarmaze_game from pybrain.optimization import SNES g = VGDLParser().parseGame(polarmaze_game) g.buildLevel(consistent_corridor) game_env = GameEnvironment(g) net = buildNet(game_env.outdim, 4, 4, temperature=0.05, recurrent=False) algo = SNES(lambda x: someEpisodes(game_env, x), net, verbose=True, desiredEvaluation=0.78) rows, cols = 2, 2 episodesPerStep = 3 for i in range(rows * cols): pylab.subplot(rows, cols, i + 1) algo.learn(episodesPerStep) net._setParameters(algo.bestEvaluable) plotBackground(game_env) plotTrajectories(game_env, net) pylab.title(str((i + 1) * episodesPerStep)) if algo.desiredEvaluation <= algo.bestEvaluation: break print pylab.show()
def test2(): from examples.gridphysics.mazes import polarmaze_game, maze_level_1 from pybrain.optimization import SNES game_str, map_str = polarmaze_game, maze_level_1 g = VGDLParser().parseGame(game_str) g.buildLevel(map_str) game_env = GameEnvironment(g, actionDelay=100, recordingEnabled=True) net = buildNet(game_env.outdim, 6, 2) algo = SNES(lambda x: someEpisodes(game_env, x), net, verbose=True, desiredEvaluation=0.43) rows, cols = 3, 3 episodesPerStep = 2 for i in range(rows * cols): pylab.subplot(rows, cols, i + 1) algo.learn(episodesPerStep) net._setParameters(algo.bestEvaluable) plotBackground(game_env) plotTrajectories(game_env, net) pylab.title(str((i + 1) * episodesPerStep)) if algo.desiredEvaluation <= algo.bestEvaluation: break print pylab.show()
def test4(): """ Same thing, but animated. """ from examples.gridphysics.mazes.windy import windy_stoch_game, windy_level from pybrain.rl.experiments.episodic import EpisodicExperiment from vgdl.interfaces import GameEnvironment, GameTask from vgdl.agents import PolicyDrivenAgent g = VGDLParser().parseGame(windy_stoch_game) g.buildLevel(windy_level) env = GameEnvironment(g, visualize=True, actionDelay=100) task = GameTask(env) agent = PolicyDrivenAgent.buildOptimal(env) exper = EpisodicExperiment(task, agent) res = exper.doEpisodes(5) print res
def __init__(self, game=None, verbose=False, actionset=BASEDIRS, env=None, avgOver=10): if env is None: env = GameEnvironment(game, actionset=actionset) self.env = env self.verbose = verbose self.sas_tuples = [] self.rewards = {} if env._game.is_stochastic: # in the stochastic case, how often is every state-action pair tried? self.avgOver = avgOver else: self.avgOver = 1
def test1(): 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) game_env = GameEnvironment(g) print 'number of observations:', game_env.outdim net = buildNet(game_env.outdim, 2, 2) for i in range(200): net.randomize() net.reset() print someEpisodes(game_env, net), if i% 20 == 19: print
def __init__(self, game, **kwargs): GameEnvironment.__init__(self, game, visualize=False, **kwargs) self.screen = SubjectiveSceen() self.screen._initScreen() assert self.orientedAvatar, 'Only oriented/directional avatars are currently supported for the first-person view.' self.reset()
def performAction(self, action): GameEnvironment.performAction(self, action) if action is not None: self._drawState() pygame.time.wait(self.actionDelay)
def reset(self): GameEnvironment.reset(self) if hasattr(self, 'screen'): self.screen.reset() pygame.display.flip()