def test_main(): # # Define Messages # buzz = Buzz('view_of_dice', ['dice_1', 'dice_2', 'dice_3', 'dice_4', 'dice_5']) belief = Belief('world_is_good', ['joy_index']) # # Define Organs and their associated messages # sensor = Sensor('check_roll', dice_sensor, buzz) interpreter = Interpreter('was_it_good_roll', roll_interpreter, buzz, belief, belief_updater=True) # # Initialize Agent # agent = Agent('test_agent', strict_engine=True) agent.set_organ(sensor) agent.set_organ(interpreter) beliefs = [] for k in range(0, 5): agent.sense('check_roll') agent.interpret('was_it_good_roll') beliefs.append(agent.belief['world_is_good'].values()[0]) assert (beliefs == REF_OUTCOME)
def test_main(): # # Define Messages # belief = Belief('stressed', ('degree', )) belief.set_values(10.0) direction1 = Direction('collect_amount', ('intake_volume', )) # # Define Scaffold and Map for it # agent_resources = Resource('internal_molecules', ('A', 'B', 'C')) agent_resources.set_values([2.0, 1.0, 0.5]) added_a = ResourceMap('get_A', 'delta_scale', 'A', ( 'add', 'dilute', )) added_b = ResourceMap('get_B', 'delta_scale', 'B', ( 'add', 'dilute', )) added_c = ResourceMap('get_C', 'delta_scale', 'C', ( 'add', 'dilute', )) add_internal = MapCollection([added_a, added_b, added_c]) # # Define Organs and their associated messages # moulder1 = Moulder('discard_or_not', make_decision, belief, direction1) env = Env() actuator1 = Actuator('collect', env.excretor, direction1, add_internal) # # Initialize Agent # agent = Agent('test_agent', strict_engine=True) agent.set_organ(moulder1) agent.set_organ(actuator1) agent.set_scaffold(agent_resources) # # Decide on direction and execute action # agent.mould('discard_or_not') agent.act('collect') for mol in REF: assert (isclose(REF[mol], agent.resource[mol], abs_tol=0.0001)) try: agent.act('collect') raise AssertionError( 'Acting twice in sequence did not raise correct exception') except EmptyFlashError: pass
def test_main(): # # Define Messages # feature = Feature('colour', ('hue', 'saturation', 'lightness')) # # Define Scaffold # agent_essence = Essence('my_parameters', ('hue', 'saturation', 'lightness', 'mood')) agent_essence.set_values([0.9, 0.4, 1.0, 'jubilant']) slicer_of_essence = MessageOperator( agent_essence, slice_labels=['hue', 'saturation', 'lightness']) # # Define Organs and their associated messages # cortex = Cortex('colour_revealer', expose_distort, slicer_of_essence, feature, cortex_func_kwargs={'distort_degree': 0.05}) # # Initialize Agent # agent = Agent('test_agent', strict_engine=True) agent.set_organ(cortex) agent.set_scaffold(agent_essence) # # Tickle the cortex # tickle_me_1 = agent.tickle('colour_revealer') for val, ref_val in zip(tickle_me_1.values(), REF1): assert (val == pytest.approx(ref_val, abs=1e-3)) tickle_me_2 = agent.tickle('colour_revealer') for val, ref_val in zip(tickle_me_2.values(), REF2): assert (val == pytest.approx(ref_val, abs=1e-3)) agent_essence.set_values([0.0, 0.0, 0.0, 'hey']) tickle_me_3 = agent.tickle('colour_revealer') for val, ref_val in zip(tickle_me_3.values(), REF3): assert (val == pytest.approx(ref_val, abs=1e-3))
def test_main(): # # Define Messages # belief_input_1 = Belief('hostile_neighbourhood', ['hostility_index']) belief_input_2 = Belief('i_am_followed', ['probability']) belief_output = Belief('i_am_about_to_be_mugged', ['best_guess']) belief_merge_input = MessageOperator([belief_input_1, belief_input_2], extend=True) # # Define Organs and their associated messages # interpreter = Interpreter('about_to_be_mugged', evaluate, belief_merge_input, belief_output) # # Initialize Agent # agent = Agent('test_agent', strict_engine=True) agent.set_organ(interpreter) agent._set('belief', 'hostile_neighbourhood', belief_input_1) agent._set('belief', 'i_am_followed', belief_input_2) belief_input_1.set_values(0.8) belief_input_2.set_values(0.95) agent.interpret('about_to_be_mugged') outcome_1 = agent.belief['i_am_about_to_be_mugged'].values() belief_input_1.set_values(0.0) belief_input_2.set_values(0.0) agent.interpret('about_to_be_mugged') outcome_2 = agent.belief['i_am_about_to_be_mugged'].values() belief_input_1.set_values(0.9) belief_input_2.set_values(1.0) agent.interpret('about_to_be_mugged') outcome_3 = agent.belief['i_am_about_to_be_mugged'].values() assert (outcome_1[0] == REFVALUES[0]) assert (outcome_2[0] == REFVALUES[1]) assert (outcome_3[0] == REFVALUES[2])
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(): # # Define Messages # buzz = Buzz('nerve_endings', ('first', 'second')) belief_1 = Belief('chance_of_rain', ('probability', )) belief_2 = Belief('ambiguity_kills_me', ('mood', )) direction = Direction('get_which_umbrella', ('any', 'colour')) # # Define Scaffold and Map for it # agent_resources = Resource('internal_resource', ('energy', )) agent_resources.set_values([100.0]) change_energy = ResourceMap('adjust_energy', 'delta', 'energy', ('how_much', )) # # Define Organs and their associated messages # interpreter_1 = Interpreter('will_it_rain', rain_predictor, buzz, belief_1, resource_map_output=change_energy) interpreter_2 = Interpreter('am_i_unlucky', mood_maker, belief_1, belief_2, resource_map_output=change_energy) total_belief = MessageOperator([belief_1, belief_2], extend=True) moulder = Moulder('fetch_umbrella_type', make_decision, total_belief, direction, change_energy) # # Initialize Agent # agent = Agent('test_agent', strict_engine=True) agent._set('buzz', 'nerve_endings', buzz) agent.set_organ(moulder) agent.set_organ(interpreter_1) agent.set_organ(interpreter_2) agent.set_scaffold(agent_resources) # # Decide on direction and execute action # agent.buzz['nerve_endings'].set_values([0.2, 0.2]) agent.interpret('will_it_rain') agent.interpret('am_i_unlucky') agent.mould('fetch_umbrella_type') assert (agent.direction['get_which_umbrella'].values() == REF[0]) assert (agent.resource.values()[0] == pytest.approx(REF_RESOURCE[0])) agent.buzz['nerve_endings'].set_values([1.0, 0.6]) agent.interpret('will_it_rain') agent.interpret('am_i_unlucky') agent.mould('fetch_umbrella_type') assert (agent.direction['get_which_umbrella'].values() == REF[1]) assert (agent.resource.values()[0] == pytest.approx(REF_RESOURCE[1])) agent.buzz['nerve_endings'].set_values([1.0, 2.6]) agent.interpret('will_it_rain') agent.interpret('am_i_unlucky') agent.mould('fetch_umbrella_type') assert (agent.direction['get_which_umbrella'].values() == REF[2]) assert (agent.resource.values()[0] == pytest.approx(REF_RESOURCE[2]))
def test_main(): # # Define Messages # belief = Belief('rich_environment', ('stuff_1', 'stuff_2', 'stuff_3', 'stuff_4')) belief.set_values([0.7, 0.3, 0.6, 0.1]) direction1 = Direction('grab_this_much', ('grab_volume', )) direction2 = Direction('shout_loud', ('volume', )) # # Define Scaffold and Map for it # agent_resources = Resource('internal_resource', ('internal_energy', 'carrot', 'leek')) agent_resources.set_values([100.0, 4, 5]) change_energy = ResourceMap('adjust_energy', 'delta', 'internal_energy', ('expend_energy', )) change_carrot = ResourceMap('adjust_carrot', 'delta', 'carrot', ('tweak_carrot', )) change_leek = ResourceMap('adjust_leek', 'delta', 'leek', ('tweak_leek', )) hoarding_food = MapCollection([change_carrot, change_leek]) # # Define Organs and their associated messages # moulder1 = Moulder('reach_and_grab', make_decision, belief, direction1, change_energy) moulder2 = Moulder('shout_how_much', shouter, belief, direction2) env = Env() actuator1 = Actuator('grab_it', env.grabber, direction1, hoarding_food) actuator2 = Actuator('shout', env.shout_into_void, direction2) # # Initialize Agent # agent = Agent('test_agent', strict_engine=True) agent.set_organ(moulder1) agent.set_organ(moulder2) agent.set_organ(actuator1) agent.set_organ(actuator2) agent.set_scaffold(agent_resources) # # Decide on direction and execute action # agent.mould('reach_and_grab') agent.act('grab_it') assert (agent.resource.values() == REF_RESOURCE_OUTCOME) agent.mould('shout_how_much') agent.act('shout') agent.mould('shout_how_much') agent.act('shout') assert (isclose(env.loud_cumsum, 3.4)) try: agent.act('shout') 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(): agent_core = Agent('Coordinator') essence = Essence('Coordinator Essence', ('param_1', 'param_2', 'param_3', 'param_4', 'param_5', 'param_6')) essence.set_values([1.0,2.0,3.0,4.0,5.0,6.0]) resource = Resource('Coordinator Resource', ('Energy Status',)) resource.set_values([75.0]) agent_core.set_scaffolds(essence, resource) agents = [agent_core] nodes = [Node('Central', agent_core)] for k in range(4): agent = Agent('Sensor Leaf %s' %(str(k)), strict_engine=True) essence = Essence('Sensor Essence', ('Sensitivity', 'Quality')) essence.set_values([78.0 + float(k), 99.0]) resource = Resource('Sensor Resource', ('Battery Power',)) resource.set_values([100.0 - 3.0 * k]) buzz = Buzz('Power Spectrum Sample', ('amplitude_max', 'freq_mode')) belief = Belief('Is There Motion', ('size', 'speed')) motion_classifier = Interpreter('From Freq to Object', pw2obj, buzz, belief) agent.set_scaffolds(essence, resource) agent.set_messages(buzz, belief) agent.set_organ(motion_classifier) agents.append(agent) nodes.append(Node('Sensor', agent, LocalEnv(k * k))) nodes.append(Node('dummy', None)) nodes.append(Node('dummy', None)) star_graph = nx.generators.classic.star_graph(nodes) ams = House('A House', agents, star_graph) for node in ams: agent = node.agent_content if not agent is None: if 'Is There Motion' in agent.belief: agent.sense('Feel microwaves') agent.interpret('From Freq to Object') central_a_sampler = AgentSampler('central_sampler', essence_args=[('Coordinator Essence', 'param_2'), ('Coordinator Essence', 'param_6')], resource_args=[('Coordinator Resource', 'Energy Status')], agent_matcher=_match_c) leaf_a_sampler = AgentSampler('leaf_sampler', resource_args=[('Sensor Resource', 'Battery Power')], belief_args=[('Is There Motion', 'size'), ('Is There Motion', 'speed')], agent_matcher=_match_l) env_sampler = EnvSampler('env_sampler', env_stuff, agent_matcher=_match_l) graph_sampler = GraphSampler('g_sampler', lambda x: x.name) io = SystemIO() io.set_write_rule('central', central_a_sampler, 'to_csv') io.set_write_rule('leaf', leaf_a_sampler, 'to_csv') io.set_write_rule('env', env_sampler, 'to_json') io.set_write_rule('graph_props', graph_sampler, 'gexf.write_gexf') io.try_stamp(ams, 0) exist_1 = os.path.isfile('central0.csv') exist_2 = os.path.isfile('leaf0.csv') exist_3 = os.path.isfile('env0.json') exist_4 = os.path.isfile('graph_props0.gexf') assert (exist_1) assert (exist_2) assert (exist_3) assert (exist_4) if exist_1: data = open('central0.csv').read() assert ('essence:Coordinator Essence:param_2,2.0' in data) assert ('essence:Coordinator Essence:param_6,6.0' in data) assert (not 'essence:Coordinator Essence:param_1' in data) assert (not 'essence:Coordinator Essence:param_3' in data) assert (not 'essence:Coordinator Essence:param_4' in data) assert (not 'essence:Coordinator Essence:param_5' in data) assert ('resource:Coordinator Resource:Energy Status,75.0' in data) os.remove('central0.csv') if exist_2: data = open('leaf0.csv').read() assert ('0,Sensor Leaf 0,' in data) assert ('0,Sensor Leaf 1,' in data) assert ('0,Sensor Leaf 2,' in data) assert ('0,Sensor Leaf 3,' in data) assert ('belief:Is There Motion:size,small' in data) assert (not 'belief:Is There Motion:size,large' in data) assert ('belief:Is There Motion:speed,fast' in data) assert ('belief:Is There Motion:speed,slow' in data) assert ('belief:Is There Motion:speed,n/a' in data) os.remove('leaf0.csv') if exist_3: data = open('env0.json').read() assert ('"[0,"Sensor Leaf 0"' in data) assert ('"[0,"Sensor Leaf 1"' in data) assert ('"[0,"Sensor Leaf 2"' in data) assert ('"[0,"Sensor Leaf 3"' in data) assert ('"env_stuff"]":0' in data) assert ('"env_stuff"]":1' in data) assert ('"env_stuff"]":4' in data) assert ('"env_stuff"]":9' in data) os.remove('env0.json') if exist_4: data = open('graph_props0.gexf').read().split('\n') REFS = ['Sensor_0', 'Sensor_1', 'Sensor_2', 'Sensor_3', 'unoccupied_0', 'unoccupied_1'] for row in data: if '<edge ' in row: assert ('"Central"' in row) for r in REFS: if r in row: REFS.remove(r) break else: raise AssertionError('Missed node %s' %(r)) assert (len(REFS) == 0) os.remove('graph_props0.gexf')