def test_emotions_self_action(self): """Test emotions for an action by an agent with multiple entities.""" hit = Action() hit.name = "Hit" agent = Agent(0, 0, 0, 0, 0) agent_entity_id_1 = agent.entity_id agent_entity_id_2 = 'test_123' preferences = agent.get_preferences() preferences.set_goodness(hit, -1) preferences.set_praiseworthiness(hit, -1) preferences.set_love(agent_entity_id_1, 1) preferences.set_love(agent_entity_id_2, 1) timestamp = 0 truth_value = 1 agent.beliefs.register_entity(agent_entity_id_1) agent.beliefs.register_entity(agent_entity_id_2) agent.beliefs.set_entity_is_entity( timestamp, agent_entity_id_1, agent_entity_id_2, truth_value ) prob = 1 emotions = agent.emotions_for_action( hit, agent_entity_id_1, agent_entity_id_2, prob) self.assertEqual(len(emotions), 2) # Feeling bad for hitting someone agent loves (agent) self.assertIsInstance(emotions[0], Remorse) # Feeling bad for being hit (by agent) self.assertIsInstance(emotions[1], Distress)
def test_tick_mood_int(self): """Test tick mood with int amounts.""" agent = Agent(.01, -.01, .01, -.01, .01) agent.emotions = [Joy(1)] agent.tick_mood() self.assertEqual(len(agent.emotions), 1) self.assertEqual(agent.emotions[0].amount, .5)
def test_set_get_preferences(self): action = Action() action.name = "test_action" p = Preferences() p.set_goodness(action, 1) agent = Agent(0, 0, 0, 0, 0) agent.set_preferences(p) p_returned = agent.get_preferences() self.assertEqual(p_returned.get_goodness(action), 1)
def test_revise_belief(self): """ agent 1 believes red knight is dead. agent 1 believes agent 2 is alive. agent 1 then believes red knight is agent 2. agent 1 believes agent 2 is not alive. """ agent_1 = Agent(0, 0, 0, 0, 0) red_knight_entity = Object() agent_2_entity = Object() agent_1.beliefs.register_entity(red_knight_entity.entity_id) agent_1.beliefs.register_entity(agent_2_entity.entity_id) # agent_1 beliefs agent_2 is alive agent_2_alive = EntityAttrBelief(0, agent_2_entity.entity_id, 'alive', 1) agent_1.beliefs.set_belief(agent_2_alive) # agent_1 believes red knight is not alive red_knight_dead = EntityAttrBelief(1, red_knight_entity.entity_id, 'alive', 0) agent_1.beliefs.set_belief(red_knight_dead) # agent_1 believes red knight is agent_2 agent_1.beliefs.set_entity_is_entity( 2, red_knight_entity.entity_id, agent_2_entity.entity_id, 1) # Check that agent 1 believes that agent 2 is not alive agent_2_alive = agent_1.beliefs.get_entity_attr( agent_2_entity.entity_id, 'alive') self.assertEqual(agent_2_alive, 0)
def test_apply_action(self): subject_agent = Agent(0, 0, 0, 0, 0) object_agent = Agent(0, 0, 0, 0, 0) viewer_agent = Agent(0, 0, 0, 0, 0) viewer_preferences = viewer_agent.get_preferences() viewer_preferences.set_love(subject_agent.entity_id, 1) viewer_preferences.set_love(object_agent.entity_id, 1) viewer_preferences.set_praiseworthiness(Kill, -1) viewer_preferences.set_goodness(Kill, -1) apply_action( action=Kill, subject=subject_agent, obj=object_agent, viewers=[viewer_agent] ) self.assertEqual(len(viewer_agent.emotions), 2)
def create_society(seed_generation_size=SEED_GENERATION_SIZE, number_of_generations=NUMBER_OF_GENERATIONS, children_per_parents=CHILDREN_PER_PARENTS): agents = [] # Create the seed generation first_generation = [] while len(first_generation) < SEED_GENERATION_SIZE - 1: # Create seed generation agent agent = Agent.create_random_agent() first_generation.append(agent) generations = [first_generation] generations_created = 1 while generations_created < NUMBER_OF_GENERATIONS: # Create a generation based on the previous generation generation = create_generation( generations[generations_created - 1], CHILDREN_PER_PARENTS ) generations.append(generation) generations_created += 1 for generation in generations: agents.extend(generation) return agents
def test_emotions_for_multiple_object_entities(self): """Test emotions for an object represented as multiple entities.""" hit = Action() hit.name = "Hit" object_entity_id_1 = '123a' object_entity_id_2 = '123b' subject_entity_id = '456' observer = Agent(0, 0, 0, 0, 0) preferences = observer.get_preferences() preferences.set_goodness(hit, -1) preferences.set_praiseworthiness(hit, -1) preferences.set_love(object_entity_id_1, 1) preferences.set_love(object_entity_id_2, -1) preferences.set_love(subject_entity_id, 1) timestamp = 0 truth_value = 1 observer.beliefs.register_entity(object_entity_id_1) observer.beliefs.register_entity(object_entity_id_2) observer.beliefs.set_entity_is_entity( timestamp, object_entity_id_1, object_entity_id_2, truth_value ) prob = 1 emotions = observer.emotions_for_action( hit, subject_entity_id, object_entity_id_1, prob) self.assertEqual(len(emotions), 3) # Anger towards the person who hit (a bad thing to do) self.assertIsInstance(emotions[0], Anger) # Sorry for the loved person being hit self.assertIsInstance(emotions[1], SorryFor) # Gloating over the hated person being hit self.assertIsInstance(emotions[2], Gloating)
def test_tick_mood(self): agent = Agent(.01, -.01, .01, -.01, .01) self.assertEqual(agent.mood.type, "Disdainful") agent.emotions = [Joy(.05)] agent.tick_mood() self.assertEqual(len(agent.emotions), 1) self.assertEqual(agent.emotions[0].amount, .025) self.assertEqual(agent.mood.type, "Relaxed") # After enough ticks, the emotion should be removed from memory agent.tick_mood() agent.tick_mood() self.assertEqual(len(agent.emotions), 0) self.assertEqual(agent.mood.type, "Disdainful")
def test_emotions_for_observed_action(self): agent_1 = Agent(0, 0, 0, 0, 0) agent_2 = Agent(-1, -1, 1, -1, 1) action = Action() action.name = "Hit" p = Preferences() p.set_goodness(action, -.5) p.set_praiseworthiness(action, -1) p.set_love(agent_2.entity_id, -.5) agent_1.set_preferences(p) agent_2.set_preferences(p) emotions = agent_1._emotions_for_observed_action( action, agent_1.entity_id, agent_2.entity_id ) self.assertEqual(len(emotions), 2) emotion_1 = emotions.pop() self.assertIsInstance(emotion_1, Gloating) self.assertEqual(emotion_1.amount, .25) emotion_2 = emotions.pop() self.assertIsInstance(emotion_2, Remorse) self.assertEqual(emotion_2.amount, 1)
def create_child(agent_1, agent_2, child_gender): """Create a new agent and relationships simulating childbirth.""" if agent_1.gender == agent_2.gender: raise ValueError("Mating not possible.") # Create a new agent based on the personalities of the parents p1 = agent_1.personality p2 = agent_2.personality p3 = combine_personalities(p1, p2, CHILD_PERSONALITY_VARIATION) child = Agent.from_personality(p3) child.set_parents(agent_1, agent_2) child.name = generate_name(child_gender) child.gender = child_gender agent_1.add_child(child) agent_2.add_child(child) print "%s and %s have a child child: %s" % ( agent_1.name, agent_2.name, child.name) return child
def create_child(agent_1, agent_2, child_gender): """Create a new agent and relationships simulating childbirth.""" if agent_1.gender == agent_2.gender: raise ValueError("Mating not possible.") # Create a new agent based on the personalities of the parents p1 = agent_1.personality p2 = agent_2.personality p3 = combine_personalities(p1, p2, CHILD_PERSONALITY_VARIATION) child = Agent.from_personality(p3) child.set_parents(agent_1, agent_2) child.name = generate_name(child_gender) child.gender = child_gender agent_1.add_child(child) agent_2.add_child(child) print "%s and %s have a child child: %s" % (agent_1.name, agent_2.name, child.name) return child
def create_society(seed_generation_size=SEED_GENERATION_SIZE, number_of_generations=NUMBER_OF_GENERATIONS, children_per_parents=CHILDREN_PER_PARENTS): agents = [] # Create the seed generation first_generation = [] while len(first_generation) < SEED_GENERATION_SIZE - 1: # Create seed generation agent agent = Agent.create_random_agent() first_generation.append(agent) generations = [first_generation] generations_created = 1 while generations_created < NUMBER_OF_GENERATIONS: # Create a generation based on the previous generation generation = create_generation(generations[generations_created - 1], CHILDREN_PER_PARENTS) generations.append(generation) generations_created += 1 for generation in generations: agents.extend(generation) return agents
def test_emotions_for_action(self): subject = Agent(0, 0, 0, 0, 0) obj = Agent(0, 0, 0, 0, 0) observer = Agent(0, 0, 0, 0, 0) hit = Action() hit.name = "Hit" preferences = observer.get_preferences() preferences.set_goodness(hit, -1) preferences.set_praiseworthiness(hit, -1) preferences.set_love(subject.entity_id, 1) preferences.set_love(obj.entity_id, -1) prob = 1 emotions = observer.emotions_for_action( hit, subject.entity_id, obj.entity_id, prob) self.assertEqual(len(emotions), 2) self.assertIsInstance(emotions[0], Anger) self.assertIsInstance(emotions[1], Gloating)
if dice_roll < 3: print "%s parries into better position." % defender.name defender_advantage = defender_advantage + 1 elif dice_roll < 5: print "%s blocks the attack." % defender.name elif dice_roll < 7: print "%s blocks the attack and %s presses forward." % (defender.name, attacker.name) attacker_advantage = attacker_advantage + 1 elif dice_roll < 9: print "%s makes contact and %s shows blood." % (attacker.name, defender.name) attacker_advantage = attacker_advantage + 1 defender_health = defender_health - 1 else: print "%s wounds %s deeply." % (attacker.name, defender.name) attacker_advantage = attacker_advantage + 1 defender_health = defender_health - 2 attacker_advantage = max(attacker_advantage, 2) defender_advantage = max(defender_advantage, 2) return (attacker_advantage, defender_health, defender_advantage) if __name__ == "__main__": runs = 1 while runs > 0: runs = runs - 1 p1 = Agent.create_random_agent() p2 = Agent.create_random_agent() advantage, p1alive, p2alive = joust(p1, p2)
if dice_roll < 3: print "%s parries into better position." % defender.name defender_advantage = defender_advantage + 1 elif dice_roll < 5: print "%s blocks the attack." % defender.name elif dice_roll < 7: print "%s blocks the attack and %s presses forward." % ( defender.name, attacker.name) attacker_advantage = attacker_advantage + 1 elif dice_roll < 9: print "%s makes contact and %s shows blood." % ( attacker.name, defender.name) attacker_advantage = attacker_advantage + 1 defender_health = defender_health - 1 else: print "%s wounds %s deeply." % (attacker.name, defender.name) attacker_advantage = attacker_advantage + 1 defender_health = defender_health - 2 attacker_advantage = max(attacker_advantage, 2) defender_advantage = max(defender_advantage, 2) return (attacker_advantage, defender_health, defender_advantage) if __name__ == "__main__": runs = 1 while runs > 0: runs = runs - 1 p1 = Agent.create_random_agent() p2 = Agent.create_random_agent() advantage, p1alive, p2alive = joust(p1, p2)
def test_from_OCEAN(self): """Create an agent given a personality.""" agent = Agent(-1, -1, 1, -1, 1) self.assertEqual(agent.mood.type, "Disdainful")
def test_create_random_agent(self): agent = Agent.create_random_agent() self.assertIsInstance(agent.name, basestring)
def test_beliefs(self): """New agents should have beliefs.""" agent = Agent(0, 0, 0, 0, 0) self.assertIsInstance(agent.beliefs, Beliefs)
def test_uuid(self): """New agents should have a uuid.""" agent = Agent(0, 0, 0, 0, 0) uuid = getattr(agent, "uuid") self.assertIsInstance(uuid, basestring)