def test_scheduled_contrastive_hebbian(self): o = pnl.TransferMechanism() m = pnl.ContrastiveHebbianMechanism( input_size=2, hidden_size=0, target_size=2, separated=False, mode=pnl.SIMPLE_HEBBIAN, integrator_mode=True, enable_learning=False, matrix=[[0, -1], [-1, 0]], # auto=0, # hetero=-1, ) # set max passes to ensure failure if no convergence instead of infinite loop m.max_passes = 1000 s = pnl.sys(m, o) ms = pnl.Scheduler(system=s) ms.add_condition(o, pnl.WhenFinished(m)) s.scheduler_processing = ms # m.reinitialize_when=pnl.Never() print('matrix:\n', m.afferents[1].matrix) results = s.run(inputs=[2, 2], num_trials=4) print(results) np.testing.assert_allclose(results, [[np.array([2.])], [np.array([2.])], [np.array([2.])], [np.array([2.])]])
def test_configure_learning(self): o = pnl.TransferMechanism() m = pnl.ContrastiveHebbianMechanism(input_size=2, hidden_size=0, target_size=2, mode=pnl.SIMPLE_HEBBIAN, separated=False, matrix=[[0, -.5], [-.5, 0]]) with pytest.warns(UserWarning) as record: m.learning_enabled = True correct_message_found = False for warning in record: if ("Learning cannot be enabled" in str(warning.message) and "because it has no LearningMechanism" in str( warning.message)): correct_message_found = True break assert correct_message_found m.configure_learning() m.reinitialize_when = pnl.Never() s = pnl.sys(m, o) ms = pnl.Scheduler(system=s) ms.add_condition(o, pnl.WhenFinished(m)) s.scheduler_processing = ms results = s.run(inputs=[2, 2], num_trials=4) np.testing.assert_allclose( results, [[[2.671875]], [[2.84093837]], [[3.0510183]], [[3.35234623]]])
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