Beispiel #1
0
    def test_control_signal_and_control_projection_names(self):
        D1 = pnl.DDM(name='D1')
        D2 = pnl.DDM(name='D2')

        # ControlSignal with one ControlProjection
        C1 = pnl.ControlMechanism(control_signals=[D1.parameter_states[
                                                       psyneulink.core.components.functions.distributionfunctions.DRIFT_RATE]])
        assert C1.control_signals[0].name == 'D1[drift_rate] ControlSignal'
        assert C1.control_signals[0].efferents[0].name == 'ControlProjection for D1[drift_rate]'

        # ControlSignal with two ControlProjection to two parameters of same Mechanism
        C2 = pnl.ControlMechanism(control_signals=[{pnl.PROJECTIONS:[D1.parameter_states[
                                                                         psyneulink.core.components.functions.distributionfunctions.DRIFT_RATE],
                                                                     D1.parameter_states[
                                                                         psyneulink.core.components.functions.distributionfunctions.THRESHOLD]]}])
        assert C2.control_signals[0].name == 'D1[drift_rate, threshold] ControlSignal'
        assert C2.control_signals[0].efferents[0].name == 'ControlProjection for D1[drift_rate]'
        assert C2.control_signals[0].efferents[1].name == 'ControlProjection for D1[threshold]'

        # ControlSignal with two ControlProjection to two parameters of different Mechanisms
        C3 = pnl.ControlMechanism(control_signals=[{pnl.PROJECTIONS:[D1.parameter_states[
                                                                         psyneulink.core.components.functions.distributionfunctions.DRIFT_RATE],
                                                                     D2.parameter_states[
                                                                         psyneulink.core.components.functions.distributionfunctions.DRIFT_RATE]]}])
        assert C3.control_signals[0].name == 'ControlSignal-0 divergent ControlSignal'
        assert C3.control_signals[0].efferents[0].name == 'ControlProjection for D1[drift_rate]'
        assert C3.control_signals[0].efferents[1].name == 'ControlProjection for D2[drift_rate]'
Beispiel #2
0
    def test_DDM(self):
        myMechanism = pnl.DDM(
            function=psyneulink.core.components.functions.nonstateful.
            distributionfunctions.DriftDiffusionAnalytical(
                drift_rate=(1.0),
                threshold=(10.0),
                starting_point=0.0,
            ),
            name='My_DDM',
        )

        myMechanism_2 = pnl.DDM(
            function=psyneulink.core.components.functions.nonstateful.
            distributionfunctions.DriftDiffusionAnalytical(drift_rate=2.0,
                                                           threshold=20.0),
            name='My_DDM_2')

        myMechanism_3 = pnl.DDM(
            function=psyneulink.core.components.functions.nonstateful.
            distributionfunctions.DriftDiffusionAnalytical(drift_rate=3.0,
                                                           threshold=30.0),
            name='My_DDM_3',
        )

        z = pnl.Composition()
        z.add_linear_processing_pathway([
            myMechanism,
            pnl.MappingProjection(matrix=pnl.IDENTITY_MATRIX), myMechanism_2,
            pnl.MappingProjection(matrix=pnl.FULL_CONNECTIVITY_MATRIX),
            myMechanism_3
        ])

        result = z.run(inputs={myMechanism: [[40]]})[0][0]

        expected_output = [
            (myMechanism.input_ports[0].parameters.value.get(z),
             np.array([40.])),
            (myMechanism.output_ports[0].parameters.value.get(z),
             np.array([10.])),
            (myMechanism_2.input_ports[0].parameters.value.get(z),
             np.array([10.])),
            (myMechanism_2.output_ports[0].parameters.value.get(z),
             np.array([20.])),
            (myMechanism_3.input_ports[0].parameters.value.get(z),
             np.array([20.])),
            (myMechanism_3.output_ports[0].parameters.value.get(z),
             np.array([30.])),
            (result, np.array([30.])),
        ]

        for i in range(len(expected_output)):
            val, expected = expected_output[i]
            # setting absolute tolerance to be in accordance with reference_output precision
            # if you do not specify, assert_allcose will use a relative tolerance of 1e-07,
            # which WILL FAIL unless you gather higher precision values to use as reference
            np.testing.assert_allclose(
                val,
                expected,
                atol=1e-08,
                err_msg='Failed on expected_output[{0}]'.format(i))
Beispiel #3
0
def test_DDM_threshold_modulation(mode):
    M = pnl.DDM(
        name='DDM',
        function=pnl.DriftDiffusionAnalytical(
            threshold=20.0,
        ),
    )
    monitor = pnl.TransferMechanism(default_variable=[[0.0]],
                                    size=1,
                                    function=pnl.Linear(slope=1, intercept=0),
                                    output_ports=[pnl.RESULT],
                                    name='monitor')

    control = pnl.ControlMechanism(
            monitor_for_control=monitor,
            control_signals=[(pnl.THRESHOLD, M)])

    C = pnl.Composition()
    C.add_node(M, required_roles=[pnl.NodeRole.ORIGIN, pnl.NodeRole.TERMINAL])
    C.add_node(monitor)
    C.add_node(control)
    inputs = {M:[1], monitor:[3]}
    val = C.run(inputs, num_trials=1, bin_execute=mode)
    # FIXME: Python version returns dtype=object
    val = np.asfarray(val)
    assert np.allclose(val[0], [60.0])
    assert np.allclose(val[1], [60.2])
    def test_multiple_modulatory_projections_with_mech_and_state_name_specs(
            self):

        M = pnl.DDM(name='MY DDM')
        C = pnl.ControlMechanism(control_signals=[{
            pnl.MECHANISM:
            M,
            pnl.PARAMETER_STATES: [pnl.DRIFT_RATE, pnl.THRESHOLD]
        }])
        G = pnl.GatingMechanism(gating_signals=[{
            pnl.MECHANISM:
            M,
            pnl.OUTPUT_STATES: [pnl.DECISION_VARIABLE, pnl.RESPONSE_TIME]
        }])
        assert len(C.control_signals) == 1
        assert len(C.control_signals[0].efferents) == 2
        assert M.parameter_states[pnl.DRIFT_RATE].mod_afferents[
            0] == C.control_signals[0].efferents[0]
        assert M.parameter_states[pnl.THRESHOLD].mod_afferents[
            0] == C.control_signals[0].efferents[1]
        assert len(G.gating_signals) == 1
        assert len(G.gating_signals[0].efferents) == 2
        assert M.output_states[pnl.DECISION_VARIABLE].mod_afferents[
            0] == G.gating_signals[0].efferents[0]
        assert M.output_states[pnl.RESPONSE_TIME].mod_afferents[
            0] == G.gating_signals[0].efferents[1]
    def test_multiple_modulatory_projection_specs(self):

        M = pnl.DDM(name='MY DDM')
        C = pnl.ControlMechanism(control_signals=[{
            pnl.PROJECTIONS: [
                M.parameter_states[pnl.DRIFT_RATE], M.parameter_states[
                    pnl.THRESHOLD]
            ]
        }])
        G = pnl.GatingMechanism(gating_signals=[{
            pnl.PROJECTIONS: [
                M.output_states[pnl.DECISION_VARIABLE], M.output_states[
                    pnl.RESPONSE_TIME]
            ]
        }])
        assert len(C.control_signals) == 1
        assert len(C.control_signals[0].efferents) == 2
        assert M.parameter_states[pnl.DRIFT_RATE].mod_afferents[
            0] == C.control_signals[0].efferents[0]
        assert M.parameter_states[pnl.THRESHOLD].mod_afferents[
            0] == C.control_signals[0].efferents[1]
        assert len(G.gating_signals) == 1
        assert len(G.gating_signals[0].efferents) == 2
        assert M.output_states[pnl.DECISION_VARIABLE].mod_afferents[
            0] == G.gating_signals[0].efferents[0]
        assert M.output_states[pnl.RESPONSE_TIME].mod_afferents[
            0] == G.gating_signals[0].efferents[1]
    def test_multiple_modulatory_projections_with_mech_and_port_Name_specs(
            self):

        M = pnl.DDM(name='MY DDM')
        C = pnl.ControlMechanism(control_signals=[{
            pnl.MECHANISM:
            M,
            pnl.PARAMETER_PORTS: [
                psyneulink.core.components.functions.distributionfunctions.
                DRIFT_RATE, psyneulink.core.globals.keywords.THRESHOLD
            ]
        }])
        G = pnl.GatingMechanism(gating_signals=[{
            pnl.MECHANISM:
            M,
            pnl.OUTPUT_PORTS: [pnl.DECISION_VARIABLE, pnl.RESPONSE_TIME]
        }])
        assert len(C.control_signals) == 1
        assert len(C.control_signals[0].efferents) == 2
        assert M.parameter_ports[
            psyneulink.core.components.functions.distributionfunctions.
            DRIFT_RATE].mod_afferents[0] == C.control_signals[0].efferents[0]
        assert M.parameter_ports[
            psyneulink.core.globals.keywords.
            THRESHOLD].mod_afferents[0] == C.control_signals[0].efferents[1]
        assert len(G.gating_signals) == 1
        assert len(G.gating_signals[0].efferents) == 2
        assert M.output_ports[pnl.DECISION_VARIABLE].mod_afferents[
            0] == G.gating_signals[0].efferents[0]
        assert M.output_ports[pnl.RESPONSE_TIME].mod_afferents[
            0] == G.gating_signals[0].efferents[1]
    def test_multiple_modulatory_projections_with_port_Name(self):

        M = pnl.DDM(name='MY DDM')
        C = pnl.ControlMechanism(control_signals=[{
            'DECISION_CONTROL': [
                M.parameter_ports[psyneulink.core.components.functions.
                                  distributionfunctions.DRIFT_RATE],
                M.parameter_ports[psyneulink.core.globals.keywords.THRESHOLD]
            ]
        }])
        G = pnl.GatingMechanism(gating_signals=[{
            'DDM_OUTPUT_GATE': [
                M.output_ports[pnl.DECISION_VARIABLE], M.output_ports[
                    pnl.RESPONSE_TIME]
            ]
        }])
        assert len(C.control_signals) == 1
        assert C.control_signals[0].name == 'DECISION_CONTROL'
        assert len(C.control_signals[0].efferents) == 2
        assert M.parameter_ports[
            psyneulink.core.components.functions.distributionfunctions.
            DRIFT_RATE].mod_afferents[0] == C.control_signals[0].efferents[0]
        assert M.parameter_ports[
            psyneulink.core.globals.keywords.
            THRESHOLD].mod_afferents[0] == C.control_signals[0].efferents[1]
        assert len(G.gating_signals) == 1
        assert G.gating_signals[0].name == 'DDM_OUTPUT_GATE'
        assert len(G.gating_signals[0].efferents) == 2
        assert M.output_ports[pnl.DECISION_VARIABLE].mod_afferents[
            0] == G.gating_signals[0].efferents[0]
        assert M.output_ports[pnl.RESPONSE_TIME].mod_afferents[
            0] == G.gating_signals[0].efferents[1]
def test_reinforcement_fixed_targets():
    input_layer = TransferMechanism(
        size=2,
        name='Input Layer',
    )

    action_selection = pnl.DDM(input_format=pnl.ARRAY,
                               function=pnl.DriftDiffusionAnalytical(),
                               output_states=[pnl.SELECTED_INPUT_ARRAY],
                               name='DDM')

    p = Process(pathway=[input_layer, action_selection],
                learning=LearningProjection(learning_function=Reinforcement(
                    learning_rate=0.05)))

    input_list = {input_layer: [[1, 1], [1, 1]]}
    s = System(processes=[p],
               # learning_rate=0.05,
               )
    targets = [[10.], [10.]]

    # logged_mechanisms = [input_layer, action_selection]
    # for mech in s.learning_mechanisms:
    #     logged_mechanisms.append(mech)
    #
    # for mech in logged_mechanisms:
    #     mech.log.set_log_conditions(items=[pnl.VALUE])

    results = s.run(inputs=input_list, targets=targets)

    assert np.allclose(action_selection.value, [[1.], [2.30401336], [0.97340301], [0.02659699], [2.30401336], \
                                                [2.08614798], [1.85006765], [2.30401336], [2.08614798], [1.85006765]])
    def test_multiple_modulatory_projection_specs(self):

        M = pnl.DDM(name='MY DDM')
        C = pnl.ControlMechanism(control_signals=[{
            pnl.PROJECTIONS: [
                M.parameter_ports[
                    psyneulink.core.components.functions.nonstateful.
                    distributionfunctions.DRIFT_RATE], M.parameter_ports[
                        psyneulink.core.globals.keywords.THRESHOLD]
            ]
        }])
        G = pnl.GatingMechanism(gating_signals=[{
            pnl.PROJECTIONS: [
                M.output_ports[pnl.DECISION_VARIABLE], M.output_ports[
                    pnl.RESPONSE_TIME]
            ]
        }])
        assert len(C.control_signals) == 1
        assert len(C.control_signals[0].efferents) == 2
        assert M.parameter_ports[
            psyneulink.core.components.functions.nonstateful.
            distributionfunctions.
            DRIFT_RATE].mod_afferents[0] == C.control_signals[0].efferents[0]
        assert M.parameter_ports[
            psyneulink.core.globals.keywords.
            THRESHOLD].mod_afferents[0] == C.control_signals[0].efferents[1]
        assert len(G.gating_signals) == 1
        assert len(G.gating_signals[0].efferents) == 2
        assert M.output_ports[pnl.DECISION_VARIABLE].mod_afferents[
            0] == G.gating_signals[0].efferents[0]
        assert M.output_ports[pnl.RESPONSE_TIME].mod_afferents[
            0] == G.gating_signals[0].efferents[1]
    def test_multiple_modulatory_projections_with_state_name(self):

        M = pnl.DDM(name='MY DDM')
        C = pnl.ControlMechanism(control_signals=[{
            'DECISION_CONTROL': [
                M.parameter_states[pnl.DRIFT_RATE], M.parameter_states[
                    pnl.THRESHOLD]
            ]
        }])
        G = pnl.GatingMechanism(gating_signals=[{
            'DDM_OUTPUT_GATE': [
                M.output_states[pnl.DECISION_VARIABLE], M.output_states[
                    pnl.RESPONSE_TIME]
            ]
        }])
        assert len(C.control_signals) == 1
        assert C.control_signals[0].name == 'DECISION_CONTROL'
        assert len(C.control_signals[0].efferents) == 2
        assert M.parameter_states[pnl.DRIFT_RATE].mod_afferents[
            0] == C.control_signals[0].efferents[0]
        assert M.parameter_states[pnl.THRESHOLD].mod_afferents[
            0] == C.control_signals[0].efferents[1]
        assert len(G.gating_signals) == 1
        assert G.gating_signals[0].name == 'DDM_OUTPUT_GATE'
        assert len(G.gating_signals[0].efferents) == 2
        assert M.output_states[pnl.DECISION_VARIABLE].mod_afferents[
            0] == G.gating_signals[0].efferents[0]
        assert M.output_states[pnl.RESPONSE_TIME].mod_afferents[
            0] == G.gating_signals[0].efferents[1]
Beispiel #11
0
    def test_2_item_tuple_from_parameter_state_to_control_signals(self):

        C = pnl.ControlMechanism(control_signals=['a','b'])
        D = pnl.DDM(name='D3',
                     function=pnl.BogaczEtAl(drift_rate=(3,C),
                                             threshold=(2,C.control_signals['b']))
                    )
        assert D.parameter_states[pnl.DRIFT_RATE].mod_afferents[0].sender==C.control_signals[0]
        assert D.parameter_states[pnl.THRESHOLD].mod_afferents[0].sender==C.control_signals[1]
Beispiel #12
0
    def test_2_item_tuple_from_gating_signal_to_output_states(self):

        D4 = pnl.DDM(name='D4')

        # Single name
        G = pnl.GatingMechanism(gating_signals=[(pnl.DECISION_VARIABLE, D4)])
        assert G.gating_signals[0].name == 'D4[DECISION_VARIABLE] GatingSignal'
        assert G.gating_signals[0].efferents[0].receiver.name == 'DECISION_VARIABLE'

        # List of names
        G = pnl.GatingMechanism(gating_signals=[([pnl.DECISION_VARIABLE, pnl.RESPONSE_TIME], D4)])
        assert G.gating_signals[0].name == 'D4[DECISION_VARIABLE, RESPONSE_TIME] GatingSignal'
        assert G.gating_signals[0].efferents[0].receiver.name == 'DECISION_VARIABLE'
        assert G.gating_signals[0].efferents[1].receiver.name == 'RESPONSE_TIME'
Beispiel #13
0
    def test_2_item_tuple_from_control_signal_to_parameter_state(self):

        D = pnl.DDM(name='D')

        # Single name
        C = pnl.ControlMechanism(control_signals=[(pnl.DRIFT_RATE, D)])
        assert C.control_signals[0].name == 'D[drift_rate] ControlSignal'
        assert C.control_signals[0].efferents[0].receiver.name == 'drift_rate'

        # List of names
        C = pnl.ControlMechanism(control_signals=[([pnl.DRIFT_RATE, pnl.THRESHOLD], D)])
        assert C.control_signals[0].name == 'D[drift_rate, threshold] ControlSignal'
        assert C.control_signals[0].efferents[0].receiver.name == 'drift_rate'
        assert C.control_signals[0].efferents[1].receiver.name == 'threshold'
    def test_2_item_tuple_from_parameter_port_to_control_signals(self):

        C = pnl.ControlMechanism(control_signals=['a', 'b'])
        D = pnl.DDM(name='D3',
                    function=psyneulink.core.components.functions.
                    distributionfunctions.DriftDiffusionAnalytical(
                        drift_rate=(3, C),
                        threshold=(2, C.control_signals['b'])))
        assert D.parameter_ports[
            psyneulink.core.components.functions.distributionfunctions.
            DRIFT_RATE].mod_afferents[0].sender == C.control_signals[0]
        assert D.parameter_ports[
            psyneulink.core.globals.keywords.
            THRESHOLD].mod_afferents[0].sender == C.control_signals[1]
    def test_2_item_tuple_from_control_signal_to_parameter_port(self):

        D = pnl.DDM(name='D')

        # Single name
        C = pnl.ControlMechanism(
            control_signals=[(psyneulink.core.components.functions.
                              distributionfunctions.DRIFT_RATE, D)])
        assert C.control_signals[0].name == 'D[drift_rate] ControlSignal'
        assert C.control_signals[0].efferents[0].receiver.name == 'drift_rate'

        # List of names
        C = pnl.ControlMechanism(control_signals=[([
            psyneulink.core.components.functions.distributionfunctions.
            DRIFT_RATE, psyneulink.core.globals.keywords.THRESHOLD
        ], D)])
        assert C.control_signals[
            0].name == 'D[drift_rate, threshold] ControlSignal'
        assert C.control_signals[0].efferents[0].receiver.name == 'drift_rate'
        assert C.control_signals[0].efferents[1].receiver.name == 'threshold'
def test_DDM_in_composition(benchmark, mode):
    M = pnl.DDM(
        name='DDM',
        function=pnl.DriftDiffusionIntegrator(
            rate=1,
            noise=0.0,
            offset=0.0,
            starting_point=0.0,
            time_step_size=0.1,
        ),
    )
    C = pnl.Composition()
    C.add_linear_processing_pathway([M])
    inputs = {M: [10]}
    val = C.run(inputs, num_trials=2, bin_execute=mode)
    # FIXME: Python version returns dtype=object
    val = np.asfarray(val)
    assert np.allclose(val[0], [2.0])
    assert np.allclose(val[1], [0.2])
    if benchmark.enabled:
        benchmark(C.run, inputs, num_trials=2, bin_execute=mode)
Beispiel #17
0
    def test_rl(self):
        input_layer = pnl.TransferMechanism(size=2, name='Input Layer')
        input_layer.log.set_log_conditions(items=pnl.VALUE)
        action_selection = pnl.DDM(input_format=pnl.ARRAY,
                                   function=pnl.DriftDiffusionAnalytical(),
                                   output_states=[pnl.SELECTED_INPUT_ARRAY],
                                   name='DDM')
        action_selection.log.set_log_conditions(items=pnl.SELECTED_INPUT_ARRAY)

        comp = pnl.Composition(name='comp')
        learning_components = comp.add_reinforcement_learning_pathway(
            pathway=[input_layer, action_selection], learning_rate=0.05)
        learned_projection = learning_components[pnl.LEARNED_PROJECTION]
        learning_mechanism = learning_components[pnl.LEARNING_MECHANISM]
        target_mechanism = learning_components[pnl.TARGET_MECHANISM]
        comparator_mechanism = learning_components[pnl.COMPARATOR_MECHANISM]

        learned_projection.log.set_log_conditions(
            items=["matrix", "mod_matrix"])

        inputs_dict = {
            input_layer: [[1., 1.], [1., 1.]],
            target_mechanism: [[10.], [10.]]
        }
        learning_mechanism.log.set_log_conditions(items=[pnl.VALUE])
        comparator_mechanism.log.set_log_conditions(items=[pnl.VALUE])

        target_mechanism.log.set_log_conditions(items=pnl.VALUE)
        comp.run(inputs=inputs_dict)

        assert np.allclose(learning_mechanism.value,
                           [np.array([0.4275, 0.]),
                            np.array([0.4275, 0.])])
        assert np.allclose(action_selection.value, [[1.], [2.30401336], [0.97340301], [0.02659699], [2.30401336], \
                                                    [2.08614798], [1.85006765], [2.30401336], [2.08614798],
                                                    [1.85006765]])
Beispiel #18
0
Distactor_Component = pnl.TransferMechanism(name='Distactor Component')

Automatic_Component.set_log_conditions(
    'value')  #, log_condition=pnl.PROCESSING)

# Decision Mechanisms
Decision = pnl.DDM(
    function=pnl.DriftDiffusionAnalytical(
        # drift_rate=(0.1170),
        threshold=(thresh),
        noise=(c),
        starting_point=(x_0),
        t0=t0),
    name='Decision',
    output_ports=[
        pnl.DECISION_VARIABLE, pnl.RESPONSE_TIME,
        pnl.PROBABILITY_UPPER_THRESHOLD, {
            pnl.NAME: 'OFFSET RT',
            pnl.VARIABLE: (pnl.OWNER_VALUE, 2),
            pnl.FUNCTION: pnl.Linear(0, slope=1.0, intercept=1)
        }
    ],
)  #drift_rate=(1.0),threshold=(0.2645),noise=(0.5),starting_point=(0), t0=0.15

Decision.set_log_conditions('InputPort-0')  #, log_condition=pnl.PROCESSING)
Decision.set_log_conditions('PROBABILITY_UPPER_THRESHOLD')
print(Decision.loggable_items)
# Outcome Mechanisms:
Reward = pnl.TransferMechanism(name='Reward')
Beispiel #19
0
    NormalDist(mean=0, standard_deviation=unit_noise).function,
    integration_rate=processing_rate,
    name='WORDS HIDDEN')

# OUTPUT LAYER
r = pnl.TransferMechanism(
    size=2,
    function=psyneulink.core.components.functions.transferfunctions.Logistic,
    integrator_mode=False,
    noise=psyneulink.core.components.functions.distributionfunctions.
    NormalDist(mean=0, standard_deviation=unit_noise).function,
    integration_rate=processing_rate,
    name='RESPONSE')

# DECISION LAYER
d = pnl.DDM(input_format=pnl.ARRAY)
l = pnl.LCAMechanism(size=2)

#   LOGGING
ch.set_log_conditions('value')
wh.set_log_conditions('value')
r.set_log_conditions('value')

#  PROJECTIONS
c_ih = pnl.MappingProjection(matrix=[[2.2, -2.2], [-2.2, 2.2]],
                             name='COLOR INPUT TO HIDDEN')
w_ih = pnl.MappingProjection(matrix=[[2.6, -2.6], [-2.6, 2.6]],
                             name='WORD INPUT TO HIDDEN')
c_hr = pnl.MappingProjection(matrix=[[1.3, -1.3], [-1.3, 1.3]],
                             name='COLOR HIDDEN TO OUTPUT')
w_hr = pnl.MappingProjection(matrix=[[2.5, -2.5], [-2.5, 2.5]],
Beispiel #20
0
# Processing Mechanism (Automatic)
Automatic_Component = pnl.TransferMechanism(name='Automatic Component',
                                            function=pnl.Linear(slope=(1.0)),
                                            prefs=mechanism_prefs)

# Decision Mechanisms
Decision = pnl.DDM(
    function=pnl.BogaczEtAl(drift_rate=(1.0),
                            threshold=(0.2645),
                            noise=(0.5),
                            starting_point=(0),
                            t0=0.15),
    prefs=mechanism_prefs,
    name='Decision',
    output_states=[
        pnl.DECISION_VARIABLE, pnl.RESPONSE_TIME,
        pnl.PROBABILITY_UPPER_THRESHOLD, {
            pnl.NAME: 'OFFSET RT',
            pnl.VARIABLE: (pnl.OWNER_VALUE, 2),
            pnl.FUNCTION: pnl.Linear(0, slope=0.3, intercept=1)
        }
    ],
)

# Outcome Mechanisms:
Reward = pnl.TransferMechanism(name='Reward')

# Processes:
TargetControlProcess = pnl.Process(default_variable=[0],
                                   pathway=[Target_Stim, Target_Rep, Decision],
    return prob_upper * reward_upper + prob_lower * reward_lower
    # return np.sum(v[0] * v[1])


color_stim = pnl.TransferMechanism(name='Color Stimulus', size=8)
word_stim = pnl.TransferMechanism(name='Word Stimulus', size=8)

color_task = pnl.TransferMechanism(name='Color Task')
word_task = pnl.ProcessingMechanism(name='Word Task', function=w_fct_UDF)

reward = pnl.TransferMechanism(name='Reward', size=2)

task_decision = pnl.DDM(
    name='Task Decision',
    # function=pnl.NavarroAndFuss,
    output_states=[
        pnl.DDM_OUTPUT.PROBABILITY_UPPER_THRESHOLD,
        pnl.DDM_OUTPUT.PROBABILITY_LOWER_THRESHOLD
    ])

task_decision.set_log_conditions('func_drift_rate')
task_decision.set_log_conditions('mod_drift_rate')
task_decision.set_log_conditions('PROBABILITY_LOWER_THRESHOLD')
task_decision.set_log_conditions('PROBABILITY_UPPER_THRESHOLD')
color_task.set_log_conditions('value')
word_task.set_log_conditions('value')

c = pnl.Composition(name='Stroop XOR Model')
c.add_node(color_stim)
c.add_node(word_stim)
c.add_node(color_task, required_roles=pnl.NodeRole.ORIGIN)
Beispiel #22
0
def get_stroop_model(unit_noise_std=.01, dec_noise_std=.1):
    # model params
    integration_rate = 1

    hidden_func = pnl.Logistic(gain=1.0, x_0=4.0)

    # input layer, color and word
    reward = pnl.TransferMechanism(name='reward')

    punish = pnl.TransferMechanism(name='punish')

    inp_clr = pnl.TransferMechanism(
        size=N_UNITS, function=pnl.Linear, name='COLOR INPUT'
    )
    inp_wrd = pnl.TransferMechanism(
        size=N_UNITS, function=pnl.Linear, name='WORD INPUT'
    )
    # task layer, represent the task instruction; color naming / word reading
    inp_task = pnl.TransferMechanism(
        size=N_UNITS, function=pnl.Linear, name='TASK'
    )
    # hidden layer for color and word
    hid_clr = pnl.TransferMechanism(
        size=N_UNITS,
        function=hidden_func,
        integrator_mode=True,
        integration_rate=integration_rate,
        # noise=pnl.NormalDist(standard_deviation=unit_noise_std).function,
        noise=pnl.NormalDist(standard_deviation=unit_noise_std),
        name='COLORS HIDDEN'
    )
    hid_wrd = pnl.TransferMechanism(
        size=N_UNITS,
        function=hidden_func,
        integrator_mode=True,
        integration_rate=integration_rate,
        # noise=pnl.NormalDist(standard_deviation=unit_noise_std).function,
        noise=pnl.NormalDist(standard_deviation=unit_noise_std),
        name='WORDS HIDDEN'
    )
    # output layer
    output = pnl.TransferMechanism(
        size=N_UNITS,
        function=pnl.Logistic,
        integrator_mode=True,
        integration_rate=integration_rate,
        # noise=pnl.NormalDist(standard_deviation=unit_noise_std).function,
        noise=pnl.NormalDist(standard_deviation=unit_noise_std),
        name='OUTPUT'
    )
    # decision layer, some accumulator

    signalSearchRange = pnl.SampleSpec(start=0.05, stop=5, step=0.05)

    decision = pnl.DDM(name='Decision',
                       input_format=pnl.ARRAY,
                       function=pnl.DriftDiffusionAnalytical(drift_rate=1,
                                                             threshold =1,
                                                             noise=1,
                                                             starting_point=0,
                                                             t0=0.35),
                       output_ports=[pnl.RESPONSE_TIME,
                                     pnl.PROBABILITY_UPPER_THRESHOLD,
                                     pnl.PROBABILITY_LOWER_THRESHOLD]
                       )

    driftrate_control_signal = pnl.ControlSignal(projections=[(pnl.SLOPE, inp_clr)],
                                                 variable=1.0,
                                                 intensity_cost_function=pnl.Exponential(rate=1),#pnl.Exponential(rate=0.8),#pnl.Exponential(rate=1),
                                                 allocation_samples=signalSearchRange)


    threshold_control_signal = pnl.ControlSignal(projections=[(pnl.THRESHOLD, decision)],
                                                 variable=1.0,
                                                 intensity_cost_function=pnl.Linear(slope=0),
                                                 allocation_samples=signalSearchRange)


    reward_rate = pnl.ObjectiveMechanism(function=pnl.LinearCombination(operation=pnl.PRODUCT,
                                                                        exponents=[[1],[1],[-1]]),
                                         monitor=[reward,
                                                  decision.output_ports[pnl.PROBABILITY_UPPER_THRESHOLD],
                                                  decision.output_ports[pnl.RESPONSE_TIME]])

    punish_rate = pnl.ObjectiveMechanism(function=pnl.LinearCombination(operation=pnl.PRODUCT,
                                                                        exponents=[[1],[1],[-1]]),
                                         monitor=[punish,
                                                  decision.output_ports[pnl.PROBABILITY_LOWER_THRESHOLD],
                                                  decision.output_ports[pnl.RESPONSE_TIME]])

    objective_mech = pnl.ObjectiveMechanism(function=pnl.LinearCombination(operation=pnl.SUM,
                                                                           weights=[[1],[-1]]),
                                            monitor=[reward_rate, punish_rate])

    # objective_mech = pnl.ObjectiveMechanism(function=object_function,
    #                                         monitor=[reward,
    #                                                  punish,
    #                                                  decision.output_ports[pnl.PROBABILITY_UPPER_THRESHOLD],
    #                                                  decision.output_ports[pnl.PROBABILITY_LOWER_THRESHOLD],
    #                                                  (decision.output_ports[pnl.RESPONSE_TIME])])



    # PROJECTIONS, weights copied from cohen et al (1990)
    wts_clr_ih = pnl.MappingProjection(
        matrix=[[2.2, -2.2], [-2.2, 2.2]], name='COLOR INPUT TO HIDDEN')
    wts_wrd_ih = pnl.MappingProjection(
        matrix=[[2.6, -2.6], [-2.6, 2.6]], name='WORD INPUT TO HIDDEN')
    wts_clr_ho = pnl.MappingProjection(
        matrix=[[1.3, -1.3], [-1.3, 1.3]], name='COLOR HIDDEN TO OUTPUT')
    wts_wrd_ho = pnl.MappingProjection(
        matrix=[[2.5, -2.5], [-2.5, 2.5]], name='WORD HIDDEN TO OUTPUT')
    wts_tc = pnl.MappingProjection(
        matrix=[[4.0, 4.0], [0, 0]], name='COLOR NAMING')
    wts_tw = pnl.MappingProjection(
        matrix=[[0, 0], [4.0, 4.0]], name='WORD READING')


    # build the model
    model = pnl.Composition(name='STROOP model')

    model.add_node(decision, required_roles=pnl.NodeRole.OUTPUT)
    model.add_node(reward, required_roles=pnl.NodeRole.OUTPUT)
    model.add_node(punish, required_roles=pnl.NodeRole.OUTPUT)


    model.add_linear_processing_pathway([inp_clr, wts_clr_ih, hid_clr])
    model.add_linear_processing_pathway([inp_wrd, wts_wrd_ih, hid_wrd])
    model.add_linear_processing_pathway([hid_clr, wts_clr_ho, output])
    model.add_linear_processing_pathway([hid_wrd, wts_wrd_ho, output])
    model.add_linear_processing_pathway([inp_task, wts_tc, hid_clr])
    model.add_linear_processing_pathway([inp_task, wts_tw, hid_wrd])
    model.add_linear_processing_pathway([output, pnl.IDENTITY_MATRIX, decision])  # 3/15/20
    # model.add_linear_processing_pathway([output, [[1,-1]], (decision, pnl.NodeRole.OUTPUT)])   # 3/15/20
    # model.add_linear_processing_pathway([output, [[1],[-1]], decision])   # 3/15/20

    model.add_nodes([reward_rate, punish_rate])

    controller = pnl.OptimizationControlMechanism(agent_rep=model,
                                                  features=[inp_clr.input_port,
                                                            inp_wrd.input_port,
                                                            inp_task.input_port,
                                                            reward.input_port,
                                                            punish.input_port],
                                                  feature_function=pnl.AdaptiveIntegrator(rate=0.1),
                                                  objective_mechanism=objective_mech,
                                                  function=pnl.GridSearch(),
                                                  control_signals=[driftrate_control_signal,
                                                                   threshold_control_signal])

    model.add_controller(controller=controller)

    # collect the node handles
    nodes = [inp_clr, inp_wrd, inp_task, hid_clr, hid_wrd, output, decision, reward, punish,controller]
    metadata = [integration_rate, dec_noise_std, unit_noise_std]
    return model, nodes, metadata
Beispiel #23
0
flanker_stim = pnl.TransferMechanism(name='Flanker Stimulus',
                                     function=pnl.Linear(slope=0.3545221843))

# Processing Mechanisms (Control)
Target_Rep = pnl.TransferMechanism(name='Target Representation')
Flanker_Rep = pnl.TransferMechanism(name='Flanker Representation')

# Processing Mechanism (Automatic)
Automatic_Component = pnl.TransferMechanism(name='Automatic Component')

# Decision Mechanism
Decision = pnl.DDM(name='Decision',
                   function=pnl.DriftDiffusionAnalytical(drift_rate=(1.0),
                                                         threshold=(0.2645),
                                                         noise=(0.5),
                                                         starting_point=(0),
                                                         t0=0.15),
                   output_ports=[
                       pnl.DECISION_VARIABLE, pnl.RESPONSE_TIME,
                       pnl.PROBABILITY_UPPER_THRESHOLD
                   ])

# Outcome Mechanism
reward = pnl.TransferMechanism(name='reward')

# Pathways
target_control_pathway = [target_stim, Target_Rep, Decision]
flanker_control_pathway = [flanker_stim, Flanker_Rep, Decision]
target_automatic_pathway = [target_stim, Automatic_Component, Decision]
flanker_automatic_pathway = [flanker_stim, Automatic_Component, Decision]
pathways = [
    target_control_pathway, flanker_control_pathway, target_automatic_pathway,
Beispiel #24
0
    function=psyneulink.core.components.functions.transferfunctions.Linear(
        slope=(1.0)),
    prefs=mechanism_prefs)

# Decision Mechanisms
Decision = pnl.DDM(
    function=psyneulink.core.components.functions.distributionfunctions.
    DriftDiffusionAnalytical(drift_rate=(1.0),
                             threshold=(0.2645),
                             noise=(0.5),
                             starting_point=(0),
                             t0=0.15),
    prefs=mechanism_prefs,
    name='Decision',
    output_ports=[
        pnl.DECISION_VARIABLE, pnl.RESPONSE_TIME,
        pnl.PROBABILITY_UPPER_THRESHOLD, {
            pnl.NAME:
            'OFFSET RT',
            pnl.VARIABLE: (pnl.OWNER_VALUE, 2),
            pnl.FUNCTION:
            psyneulink.core.components.functions.transferfunctions.Linear(
                0, slope=0.3, intercept=1)
        }
    ],
)

# Outcome Mechanisms:
Reward = pnl.TransferMechanism(name='Reward')

# Processes:
    integrator_function=pnl.LeakyCompetingIntegrator(
        name="LeakyCompetingIntegrator_Function_0",
        initializer=[[0.5, 0.5]],
        rate=0.5,
        default_variable=[[0.0, 0.0]],
    ),
    output_ports=["RESULTS"],
    termination_comparison_op=">=",
    default_variable=[[0.0, 0.0]],
)
DECISION = pnl.DDM(
    name="DECISION",
    function=pnl.DriftDiffusionAnalytical(default_variable=[[0.0]]),
    input_ports=[{
        pnl.NAME:
        pnl.ARRAY,
        pnl.VARIABLE: [[0.0, 0.0]],
        pnl.FUNCTION:
        pnl.Reduce(default_variable=[[0.0, 0.0]], weights=[1, -1]),
    }],
)
Conflict_Monitor = pnl.ObjectiveMechanism(
    name="Conflict_Monitor",
    function=pnl.Energy(matrix=[[0, -2.5], [-2.5, 0]],
                        default_variable=[[0.0, 0.0]]),
    monitor=[OUTPUT],
    default_variable=[[0.0, 0.0]],
)

CONTROL = pnl.ControlMechanism(
    name="CONTROL",
                                  function=pnl.Linear(slope=optimal_color_control))
motion_input = pnl.ProcessingMechanism(name='Motion',
                                  function=pnl.Linear(slope=optimal_motion_control))
# decision = pnl.DDM(name='Decision',
#                function= pnl.DriftDiffusionAnalytical(
#                        starting_point=0,
#                        noise=0.5,
#                        t0=0.2,
#                        threshold=0.45),
#                output_states=[pnl.DDM_OUTPUT.PROBABILITY_UPPER_THRESHOLD, pnl.DDM_OUTPUT.RESPONSE_TIME], this seems to have been removed.
#               )
decision = pnl.DDM(
    function=pnl.DriftDiffusionAnalytical(
        starting_point=0,
        noise=0.5,
        t0=0.2,
        threshold=0.45
    ),
    name='Decision'
)

c = pnl.Composition(name='ColorMotion Task')
c.add_linear_processing_pathway([color_input, decision])
c.add_linear_processing_pathway([motion_input, decision])

# c.show_graph(show_node_structure=ALL)

stimuli = {color_input: colorInputSequence,
              motion_input: motionInputSequence}

c.run(inputs=stimuli)
Beispiel #27
0
input_layer = pnl.TransferMechanism(size=2, name='Input Layer')

# Takes sum of input layer elements as external component of drift rate
# Notes:
#  - drift_rate parameter in constructor for DDM is the "internally modulated" component of the drift_rate;
#  - arguments to DDM's function (DriftDiffusionAnalytical) are specified as CONTROL, so their values are determined
#      by the OptimizationControlMechanism of the Composition to which the action_selection Mechanism is assigned
#  - the input_format argument specifies that the input to the DDM should be one-hot encoded two-element array
#  - the output_ports argument specifies use of the DECISION_VARIABLE_ARRAY OutputPort, which encodes the
#      response in the same format as the ARRAY input_format/.
action_selection = pnl.DDM(
    input_format=pnl.ARRAY,
    function=psyneulink.core.components.functions.nonstateful.
    distributionfunctions.DriftDiffusionAnalytical(
        drift_rate=pnl.CONTROL,
        threshold=pnl.CONTROL,
        starting_point=pnl.CONTROL,
        noise=pnl.CONTROL,
    ),
    output_ports=[pnl.SELECTED_INPUT_ARRAY],
    name='DDM')

# Construct Process
# Notes:
#    The np.array specifies the matrix used as the Mapping Projection from input_layer to action_selection,
#        which insures the left element of the input favors the left action (positive value of DDM decision variable),
#        and the right element favors the right action (negative value of DDM decision variable)
#    The learning argument specifies Reinforcement as the learning function for the Projection

comp = pnl.Composition()
p = comp.add_reinforcement_learning_pathway(
Beispiel #28
0
import psyneulink.core.components.functions.nonstateful.transferfunctions

myInputLayer = pnl.TransferMechanism(
    name='Input Layer',
    function=psyneulink.core.components.functions.nonstateful.
    transferfunctions.Linear(),
    default_variable=[0, 0])

myHiddenLayer = pnl.TransferMechanism(
    name='Hidden Layer 1',
    function=psyneulink.core.components.functions.nonstateful.
    transferfunctions.Logistic(gain=1.0, x_0=0),
    default_variable=np.zeros((5, )))

myDDM = pnl.DDM(name='My_DDM',
                function=psyneulink.core.components.functions.nonstateful.
                distributionfunctions.DriftDiffusionAnalytical(
                    drift_rate=0.5, threshold=1, starting_point=0.0))

comp = pnl.Composition(name='Neural Network DDM Process',
                       pathways=[[
                           myInputLayer,
                           pnl.get_matrix(pnl.RANDOM_CONNECTIVITY_MATRIX, 2,
                                          5), myHiddenLayer,
                           pnl.FULL_CONNECTIVITY_MATRIX, myDDM
                       ]])

comp.reportOutputPref = True
myInputLayer.reportOutputPref = True
myHiddenLayer.reportOutputPref = True
myDDM.reportOutputPref = pnl.PreferenceEntry(True,
                                             pnl.PreferenceLevel.INSTANCE)
Beispiel #29
0
controlledElement.set_log_conditions([pnl.RESULT])

ddmCombination = pnl.TransferMechanism(size=1,
                                       function=pnl.Linear(slope=1,
                                                           intercept=0),
                                       output_ports=[pnl.RESULT],
                                       name="DDM Integrator")
ddmCombination.set_log_conditions([pnl.RESULT])

decisionMaker = pnl.DDM(
    function=pnl.DriftDiffusionAnalytical(drift_rate=DRIFT,
                                          starting_point=STARTING_POINT,
                                          threshold=THRESHOLD,
                                          noise=NOISE,
                                          t0=T0),
    output_ports=[
        pnl.DECISION_VARIABLE, pnl.RESPONSE_TIME,
        pnl.PROBABILITY_UPPER_THRESHOLD, pnl.PROBABILITY_LOWER_THRESHOLD
    ],
    name='DDM')

decisionMaker.set_log_conditions([
    pnl.PROBABILITY_UPPER_THRESHOLD, pnl.PROBABILITY_LOWER_THRESHOLD,
    pnl.DECISION_VARIABLE, pnl.RESPONSE_TIME
])

# decisionMaker.set_log_conditions([pnl.PROBABILITY_UPPER_THRESHOLD, pnl.PROBABILITY_LOWER_THRESHOLD])

########### Composition
Beispiel #30
0
# Processing Mechanism (Automatic)
Automatic_Component = pnl.TransferMechanism(name='Automatic Component',
                                            function=pnl.Linear)
Automatic_Component.loggable_items
Automatic_Component.set_log_conditions('value')

# Decision Mechanisms
Decision = pnl.DDM(
    function=pnl.BogaczEtAl(
        drift_rate=(0.5),
        threshold=(1.0),
        # noise=(0.8),
        starting_point=(0),
        t0=0.15),
    name='Decision',
    output_states=[
        pnl.DECISION_VARIABLE, pnl.RESPONSE_TIME,
        pnl.PROBABILITY_UPPER_THRESHOLD, {
            pnl.NAME: 'OFFSET RT',
            pnl.INDEX: 2,
            pnl.ASSIGN: pnl.Linear(0, slope=1.0, intercept=1).function
        }
    ],
)  #drift_rate=(1.0),threshold=(0.2645),noise=(0.5),starting_point=(0), t0=0.15
Decision.set_log_conditions('DECISION_VARIABLE')
Decision.set_log_conditions('value')
Decision.set_log_conditions('PROBABILITY_UPPER_THRESHOLD')
Decision.set_log_conditions('InputState-0')

Decision.loggable_items