当前位置: 首页>>代码示例>>Python>>正文


Python Agent.update方法代码示例

本文整理汇总了Python中agent.Agent.update方法的典型用法代码示例。如果您正苦于以下问题:Python Agent.update方法的具体用法?Python Agent.update怎么用?Python Agent.update使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在agent.Agent的用法示例。


在下文中一共展示了Agent.update方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: open

# 需要导入模块: from agent import Agent [as 别名]
# 或者: from agent.Agent import update [as 别名]
import json

with open('API.json') as api:
    data = json.load(api)

env = gym.make('FrozenLake-v0')
env.monitor.start('/tmp/frozenlake-experiment-9')

observation = env.reset()
action_space = env.action_space
observation, reward, done, info = env.step(action_space.sample())
agent = Agent(observation, reward, info, action_space.sample(), action_space)
num_episodes = 5000

for i_episode in range(num_episodes):
    observation = env.reset()
    done = False
    t = 0
    while not done:
        env.render()
        action = agent.take_action()
        (observation, reward, done, info) = env.step(action)
        agent.update(observation, reward, info, action, action_space)
        t = t + 1
        if done:
            break
            print("Episode finished after {} timesteps".format(t+1))

env.monitor.close()
gym.scoreboard.api_key = data["api_key"]
gym.upload('/tmp/frozenlake-experiment-9')
开发者ID:mcharawi,项目名称:openai_gym_solutions,代码行数:33,代码来源:test.py

示例2: main

# 需要导入模块: from agent import Agent [as 别名]
# 或者: from agent.Agent import update [as 别名]

#.........这里部分代码省略.........
    draw_vectors = False

    level_change = False

    # The main game event loop.
    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                exit()
            elif event.type == KEYDOWN:
                if event.key == K_m:
                    backgroundMusic()
                if event.key == K_d:
                    draw_vectors = not draw_vectors
                    print "Toggled:", draw_vectors
                    # Magic keys to change in-game behavior.
                if event.key == K_w:
                    behavior = 'wander'
                elif event.key == K_x:
                    LEVEL['level'][1][3] = 'water block'
                elif event.key == K_s:
                    behavior = 'seek'
                elif event.key == K_a:
                    behavior = 'a*'
                elif event.key == K_f:
                    behavior = 'flee'
                elif event.key == K_v:
                    behavior = 'avoid'
                elif event.key == K_r:
                    behavior = 'arrive'
                elif event.key == K_1:
                    LEVEL = levels.EASY
                    level_change = True
                elif event.key == K_2:
                    LEVEL = levels.MEDIUM
                    level_change = True
                elif event.key == K_3:
                    LEVEL = levels.HARD
                    level_change = True

        # If level change was indicated.
        if level_change:
            world = World(images, LEVEL['level'])

            player_pos = np.array(LEVEL['player'])
            player = Agent(world, images["boy"], player_pos, MAX_PLAYER_SPEED)

            enemy_pos = np.array(LEVEL['enemy'])
            enemy = Agent(world, images["girl"], enemy_pos, MAX_ENEMY_SPEED,
                          is_npc=True)

            level_change = False
            pass

        # Handle movement.
        pressed_keys = pygame.key.get_pressed()

        # Compute the vector indicating the acceleration that the
        # player will experience.
        acceleration = np.array([0, 0])

        if pressed_keys[K_LEFT]:
            acceleration += [-1, 0]
        elif pressed_keys[K_RIGHT]:
            acceleration += [1, 0]

        if pressed_keys[K_UP]:
            acceleration += [0, -1]
        elif pressed_keys[K_DOWN]:
            acceleration += [0, 1]

        if not np.array_equal(acceleration, [0, 0]):
            # Using /= here breaks. NumPy issue?
            acceleration = acceleration / np.sqrt(
                np.dot(acceleration, acceleration))

        time_passed = clock.tick(FPS)
        time_passed_seconds = time_passed / 1000.0

        player.update(acceleration, time_passed_seconds)

        # This is where the magic happens.
        executeAIBehavior(behavior, enemy, player, time_passed_seconds)

        # Render to intermediate memory buffer.
        refreshBlit()

        if draw_vectors is True:
            if behavior == "a*":
                drawLineTile(apath.path, tiles)
            else:
                line = [(enemy.position[0], enemy.position[1]),
                    (player.position[0], player.position[1])]
                drawLinePixel(line, tiles)

        # Intermediate buffer to screen.
        screen.blit(tiles, (0, 0))

        # Update the display, and loop again!
        pygame.display.update()
开发者ID:recardona,项目名称:CGS-Introduction-to-AI,代码行数:104,代码来源:main.py

示例3: World

# 需要导入模块: from agent import Agent [as 别名]
# 或者: from agent.Agent import update [as 别名]
class World(object):
    def __init__(self, cx, cy):
        self.cx = cx
        self.cy = cy
        self.target = Vector2D(cx / 2, cy / 2)
        self.hunter = Agent(self, 10, 0.8, 'wander')
        self.agents = []
        self.paused = True
        self.show_info = True

        self.obstacles = []

        self.hidingspots = []



    def update(self, delta):
        if not self.paused:
            self.hidingspots[:]=[]
            for obstacle in self.obstacles:
                self.hidingspots.append(self.get_hiding_position(self.hunter, obstacle))

            for agent in self.agents:
                agent.update(delta)
            self.hunter.update(delta)

    def render(self):
        
        for hidingspot in self.hidingspots:
            egi.blue_pen()
            egi.cross(hidingspot, 5)

        for obstacle in self.obstacles:
            obstacle.render()

        self.hunter.render()

        for agent in self.agents:
            agent.render()

        if self.show_info:
            infotext = ', '.join(set(agent.mode for agent in self.agents))
            egi.white_pen()
            egi.text_at_pos(0, 0, infotext)

    def wrap_around(self, pos):
        ''' Treat world as a toroidal space. Updates parameter object pos '''
        max_x, max_y = self.cx, self.cy
        if pos.x > max_x:
            pos.x = pos.x - max_x
        elif pos.x < 0:
            pos.x = max_x - pos.x
        if pos.y > max_y:
            pos.y = pos.y - max_y
        elif pos.y < 0:
            pos.y = max_y - pos.y

    def transform_points(self, points, pos, forward, side, scale):
        ''' Transform the given list of points, using the provided position,
            direction and scale, to object world space. '''
        # make a copy of original points (so we don't trash them)
        wld_pts = [pt.copy() for pt in points]
        # create a transformation matrix to perform the operations
        mat = Matrix33()
        # scale,
        mat.scale_update(scale.x, scale.y)
        # rotate
        mat.rotate_by_vectors_update(forward, side)
        # and translate
        mat.translate_update(pos.x, pos.y)
        # now transform all the points (vertices)
        mat.transform_vector2d_list(wld_pts)
        # done
        return wld_pts
    def transform_point(self, point, pos, forward, side):
        ''' Transform the given single point, using the provided position,
            and direction (forward and side unit vectors), to object world space. '''
        # make a copy of the original point (so we don't trash it)
        wld_pt = point.copy()
        # create a transformation matrix to perform the operations
        mat = Matrix33()
        # rotate
        mat.rotate_by_vectors_update(forward, side)
        # and translate
        mat.translate_update(pos.x, pos.y)
        # now transform the point (in place)
        mat.transform_vector2d(wld_pt)
        # done
        return wld_pt

    def get_hiding_position(self, hunter, obstacle):
        DistFromBoundry = 30.0
        DistAway = 30 + DistFromBoundry

        ToObj = obstacle.pos - hunter.pos

        ToObj.normalise()

        return(ToObj*DistAway)+obstacle.pos
开发者ID:csillamitterer,项目名称:AI-For-Games,代码行数:101,代码来源:world.py


注:本文中的agent.Agent.update方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。