def __init__(self, cfg):
        # Replay memory
        self.memory = ReplayBuffer(**cfg['agent']['memory'])

        # Environment configuration

        self.action_shape = cfg['env']['action_shape']

        # Algorithm parameters
        self.exploration_mu, self.exploration_theta, self.exploration_sigma = cfg['agent']['noise']
        self.noise = OUNoise(self.action_shape, self.exploration_mu, self.exploration_theta, self.exploration_sigma)

        self.gamma = cfg['agent']['gamma']
        self.tau = cfg['agent']['tau']

        state_flatten_shape = [np.prod(self.memory.flatten_state_shape)]
        # Actor Model
        self.actor = Actor(state_flatten_shape, self.action_shape, cfg['env']['action_range'],
                           self.tau, self.memory.batch_size, cfg['actor'])

        # Critic Model
        self.critic = Critic(state_flatten_shape, self.action_shape, self.tau, cfg['critic'])

        # Flag & Counter
        self.add_noise = True
        self.episode = 0
        self.max_episode_explore = 100
Esempio n. 2
0
    def __init__(self, mother_ship, melee, body):
        self.body = body
        Actor.__init__(self, melee)
        self.mother_ship = mother_ship

        # Register shapes for collision callbacks
        for s in self.body.shapes:
            melee.contact_register[hash(s)] = self
Esempio n. 3
0
 def __init__(self, surface):
     image_file = 'resources/sprites/bear.png'
     bullet = Bullet('resources/sprites/bullet02.png', surface, 60)
     railgun = Railgun(bullet, 100000, self)
     Actor.__init__(self, image_file=image_file, surface=surface, health=100, damage=5,
                    fire_direction=globals.DIRECTION["TOP"], speed=8, weapon=railgun)
     scaled_size = self._scale(self._size, 2)
     self._image = pygame.transform.scale(self._img, scaled_size)
     self._rect.size = scaled_size
Esempio n. 4
0
 def __init__(self, surface):
     image_file = 'resources/sprites/chungus.jpg'
     bullet = Bullet('resources/sprites/enemy_bullet01.png', surface, 60)
     railgun = Railgun(bullet, 100000, self)
     Actor.__init__(self,
                    image_file=image_file,
                    surface=surface,
                    health=80,
                    damage=1,
                    fire_direction=globals.DIRECTION["BOTTOM"],
                    speed=6,
                    weapon=railgun)
     scaled_size = self._scale(self._size, 6)
     self._image = pygame.transform.scale(self._img, scaled_size)
     self._rect.size = scaled_size
Esempio n. 5
0
    def __init__(self, height, width):

        tile_names = [
            "Resources/images/roadA.png", "Resources/images/treesA.png",
            "Resources/images/grassA.png"
        ]

        self.height = height
        self.width = width
        tiles = [0] * self.width
        for i in range(0, self.width):
            tiles[i] = [0] * self.height
            for j in range(0, self.height):
                tiles[i][j] = open_image(tile_names[randint(0, 2)])
        board_texture = merge_images(tiles)
        board_actor = Actor()
        board_actor.set_sprite(board_texture)
Esempio n. 6
0
    def player(self, x, y):
        sprites = self.sprites
        alive = True
        pc = creatures.Creature('Bert', 40)
        container = containers.Container()
        player = Actor(x, y, sprites.width, sprites.height, sprites.S_PLAYER,
                       'Human', self.log, alive, pc, None, container)

        return player
Esempio n. 7
0
 def debugItem(self, x, y):
     sprites = self.sprites
     alive = False
     creature = None
     ai = None
     item = items.Item(self.level, identified=True)
     container = None
     debugItem = Actor(x, y, sprites.width, sprites.height,
                       sprites.S_DBG_ITEM, 'Debug_Item', self.log, alive,
                       creature, ai, container, item)
     return debugItem
Esempio n. 8
0
    def blode(self, x, y):
        sprites = self.sprites
        on_death = deaths.drop_corpse()
        alive = True

        creature = creatures.Creature('Blode', 5, on_death)
        ai = ais.Ai_Test()
        container = None
        item = items.Item(self.level)

        blode = Actor(x, y, sprites.width, sprites.height, sprites.S2_BLODE,
                      'Slime', self.log, alive, creature, ai, container, item)
        return blode
Esempio n. 9
0
    def heal_potion(self, x, y):
        on_use = heal
        sprites = self.sprites
        alive = False
        creature = None
        ai = None
        item = items.Item(self.level, 0, 0, 1, on_use)
        container = None

        heal_potion = Actor(x, y, sprites.width, sprites.height,
                            sprites.S_HEAL_POTION, 'Potion of Minor Healing',
                            self.log, alive, creature, ai, container, item)
        return heal_potion
Esempio n. 10
0
    def beano(self, x, y):
        sprites = self.sprites
        on_death = deaths.drop_corpse()
        alive = True

        creature = creatures.Creature('Beano', 15, on_death)
        ai = ais.basic_chase(4)
        container = None
        item = items.Item(self.level)

        beano = Actor(x, y, sprites.width, sprites.height, sprites.S2_BEANO,
                      'Bean', self.log, alive, creature, ai, container, item)
        return beano
Esempio n. 11
0
 def __init__(self, melee):
     Actor.__init__(self, melee)
     
     # Set max linear and angular velocity
     self.max_linear_velocity = 50
     self.max_angular_velocity = pi
     
     # Physics (based on SVG shapes)
     self.translate = calc_center(self.lines[self.parts.index(self.center_part)])
     self.svg.init(self.translate, self.scale)
     
     bodydef = Body()
     bodydef.ccd = True
     bodydef.position = self.initial_position
     self.body = melee.world.append_body(bodydef)
     self.body.linear_velocity = self.initial_velocity
     self.body.angular_velocity = self.initial_ang_vel
     
     for p in self.lines:
         polygondef = Polygon()
         polygondef.density = self.density
         # Ensure points are oriented ccw
         ccw = convex_hull(p)
         # Translate and scale points
         verts = []
         for v in ccw:
             x = (v[0] - self.translate[0]) * self.scale
             y = (v[1] - self.translate[1]) * self.scale
             verts.append(Vec2(x, y))   
         polygondef.vertices = verts
         polygondef.collision_group = self.group
         shape = self.body.append_shape(polygondef)
         # Register shapes for collision callbacks
         melee.contact_register[hash(shape)] = self
         
     self.body.set_mass_from_shapes()
Esempio n. 12
0
    def commence_battle(attacker: PlayerCharacter, defender: Actor) -> bool:

        attacker_roll = attacker.attack()
        defender_roll = defender.defend()
        dealt_damage = attacker_roll - defender_roll

        print("{} attacks {}: {} against {}.".format(attacker, defender,
                                                     attacker_roll,
                                                     defender_roll))
        if dealt_damage >= 0:
            print("{} defeats {}.".format(attacker, defender))
            return True
        else:
            print("{} was defeated by {}.".format(attacker, defender))
            return False
Esempio n. 13
0
 def add_members(self, gender='male', num=1):
     """
     Add members into family
     :param gender: male/female. if num > 1 then randomize
     :param num: default 1.
     :return: list of node ids added, new node id
     """
     node_ct = self.node_ct
     added_nodes = []
     for x in range(num):
         if num > 1:
             gender = random.choice(['male', 'female'])
         # select a name that is not taken
         name = names.get_first_name(gender=gender)
         while name in self.taken_names:
             name = names.get_first_name(gender=gender)
         self.taken_names.add(name)
         node = Actor(
             name=name, gender=gender, node_id=node_ct)
         added_nodes.append(node)
         self.family.add_node(node_ct, data=node)
         node_ct += 1
     self.node_ct = node_ct
     return added_nodes
Esempio n. 14
0
 def advantage(self, obs: Arrayable, action: Tensorable,
               actor: Actor) -> Tensor:
     return self._dt * (self.critic(obs, action) -
                        self.critic(obs, actor.act(obs)))
Esempio n. 15
0
 def __init__(self, x, y, sprite):
     self.actor = Actor()
     self.actor.set_position(x, y)
     self.actor.set_sprite(sprite)
     world.add(self.actor)
Esempio n. 16
0
def initialize():

    camera.position = Vector3(0.0, 0.0, 5.0)

    board = Board(30, 25)

    cursor = Actor()
    cursor.set_position(3, 3)
    cursor.set_size(1.25)
    world.update_layer(cursor, 5)
    cursor.set_sprite("Resources/Images/cursor.png")

    soldier = Actor()
    soldier.load_sprite_frames("Resources/Images/soldierA")
    soldier.set_size(1.0, 1.0)
    soldier.set_position(3.0, 3.0)
    soldier.play_sprite_animation(1.0, 0, 1)
    world.add(soldier)

    count = Actor()
    count.set_position(3.0, 3.0)
    count.set_sprite("Resources/Images/10.png")
    world.add(count)
Esempio n. 17
0
class Agent():
    def __init__(self, cfg):
        # Replay memory
        self.memory = ReplayBuffer(**cfg['agent']['memory'])

        # Environment configuration

        self.action_shape = cfg['env']['action_shape']

        # Algorithm parameters
        self.exploration_mu, self.exploration_theta, self.exploration_sigma = cfg['agent']['noise']
        self.noise = OUNoise(self.action_shape, self.exploration_mu, self.exploration_theta, self.exploration_sigma)

        self.gamma = cfg['agent']['gamma']
        self.tau = cfg['agent']['tau']

        state_flatten_shape = [np.prod(self.memory.flatten_state_shape)]
        # Actor Model
        self.actor = Actor(state_flatten_shape, self.action_shape, cfg['env']['action_range'],
                           self.tau, self.memory.batch_size, cfg['actor'])

        # Critic Model
        self.critic = Critic(state_flatten_shape, self.action_shape, self.tau, cfg['critic'])

        # Flag & Counter
        self.add_noise = True
        self.episode = 0
        self.max_episode_explore = 100

    def init_actor_critic(self):
        # Initialize target model
        self.critic.copy_local_in_target()
        self.actor.copy_local_in_target()

    def reset(self):
        self.memory.reset_past()
        self.noise = OUNoise(self.action_shape, self.exploration_mu, self.exploration_theta, self.exploration_sigma)

    def step(self, action, reward, next_state, done):
        # Save experience / reward
        self.memory.add(self.last_state, action, reward,
                        next_state, done)
        if done:
            self.reset()


    def act(self, state):
        self.last_state = state

        window_states = self.memory.get_state_vector(state).reshape(1, -1)
        action = self.actor.predict(window_states)

        if self.add_noise and self.episode < self.max_episode_explore:
            p = self.episode / self.max_episode_explore
            action = np.clip(action*p + (1-p)*self.noise.sample(), a_max=1, a_min=-1)

        return action

    def learn(self):
        if self.memory.is_sufficient():
            experiences = self.memory.sample()

            states = experiences['state'][:, 0].reshape(self.memory.batch_size, -1)
            actions = experiences['action']
            rewards = experiences['reward']
            dones = experiences['done']
            next_states = experiences['next_state'][:, 0].reshape(self.memory.batch_size, -1)

            # get predicted next state action and Q values from target models
            actions_next = self.actor.get_targets(next_states)
            Q_targets_next = self.critic.get_targets(next_states, actions_next)

            # Compute Q targets for current states and train critic model
            Q_targets = rewards + self.gamma * Q_targets_next * (1 - dones)
            critic_summaries = self.critic.fit(states, actions, Q_targets)

            # Train actor model
            action_gradients = self.critic.get_actions_grad(states, actions)[0]
            actor_summaries = self.actor.fit(states, action_gradients)

            # Soft-update target models
            self.critic.soft_update()
            self.actor.soft_update()

            summary_reward = summary('sample_rewards', rewards)

            return critic_summaries, actor_summaries, summary_reward
Esempio n. 18
0
 def __init__(self, melee, body):
     self.body = body
     Actor.__init__(self, melee)