コード例 #1
0
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
コード例 #2
0
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)
コード例 #3
0
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')
コード例 #4
0
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'
        )