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():

    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)
Ejemplo n.º 3
0
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])
Ejemplo n.º 4
0
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]))
Ejemplo n.º 5
0
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')
Ejemplo n.º 6
0
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])