예제 #1
0
def test_control_mechanism_assignment():
    """ControlMechanism assignment/replacement,  monitor_for_control, and control_signal specifications"""

    T1 = pnl.TransferMechanism(size=3, name='T-1')
    T2 = pnl.TransferMechanism(function=psyneulink.core.components.functions.
                               transferfunctions.Logistic,
                               output_ports=[{
                                   pnl.NAME: 'O-1'
                               }],
                               name='T-2')
    T3 = pnl.TransferMechanism(function=psyneulink.core.components.functions.
                               transferfunctions.Logistic,
                               name='T-3')
    T4 = pnl.TransferMechanism(function=psyneulink.core.components.functions.
                               transferfunctions.Logistic,
                               name='T-4')
    P = pnl.Process(pathway=[T1, T2, T3, T4])
    S = pnl.System(
        processes=P,
        # controller=pnl.EVCControlMechanism,
        controller=pnl.EVCControlMechanism(control_signals=[(pnl.GAIN, T2)],
                                           monitor_for_control=T4),
        enable_controller=True,
        # Test for use of 4-item tuple with matrix in monitor_for_control specification
        monitor_for_control=[(T1, None, None, np.ones((3, 1))),
                             ('O-1', 1, -1)],
        control_signals=[(pnl.GAIN, T3)])
    assert len(S.controller.objective_mechanism.monitor) == 3
    assert len(S.control_signals) == 2

    # Test for avoiding duplicate assignment of monitor and control_signals
    C1 = pnl.EVCControlMechanism(name='C-1',
                                 objective_mechanism=[(T1, None, None,
                                                       np.ones((3, 1)))],
                                 control_signals=[(pnl.GAIN, T3)])

    # Test direct assignment
    S.controller = C1
    assert len(C1.monitored_output_ports) == 2
    assert len(S.control_signals) == 3
    assert S.controller.name == 'C-1'

    # Test for adding a monitored_output_port and control_signal
    C2 = pnl.EVCControlMechanism(
        name='C-2',
        objective_mechanism=[T3.output_ports[pnl.RESULT]],
        control_signals=[(pnl.GAIN, T4)])
    # Test use of assign_as_controller method
    C2.assign_as_controller(S)
    assert len(C2.monitored_output_ports) == 3
    assert len(S.control_signals) == 4
    assert S.controller.name == 'C-2'
def test_prediction_mechanism_assignment():
    '''Tests prediction mechanism assignment and more tests for ObjectiveMechanism and ControlSignal assignments'''

    T1 = pnl.TransferMechanism(name='T1')
    T2 = pnl.TransferMechanism(name='T2')
    T3 = pnl.TransferMechanism(name='T3')

    S = pnl.sys([T1, T2, T3],
                controller=pnl.EVCControlMechanism(name='EVC',
                                                   prediction_mechanisms=(pnl.PredictionMechanism,
                                                                          {pnl.FUNCTION: pnl.INPUT_SEQUENCE,
                                                                           pnl.RATE: 1,
                                                                           pnl.WINDOW_SIZE: 3,
                                                                           }),
                                                   monitor_for_control=[T1],
                                                   objective_mechanism=[T2]
                                                   ),
                control_signals=pnl.ControlSignal(allocation_samples=[1, 5, 10],
                                                  projections=(pnl.SLOPE, T1)),
                monitor_for_control=T3,
                enable_controller=True
                )
    assert len(S.controller.objective_mechanism.input_states)==3

    S.recordSimulationPref = True

    input_dict = {T1:[1,2,3,4]}
    results = S.run(inputs=input_dict)
    assert results == [[[1.]], [[2.]], [[15.]], [[20.]]]
    assert S.simulation_results ==  [[[1.]], [[5.]], [[10.]],
                                    [[1.]], [[2.]], [[5.]], [[10.]], [[10.]], [[20.]],
                                    [[1.]], [[2.]], [[3.]], [[5.]], [[10.]], [[15.]], [[10.]], [[20.]], [[30.]],
                                    [[2.]], [[3.]], [[4.]], [[10.]], [[15.]], [[20.]], [[20.]], [[30.]], [[40.]]]
def test_prediction_mechanism_filter_function():
    '''Tests prediction mechanism assignment and more tests for ObjectiveMechanism and ControlSignal assignments'''

    f = lambda x: [x[0]*7]
    T = pnl.TransferMechanism(name='T')

    S = pnl.sys(T,
                controller=pnl.EVCControlMechanism(name='EVC',
                                                   prediction_mechanisms=(pnl.PredictionMechanism,
                                                                          {pnl.FUNCTION: pnl.INPUT_SEQUENCE,
                                                                           pnl.RATE: 1,
                                                                           pnl.WINDOW_SIZE: 3,
                                                                           pnl.FILTER_FUNCTION: f
                                                                           }),
                                                   objective_mechanism=[T]
                                                   ),
                control_signals=pnl.ControlSignal(allocation_samples=[1, 5, 10],
                                                  projections=(pnl.SLOPE, T)),
                enable_controller=True
                )

    S.recordSimulationPref = True
    input_dict = {T: [1, 2, 3, 4]}
    results = S.run(inputs=input_dict)
    expected_results = [[[1.0]], [[2.0]], [[3.0]], [[4.0]]]
    expected_sim_results = [[[1.]], [[5.]], [[10.]],    # before EVC | [1]
                            [[7.]], [[35.]], [[70.]],   # [1, 2]
                            [[7.]], [[35.]], [[70.]],   # [1, 2, 3]
                            [[14.]], [[70.]], [[140.]]] # [2, 3, 4]

    np.testing.assert_allclose(results, expected_results, atol=1e-08, err_msg='Failed on results')
    np.testing.assert_allclose(S.simulation_results, expected_sim_results, atol=1e-08, err_msg='Failed on results')
def test_control_mechanism_assignment_additional():
    '''Tests "free-standing" specifications of monitor_for_control and ControlSignal (i.e., outside of a list)'''
    T_1 = pnl.TransferMechanism(name='T_1')
    T_2 = pnl.TransferMechanism(name='T_2')
    S = pnl.sys([T_1,T_2],
                controller=pnl.EVCControlMechanism(control_signals=(pnl.SLOPE, T_1)),
                monitor_for_control=T_1,
                control_signals=(pnl.SLOPE, T_2),
                enable_controller=True)
    assert S.controller.objective_mechanism.input_state.path_afferents[0].sender.owner == T_1
    assert T_1.parameter_states[pnl.SLOPE].mod_afferents[0].sender.owner == S.controller
    assert T_2.parameter_states[pnl.SLOPE].mod_afferents[0].sender.owner == S.controller
예제 #5
0
    pathway=[Flanker_Stim, Automatic_Component, Decision],
    prefs=process_prefs,
    name='Flanker1 Automatic Process')

RewardProcess = pnl.Process(default_variable=[0],
                            pathway=[Reward],
                            prefs=process_prefs,
                            name='RewardProcess')

# System:
mySystem = pnl.System(
    processes=[
        TargetControlProcess, FlankerControlProcess, TargetAutomaticProcess,
        FlankerAutomaticProcess, RewardProcess
    ],
    controller=pnl.EVCControlMechanism(name='Task Controller', ),
    enable_controller=True,
    monitor_for_control=[
        Reward,
        Decision.PROBABILITY_UPPER_THRESHOLD,
        ('OFFSET RT', 1, -1),
    ],
    # monitor_for_control=[Reward, DDM_PROBABILITY_UPPER_THRESHOLD, (DDM_RESPONSE_TIME, -1, 1)],
    name='EVC Gratton System')

# Show characteristics of system:
mySystem.show()
mySystem.controller.show()

# Show graph of system (with control components)
# mySystem.show_graph()
    pathway=[Flanker_Stim, Automatic_Component, Decision],
    name='Flanker1 Automatic Process')

RewardProcess = pnl.Process(default_variable=[0],
                            pathway=[Reward, test_mech],
                            name='RewardProcess')

# System:
mySystem = pnl.System(
    processes=[
        TargetControlProcess, FlankerControlProcess, TargetAutomaticProcess,
        FlankerAutomaticProcess, RewardProcess
    ],
    controller=pnl.EVCControlMechanism(
        prefs={
            pnl.LOG_PREF:
            pnl.PreferenceEntry(pnl.LogCondition.INITIALIZATION,
                                pnl.PreferenceLevel.INSTANCE)
        }),
    enable_controller=True,
    monitor_for_control=[
        # (None, None, np.ones((1,1))),
        Reward,
        Decision.PROBABILITY_UPPER_THRESHOLD,
        ('OFFSET_RT', 1, -1),
    ],
    name='EVC Markus System')

# Show characteristics of system:
mySystem.show()
# mySystem.controller.show()
예제 #7
0
RewardProcess = pnl.Process(
    pathway=[Reward],
    name='RewardProcess'
)

# System:
mySystem = pnl.System(processes=[TargetControlProcess,
                                 FlankerControlProcess,
                                 TargetAutomaticProcess,
                                 FlankerAutomaticProcess,
                                 RewardProcess],
                      controller=pnl.EVCControlMechanism(
                              control_signals=pnl.ControlSignal(projections=[(pnl.SLOPE, Target_Rep),
                                                                              (pnl.SLOPE, Distractor_Rep)
                                                                              ],
                                                                function=psyneulink.core.components.functions.transferfunctions.Logistic,
                                                                cost_options=[pnl.ControlSignalCosts.INTENSITY,
                                                                               pnl.ControlSignalCosts.ADJUSTMENT],
                                                                allocation_samples=signalSearchRange
                                                                )),
                      enable_controller=True,
                      monitor_for_control=[
                          # (None, None, np.ones((2,1))), # what the **** is this for? Markus October 25 2018
                          Reward,
                          Decision.PROBABILITY_UPPER_THRESHOLD,
                          ('OFFSET RT', 1, -1),
                      ],
                      name='EVC Markus System')

# log controller
예제 #8
0
import psyneulink as pnl
import psyneulink.core.components.functions.transferfunctions

ci = pnl.TransferMechanism(size=2, name='COLORS INPUT')
wi = pnl.TransferMechanism(size=2, name='WORDS INPUT')
ch = pnl.TransferMechanism(
    size=2,
    function=psyneulink.core.components.functions.transferfunctions.Logistic,
    name='COLORS HIDDEN')
wh = pnl.TransferMechanism(
    size=2,
    function=psyneulink.core.components.functions.transferfunctions.Logistic,
    name='WORDS HIDDEN')
tl = pnl.TransferMechanism(
    size=2,
    function=psyneulink.core.components.functions.transferfunctions.Logistic(
        gain=pnl.CONTROL),
    name='TASK CONTROL')
rl = pnl.LCAMechanism(
    size=2,
    function=psyneulink.core.components.functions.transferfunctions.Logistic,
    name='RESPONSE')
cp = pnl.Process(pathway=[ci, ch, rl])
wp = pnl.Process(pathway=[wi, wh, rl])
tc = pnl.Process(pathway=[tl, ch])
tw = pnl.Process(pathway=[tl, wh])
s = pnl.System(processes=[tc, tw, cp, wp],
               controller=pnl.EVCControlMechanism(name='EVC Mechanimsm'),
               monitor_for_control=[rl])
s.show_graph()
예제 #9
0
# 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
p = pnl.Process(
    default_variable=[0, 0],
    # pathway=[input_layer, np.array([[1],[-1]]), action_selection],
    pathway=[input_layer, pnl.IDENTITY_MATRIX, action_selection],
    learning=pnl.LearningProjection(learning_function=pnl.Reinforcement(
        learning_rate=0.5)),
    target=0)

s = pnl.System(processes=[p],
               controller=pnl.EVCControlMechanism(
                   control_signals=(pnl.LEARNING_RATE,
                                    p.learning_mechanisms[1])))

# EXECUTION:

# Prints initial weight matrix for the Projection from the input_layer to the action_selection Mechanism
print('reward prediction weights: \n',
      action_selection.input_state.path_afferents[0].matrix)


# Used by *call_before_trial* and *call_after_trial* to generate printouts.
# Note:  should be replaced by use of logging functionality that has now been implemented.
def print_header(system):
    print("\n\n**** Time: ", system.scheduler_processing.clock.simple_time)