def __init__(self, name, rude, loud, big, foul): super().__init__(name, strict_engine=True) resource = Resource('Storage', ('item_1', 'item_2', 'External gene')) resource.set_values([2, 4, 0]) essence = Essence('Persona', ('rude', 'loud', 'big', 'foul')) essence.set_values([rude, loud, big, foul]) self.set_scaffolds(resource, essence) buzz = Buzz('Env Feeling', ('gene_bump', )) belief = Belief('Gene in Env', ('yes_no', )) interpreter = Interpreter('Is gene in env?', lambda x: x, buzz, belief) direction_r = Direction('Receive', ('yes_no', )) moulder_r = Moulder('Receive gene', lambda x: x, belief, direction_r) belief_eject = Belief('Time to eject', ('yes_no', )) direction_e = Direction('Eject', ('payload', )) r1_map = ResourceMap('item1', 'delta', 'item_1', ('value', )) r2_map = ResourceMap('item2', 'delta', 'item_2', ('value', )) r_map = MapCollection([r1_map, r2_map]) moulder_e = Moulder('Eject gene', self.ejector, belief_eject, direction_e, r_map) consume_payload = ResourceMap('consume', 'reset', 'External gene', ('value', )) moulder_cross = Moulder('Make cross-over baby', self.make_baby, None, direction_e, consume_payload) self.set_organs(interpreter, moulder_r, moulder_e, moulder_cross) self.set_messages(buzz, belief, direction_r, direction_e)
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 __init__(self, name): super().__init__(name, strict_engine=True) essence = Essence('Trusting', ('degree',)) essence.set_values([100.0]) belief = Belief('Knowledge', ('path to water', 'time to sow')) belief.set_values(['dummy_instruction_1', 20180101]) direction = Direction('Share this', ('knowledge label', 'knowledge unit')) moulder = Moulder('What to share', self.what_to_share, belief, direction, essence_op_input=essence) self.set_organ(moulder) self.set_scaffold(essence) self.set_message(belief)
def __init__(self, name, intent, strength, precision): super().__init__(name) essence = Essence('Skills', ('strength', 'precision')) essence.set_values([strength, precision]) belief = Belief('Intent to kill', ('yes_no', )) belief.set_values([intent]) direction = Direction('Push spear outward', ('yes_no', )) moulder = Moulder('Attempt to kill', lambda x: x, belief, direction) self.set_organ(moulder) self.set_messages(belief, direction) self.set_scaffold(essence)
def __init__(self, name): super().__init__(name, True) essence = Essence('Trusting', ('degree',)) essence.set_values([10.0]) belief = Belief('Knowledge', ('path to water', 'time to sow')) belief.set_values(['stupid_idea', 20180731]) buzz = Buzz('Knowledge feed', ('words',)) interpreter = Interpreter('Should I accept teaching', self.what_to_accept, buzz, belief, essence_op_input=essence, belief_updater=True) self.set_organ(interpreter) self.set_scaffold(essence) self.set_message(buzz)
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 __init__(self, name, diameter_init, energy_init, essence_value): super().__init__(name, strict_engine=True) belief = Belief('The diameter of the world', ('value', 'precision')) belief.set_values([diameter_init, 10.0]) resource = Resource('Dietary energy', ('value',)) essence = Essence('Persistence', ('value',)) resource.set_values(energy_init) essence.set_values(essence_value) self.set_scaffolds(resource, essence) metabolism = ResourceMap('Dietary energy adjustment', 'delta', 'value', ('shift',)) interpreter = Interpreter('Contemplate', self.contemplation, belief, belief, metabolism) self.set_organ(interpreter)
def test_main(): # Not fully implemented assert 1==2 battery = Resource('battery power', ('potential',)) battery.set_values(0.401) suck_power = ResourceMap('suck power', 'delta', 'potential', ('reduction',)) b1 = Buzz('sound stimulation', ('band_1', 'band_2', 'band_3')) s1 = Sensor('sound in surrounding', listen, b1, suck_power) belief_1 = Belief('The word was spoken', ('probability',)) int1 = Interpreter('Was the word spoken?', word_estimator, b1, belief_1) dir_1 = Direction('follow up request', ('word_section_1', 'word_section_2', 'word_section_3', 'word_section_4')) moul1 = Moulder('What response to give', responder, belief_1, dir_1) a1 = Actuator('loudspeaker vibrations', loudspeaker_api, dir_1, suck_power) moul2 = Moulder('Select warn statement', warn_statement, None, dir_1, resource_op_input=battery) bc = AutoBeliefCondition('Sufficiently confident of word spoken', lambda p: p > 0.75, 'The word was spoken') rc = AutoResourceCondition('Sufficient power left', lambda pot: pot > 0.2, 'potential') clausul_1 = Clause('listen for the word', [('sense', 'sound in surrounding'), ('interpret', 'Was the word spoken?')], condition=bc) clausul_2 = Clause('say something to the user', [('mould', 'What response to give'), ('act', 'loudspeaker vibrations')]) clausul_3 = Clause('power check', condition=rc) clausul_4 = Clause('power warn', [('mould', 'Select warn statement'), ('act', 'loudspeaker vibrations')]) plan = Plan('Clever stuff') plan.add_cargo('pronounce', 'listen for the word') plan.add_cargo('pronounce', 'say something to the user') plan.add_cargo('pronounce', 'power check') plan.add_cargo('pronounce', 'power warn') plan.add_dependency(2, 0, 3) plan.add_dependency(0, 1) plan.stamp_and_approve() agent = Agent('smart loudspeaker') agent.set_organs(s1, int1, moul1, moul2, a1) agent.set_messages(b1, belief_1, dir_1) agent.set_scaffold(battery) agent.set_policies(clausul_1, clausul_2, clausul_3, clausul_4, plan) agent.enact('Clever stuff') agent.enact('Clever stuff') agent.enact('Clever stuff') agent.enact('Clever stuff')
def __init__(self, name, e1, e2, r1, r2, b1, b2): super().__init__(name, strict_engine=True) essence = Essence('Bacteria Essence', ['E1', 'E2']) essence.set_values([e1, e2]) resource = Resource('Bacteria Resource', ['R1', 'R2']) resource.set_values([r1, r2]) belief = Belief('Bacteria Belief', ['B1', 'B2']) belief.set_values([b1, b2]) self.set_scaffolds(essence, resource) self.set_message(belief) imprints = self.get_imprint_repr() total_a_sampler = AgentSampler('full_state', resource_args=imprints['resource'], essence_args=imprints['essence'], belief_args=imprints['belief']) self.set_sampler(total_a_sampler)
def test_main(): # Define Messages # buzz = Buzz('audio_trigger', (tuple(['a' + str(n) for n in range(1, 20)]), )) belief = Belief('trigger_spoken', ['probability']) direction = Direction('say_this', ['sentence']) # # Define Organs and their associated messages # env = Env() sensor = Sensor('listen', env.ear, buzz) interpreter = Interpreter('was_trigger_word_spoken', trigger_word, buzz, belief) moulder = Moulder('follow_up_question', question_maker, belief, direction) actuator = Actuator('speak', env.mouth, direction) # # Autonomous constraints # belief_condition = AutoBeliefCondition('heard_it', lambda x: x > 0.9, 'trigger_spoken') # # Plan # clause_1 = Clause('sound_trigger', [('sense', 'listen'), ('interpret', 'was_trigger_word_spoken')], condition=belief_condition) clause_2 = Clause('response_formation', [('mould', 'follow_up_question'), ('act', 'speak')]) heart = Heartbeat('beater', max_ticker=N_HEARTS) # # Initialize Agent # agent = SlimAgent('test_agent') agent.set_organ(sensor) agent.set_organ(interpreter) agent.set_organ(moulder) agent.set_organ(actuator) agent.set_policy(clause_1) agent.set_policy(clause_2) agent.set_policy(heart) agent() assert (env.env_interactions == REF)
def test_main(): buzz = Buzz('nose_tingle', ['nerve_1', 'nerve_2']) sensor = Sensor('smell_the_roses', smeller, buzz) belief = Belief('world_blossoms', ['certainty']) interpreter = Interpreter('does_the_world_blossom', nerve_analyzer, buzz, belief) direction = Direction('words_to_say', ['first_word', 'second_word']) moulder = Moulder('what_to_say', word_smith, belief, direction) actuator = Actuator('say_it', speak, direction, actuator_func_kwargs={'well': True}) agent = Agent('simple human', strict_engine=True) agent.set_organs(sensor, interpreter, moulder, actuator) agent.sense('smell_the_roses') agent.interpret('does_the_world_blossom') agent.mould('what_to_say') agent.act('say_it') agent.sense('smell_the_roses') agent.interpret('does_the_world_blossom') agent.mould('what_to_say') agent.act('say_it') agent.sense('smell_the_roses') agent.interpret('does_the_world_blossom') agent.mould('what_to_say') agent.act('say_it') agent.sense('smell_the_roses') agent.interpret('does_the_world_blossom') agent.mould('what_to_say') agent.act('say_it') agent.sense('smell_the_roses') agent.interpret('does_the_world_blossom') agent.mould('what_to_say') agent.act('say_it') assert (read_env() == REF)
def __init__(self, name, n_people, n_fishes, n_potato, how_low, max_fish): super().__init__(name) # # Resources # village_resource = Resource('village items', ['n_fishes', 'n_potato', 'n_people']) village_resource.set_values([n_fishes, n_potato, n_people]) self.set_scaffold(village_resource) village_food = MessageOperator(village_resource, slice_labels=['n_fishes', 'n_potato']) # # Essence # disposition = Essence('disposition', ['how_low', 'max_extraction']) disposition.set_values([how_low, max_fish]) self.set_scaffold(disposition) # # Belief # fish_status = Belief('must fish', ['assessment']) potato_status = Belief('must grow', ['assessment']) trust_in_neigh = Belief('trust of neighbours', ['degree']) self.set_messages(fish_status, potato_status, trust_in_neigh) food_belief = MessageOperator([fish_status, potato_status], extend=True) # # Flash messages # direct_fishing_act = Direction('go fish like this', ['number_boats', 'upper_limit']) direct_grow_act = Direction('go grow field like this', ['number_farmers']) self.set_messages(direct_fishing_act, direct_grow_act) direct_food_acquisition = MessageOperator( [direct_fishing_act, direct_grow_act], extend=True) # # Organs # stock_ok = Interpreter('storage conditions', self.check_storage, None, food_belief, resource_op_input=village_food, essence_op_input=XXX) get_food = Moulder('how to acquire food', self.food_instructions, food_belief, direct_food_acquisition) self.set_organs(stock_ok, get_food) # # Survival condition # heart_cond = AutoResourceCondition('still alive', lambda x: x > 0, ('n_people', )) heart = Heartbeat('alive', (heart_cond, )) self.set_policies(heart)
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 # 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 # 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 __init__(self, name, midpoint_share=0.0, max_share=0.0, midpoint_gulp=0.0, max_gulp=0.0, midpoint_tox=0.0, max_tox=0.0, truthful_reveal=1.0, inverse_forget_rate=0.5, birth_cost=2.0, agent_id=None): super().__init__(name, agent_id_system=agent_id, strict_engine=STRICT_ENGINE) # # Essence unit_essence = Essence('Exterior Disposition', ('midpoint_share', 'max_share', 'midpoint_gulp', 'max_gulp', 'midpoint_tox', 'max_tox', 'truthful_reveal', 'inv_forget_rate')) unit_essence.set_values([midpoint_share, max_share, midpoint_gulp, max_gulp, midpoint_tox, max_tox, truthful_reveal, inverse_forget_rate]) self.set_scaffold(unit_essence) # Essence reset map, convenience function for offspring creation self.essence_map_reset = universal_map_maker(unit_essence, 'reset', ('value',)) # # Resource unit_resource = Resource('Internal Resources', ('info_a', 'info_b', 'info_c', 'bad_info')) unit_resource.set_values([0.0, 0.0, 0.0, 0.0]) self.set_scaffold(unit_resource) # Resource reset and scale map, convenience function for offspring creation self.resource_reset = universal_map_maker(unit_resource, 'reset', ('value',)) self.resource_scale = universal_map_maker(unit_resource, 'delta_scale', ('value1', 'value2')) # Resource operator only relating to info resource, not toxin unit_resource_info = MessageOperator(unit_resource, slice_labels=['info_a', 'info_b', 'info_c']) # # Belief unit_belief = Belief('Surrounding', ('cooperative_env',)) unit_belief.set_values([0.0]) self.set_message(unit_belief) # # Interpreters buzz = Buzz('Neighbour Cooperator', ('revealed_coop',)) self.set_message(buzz) interpreter = Interpreter('Friendly Environment', self._cmp_friendly_env, buzz, unit_belief, belief_updater=True) self.set_organ(interpreter) # # Moulders direction = Direction('Resources to Share', ('d_info_a', 'd_info_b', 'd_info_c')) moulder = Moulder('Share Resources', self._cmp_share_resources, unit_belief, direction, resource_op_input=unit_resource_info) self.set_organ(moulder) direction = Direction('Fraction to Gulp', ('f_gulp',)) moulder = Moulder('Gulp from Env', self._cmp_gulp_fraction, unit_belief, direction) self.set_organ(moulder) split_resource = universal_map_maker(self.resource, 'delta_scale', ('delta', 'factor',)) direction = Direction('Offspring', ('agent_split',)) moulder = Moulder('Create Agent Offspring', self._cmp_offspring, None, direction, resource_map_output=split_resource, moulder_func_kwargs={'birth_delta':birth_cost}) self.set_organ(moulder) direction = Direction('Lies to Eject', ('amount',)) moulder = Moulder('Eject Lies', self._cmp_lies_ejection, unit_belief, direction, resource_op_input=unit_resource_info) self.set_organ(moulder) # # Cortex coop_expose = Feature('Cooperative Reveal', ('coop_with_coop',)) cortex = Cortex('Reveal Cooperation', self._cmp_cooperative_feature, None, coop_expose) self.set_organ(cortex)
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(): 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])
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')