def _birth(self): if not self._grid.has_empty_tiles: return new_tile_coordinates = self._grid.get_random_empty_square_coordinates() cooperation_strategy, punishment_strategy = Agent.get_random_strategies( ) agent = Agent(cooperation_strategy, punishment_strategy, 0, 0) self._grid.set_agent(agent, new_tile_coordinates)
def _mutations(self): agent_coordinates = self._grid.get_occupied_tile_coordinates() for agent_coordinate in agent_coordinates: if random.random() <= self._mutation_rate: # TODO: random strategies might include the strategies we already had, is this okay? new_coop_strategy, new_punish_strategy = Agent.get_random_strategies( ) agent = Agent(new_coop_strategy, new_punish_strategy, 0, 0) self._grid.set_agent(agent, agent_coordinate)
def test_cooperated_set_defaults_true(self): cooperation_strategy = cs.COOPERATOR punishment_strategy = ps.ANTI_SOCIAL payoff = 0 cooperated = 0 agent = Agent(cooperation_strategy, punishment_strategy, payoff, cooperated) new_agent = agent.set_cooperated() self.assertEqual(1, new_agent.cooperated)
def test_can_set_cooperated_false(self): cooperation_strategy = cs.COOPERATOR punishment_strategy = ps.ANTI_SOCIAL payoff = 0 cooperated = 0 agent = Agent(cooperation_strategy, punishment_strategy, payoff, cooperated) new_agent = agent.set_cooperated(False) self.assertEqual(0, new_agent.cooperated)
def test_payoff_with_fractions_can_marshal(self): cooperation_strategy = cs.COOPERATOR punishment_strategy = ps.ANTI_SOCIAL payoff = 10.125 cooperated = 0 agent = Agent(cooperation_strategy, punishment_strategy, payoff, cooperated) bitmap = agent.to_bitmap() agent_clone = Agent.bits_to_agent(bitmap) self.assertEqual(payoff, agent_clone.payoff)
def test_payoff_does_not_go_above_max_payoff(self): cooperation_strategy = cs.COOPERATOR punishment_strategy = ps.ANTI_SOCIAL payoff = 0 cooperated = 0 payoff_change = MAX_PAYOFF + 1 agent = Agent(cooperation_strategy, punishment_strategy, payoff, cooperated) new_agent = agent.change_payoff(payoff_change) self.assertEqual(100, new_agent.payoff)
def test_payoff_does_not_go_negative(self): cooperation_strategy = cs.COOPERATOR punishment_strategy = ps.ANTI_SOCIAL payoff = 0 cooperated = 0 payoff_change = -5 agent = Agent(cooperation_strategy, punishment_strategy, payoff, cooperated) new_agent = agent.change_payoff(payoff_change) self.assertEqual(0, new_agent.payoff)
def test_can_increase_payoff(self): cooperation_strategy = cs.COOPERATOR punishment_strategy = ps.ANTI_SOCIAL payoff = 0 cooperated = 0 payoff_change = 5 agent = Agent(cooperation_strategy, punishment_strategy, payoff, cooperated) new_agent = agent.change_payoff(payoff_change) self.assertEqual(payoff_change + payoff, new_agent.payoff)
def test_bitmap_keeps_all_data(self): cooperation_strategy = cs.COOPERATOR punishment_strategy = ps.ANTI_SOCIAL payoff = 0 cooperated = 0 agent = Agent(cooperation_strategy, punishment_strategy, payoff, cooperated) bitmap = agent.to_bitmap() agent_clone = Agent.bits_to_agent(bitmap) self.assertEqual(agent, agent_clone, "clone is not identical to original")
def test_payoff_can_have_fractions(self): cooperation_strategy = cs.COOPERATOR punishment_strategy = ps.ANTI_SOCIAL payoff = 10.125 cooperated = 0 agent = Agent(cooperation_strategy, punishment_strategy, payoff, cooperated) self.assertEqual(payoff, agent.payoff)
def test_can_fetch_all_input_data(self): cooperation_strategy = cs.COOPERATOR punishment_strategy = ps.ANTI_SOCIAL payoff = 0 cooperated = 0 agent = Agent(cooperation_strategy, punishment_strategy, payoff, cooperated) self.assertEqual(cooperation_strategy, agent.coop_strategy) self.assertEqual(punishment_strategy, agent.punish_strategy) self.assertEqual(payoff, agent.payoff) self.assertEqual(cooperated, agent.cooperated)
def _reproduce_agent(self, agent, agent_coordinate): candidate_reproduction_coordinates = [] if self._grid.is_tile_vacant( (agent_coordinate[0] - 1, agent_coordinate[1])): candidate_reproduction_coordinates.append( (agent_coordinate[0] - 1, agent_coordinate[1])) if self._grid.is_tile_vacant( (agent_coordinate[0] + 1, agent_coordinate[1])): candidate_reproduction_coordinates.append( (agent_coordinate[0] + 1, agent_coordinate[1])) if self._grid.is_tile_vacant( (agent_coordinate[0], agent_coordinate[1] - 1)): candidate_reproduction_coordinates.append( (agent_coordinate[0], agent_coordinate[1] - 1)) if self._grid.is_tile_vacant( (agent_coordinate[0], agent_coordinate[1] + 1)): candidate_reproduction_coordinates.append( (agent_coordinate[0], agent_coordinate[1] + 1)) if len(candidate_reproduction_coordinates) == 0: return fitness = Agent.get_fitness_from_payoff(agent.payoff) if random.random() > fitness: # It didn't work out today, sorry buddy. Better luck next time! return # Congratulations! you were fit enough to reproduce reproduction_coordinate_index = random.randrange( 0, len(candidate_reproduction_coordinates)) reproduction_coordinate = candidate_reproduction_coordinates[ reproduction_coordinate_index] offspring = Agent(agent.coop_strategy, agent.punish_strategy, 0, 0) self._grid.set_agent(offspring, reproduction_coordinate)
def __str__(self): sb = '' sb += '[\n' for x in range(0, self._size): for y in range(0, self._size): agent_bits = self._grid[x, y] if agent_bits == 0: sb += ' < EMPTY TILE > ' else: sb += ' <{0}> '.format( Agent.bits_to_agent(self._grid[x, y])) sb += '\n' sb += ']' return sb
def get_agent(self, coordinates): agent_bits = self._grid[coordinates[0], coordinates[1]] return Agent.bits_to_agent(agent_bits)