Exemplo n.º 1
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
Exemplo n.º 2
0
def runLunarLander():
    # import lunar lander
    from vgdl.examples.continuousphysics.lander import lander_game, lander_level

    # build the game
    g = VGDLParser().parseGame(lander_game)
    g.buildLevel(lander_level)

    # TODO: Determine how to not need to bring up the pygame display in order to run the game.
    g._initScreen([1, 1])

    ship = g.getAvatars()[0]

    # store initial ship state
    initState = [ship.rect.x, ship.rect.y, ship.speed, ship.orientation]

    print "starting position: " + str(ship)
    print "starting state: " + str(initState)
    # get random actions
    actions = generateInput(ACTIONS)

    states = [initState]
    # move ship based on random actions
    print actions
    for a in actions:
        for i in range(REPEATS):
            ship.action = a
            updateGame(g, a)
            if ended:
                print a, i
                break
        states.append(makeState(ship))

    endState = states[len(states)-1]

    # confirm final position
    print "first final position after actions: " + str(ship)
    print "final state: " + str(endState)

    # reroll ship back to initial state
    setState(ship, initState)

    # vary action sequence
    # first pick a point to vary
    random.seed(10466)
    varyIndex = random.randint(0, len(actions) - 1)

    # then change that action
    oldAction = actions[varyIndex]
    actions[varyIndex] = BASEDIRS[random.randint(0, len(BASEDIRS) - 1)]

    # print out the change and the full list of actions
    print "changed action " + str(varyIndex) + " to " + str(actions[varyIndex])
    print "new actions: " + str(actions)

    # predict through simple calculation how the final position should be
    predictState = predictOutcome(states, actions, oldAction, varyIndex)
    print "predicted state " + str(predictState)

    # find out where the actual final position is
    for a in actions:
        for i in range(REPEATS):
            updateGame(g, a)
            if ended:
                print a, i
                break

    endState = makeState(ship)
    print "actual ending position: " + str(ship)
    print "ending state: " + str(endState)

    # get error
    error = [endState[0] - predictState[0], endState[1] - predictState[1]]
    print "prediction error: " + str(error)