def test_modulators_sensor_actor_connection(test_nodenet, test_world):
    nodenet = micropsi.get_nodenet(test_nodenet)
    micropsi.set_nodenet_properties(test_nodenet, worldadapter="Braitenberg", world_uid=test_world)
    res, s1_id = micropsi.add_node(test_nodenet, "Sensor", [10, 10], None, name="brightness_l", parameters={'datasource': 'brightness_l'})
    res, s2_id = micropsi.add_node(test_nodenet, "Sensor", [20, 20], None, name="emo_activation", parameters={'datasource': 'emo_activation'})
    res, a1_id = micropsi.add_node(test_nodenet, "Actor", [30, 30], None, name="engine_l", parameters={'datatarget': 'engine_l'})
    res, a2_id = micropsi.add_node(test_nodenet, "Actor", [40, 40], None, name="base_importance_of_intention", parameters={'datatarget': 'base_importance_of_intention'})
    res, r1_id = micropsi.add_node(test_nodenet, "Register", [10, 30], None, name="r1")
    res, r2_id = micropsi.add_node(test_nodenet, "Register", [10, 30], None, name="r2")
    s1 = nodenet.get_node(s1_id)
    s2 = nodenet.get_node(s2_id)
    r1 = nodenet.get_node(r1_id)
    r2 = nodenet.get_node(r2_id)
    s2.set_gate_parameter('gen', 'maximum', 999)
    micropsi.add_link(test_nodenet, r1_id, 'gen', a1_id, 'gen')
    micropsi.add_link(test_nodenet, r2_id, 'gen', a2_id, 'gen')
    r1.activation = 0.3
    r2.activation = 0.7
    emo_val = nodenet.get_modulator("emo_activation")

    # patch reset method, to check if datatarget was written
    def nothing():
        pass
    nodenet.worldadapter_instance.reset_datatargets = nothing

    nodenet.step()
    assert round(nodenet.worldadapter_instance.datatargets['engine_l'], 3) == 0.3
    assert round(s1.activation, 3) == round(nodenet.worldadapter_instance.get_datasource_value('brightness_l'), 3)
    assert round(s2.activation, 3) == round(emo_val, 3)
    assert round(nodenet.get_modulator('base_importance_of_intention'), 3) == 0.7
    assert round(nodenet.worldadapter_instance.datatargets['engine_l'], 3) == 0.3
    emo_val = nodenet.get_modulator("emo_activation")
    nodenet.step()
    assert round(s2.activation, 3) == round(emo_val, 3)
Esempio n. 2
0
def test_sensor_actuator_indices(test_nodenet):
    nodenet = micropsi.get_nodenet(test_nodenet)
    netapi = nodenet.netapi
    result, world_uid = micropsi.new_world('default', 'World')
    micropsi.set_nodenet_properties(test_nodenet,
                                    worldadapter='Default',
                                    world_uid=world_uid)
    sensor = netapi.create_node("Sensor", None, "static_sensor")
    sensor.set_parameter("datasource", "static_on")
    actor = netapi.create_node("Actor", None, "echo_actor")
    actor.set_parameter("datatarget", "echo")
    register = netapi.create_node("Register", None, "source")
    register.activation = 0.8
    netapi.link(register, 'gen', register, 'gen', weight=0.5)
    netapi.link(register, 'gen', actor, 'gen')
    assert sensor.activation == 0
    assert actor.get_gate('gen').activation == 0
    micropsi.step_nodenet(test_nodenet)
    micropsi.step_nodenet(test_nodenet)
    assert sensor.activation == 1
    assert round(actor.get_gate('gen').activation, 3) == 0.8
    netapi.delete_node(sensor)
    netapi.delete_node(actor)
    assert set(nodenet.rootpartition.actuator_indices) == {0}
    assert set(nodenet.rootpartition.sensor_indices) == {0}
Esempio n. 3
0
def test_node_logic_sensor_datasource(test_nodenet, default_world):
    net, netapi, source = prepare(test_nodenet)
    micropsi.set_nodenet_properties(test_nodenet, worldadapter="Default", world_uid=default_world)
    register = netapi.create_node("Register", None)
    netapi.link_sensor(register, "static_on", "gen", weight=0.35)
    micropsi.step_nodenet(test_nodenet)
    micropsi.step_nodenet(test_nodenet)
    assert round(register.get_gate("gen").activation, 3) == 0.35
Esempio n. 4
0
def test_worldadapter_update_config(default_world, default_nodenet):
    runtime.set_nodenet_properties(default_nodenet, worldadapter="Default", world_uid=default_world)
    runtime.save_nodenet(default_nodenet)
    assert runtime.nodenets[default_nodenet].worldadapter_instance.foo == 'bar'
    runtime.set_nodenet_properties(default_nodenet, worldadapter="Default", world_uid=default_world, worldadapter_config={'foo': 'changed'})
    assert runtime.nodenets[default_nodenet].worldadapter_instance.foo == 'changed'
    assert runtime.nodenets[default_nodenet].worldadapter_instance.config['foo'] == 'changed'
    assert runtime.worlds[default_world].agents[default_nodenet].foo == 'changed'
def test_worldadapter_update_calls_reset_datatargets(test_world, test_nodenet):
    world = runtime.worlds[test_world]
    nodenet = runtime.get_nodenet(test_nodenet)
    runtime.load_nodenet(test_nodenet)
    nodenet.world = world
    runtime.set_nodenet_properties(nodenet.uid, worldadapter='Braitenberg', world_uid=world.uid)
    world.agents[test_nodenet].reset_datatargets = mock.MagicMock(name='reset')
    runtime.step_nodenet(test_nodenet)
    world.agents[test_nodenet].reset_datatargets.assert_called_once()
Esempio n. 6
0
def test_worldadapter_update_calls_reset_datatargets(test_world, test_nodenet):
    world = runtime.worlds[test_world]
    nodenet = runtime.get_nodenet(test_nodenet)
    runtime.load_nodenet(test_nodenet)
    nodenet.world = test_world
    runtime.set_nodenet_properties(nodenet.uid, worldadapter='Braitenberg', world_uid=world.uid)
    world.agents[test_nodenet].reset_datatargets = mock.MagicMock(name='reset')
    runtime.step_nodenet(test_nodenet)
    world.agents[test_nodenet].reset_datatargets.assert_called_once()
Esempio n. 7
0
def test_set_agent_properties(test_world, test_nodenet):
    world = runtime.worlds[test_world]
    runtime.set_nodenet_properties(test_nodenet,
                                   worldadapter='Braitenberg',
                                   world_uid=test_world)
    runtime.set_worldagent_properties(test_world,
                                      test_nodenet,
                                      position=(5, 5))
    assert world.agents[test_nodenet].position == (5, 5)
    assert world.data['agents'][test_nodenet]['position'] == (5, 5)
Esempio n. 8
0
def test_node_logic_sensor_datasource(test_nodenet, default_world):
    net, netapi, source = prepare(test_nodenet)
    micropsi.set_nodenet_properties(test_nodenet,
                                    worldadapter="Default",
                                    world_uid=default_world)
    register = netapi.create_node("Register", None)
    netapi.link_sensor(register, "static_on", "gen", weight=0.35)
    micropsi.step_nodenet(test_nodenet)
    micropsi.step_nodenet(test_nodenet)
    assert round(register.get_gate("gen").activation, 3) == 0.35
Esempio n. 9
0
def test_node_logic_actor_datatarget(test_nodenet, default_world):
    net, netapi, source = prepare(test_nodenet)
    micropsi.set_nodenet_properties(test_nodenet, worldadapter="Default", world_uid=default_world)
    netapi.link_actor(source, "echo", weight=0.5, gate="gen")
    register = netapi.create_node("Register", None)
    actor = netapi.get_nodes(node_name_prefix="echo")[0]
    netapi.link(actor, "gen", register, "gen")
    micropsi.step_nodenet(test_nodenet)
    micropsi.step_nodenet(test_nodenet)
    micropsi.step_nodenet(test_nodenet)
    assert round(register.get_gate("gen").activation, 1) == 0.5
Esempio n. 10
0
def test_get_worldadapters(test_world, test_nodenet):
    wa = micropsi.get_worldadapters(test_world)
    assert 'Braitenberg' in wa
    assert 'description' in wa['Braitenberg']
    assert 'datasources' not in wa['Braitenberg']
    runtime.set_nodenet_properties(test_nodenet,
                                   worldadapter='Braitenberg',
                                   world_uid=test_world)
    wa = micropsi.get_worldadapters(test_world, test_nodenet)
    assert wa['Braitenberg']['datatargets'] == ['engine_l', 'engine_r']
    assert wa['Braitenberg']['datasources'] == ['brightness_l', 'brightness_r']
def test_reset_datatargets(test_world, test_nodenet):
    world = runtime.worlds[test_world]
    nodenet = runtime.get_nodenet(test_nodenet)
    runtime.load_nodenet(test_nodenet)
    nodenet.world = world
    runtime.set_nodenet_properties(nodenet.uid, worldadapter='Braitenberg', world_uid=world.uid)
    world.agents[test_nodenet].datatargets['engine_r'] = 0.7
    world.agents[test_nodenet].datatargets['engine_l'] = 0.2
    world.agents[test_nodenet].reset_datatargets()
    assert world.agents[test_nodenet].datatargets['engine_l'] == 0
    assert world.agents[test_nodenet].datatargets['engine_r'] == 0
Esempio n. 12
0
def test_agent_dying_unregisters_agent(test_world, test_nodenet):
    world = runtime.worlds[test_world]
    nodenet = runtime.get_nodenet(test_nodenet)
    runtime.load_nodenet(test_nodenet)
    nodenet.world = test_world
    runtime.set_nodenet_properties(nodenet.uid, worldadapter='Braitenberg', world_uid=world.uid)
    assert nodenet.uid in world.agents
    mockdead = mock.Mock(return_value=False)
    world.agents[nodenet.uid].is_alive = mockdead
    world.step()
    assert nodenet.uid not in world.agents
Esempio n. 13
0
def test_reset_datatargets(test_world, test_nodenet):
    world = runtime.worlds[test_world]
    nodenet = runtime.get_nodenet(test_nodenet)
    runtime.load_nodenet(test_nodenet)
    nodenet.world = test_world
    runtime.set_nodenet_properties(nodenet.uid, worldadapter='Braitenberg', world_uid=world.uid)
    world.agents[test_nodenet].datatargets['engine_r'] = 0.7
    world.agents[test_nodenet].datatargets['engine_l'] = 0.2
    world.agents[test_nodenet].reset_datatargets()
    assert world.agents[test_nodenet].datatargets['engine_l'] == 0
    assert world.agents[test_nodenet].datatargets['engine_r'] == 0
def test_register_agent(test_world, test_nodenet):
    world = runtime.worlds[test_world]
    nodenet = runtime.get_nodenet(test_nodenet)
    assert nodenet.uid not in world.data['agents']
    runtime.load_nodenet(test_nodenet)
    nodenet.world = world
    runtime.set_nodenet_properties(nodenet.uid, worldadapter='Braitenberg', world_uid=world.uid)
    assert nodenet.uid in world.data['agents']
    assert nodenet.uid in world.agents
    runtime.save_world(test_world)
    runtime.revert_world(test_world)
    assert nodenet.uid in world.data['agents']
    assert nodenet.uid in world.agents
Esempio n. 15
0
def test_node_logic_actor_datatarget(test_nodenet, default_world):
    net, netapi, source = prepare(test_nodenet)
    micropsi.set_nodenet_properties(test_nodenet,
                                    worldadapter="Default",
                                    world_uid=default_world)
    netapi.link_actor(source, "echo", weight=0.5, gate="gen")
    register = netapi.create_node("Register", None)
    actor = netapi.get_nodes(node_name_prefix="echo")[0]
    netapi.link(actor, "gen", register, "gen")
    micropsi.step_nodenet(test_nodenet)
    micropsi.step_nodenet(test_nodenet)
    micropsi.step_nodenet(test_nodenet)
    assert round(register.get_gate("gen").activation, 1) == 0.5
Esempio n. 16
0
def test_register_agent(test_world, test_nodenet):
    world = runtime.worlds[test_world]
    nodenet = runtime.get_nodenet(test_nodenet)
    assert nodenet.uid not in world.data['agents']
    runtime.load_nodenet(test_nodenet)
    nodenet.world = test_world
    runtime.set_nodenet_properties(nodenet.uid, worldadapter='Braitenberg', world_uid=world.uid)
    assert nodenet.uid in world.data['agents']
    assert nodenet.uid in world.agents
    runtime.save_world(test_world)
    runtime.revert_world(test_world)
    assert nodenet.uid in world.data['agents']
    assert nodenet.uid in world.agents
Esempio n. 17
0
def test_node_netapi_import_sensors(fixed_nodenet, test_world):
    # test importing data sources as sensors
    net, netapi, source = prepare(fixed_nodenet)
    micropsi.set_nodenet_properties(fixed_nodenet, world_uid=test_world, worldadapter='Braitenberg')
    root_ns = netapi.get_nodespace(None)
    netapi.import_sensors(root_ns.uid)
    sensors = netapi.get_nodes(root_ns.uid, nodetype="Sensor")
    assert len(sensors) == 2
    assert set([s.get_parameter('datasource') for s in sensors]) == set(net.worldadapter_instance.datasources.keys())

    # do it again, make sure we can call import multiple times
    netapi.import_sensors(root_ns.uid)
    sensors = netapi.get_nodes(root_ns.uid, nodetype="Sensor")
    assert len(sensors) == 2
Esempio n. 18
0
def test_node_netapi_import_sensors(fixed_nodenet, test_world):
    # test importing data sources as sensors
    net, netapi, source = prepare(fixed_nodenet)
    micropsi.set_nodenet_properties(fixed_nodenet,
                                    world_uid=test_world,
                                    worldadapter='Braitenberg')
    root_ns = netapi.get_nodespace(None)
    netapi.import_sensors(root_ns.uid)
    sensors = netapi.get_nodes(root_ns.uid, nodetype="Sensor")
    assert len(sensors) == 2
    assert set([s.get_parameter('datasource') for s in sensors
                ]) == set(net.worldadapter_instance.datasources.keys())

    # do it again, make sure we can call import multiple times
    netapi.import_sensors(root_ns.uid)
    sensors = netapi.get_nodes(root_ns.uid, nodetype="Sensor")
    assert len(sensors) == 2
Esempio n. 19
0
def test_actuators_do_not_reset_each_others_datatarget(test_world, test_nodenet):
    world = runtime.worlds[test_world]
    nodenet = runtime.get_nodenet(test_nodenet)
    runtime.load_nodenet(test_nodenet)
    nodenet.world = test_world
    runtime.set_runner_properties(200, 1)
    runtime.set_nodenet_properties(nodenet.uid, worldadapter='Braitenberg', world_uid=world.uid)
    actor1 = nodenet.netapi.create_node("Actor", None)
    actor2 = nodenet.netapi.create_node("Actor", None)
    actor1.set_parameter('datatarget', 'engine_r')
    actor2.set_parameter('datatarget', 'engine_r')
    reg1 = nodenet.netapi.create_node("Register", None)
    reg2 = nodenet.netapi.create_node("Register", None)
    nodenet.netapi.link(reg1, 'gen', actor1, 'gen')
    nodenet.netapi.link(reg2, 'gen', actor2, 'gen')
    reg1.activation = 0.7
    reg2.activation = 0.3
    mock_reset = mock.Mock(return_value=None)
    world.agents[test_nodenet].reset_datatargets = mock_reset
    runtime.step_nodenet(test_nodenet)
    assert world.agents[test_nodenet].datatargets['engine_r'] == 1
def test_actuators_do_not_reset_each_others_datatarget(test_world, test_nodenet):
    world = runtime.worlds[test_world]
    nodenet = runtime.get_nodenet(test_nodenet)
    runtime.load_nodenet(test_nodenet)
    nodenet.world = world
    runtime.set_runner_properties(200, 1)
    runtime.set_nodenet_properties(nodenet.uid, worldadapter='Braitenberg', world_uid=world.uid)
    actor1 = nodenet.netapi.create_node("Actor", "Root")
    actor2 = nodenet.netapi.create_node("Actor", "Root")
    actor1.set_parameter('datatarget', 'engine_r')
    actor2.set_parameter('datatarget', 'engine_r')
    reg1 = nodenet.netapi.create_node("Register", "Root")
    reg2 = nodenet.netapi.create_node("Register", "Root")
    nodenet.netapi.link(reg1, 'gen', actor1, 'gen')
    nodenet.netapi.link(reg2, 'gen', actor2, 'gen')
    reg1.activation = 0.7
    reg2.activation = 0.3
    runtime.step_nodenet(test_nodenet)
    actor1.node_function()
    actor2.node_function()
    assert world.agents[test_nodenet].datatargets['engine_r'] == 1
def test_sensor_actuator_indices(test_nodenet):
    nodenet = micropsi.get_nodenet(test_nodenet)
    netapi = nodenet.netapi
    result, world_uid = micropsi.new_world('default', 'World')
    micropsi.set_nodenet_properties(test_nodenet, worldadapter='Default', world_uid=world_uid)
    sensor = netapi.create_node("Sensor", None, "static_sensor")
    sensor.set_parameter("datasource", "static_on")
    actor = netapi.create_node("Actor", None, "echo_actor")
    actor.set_parameter("datatarget", "echo")
    register = netapi.create_node("Register", None, "source")
    register.activation = 0.8
    netapi.link(register, 'gen', register, 'gen', weight=0.5)
    netapi.link(register, 'gen', actor, 'gen')
    assert sensor.activation == 0
    assert actor.get_gate('gen').activation == 0
    micropsi.step_nodenet(test_nodenet)
    micropsi.step_nodenet(test_nodenet)
    assert sensor.activation == 1
    assert round(actor.get_gate('gen').activation, 3) == 0.8
    netapi.delete_node(sensor)
    netapi.delete_node(actor)
    assert set(nodenet.rootpartition.actuator_indices) == {0}
    assert set(nodenet.rootpartition.sensor_indices) == {0}
Esempio n. 22
0
def write_nodenet():
    user_id, permissions, token = get_request_data()
    params = dict((key, request.forms.getunicode(key)) for key in request.forms)
    worldadapter_name = params['nn_worldadapter']
    wa_params = {}
    device_map = {}
    for key in params:
        if key.startswith('worldadapter_%s_' % worldadapter_name):
            strip = len("worldadapter_%s_" % worldadapter_name)
            wa_params[key[strip:]] = params[key]
        elif key.startswith('device-map-'):
            uid = key[11:]
            device_map[uid] = params['device-name-%s' % uid]

    if "manage nodenets" in permissions:
        if not params.get('nodenet_uid'):
            result, nodenet_uid = runtime.new_nodenet(
                params['nn_name'],
                engine=params['nn_engine'],
                worldadapter=params['nn_worldadapter'],
                template=params.get('nn_template'),
                owner=user_id,
                world_uid=params.get('nn_world'),
                use_modulators=params.get('nn_modulators', False),
                worldadapter_config=wa_params,
                device_map=device_map)
            if result:
                return dict(status="success", msg="Agent created", nodenet_uid=nodenet_uid)
            else:
                return dict(status="error", msg="Error saving agent: %s" % nodenet_uid)
        else:
            result = runtime.set_nodenet_properties(
                params['nodenet_uid'],
                nodenet_name=params['nn_name'],
                worldadapter=params['nn_worldadapter'],
                world_uid=params['nn_world'],
                owner=user_id,
                worldadapter_config=wa_params,
                device_map=device_map)
            if result:
                return dict(status="success", msg="Changes saved")
            else:
                return dict(status="error", msg="Error saving changes!")
    return dict(status="error", msg="Insufficient rights to write agent")
Esempio n. 23
0
def test_modulators_sensor_actor_connection(test_nodenet, test_world):
    nodenet = micropsi.get_nodenet(test_nodenet)
    micropsi.set_nodenet_properties(test_nodenet,
                                    worldadapter="Braitenberg",
                                    world_uid=test_world)
    res, s1_id = micropsi.add_node(test_nodenet,
                                   "Sensor", [10, 10],
                                   None,
                                   name="brightness_l",
                                   parameters={'datasource': 'brightness_l'})
    res, s2_id = micropsi.add_node(test_nodenet,
                                   "Sensor", [20, 20],
                                   None,
                                   name="emo_activation",
                                   parameters={'datasource': 'emo_activation'})
    res, a1_id = micropsi.add_node(test_nodenet,
                                   "Actor", [30, 30],
                                   None,
                                   name="engine_l",
                                   parameters={'datatarget': 'engine_l'})
    res, a2_id = micropsi.add_node(
        test_nodenet,
        "Actor", [40, 40],
        None,
        name="base_importance_of_intention",
        parameters={'datatarget': 'base_importance_of_intention'})
    res, r1_id = micropsi.add_node(test_nodenet,
                                   "Register", [10, 30],
                                   None,
                                   name="r1")
    res, r2_id = micropsi.add_node(test_nodenet,
                                   "Register", [10, 30],
                                   None,
                                   name="r2")
    s1 = nodenet.get_node(s1_id)
    s2 = nodenet.get_node(s2_id)
    r1 = nodenet.get_node(r1_id)
    r2 = nodenet.get_node(r2_id)
    s2.set_gate_parameter('gen', 'maximum', 999)
    micropsi.add_link(test_nodenet, r1_id, 'gen', a1_id, 'gen')
    micropsi.add_link(test_nodenet, r2_id, 'gen', a2_id, 'gen')
    r1.activation = 0.3
    r2.activation = 0.7
    emo_val = nodenet.get_modulator("emo_activation")

    # patch reset method, to check if datatarget was written
    def nothing():
        pass

    nodenet.worldadapter_instance.reset_datatargets = nothing

    nodenet.step()
    assert round(nodenet.worldadapter_instance.datatargets['engine_l'],
                 3) == 0.3
    assert round(s1.activation, 3) == round(
        nodenet.worldadapter_instance.get_datasource_value('brightness_l'), 3)
    assert round(s2.activation, 3) == round(emo_val, 3)
    assert round(nodenet.get_modulator('base_importance_of_intention'),
                 3) == 0.7
    assert round(nodenet.worldadapter_instance.datatargets['engine_l'],
                 3) == 0.3
    emo_val = nodenet.get_modulator("emo_activation")
    nodenet.step()
    assert round(s2.activation, 3) == round(emo_val, 3)
Esempio n. 24
0
def set_nodenet_properties(nodenet_uid, nodenet_name=None, worldadapter=None, world_uid=None, owner=None):
    return runtime.set_nodenet_properties(nodenet_uid, nodenet_name=nodenet_name, worldadapter=worldadapter, world_uid=world_uid, owner=owner)
Esempio n. 25
0
def set_nodenet_properties(nodenet_uid, nodenet_name=None, worldadapter=None, world_uid=None, owner=None, worldadapter_config={}, device_map={}):
    """ Set the nodenet's properties. """
    return runtime.set_nodenet_properties(nodenet_uid, nodenet_name=nodenet_name, worldadapter=worldadapter, world_uid=world_uid, owner=owner, worldadapter_config=worldadapter_config, device_map=device_map)
def test_set_agent_properties(test_world, test_nodenet):
    world = runtime.worlds[test_world]
    runtime.set_nodenet_properties(test_nodenet, worldadapter='Braitenberg', world_uid=test_world)
    runtime.set_worldagent_properties(test_world, test_nodenet, position=(5, 5))
    assert world.agents[test_nodenet].position == (5, 5)
    assert world.data['agents'][test_nodenet]['position'] == (5, 5)