Exemple #1
0
def plotLSPIValues(gametype, layout, discountFactor=0.9, useTD=False, showValue=False):
    # build the game
    g = VGDLParser().parseGame(gametype)
    g.buildLevel(layout)
    
    # transform into an MDP and the mapping to observations
    C = MDPconverter(g)
    Ts, R, fMap = C.convert()    
    
    # find the the best least-squares approximation to the policy,
    # given only observations, not the state information
    if useTD:
        # state-based
        _, Tlspi = LSTD_PI_policy(fMap, Ts, R, discountFactor=discountFactor)
    else:
        # state-action-based
        _, Tlspi = LSPI_policy(fMap, Ts, R, discountFactor=discountFactor)
    
    # evaluate the policy
    Vlspi = trueValues(Tlspi, R, discountFactor=discountFactor)
        
    # plot those values    
    featurePlot((g.width, g.height), C.states, Vlspi)
    
    if showValue:
        # expected discounted reward at initial state
        Vinit = Vlspi[C.initIndex()]
        pylab.xlabel("V0=%.4f"%Vinit)
Exemple #2
0
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()
Exemple #3
0
def plotLSPIValues(gametype, layout, discountFactor=0.9, useTD=False, showValue=False):
    # build the game
    g = VGDLParser().parseGame(gametype)
    g.buildLevel(layout)
    
    # transform into an MDP and the mapping to observations
    C = MDPconverter(g)
    Ts, R, fMap = C.convert()    
    
    # find the the best least-squares approximation to the policy,
    # given only observations, not the state information
    if useTD:
        # state-based
        _, Tlspi = LSTD_PI_policy(fMap, Ts, R, discountFactor=discountFactor)
    else:
        # state-action-based
        _, Tlspi = LSPI_policy(fMap, Ts, R, discountFactor=discountFactor)
    
    # evaluate the policy
    Vlspi = trueValues(Tlspi, R, discountFactor=discountFactor)
        
    # plot those values    
    featurePlot((g.width, g.height), C.states, Vlspi)
    
    if showValue:
        # expected discounted reward at initial state
        Vinit = Vlspi[C.initIndex()]
        pylab.xlabel("V0=%.4f"%Vinit)
def testAugmented():
    from vgdl.core import VGDLParser
    from pybrain.rl.experiments.episodic import EpisodicExperiment
    from vgdl.mdpmap import MDPconverter
    from vgdl.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 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()
Exemple #6
0
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()
Exemple #7
0
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 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()
Exemple #9
0
def main():
    from vgdl.core import VGDLParser
    from MCTSPlayer.Agent import Agent
    from RandomRuleGenerator.RandomRuleGenerator import RandomRuleGenerator
    from aiGame import aiGame
    from Timer import ElapsedCPUTimer

    # parse, run and play.
    parser = VGDLParser()
    game = parser.parseGame(rules)
    game.buildLevel(map)

    # Creating timer state and controller
    timer = ElapsedCPUTimer()
    timer.setMaxTime(10)

    for i in range(0, 10):
        gen = RandomRuleGenerator(map, game, timer)
        interactions, terminations = gen.generateRuleSet(timer)
        interactionSTR = 'InteractionSet\n'
        terminationSTR = 'TerminationSet\n'

        for interaction in interactions:
            interactionSTR += '        ' + interaction + '\n'
        for termination in terminations:
            terminationSTR += '        ' + termination + '\n'

        newRules = """
BasicGame
    LevelMapping
        - > wind
        = > ice
        G > goal
        < > tvleft
        ^ > tvup
        
    SpriteSet         
        structure > Immovable
            goal  > color=GREEN
            tv    > Conveyor color=RED
                tvup    > orientation=UP
                tvleft  > orientation=LEFT
            ice   > color=WHITE
            wind  > Conveyor orientation=RIGHT strength=1                                         
        avatar   > RotatingAvatar
        
    %s
        avatar wall  > stepBack
        
    %s
""" % (interactionSTR, terminationSTR)
        print(map)

        print(newRules)
        game = parser.parseGame(newRules)
        game.buildLevel(map)

        game.startGame(False, False)

    return 0
Exemple #10
0
def testAugmented():
    from vgdl.core import VGDLParser
    from pybrain.rl.experiments.episodic import EpisodicExperiment
    from vgdl.mdpmap import MDPconverter
    from vgdl.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)
Exemple #11
0
def testRecordingToGif(human=False):
    from pybrain.rl.experiments.episodic import EpisodicExperiment
    from vgdl.core import VGDLParser
    from examples.gridphysics.mazes import polarmaze_game, maze_level_2
    from vgdl.agents import PolicyDrivenAgent, InteractiveAgent
    from vgdl.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)
Exemple #12
0
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()
Exemple #13
0
def _createVGDLGame( gameSpec, levelSpec ):
    import uuid
    from vgdl.core import VGDLParser
    # parse, run and play.
    game = VGDLParser().parseGame(gameSpec)
    game.buildLevel(levelSpec)
    game.uiud = uuid.uuid4()
    return game
Exemple #14
0
def testRollout(actions=[0, 0, 2, 2, 0, 3] * 20):
    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)
    env = GameEnvironment(g, visualize=True, actionDelay=100)
    env.rollOut(actions)
def testRollout(actions=[0, 0, 2, 2, 0, 3] * 20):        
    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)    
    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 vgdl.core import VGDLParser
    from vgdl.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)
Exemple #17
0
def testRolloutVideo(actions=[0, 0, 2, 2, 0, 3] * 2):
    from examples.gridphysics.mazes import polarmaze_game, maze_level_1
    from vgdl.core import VGDLParser
    from vgdl.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 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)
Exemple #19
0
def testStochMaze():
    from vgdl.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
Exemple #20
0
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)
Exemple #21
0
def testStochMaze():
    from vgdl.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
Exemple #22
0
def testMaze():
    from vgdl.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
Exemple #23
0
def testMaze():
    from vgdl.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
Exemple #24
0
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
Exemple #25
0
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 evaluate(fnn,iteration=5,isScreen=False):
  global zelda_level,zelda_game,now_zelda_game,red,blue,green,scorelimit,mapgenerator,nowlevel
  if __name__ == "__main__":
    from vgdl.core import VGDLParser
    TotalScore = 0.
    for i in range(iteration):
      redCount = 0
      greenCount = 0
      blueCount = 0
      thislevel = mapgenerator.generate()
      nowlevel = thislevel
      zelda_level_list = list(thislevel)
      #link
      for i in range(99999):
        if(thislevel.find(' ')):
          randIndex = int(random.uniform(0, len(zelda_level_list)))
          if(zelda_level_list[randIndex] == ' '):
            zelda_level_list[randIndex] = 'L'
            break
        else:
          break
      #red
      for i in range(99999):
        if(thislevel.find(' ')):
          randIndex = int(random.uniform(0, len(zelda_level_list)))
          if(zelda_level_list[randIndex] == ' '):
            zelda_level_list[randIndex] = 'R'
            redCount+=1
            if(redCount >= red):
              break
        else:
          break
      #green
      for i in range(99999):
        if(thislevel.find(' ')):
          randIndex = int(random.uniform(0, len(zelda_level_list)))
          if(zelda_level_list[randIndex] == ' '):
            zelda_level_list[randIndex] = 'G'
            greenCount+=1
            if(greenCount >= green):
              break
        else:
          break
      #blue
      for i in range(99999):
        if(thislevel.find(' ')):
          randIndex = int(random.uniform(0, len(zelda_level_list)))
          if(zelda_level_list[randIndex] == ' '):
            zelda_level_list[randIndex] = 'B'
            blueCount+=1
            if(blueCount >= blue):
              break
        else:
          break
      zelda_this_level = ''.join(zelda_level_list)
      print now_zelda_game
      os.system('read')
      game = VGDLParser.playGame(now_zelda_game, zelda_this_level, fnn = fnn , isScreen=True)
      TotalScore += (game.score/float(scorelimit))
    return TotalScore/float(iteration)
Exemple #27
0
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 testPolicyAgent():
    from pybrain.rl.experiments.episodic import EpisodicExperiment
    from vgdl.core import VGDLParser
    from examples.gridphysics.mazes import polarmaze_game, maze_level_2
    from vgdl.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
Exemple #29
0
def testPolicyAgent():
    from pybrain.rl.experiments.episodic import EpisodicExperiment
    from vgdl.core import VGDLParser
    from examples.gridphysics.mazes import polarmaze_game, maze_level_2
    from vgdl.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
Exemple #30
0
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 planActLoop(rleCreateFunc,
                filename,
                max_actions_per_plan,
                planning_steps,
                defaultPolicyMaxSteps,
                playback=False):

    rle = rleCreateFunc(OBSERVATION_GLOBAL)
    game, level = defInputGame(filename)
    outdim = rle.outdim
    print rle.show()

    terminal = rle._isDone()[0]

    i = 0
    finalStates = [rle._game.getFullState()]
    while not terminal:
        mcts = Basic_MCTS(existing_rle=rle, game=game, level=level)
        mcts.startTrainingPhase(planning_steps, defaultPolicyMaxSteps, rle)
        # mcts.debug(mcts.rle, output=True, numActions=3)
        # break
        actions = mcts.getBestActionsForPlayout((1, 0, 0))

        # if len(actions)<max_actions_per_plan:
        # 	print "We only computed", len(actions), "actions."

        new_state = rle._getSensors()
        terminal = rle._isDone()[0]

        for j in range(min(len(actions), max_actions_per_plan)):
            if actions[j] is not None and not terminal:
                print ACTIONS[actions[j]]
                res = rle.step(actions[j])
                new_state = res["observation"]
                terminal = not res['pcontinue']
                print rle.show()
                finalStates.append(rle._game.getFullState())

        i += 1

    if playback:
        from vgdl.core import VGDLParser
        VGDLParser.playGame(game, level, finalStates)
        embed()
Exemple #32
0
def planActLoop(rleCreateFunc, max_actions_per_plan, planning_steps, defaultPolicyMaxSteps, playback=False):
	
	rle = rleCreateFunc(OBSERVATION_GLOBAL)

	outdim = rle.outdim

	rle.show()
	
	terminal = rle._isDone()[0]
	
	i=0
	finalStates = [rle._game.getFullState()]
	while not terminal:
		mcts = Basic_MCTS(existing_rle=rle)
		mcts.startTrainingPhase(planning_steps, defaultPolicyMaxSteps, rle)
		# mcts.debug(mcts.rle, output=True, numActions=3)
		# break
		actions = mcts.getBestActionsForPlayout()

		# if len(actions)<max_actions_per_plan:
		# 	print "We only computed", len(actions), "actions."

		new_state = rle._getSensors()
		terminal = rle._isDone()[0]

		for j in range(min(len(actions), max_actions_per_plan)):
			if actions[j] is not None and not terminal:
				# dist = mcts.getManhattanDistanceComponents(new_state)
				print ACTIONS[actions[j]]
				res = rle.step(actions[j])
				new_state = res["observation"]
				terminal = not res['pcontinue']
				print rle.show()
				finalStates.append(rle._game.getFullState())

		i+=1

	if playback:
		from vgdl.core import VGDLParser
		from examples.gridphysics.simpleGame_randomNPC import box_level, push_game
		game = push_game
		level = box_level
		VGDLParser.playGame(game, level, finalStates)
Exemple #33
0
def test3():
    from examples.gridphysics.mazes import polarmaze_game
    from examples.gridphysics.mazes.simple import maze_level_1b
    from vgdl.core import VGDLParser
    from pybrain.rl.experiments.episodic import EpisodicExperiment
    from vgdl.interfaces import GameTask
    from vgdl.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 vgdl.core import VGDLParser
    from pybrain.rl.experiments.episodic import EpisodicExperiment
    from vgdl.interfaces import GameTask
    from vgdl.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
Exemple #35
0
def plotOptimalValues(gametype, layout, discountFactor=0.9, showValue=False):
    # build the game
    g = VGDLParser().parseGame(gametype)
    g.buildLevel(layout)
    
    # transform into an MDP
    C = MDPconverter(g)
    Ts, R, _ = C.convert()
    
    # find the optimal policy
    _, Topt = policyIteration(Ts, R, discountFactor=discountFactor)
    
    # evaluate the policy
    Vopt = trueValues(Topt, R, discountFactor=discountFactor)
        
    # plot those values    
    featurePlot((g.width, g.height), C.states, Vopt, plotdirections=True)
    
    if showValue:
        # expected discounted reward at initial state
        Vinit = Vopt[C.initIndex()]
        pylab.xlabel("V0=%.4f"%Vinit)
Exemple #36
0
def plotOptimalValues(gametype, layout, discountFactor=0.9, showValue=False):
    # build the game
    g = VGDLParser().parseGame(gametype)
    g.buildLevel(layout)

    # transform into an MDP
    C = MDPconverter(g)
    Ts, R, _ = C.convert()

    # find the optimal policy
    _, Topt = policyIteration(Ts, R, discountFactor=discountFactor)

    # evaluate the policy
    Vopt = trueValues(Topt, R, discountFactor=discountFactor)

    # plot those values
    featurePlot((g.width, g.height), C.states, Vopt, plotdirections=True)

    if showValue:
        # expected discounted reward at initial state
        Vinit = Vopt[C.initIndex()]
        pylab.xlabel("V0=%.4f" % Vinit)
Exemple #37
0
def testLoadSave():
    from vgdl.core import VGDLParser
    from examples.gridphysics.aliens import aliens_level, aliens_game
        
    map_str, game_str = aliens_level, aliens_game
    g = VGDLParser().parseGame(game_str)
    g.buildLevel(map_str)
    
    for _ in range(1000):
        s = g.getFullState()
        g.setFullState(s)
def testInteractions():
    from random import randint
    from pybrain.rl.experiments.episodic import EpisodicExperiment
    from vgdl.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 testRecordingToGif(human=False):
    from pybrain.rl.experiments.episodic import EpisodicExperiment
    from vgdl.core import VGDLParser
    from examples.gridphysics.mazes import polarmaze_game, maze_level_2
    from vgdl.agents import PolicyDrivenAgent, InteractiveAgent
    from vgdl.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)
Exemple #40
0
def test3():
    from examples.gridphysics.mazes.simple import office_layout_2, 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()
Exemple #41
0
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()
Exemple #42
0
def testInteractions():
    from random import randint
    from pybrain.rl.experiments.episodic import EpisodicExperiment
    from vgdl.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 load_game(module_str, level_str):
   
    module  = importlib.import_module(module_str)

    game = None    
    level = None
    
    for key in module.__dict__.keys():
        #print key
        if(key.endswith("game")):
            game = module.__dict__[key]
        if(key.endswith("level") and key == level_str):
            level = module.__dict__[key]
            
    if(level is None):
        return None
    #print game, level
    g = VGDLParser.playGame(game, level, headless = True, persist_movie = True, movie_dir = "./tmp/")
    #g.startGameExternalPlayer(headless = True, persist_movie = True)
    return g
Exemple #44
0
def testInteractions():
    from vgdl.core import VGDLParser
    from examples.gridphysics.aliens import aliens_level, aliens_game
    from pygame.locals import K_SPACE
    # from examples.gridphysics.sokoban import so
    from pybrain.rl.agents.agent import Agent
    
    class DummyAgent(Agent):
        total = 4
        def getAction(self):
            # res = randint(0, self.total - 1)
            return 1
        
    map_str, game_str = aliens_level, aliens_game
    g = VGDLParser().parseGame(game_str)
    g.buildLevel(map_str)
    g._initScreen(g.screensize,headless=True)
        
    for _ in range(300):
        win, _ = g.tick(K_SPACE)
        if win is not None:
            break
def load_game(module_str, level_str):

    module = importlib.import_module(module_str)

    game = None
    level = None

    for key in module.__dict__.keys():
        #print key
        if (key.endswith("game")):
            game = module.__dict__[key]
        if (key.endswith("level") and key == level_str):
            level = module.__dict__[key]

    if (level is None):
        return None
    #print game, level
    g = VGDLParser.playGame(game,
                            level,
                            headless=True,
                            persist_movie=True,
                            movie_dir="./tmp/")
    #g.startGameExternalPlayer(headless = True, persist_movie = True)
    return g
Exemple #46
0
        avatar  > MovingAvatar 
            naked   > 
            nokey   > color=RED
            withkey > color=YELLOW
    LevelMapping
        G > goal
        k > key        
        + > sword
        A > naked
        0 > door
        1 > monster            
    InteractionSet
        avatar wall    > stepBack
        nokey door     > stepBack
        nokey key      > transformTo stype=withkey
        naked sword    > transformTo stype=nokey                
        goal avatar    > killSprite        
        monster nokey  > killSprite        
        naked monster  > killSprite
        withkey monster> killSprite
        key  avatar    > killSprite
        sword avatar   > killSprite
    TerminationSet
        SpriteCounter stype=goal   limit=0 win=True
        SpriteCounter stype=avatar limit=0 win=False              
"""

if __name__ == "__main__":
    from vgdl.core import VGDLParser
    VGDLParser.playGame(rigidzelda_game, zelda_level)    
Exemple #47
0
w       w 0 w
w 1        ww
w          ww
wwwwwwwwwwwww
"""

        
push_game = """
BasicGame frame_rate=30
    SpriteSet        
        hole   > Immovable color=DARKBLUE
        movable >
            avatar > MovingAvatar cooldown=4
            box    > Passive                
    LevelMapping
        0 > hole
        1 > box            
    InteractionSet
        avatar wall > stepBack        
        box avatar  > bounceForward
        box wall    > undoAll        
        box box     > undoAll
        box hole    > killSprite        
    TerminationSet
        SpriteCounter stype=box    limit=0 win=True          
"""

if __name__ == "__main__":
    from vgdl.core import VGDLParser
    VGDLParser.playGame(push_game, box_level)    
Exemple #48
0
        b > butterfly
    InteractionSet
        dirt avatar > killSprite
        dirt sword  > killSprite
        diamond avatar > collectResource scoreChange=5
        diamond avatar > killSprite
        moving wall > stepBack
        moving boulder > stepBack
        avatar boulder > killIfFromAbove
        avatar butterfly > killSprite
        avatar crab > killSprite
        boulder dirt > stepBack
        boulder wall > stepBack
        boulder boulder > stepBack
        boulder diamond > stepBack
        enemy dirt > stepBack
        enemy diamond > stepBack
        crab butterfly > killSprite
        butterfly crab > transformTo stype=diamond scoreChange=1
        exitdoor avatar > killIfOtherHasMore resource=diamond limit=9 scoreChange=100

    TerminationSet
        SpriteCounter stype=avatar limit=0 win=False
        SpriteCounter stype=exitdoor limit=0 win=True

"""

if __name__ == "__main__":
    from vgdl.core import VGDLParser
    VGDLParser.playGame(boulderdash_game, boulderdash_level)
Exemple #49
0
        missile > Missile
            sam  > orientation=UP    color=BLUE singleton=True
            bomb > orientation=DOWN  color=RED  speed=0.5
        alien   > Bomber       stype=bomb   prob=0.01  cooldown=3 speed=0.75
        portal  > SpawnPoint   stype=alien  delay=16   total=20
    
    LevelMapping
        0 > base
        1 > portal

    TerminationSet
        SpriteCounter      stype=avatar               limit=0 win=False
        MultiSpriteCounter stype1=portal stype2=alien limit=0 win=True
        
    InteractionSet
        avatar  EOS  > stepBack
        alien   EOS  > turnAround        
        missile EOS  > killSprite
        missile base > killSprite
        base missile > killSprite
        base   alien > killSprite
        avatar alien > killSprite
        avatar bomb  > killSprite
        alien  sam   > killSprite         
"""

if __name__ == "__main__":
    from vgdl.core import VGDLParser
    # parse, run and play.
    VGDLParser.playGame(aliens_game, aliens_level)
Exemple #50
0
        wall bullet > killSprite 
        bullet wall > killSprite 
        pad bullet > killSprite
        avatar wall > stepBack
        avatar EOS > stepBack
        bullet EOS > killSprite

    LevelMapping
        G > pad
"""

artillery_level = """
wwwwwwwwwwwwwwwwwwwwww
w        w    w      w
w           www      w
w             w     ww
w           G        w
w   w                w
w    www          w  w
w      wwwwwww  www  w
w              ww    w
w  G            w    w
w  ww  G           G w
wA    wwwwww         w
wwwwwwwwwwwwwwwwwwwwww
"""

if __name__ == "__main__":
    from vgdl.core import VGDLParser
    VGDLParser.playGame(artillery_game, artillery_level)
Exemple #51
0
        moving elevator > pullWithIt        
        elevator EOS    > wrapAround
        
    LevelMapping
        G > goal
        1 > goomba
        2 > paratroopa
        = > elevator
"""

mario_level = """
wwwwwwwwwwwwwwwwwwwwwwwwwwww
w                          w
w                        G1w
w             ===       wwww
w                     1    w
w                w  2 ww   w
w                wwwwwww   w
w                          w
w                          w
w          2        2      w
w        www      wwwwww   w
w A      1   ===           w
wwww   wwww        2       w
wwwwwwwwww      wwww       w
"""

if __name__ == "__main__":
    from vgdl.core import VGDLParser
    VGDLParser.playGame(mario_game, mario_level)
Exemple #52
0
           
    InteractionSet
        inertial wall > wallBounce 
        avatar bullet > killSprite
        pad avatar    > killSprite
        
    LevelMapping
        G > pad
        1 > bullet
"""

ptsp_level = """
wwwwwwwwwwwwwwwwwwwwwwwwwwww
w        w    w    w       w
w    A    wwww    www      w
w                   w     ww
w             G     w  G   w
w   w                      w
w    www   1            w  w
w      wwwwwww        www  w
w                    ww    w
w  G                  w    w
w        ww  G           G w
w     wwwwwwwwww           w
wwwwwwwwwwwwwwwwwwwwwwwwwwww
"""

if __name__ == "__main__":
    from vgdl.core import VGDLParser
    VGDLParser.playGame(ptsp_game, ptsp_level)
        
Exemple #53
0
		b > butterfly
	InteractionSet
		dirt avatar > killSprite
		dirt sword  > killSprite
		diamond avatar > collectResource scoreChange=5
		diamond avatar > killSprite
		moving wall > stepBack
		moving boulder > stepBack
		avatar boulder > killIfFromAbove
		avatar butterfly > killSprite
		avatar crab > killSprite
		boulder dirt > stepBack
		boulder wall > stepBack
		boulder boulder > stepBack
		boulder diamond > stepBack
		enemy dirt > stepBack
		enemy diamond > stepBack
		crab butterfly > killSprite
		butterfly crab > transformTo stype=diamond scoreChange=1
		exitdoor avatar > killIfOtherHasMore resource=diamond limit=9 scoreChange=100

	TerminationSet
		SpriteCounter stype=avatar limit=0 win=False
		SpriteCounter stype=exitdoor limit=0 win=True

"""

if __name__ == "__main__":
    from vgdl.core import VGDLParser
    VGDLParser.playGame(boulderdash_game, boulderdash_level)
Exemple #54
0
        normal infected   > transformTo stype=carrier
        host infected     > transformTo stype=infected
        normal virus      > transformTo stype=carrier        
        host virus        > transformTo stype=infected
        guardian virus    > killSprite 
        
    LevelMapping
        1 > guardian
        0 > host
        X > virus
        A > normal
"""

chase_level = """
wwwwwwwwwwwwwwwwwwwwwwwwwwww
www  0  w   w   0 www 0w0  w
w       1   w        X w  0w
w 0     0 w   A        w0  w
w   wwwwwwww             0 w
w0                  ww  wwww
w    0  1 w        w    X  w
w0  w      wwwww   wX w  10w
wwww                wwX 1 Xw
wwww     0   0   0 www 0 Xww
wwwwwwwwwwwwwwwwwwwwwwwwwwww
"""

if __name__ == "__main__":
    from vgdl.core import VGDLParser
    VGDLParser.playGame(infection_game, chase_level)    
        = > ice
        G > goal
        < > tvleft
        ^ > tvup
        
    SpriteSet         
        structure > Immovable
            goal  > color=GREEN
            tv    > Conveyor color=RED
                tvup    > orientation=UP
                tvleft  > orientation=LEFT
            ice   > color=WHITE
            wind  > Conveyor orientation=RIGHT strength=1                                         
        avatar   > RotatingAvatar

    TerminationSet
        SpriteCounter stype=goal limit=0 win=True
        
    InteractionSet
        goal avatar  > killSprite
        avatar wind  > windGust
        avatar tv    > attractGaze prob=1
        avatar ice   > slipForward prob=0.3
        avatar wall  > stepBack        
"""


if __name__ == "__main__":
    from vgdl.core import VGDLParser
    VGDLParser.playGame(stoch_game, stoch_level)
Exemple #56
0
w.wwww.ww.wwwwwwww.ww.wwww.w
w.wwww.ww.wwwwwwww.ww.wwww.w
w......ww....ww....ww......w
wwwwww.wwwww ww wwwww.wwwwww
wwwwww.wwwww ww wwwww.wwwwww
wwwwww.ww          ww.wwwwww
wwwwww.ww          ww.wwwwww
wwwwww.ww www  www ww.wwwwww
      .   ww1234ww   .      
wwwwww.ww wwwwwwww ww.wwwwww
wwwwww.ww          ww.wwwwww
wwwwww.ww          ww.wwwwww
wwwwww.ww wwwwwwww ww.wwwwww
wwwwww.ww wwwwwwww ww.wwwwww
w............ww............w
w.wwww.wwwww.ww.wwwww.wwww.w
w0wwww.wwwww.ww.wwwww.wwww0w
w...ww.......A........ww...w
www.ww.ww.wwwwwwww.ww.ww.www
www.ww.ww.wwwwwwww.ww.ww.www
w......ww....ww....ww......w
w.wwwwwwwwww.ww.wwwwwwwwww.w
w.wwwwwwwwww.ww.wwwwwwwwww.w
w..........................w
wwwwwwwwwwwwwwwwwwwwwwwwwwww
"""

if __name__ == "__main__":
    from vgdl.core import VGDLParser
    VGDLParser.playGame(pacman_game, pacman_level)