Ejemplo n.º 1
0
    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(set_random):

    essence = Essence('inclinations', ['t_1', 't_2', 't_3', 't_4', 't_5'])
    essence.set_values([0.0, 0.0, 0.0, 0.0, 0.0])

    agent = Agent('thin agent', strict_engine=True)
    agent.set_scaffold(essence)

    ams = AgentManagementSystem('exterior laws', [agent])

    mapper_1 = EssenceMap('tweak', 'wiener', 't_1', ('range_step', ))
    mapper_2 = EssenceMap('tweak', 'wiener', 't_2', ('range_step', ))
    mapper_3 = EssenceMap('tweak', 'wiener', 't_3', ('range_step', ))
    mapper_4 = EssenceMap('tweak', 'wiener', 't_4', ('range_step', ))
    mapper_5 = EssenceMap('tweak', 'wiener', 't_5', ('range_step', ))
    mapper = MapCollection([mapper_1, mapper_2, mapper_3, mapper_4, mapper_5])
    mutate = MultiMutation('jumps', lambda: 1.0, mapper, mutation_prob=0.5)
    ams.set_law(mutate)

    vals = agent.essence.values()
    assert (all([x == pytest.approx(y) for x, y in zip(vals, REF[0])]))
    ams.mutate(agent, 'jumps')
    vals = agent.essence.values()
    assert (all([x == pytest.approx(y) for x, y in zip(vals, REF[1])]))
    ams.mutate(agent, 'jumps')
    vals = agent.essence.values()
    assert (all([x == pytest.approx(y) for x, y in zip(vals, REF[2])]))
    ams.mutate(agent, 'jumps')
    vals = agent.essence.values()
    assert (all([x == pytest.approx(y) for x, y in zip(vals, REF[3])]))
    ams.mutate(agent, 'jumps')
    vals = agent.essence.values()
    assert (all([x == pytest.approx(y) for x, y in zip(vals, REF[4])]))
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
Ejemplo n.º 4
0
    def ejector(self, should_eject):

        e_map = universal_map_maker(self.essence, 'reset', ('value', ))
        e_map.set_values(self.essence.values())

        r1_map = ResourceMap('item1', 'delta', 'item_1', ('value', ))
        r2_map = ResourceMap('item2', 'delta', 'item_2', ('value', ))
        r_map = MapCollection([r1_map, r2_map])

        split_half = [
            x / 2.0 for key, x in self.resource.items() if 'item' in key
        ]
        r_map.set_values(split_half)

        ret = [(e_map, r_map)]
        ret.extend([-1.0 * x for x in split_half])

        return tuple(ret)
Ejemplo n.º 5
0
def test_main():
    resource = Resource('harvest', ('wheat', 'rye', 'barley', 'oats'))
    resource.set_values([35.0, 12.0, 8.0, 6.0])
    agent1 = Agent('farmer Joe', strict_engine=True)
    agent1.set_scaffold(resource)

    resource = Resource('food producer', ('wheat', 'rye', 'barley', 'oats',
                                          'cash_on_hand'))
    resource.set_values([0.0, 0.0, 0.0, 0.0, 100.0])
    agent2 = Agent('yummy Inc', strict_engine=True)
    agent2.set_scaffold(resource)

    ams = AgentManagementSystem('food market', [agent1, agent2])
    uuid_of_yummy = list(ams.agents_in_scope.values())[1].agent_id_system

    mapper = ResourceMap('cash loss', 'scale', 'cash_on_hand', ('factor',))
    amort = Compulsion('amortization', interest_rate, mapper)
    ams.set_law(amort)

    mapper_wheat = ResourceMap('loss of wheat', 'scale', 'wheat', ('factor',))
    mapper_rye = ResourceMap('loss of rye', 'scale', 'rye', ('factor',))
    mapper_barley = ResourceMap('loss of barley', 'scale', 'barley', ('factor',))
    mapper_oats = ResourceMap('loss of oats', 'scale', 'oats', ('factor',))
    mapper_cereal = MapCollection([mapper_wheat, mapper_rye, mapper_barley, mapper_oats])
    sloppy = Compulsion('sloppy', lambda : 4*[0.88], mapper_cereal)
    ams.set_law(sloppy)

    ams.make_lawbook_entry(['sloppy'], agent_name_selector=farm_name_test)
    ams.make_lawbook_entry(['amortization'], agent_ids=[uuid_of_yummy])

    for agent in ams.cycle_nodes(True, 2):
        ams.engage_all_verbs(agent, validate_lawbook=True)

    vals1 = agent1.resource.values()
    vals2 = agent2.resource.values()

    assert (vals1 == REF1)
    assert (vals2 == REF2)

    ams.compel(agent2, 'sloppy')

    try:
        ams.compel(agent2, 'sloppy', validate_lawbook=True)
    except RuntimeError as e:
        if 'Compulsion sloppy is not in law book' in str(e):
            pass
        else:
            raise AssertionError('Exception was not properly raised')

    try:
        ams.compel(agent1, 'amortization')
    except KeyError as e:
        if 'cash_on_hand' in str(e):
            pass
        else:
            raise AssertionError('Exception was not properly raised')
Ejemplo n.º 6
0
    def __init__(self, name, agents, lake):

        super().__init__(name, agents, common_env=lake)

        map_fish = ResourceMap('fish stock change', 'delta', 'n_fishes',
                               ('adjust_fish', ))
        map_potato = ResourceMap('potato stock change', 'delta', 'n_potatoes',
                                 ('adjust_potato', ))
        map_people = ResourceMap('birth death', 'delta', 'n_people',
                                 ('adjust_people', ))
        eat_love_death = MapCollection([map_fish, map_potato, map_people])

        natural_reqs = Compulsion('survival demands', self.eat_life_die,
                                  eat_love_death)
        self.set_law(natural_reqs)

        for agent in agents:
            more_fish = ResourceMap('add_fish', 'delta', 'n_fishes',
                                    ('add_fishes', ))
            fish_from_lake = Actuator('fish from lake', self.extract_from_lake,
                                      agent.direction['go fish like this'],
                                      more_fish)
            agent.set_organ(fish_from_lake)
Ejemplo n.º 7
0
    def __init__(self, name, agents, full_agents_graph, midpoint_max_move,
                 max_max_move, mutate_prob, resource_jump_magnitude,
                 resource_jump_prob, mutate_essence):

        super().__init__(name,
                         agents,
                         full_agents_graph=full_agents_graph,
                         strict_engine=STRICT_ENGINE)

        self.midpoint_max_move = midpoint_max_move
        self.max_max_move = max_max_move

        for agent in agents:

            #
            # Sensor
            sensor = Sensor('Feel Neighbour Surface',
                            self._cmp_neighbour_coop,
                            agent.buzz['Neighbour Cooperator'],
                            agent_id_to_engine=True)
            agent.set_organ(sensor)

            #
            # Actuator
            a_a_subtract = ResourceMap('Consume A', 'delta', 'info_a',
                                       ('removal', ))
            a_b_subtract = ResourceMap('Consume B', 'delta', 'info_b',
                                       ('removal', ))
            a_c_subtract = ResourceMap('Consume C', 'delta', 'info_c',
                                       ('removal', ))
            consume_resources = MapCollection(
                [a_a_subtract, a_b_subtract, a_c_subtract])
            actuator = Actuator('Share Resources to Neighbours',
                                self._cmp_alter_env_resources,
                                agent.direction['Resources to Share'],
                                agent_id_to_engine=True,
                                resource_map_output=consume_resources)
            agent.set_organ(actuator)

            actuator = Actuator('Spread Lies to Neighbours',
                                self._cmp_spread_lies,
                                agent.direction['Lies to Eject'],
                                agent_id_to_engine=True,
                                resource_map_output=consume_resources)
            agent.set_organ(actuator)

            add_from_env = universal_map_maker(agent.resource, 'delta',
                                               ('add', ))
            actuator = Actuator('Gulp Environment',
                                self._cmp_gulp_env,
                                agent.direction['Fraction to Gulp'],
                                agent_id_to_engine=True,
                                resource_map_output=add_from_env)
            agent.set_organ(actuator)

            actuator = Actuator('Push Offspring Onto World',
                                self._cmp_offspring_onto_world,
                                agent.direction['Offspring'],
                                agent_id_to_engine=True)
            agent.set_organ(actuator)

        #
        # Mutation
        map_midpoint_share = EssenceMap('mutate_1', 'wiener', 'midpoint_share',
                                        ('range_step', ))
        map_midpoint_gulp = EssenceMap('mutate_2', 'wiener', 'midpoint_gulp',
                                       ('range_step', ))
        map_midpoint_tox = EssenceMap('mutate_3', 'wiener', 'midpoint_tox',
                                      ('range_step', ))
        map_max_share = EssenceMap('mutate_1b', 'wiener_bounded', 'max_share',
                                   ('range_step', 'lower', 'upper'))
        map_max_gulp = EssenceMap('mutate_2b', 'wiener_bounded', 'max_gulp',
                                  ('range_step', 'lower', 'upper'))
        map_max_tox = EssenceMap('mutate_3b', 'wiener_bounded', 'max_tox',
                                 ('range_step', 'lower', 'upper'))

        list_mid = []
        list_max = []
        if 'share' in mutate_essence:
            list_mid.append(map_midpoint_share)
            list_max.append(map_max_share)
        if 'gulp' in mutate_essence:
            list_mid.append(map_midpoint_gulp)
            list_max.append(map_max_gulp)
        if 'tox' in mutate_essence:
            list_mid.append(map_midpoint_tox)
            list_max.append(map_max_tox)

        if len(list_mid) > 0:
            mapper_midpoint = MapCollection(list_mid)
            mapper_max = MapCollection(list_max)
            mutate_midpoint = MultiMutation('Perturb Essence 1',
                                            self._midpoint_move,
                                            mapper_midpoint,
                                            mutation_prob=mutate_prob)
            mutate_max = MultiMutation('Perturb Essence 2',
                                       self._max_move,
                                       mapper_max,
                                       mutation_prob=mutate_prob)
            self.set_laws(mutate_midpoint, mutate_max)

        #
        # Random increase in internal resources
        self.resource_jump_magnitude = resource_jump_magnitude
        self.resource_jump_prob = resource_jump_prob
        a_jump = ResourceMap('Jump Increase A', 'delta', 'info_a', ('add', ))
        b_jump = ResourceMap('Jump Increase B', 'delta', 'info_b', ('add', ))
        c_jump = ResourceMap('Jump Increase C', 'delta', 'info_c', ('add', ))
        jump_resources = MapCollection([a_jump, b_jump, c_jump])
        resource_jump = Compulsion('Random Jump of Resources',
                                   self._cmp_resource_jump, jump_resources)
        self.set_law(resource_jump)
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'
        )