コード例 #1
0
    def test_reset_state_transfer_mechanism(self):
        A = TransferMechanism(name='A', integrator_mode=True)

        # Execute A twice
        original_output = [A.execute(1.0), A.execute(1.0)]

        # SAVING STATE  - - - - - - - - - - - - - - - - - - - - - - - - -
        reinitialize_values = []

        for attr in A.integrator_function.stateful_attributes:
            reinitialize_values.append(getattr(A.integrator_function, attr))

        # Execute A twice AFTER saving the state so that it continues accumulating.
        # We expect the next two outputs to repeat once we reset the state b/c we will return it to the current state
        output_after_saving_state = [A.execute(1.0), A.execute(1.0)]

        # RESETTING STATE - - - - - - - - - - - - - - - - - - - - - - - -
        A.reinitialize(*reinitialize_values)

        # We expect these results to match the results from immediately after saving the state
        output_after_reinitialization = [A.execute(1.0), A.execute(1.0)]

        assert np.allclose(output_after_saving_state,
                           output_after_reinitialization)
        assert np.allclose(
            original_output,
            [np.array([[0.5]]), np.array([[0.75]])])
        assert np.allclose(
            output_after_reinitialization,
            [np.array([[0.875]]), np.array([[0.9375]])])
コード例 #2
0
    def test_reinitialize_not_integrator(self):

        with pytest.raises(MechanismError) as err_txt:
            T_not_integrator = TransferMechanism()
            T_not_integrator.execute(1.0)
            T_not_integrator.reinitialize(0.0)
        assert "not allowed because this Mechanism is not stateful." in str(err_txt) \
               and "try setting the integrator_mode argument to True." in str(err_txt)
コード例 #3
0
    def test_reinitialize_run_2darray(self):

        initial_val = [[0.5, 0.5, 0.5]]
        T = TransferMechanism(name="T",
                              default_variable=[[0.0, 0.0, 0.0]],
                              initial_value=initial_val,
                              integrator_mode=True,
                              integration_rate=0.1,
                              noise=0.0)
        P = Process(name="P",
                    pathway=[T])
        S = System(name="S",
                   processes=[P])
        T.reinitialize_when = Never()

        assert np.allclose(T.integrator_function.previous_value, initial_val)

        S.run(inputs={T: [1.0, 1.0, 1.0]}, num_trials=2)
        # Trial 1
        # integration: 0.9*0.5 + 0.1*1.0 + 0.0 = 0.55  --->  previous value = 0.55
        # linear fn: 0.55*1.0 = 0.55
        # Trial 2
        # integration: 0.9*0.55 + 0.1*1.0 + 0.0 = 0.595  --->  previous value = 0.595
        # linear fn: 0.595*1.0 = 0.595
        assert np.allclose(T.integrator_function.previous_value, [0.595, 0.595, 0.595])

        T.integrator_function.reinitialize([0.9, 0.9, 0.9])

        assert np.allclose(T.integrator_function.previous_value, [0.9, 0.9, 0.9])
        assert np.allclose(T.value, [0.595, 0.595, 0.595])

        T.reinitialize(initial_val)

        assert np.allclose(T.integrator_function.previous_value, initial_val)
        assert np.allclose(T.value, initial_val)

        S.run(inputs={T: [1.0, 1.0, 1.0]}, num_trials=2)
        # Trial 3
        # integration: 0.9*0.5 + 0.1*1.0 + 0.0 = 0.55  --->  previous value = 0.55
        # linear fn: 0.55*1.0 = 0.55
        # Trial 4
        # integration: 0.9*0.55 + 0.1*1.0 + 0.0 = 0.595  --->  previous value = 0.595
        # linear fn: 0.595*1.0 = 0.595
        assert np.allclose(T.integrator_function.previous_value, [0.595, 0.595, 0.595])
コード例 #4
0
    def test_reinitialize_run(self):
        T = TransferMechanism(name="T",
                              initial_value=0.5,
                              integrator_mode=True,
                              integration_rate=0.1,
                              noise=0.0)
        P = Process(name="P",
                    pathway=[T])
        S = System(name="S",
                   processes=[P])

        T.reinitialize_when = Never()

        assert np.allclose(T.integrator_function.previous_value, 0.5)

        S.run(inputs={T: 1.0}, num_trials=2)
        # Trial 1
        # integration: 0.9*0.5 + 0.1*1.0 + 0.0 = 0.55  --->  previous value = 0.55
        # linear fn: 0.55*1.0 = 0.55
        # Trial 2
        # integration: 0.9*0.55 + 0.1*1.0 + 0.0 = 0.595  --->  previous value = 0.595
        # linear fn: 0.595*1.0 = 0.595
        assert np.allclose(T.integrator_function.previous_value, 0.595)

        T.integrator_function.reinitialize(0.9)

        assert np.allclose(T.integrator_function.previous_value, 0.9)
        assert np.allclose(T.value, 0.595)

        T.reinitialize(0.5)

        assert np.allclose(T.integrator_function.previous_value, 0.5)
        assert np.allclose(T.value, 0.5)

        S.run(inputs={T: 1.0}, num_trials=2)
        # Trial 3
        # integration: 0.9*0.5 + 0.1*1.0 + 0.0 = 0.55  --->  previous value = 0.55
        # linear fn: 0.55*1.0 = 0.55
        # Trial 4
        # integration: 0.9*0.55 + 0.1*1.0 + 0.0 = 0.595  --->  previous value = 0.595
        # linear fn: 0.595*1.0 = 0.595
        assert np.allclose(T.integrator_function.previous_value, 0.595)
コード例 #5
0
    def test_previous_value_reinitialize_execute(self):
        T = TransferMechanism(name="T",
                              initial_value=0.5,
                              integrator_mode=True,
                              integration_rate=0.1,
                              noise=0.0)
        T.reinitialize_when = Never()
        assert np.allclose(T.integrator_function.previous_value, 0.5)
        T.execute(1.0)
        # integration: 0.9*0.5 + 0.1*1.0 + 0.0 = 0.55  --->  previous value = 0.55
        # linear fn: 0.55*1.0 = 0.55
        assert np.allclose(T.integrator_function.previous_value, 0.55)
        assert np.allclose(T.value, 0.55)

        # Reset integrator_function ONLY
        T.integrator_function.reinitialize(0.6)

        assert np.allclose(T.integrator_function.previous_value, 0.6)   # previous_value is a property that looks at integrator_function
        assert np.allclose(T.value, 0.55)           # on mechanism only, so does not update until execution

        T.execute(1.0)
        # integration: 0.9*0.6 + 0.1*1.0 + 0.0 = 0.64  --->  previous value = 0.55
        # linear fn: 0.64*1.0 = 0.64
        assert np.allclose(T.integrator_function.previous_value, 0.64)   # property that looks at integrator_function
        assert np.allclose(T.value, 0.64)            # on mechanism, but updates with execution

        T.reinitialize(0.4)
        # linear fn: 0.4*1.0 = 0.4
        assert np.allclose(T.integrator_function.previous_value, 0.4)   # property that looks at integrator, which updated with mech reset
        assert np.allclose(T.value, 0.4)  # on mechanism, but updates with mech reset

        T.execute(1.0)
        # integration: 0.9*0.4 + 0.1*1.0 + 0.0 = 0.46  --->  previous value = 0.46
        # linear fn: 0.46*1.0 = 0.46
        assert np.allclose(T.integrator_function.previous_value, 0.46)  # property that looks at integrator, which updated with mech exec
        assert np.allclose(T.value, 0.46)  # on mechanism, but updates with exec