def test_reinitialize_one_mechanism_at_trial_2_condition(self): A = TransferMechanism(name='A') B = TransferMechanism(name='B', integrator_mode=True, integration_rate=0.5) C = TransferMechanism(name='C') abc_process = Process(pathway=[A, B, C]) abc_system = System(processes=[abc_process]) # Set reinitialization condition B.reinitialize_when = AtTrial(2) C.log.set_log_conditions('value') abc_system.run(inputs={A: [1.0]}, reinitialize_values={B: [0.]}, num_trials=5) # Trial 0: 0.5, Trial 1: 0.75, Trial 2: 0.5, Trial 3: 0.75. Trial 4: 0.875 assert np.allclose( C.log.nparray_dictionary('value')[ abc_system.default_execution_id]['value'], [[np.array([0.5])], [np.array([0.75])], [np.array([0.5])], [np.array([0.75])], [np.array([0.875])]])
def test_stateful_mechanism_in_simulation(): # Mechanisms # integrator_mode = True on the Input mechanism makes the system stateful # (though not necessarily an interesting/meaningful model) Input = TransferMechanism( name='Input', integrator_mode=True, ) Reward = TransferMechanism( output_states=[RESULT, OUTPUT_MEAN, OUTPUT_VARIANCE], name='Reward') Decision = DDM( function=DriftDiffusionAnalytical(drift_rate=( 1.0, ControlProjection( function=Linear, control_signal_params={ ALLOCATION_SAMPLES: np.arange(0.1, 1.01, 0.3) }, ), ), threshold=( 1.0, ControlProjection( function=Linear, control_signal_params={ ALLOCATION_SAMPLES: np.arange( 0.1, 1.01, 0.3) }, ), ), noise=(0.5), starting_point=(0), t0=0.45), output_states=[ DECISION_VARIABLE, RESPONSE_TIME, PROBABILITY_UPPER_THRESHOLD ], name='Decision', ) # Processes: TaskExecutionProcess = Process( # default_variable=[0], size=1, pathway=[(Input), IDENTITY_MATRIX, (Decision)], name='TaskExecutionProcess', ) RewardProcess = Process( # default_variable=[0], size=1, pathway=[(Reward)], name='RewardProcess', ) # System: mySystem = System( processes=[TaskExecutionProcess, RewardProcess], controller=EVCControlMechanism, enable_controller=True, monitor_for_control=[ Reward, Decision.PROBABILITY_UPPER_THRESHOLD, (Decision.RESPONSE_TIME, -1, 1) ], name='EVC Test System', ) mySystem.recordSimulationPref = True Input.reinitialize_when = Never() # Stimuli stim_list_dict = {Input: [0.5, 0.123], Reward: [20, 20]} mySystem.run(inputs=stim_list_dict, ) # rearranging mySystem.results into a format that we can compare with pytest results_array = [] for elem in mySystem.results: elem_array = [] for inner_elem in elem: elem_array.append(float(inner_elem)) results_array.append(elem_array) expected_results_array = [[ 20.0, 20.0, 0.0, 1.0, 3.4963766238230596, 0.8807970779778824 ], [20.0, 20.0, 0.0, 0.1, 0.4899992579951842, 0.503729930808051]] # rearranging mySystem.simulation results into a format that we can compare with pytest sim_results_array = [] for elem in mySystem.simulation_results: elem_array = [] for inner_elem in elem: elem_array.append(float(inner_elem)) sim_results_array.append(elem_array) # # mySystem.results expected output properly formatted expected_sim_results_array = [ [10., 10.0, 0.0, -0.1, 0.48999867, 0.50499983], [10., 10.0, 0.0, -0.4, 1.08965888, 0.51998934], [10., 10.0, 0.0, 0.7, 2.40680493, 0.53494295], [10., 10.0, 0.0, -1., 4.43671978, 0.549834], [10., 10.0, 0.0, 0.1, 0.48997868, 0.51998934], [10., 10.0, 0.0, -0.4, 1.08459402, 0.57932425], [10., 10.0, 0.0, 0.7, 2.36033556, 0.63645254], [10., 10.0, 0.0, 1., 4.24948962, 0.68997448], [10., 10.0, 0.0, 0.1, 0.48993479, 0.53494295], [10., 10.0, 0.0, 0.4, 1.07378304, 0.63645254], [10., 10.0, 0.0, 0.7, 2.26686573, 0.72710822], [10., 10.0, 0.0, 1., 3.90353015, 0.80218389], [10., 10.0, 0.0, 0.1, 0.4898672, 0.549834], [10., 10.0, 0.0, -0.4, 1.05791834, 0.68997448], [10., 10.0, 0.0, 0.7, 2.14222978, 0.80218389], [10., 10.0, 0.0, 1., 3.49637662, 0.88079708], [15., 15.0, 0.0, 0.1, 0.48999926, 0.50372993], [15., 15.0, 0.0, -0.4, 1.08981011, 0.51491557], [15., 15.0, 0.0, 0.7, 2.40822035, 0.52608629], [15., 15.0, 0.0, 1., 4.44259627, 0.53723096], [15., 15.0, 0.0, 0.1, 0.48998813, 0.51491557], [15., 15.0, 0.0, 0.4, 1.0869779, 0.55939819], [15., 15.0, 0.0, -0.7, 2.38198336, 0.60294711], [15., 15.0, 0.0, 1., 4.33535807, 0.64492386], [15., 15.0, 0.0, 0.1, 0.48996368, 0.52608629], [15., 15.0, 0.0, 0.4, 1.08085171, 0.60294711], [15., 15.0, 0.0, 0.7, 2.32712843, 0.67504223], [15., 15.0, 0.0, 1., 4.1221271, 0.7396981], [15., 15.0, 0.0, 0.1, 0.48992596, 0.53723096], [15., 15.0, 0.0, -0.4, 1.07165729, 0.64492386], [15., 15.0, 0.0, 0.7, 2.24934228, 0.7396981], [15., 15.0, 0.0, 1., 3.84279648, 0.81637827] ] expected_output = [ # System Results Array # (all intermediate output.parameters.value.get(mySystem)s of system) (results_array, expected_results_array), # System Simulation Results Array # (all simulation output.parameters.value.get(mySystem)s of system) (sim_results_array, expected_sim_results_array) ] for i in range(len(expected_output)): val, expected = expected_output[i] np.testing.assert_allclose( val, expected, atol=1e-08, err_msg='Failed on expected_output[{0}]'.format(i))