def test_main(): # # Define Messages # belief = Belief('Projectile status', ('speed', 'height', 'angle')) belief.set_values([100.0, 0.3, 0.2]) direction = Direction('motion', ('horizontal direction', 'horizontal magnitude', 'vertical direction', 'vertical magnitude')) # # Define Scaffold and Map for it # agent_resources = Resource('internal_resource', ('internal_energy',)) agent_resources.set_values(20.0) change_energy = ResourceMap('adjust_energy', 'delta', 'internal_energy', ('expend_energy',)) # # Define Organs and their associated messages # moulder = Moulder('take evasive action?', make_decision, belief, direction, change_energy) splitter_1 = MessageOperator(direction, slice_labels=['horizontal direction', 'horizontal magnitude']) splitter_2 = MessageOperator(direction, slice_labels=['vertical direction', 'vertical magnitude']) actuator1 = Actuator('move left right', move_rule_horizontal, splitter_1) actuator2 = Actuator('move up down', move_rule_vertical, splitter_2) # # Initialize Agent # agent = Agent('test_agent', strict_engine=True) agent.set_organ(moulder) agent.set_organ(actuator1) agent.set_organ(actuator2) agent.set_scaffold(agent_resources) agent.set_message(belief) # # Decide on direction and execute action # agent.mould('take evasive action?') agent.act('move up down') agent.act('move left right') assert (agent.resource['internal_energy'] == REF_E[0]) belief.set_values([100.0, 1.3, -0.5]) agent.mould('take evasive action?') agent.act('move up down') agent.act('move left right') assert (agent.resource['internal_energy'] == REF_E[1]) for e1, e2 in zip(REPO, REF_REPO): assert(e1 == e2) try: agent.act('move up down') raise AssertionError('Action without preceding moulding did not raise exception') except EmptyFlashError: pass else: raise AssertionError('Action without preceding moulding did not raise expected exception') try: agent.act('move left right') raise AssertionError('Action without preceding moulding did not raise exception') except EmptyFlashError: pass else: raise AssertionError('Action without preceding moulding did not raise expected exception')
def test_main(): belief = Belief('dummy', ('a1', )) resource = Resource('internal energy', ('level', )) mapper = ResourceMap('eat energy', 'delta', 'level', ('shift', )) interpreter1 = Interpreter('thinker', lambda x: (x + 1, -1), belief, belief, resource_map_output=mapper) interpreter2 = Interpreter('thinker X', lambda x: (x + 1, -3), belief, belief, resource_map_output=mapper) battery = AutoResourceCondition('battery left', lambda x: x > 0) heart_1 = Heartbeat('big heart', battery, ticker_arithmetic=lambda: 2, max_ticker=4) heart_2 = Heartbeat('small heart', battery, ticker_arithmetic=lambda: 1, max_ticker=4) agent_1 = Agent('A1') agent_1.set_organs(interpreter1, interpreter2) belief.set_values([1]) resource.set_values([10]) agent_1.set_message(belief) agent_1.set_scaffold(resource) agent_1.set_policies(heart_1, heart_2) assert (agent_1.belief['dummy'].values()[0] == REF_B1[0]) assert (agent_1.inert == REF_I1[0]) agent_1.pump('big heart') agent_1.interpret('thinker') assert (agent_1.belief['dummy'].values()[0] == REF_B1[1]) assert (agent_1.inert == REF_I1[1]) agent_1.pump('big heart') agent_1.interpret('thinker') assert (agent_1.belief['dummy'].values()[0] == REF_B1[2]) assert (agent_1.inert == REF_I1[2]) agent_1.pump('big heart') agent_1.interpret('thinker') assert (agent_1.belief['dummy'].values()[0] == REF_B1[3]) assert (agent_1.inert == REF_I1[3]) agent_1.revive() assert (agent_1.inert == False) agent_1.pump('small heart') agent_1.interpret('thinker X') assert (agent_1.belief['dummy'].values()[0] == REF_B2[0]) assert (agent_1.inert == REF_I2[0]) agent_1.pump('small heart') agent_1.interpret('thinker X') assert (agent_1.belief['dummy'].values()[0] == REF_B2[1]) assert (agent_1.inert == REF_I2[1]) agent_1.pump('small heart') agent_1.interpret('thinker X') assert (agent_1.belief['dummy'].values()[0] == REF_B2[2]) assert (agent_1.inert == REF_I2[2]) agent_1.pump('small heart') agent_1.interpret('thinker X') assert (agent_1.belief['dummy'].values()[0] == REF_B2[3]) assert (agent_1.inert == REF_I2[3]) agent_1.pump('small heart') agent_1.interpret('thinker X') assert (agent_1.belief['dummy'].values()[0] == REF_B2[4]) assert (agent_1.inert == REF_I2[4])