def test_AGTUtility_valid(self):
        I = IntegratorMechanism(name="I",
                function=AGTUtilityIntegrator())
        I.reinitialize_when = Never()
        assert np.allclose([[0.0]], I.function_object.previous_short_term_utility)
        assert np.allclose([[0.0]], I.function_object.previous_long_term_utility)

        I.function_object.reinitialize(0.2, 0.8)

        assert np.allclose([[0.2]], I.function_object.previous_short_term_utility)
        assert np.allclose([[0.8]], I.function_object.previous_long_term_utility)

        I.function_object.reinitialize()

        assert np.allclose([[0.0]], I.function_object.previous_short_term_utility)
        assert np.allclose([[0.0]], I.function_object.previous_long_term_utility)

        I.reinitialize(0.3, 0.7)

        assert np.allclose([[0.3]], I.function_object.previous_short_term_utility)
        assert np.allclose([[0.7]], I.function_object.previous_long_term_utility)
        print(I.value)
        print(I.function_object.combine_utilities(0.3, 0.7))
        assert np.allclose(I.function_object.combine_utilities(0.3, 0.7), I.value)

        I.reinitialize()

        assert np.allclose([[0.0]], I.function_object.previous_short_term_utility)
        assert np.allclose([[0.0]], I.function_object.previous_long_term_utility)
        assert np.allclose(I.function_object.combine_utilities(0.0, 0.0), I.value)
Exemple #2
0
    def test_reset_state_integrator_mechanism(self):
        A = IntegratorMechanism(name='A', function=DriftDiffusionIntegrator())

        # Execute A twice
        #  [0] saves decision variable only (not time)
        original_output = [A.execute(1.0)[0], A.execute(1.0)[0]]

        # SAVING STATE  - - - - - - - - - - - - - - - - - - - - - - - - -
        reinitialize_values = []
        for attr in A.function_object.stateful_attributes:
            reinitialize_values.append(getattr(A.function_object, 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)[0], A.execute(1.0)[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)[0], A.execute(1.0)[0]]

        assert np.allclose(output_after_saving_state,
                           output_after_reinitialization)
        assert np.allclose(
            original_output,
            [np.array([[1.0]]), np.array([[2.0]])])
        assert np.allclose(
            output_after_reinitialization,
            [np.array([[3.0]]), np.array([[4.0]])])
    def test_reinitialize_not_integrator(self):

        with pytest.raises(MechanismError) as err_txt:
            I_not_integrator = IntegratorMechanism(function=Linear)
            I_not_integrator.execute(1.0)
            I_not_integrator.reinitialize(0.0)
        assert "not allowed because this Mechanism is not stateful." in str(err_txt) \
               and "(It does not have an accumulator to reinitialize)" in str(err_txt)
    def test_Constant_valid(self):
        I = IntegratorMechanism(
            name='IntegratorMechanism',
            function=ConstantIntegrator(
                rate=1.0
            ),
        )

        #  returns previous_value + rate + noise
        # so in this case, returns 0.0 + 1.0
        I.execute(1000)
        assert np.allclose(I.value, 1.0)
        assert np.allclose(I.output_state.value, 1.0)

        # reinitialize function
        I.function_object.reinitialize(2.0)
        assert np.allclose(I.function_object.value, 2.0)
        assert np.allclose(I.value, 1.0)
        assert np.allclose(I.output_states[0].value, 1.0)

        # reinitialize function without value spec
        I.function_object.reinitialize()
        assert np.allclose(I.function_object.value, 0.0)
        assert np.allclose(I.value, 1.0)
        assert np.allclose(I.output_states[0].value, 1.0)

        # reinitialize mechanism
        I.reinitialize(2.0)
        assert np.allclose(I.function_object.value, 2.0)
        assert np.allclose(I.value, 2.0)
        assert np.allclose(I.output_states[0].value, 2.0)

        I.execute(1.0)
        #  2.0 + 1.0 = 3.0
        assert np.allclose(I.value, 3.0)
        assert np.allclose(I.output_states[0].value, 3.0)

        # reinitialize mechanism without value spec
        I.reinitialize()
        assert np.allclose(I.function_object.value, 0.0)
        assert np.allclose(I.value, 0.0)
        assert np.allclose(I.output_states[0].value, 0.0)
    def test_Adaptive_valid(self):
        I = IntegratorMechanism(
            name='IntegratorMechanism',
            function=AdaptiveIntegrator(
                rate=0.5
            ),
        )

        #  returns (1-rate)*previous_value + rate*variable + noise
        # so in this case, returns 0.5*0 + 0.5*10 + 0 = 5.0
        I.execute(10)
        assert np.allclose(I.value, 5.0)
        assert np.allclose(I.output_state.value, 5.0)

        # reinitialize function
        I.function_object.reinitialize(1.0)
        assert np.allclose(I.function_object.value, 1.0)
        assert np.allclose(I.value, 5.0)
        assert np.allclose(I.output_states[0].value, 5.0)

        # reinitialize function without value spec
        I.function_object.reinitialize()
        assert np.allclose(I.function_object.value, 0.0)
        assert np.allclose(I.value, 5.0)
        assert np.allclose(I.output_states[0].value, 5.0)

        # reinitialize mechanism
        I.reinitialize(2.0)
        assert np.allclose(I.function_object.value, 2.0)
        assert np.allclose(I.value, 2.0)
        assert np.allclose(I.output_states[0].value, 2.0)

        I.execute(1.0)
        #  (1-0.5)*2.0 + 0.5*1.0 + 0 = 1.5
        assert np.allclose(I.value, 1.5)
        assert np.allclose(I.output_states[0].value, 1.5)

        # reinitialize mechanism without value spec
        I.reinitialize()
        assert np.allclose(I.function_object.value, 0.0)
        assert np.allclose(I.value, 0.0)
        assert np.allclose(I.output_states[0].value, 0.0)
    def test_LCA_valid(self):
        I = IntegratorMechanism(
            name='IntegratorMechanism',
            function=LCAIntegrator(),
        )

        # previous_value + (rate*previous_value + new_value)*time_step_size + noise
        # initializer=0.0, rate=1.0, time_step_size=0.1, noise=0.0
        # returns 0.0 + (1.0*0.0 + 2.0)*0.1 = 2.0
        I.execute(2.0)
        assert np.allclose(I.value, 0.2)
        assert np.allclose(I.output_state.value, 0.2)

        # reinitialize function
        I.function_object.reinitialize(5.0)
        assert np.allclose(I.function_object.value, 5.0)
        assert np.allclose(I.value, 0.2)
        assert np.allclose(I.output_states[0].value, 0.2)

        # reinitialize function without value spec
        I.function_object.reinitialize()
        assert np.allclose(I.function_object.value, 0.0)
        assert np.allclose(I.value, 0.2)
        assert np.allclose(I.output_states[0].value, 0.2)

        # reinitialize mechanism
        I.reinitialize(4.0)
        assert np.allclose(I.function_object.value, 4.0)
        assert np.allclose(I.value, 4.0)
        assert np.allclose(I.output_states[0].value, 4.0)

        I.execute(1.0)
        # 4.0 + (1.0*4.0 + 1.0)*0.1 + 0.0
        assert np.allclose(I.value, 4.5)
        assert np.allclose(I.output_states[0].value, 4.5)

        # reinitialize mechanism without value spec
        I.reinitialize()
        assert np.allclose(I.function_object.value, 0.0)
        assert np.allclose(I.value, 0.0)
        assert np.allclose(I.output_states[0].value, 0.0)
    def test_Accumulator_valid(self):
        I = IntegratorMechanism(
            name='IntegratorMechanism',
            function=AccumulatorIntegrator(increment=0.1),
        )

        #  returns previous_value * rate + noise + increment
        # initializer = 0.0, rate = 1.0, noise = 0.0, increment = 0.1
        # returns 0.0*1.0 + 0.0 + 0.1 = 0.1
        I.execute(10000)
        assert np.allclose(I.value, 0.1)
        assert np.allclose(I.output_state.value, 0.1)

        # reinitialize function
        I.function_object.reinitialize(2.0)
        assert np.allclose(I.function_object.value, 2.0)
        assert np.allclose(I.value, 0.1)
        assert np.allclose(I.output_states[0].value, 0.1)

        # reinitialize function without value spec
        I.function_object.reinitialize()
        assert np.allclose(I.function_object.value, 0.0)
        assert np.allclose(I.value, 0.1)
        assert np.allclose(I.output_states[0].value, 0.1)

        # reinitialize mechanism
        I.reinitialize(5.0)
        assert np.allclose(I.function_object.value, 5.0)
        assert np.allclose(I.value, 5.0)
        assert np.allclose(I.output_states[0].value, 5.0)

        I.execute(10000)
        #  5.0 * 1.0 + 0.0 + 0.1
        assert np.allclose(I.value, 5.1)
        assert np.allclose(I.output_states[0].value, 5.1)

        # reinitialize mechanism without value spec
        I.reinitialize()
        assert np.allclose(I.function_object.value, 0.0)
        assert np.allclose(I.value, 0.0)
        assert np.allclose(I.output_states[0].value, 0.0)
    def test_OU_valid(self):
        I = IntegratorMechanism(
            name='IntegratorMechanism',
            function=OrnsteinUhlenbeckIntegrator(),
        )

        # previous_value + (decay * previous_value - rate * variable) * time_step_size + noise
        # decay=1.0, initializer=0.0, rate=1.0, time_step_size=1.0, noise=0.0
        # returns 0.0 + (1.0*0.0 - 1.0*10.0*1.0) + 0.0 = -10.0
        I.execute(2.0)
        assert np.allclose(I.value, -2.0)
        assert np.allclose(I.output_state.value, -2.0)

        # reinitialize function
        I.function_object.reinitialize(5.0)
        assert np.allclose(I.function_object.value, 5.0)
        assert np.allclose(I.value, -2.0)
        assert np.allclose(I.output_states[0].value, -2.0)

        # reinitialize function without value spec
        I.function_object.reinitialize()
        assert np.allclose(I.function_object.value, 0.0)
        assert np.allclose(I.value, -2.0)
        assert np.allclose(I.output_states[0].value, -2.0)

        # reinitialize mechanism
        I.reinitialize(4.0)
        assert np.allclose(I.function_object.value, 4.0)
        assert np.allclose(I.value, 4.0)
        assert np.allclose(I.output_states[0].value, 4.0)

        I.execute(1.0)
        # 4.0 + (1.0 * 4.0 - 1.0 * 1.0) * 1.0 = 4 + 3 = 7
        assert np.allclose(I.value, 7.0)
        assert np.allclose(I.output_states[0].value, 7.0)

        # reinitialize mechanism without value spec
        I.reinitialize()
        assert np.allclose(I.function_object.value, 0.0)
        assert np.allclose(I.value, 0.0)
        assert np.allclose(I.output_states[0].value, 0.0)
    def test_Simple_valid(self):
        I = IntegratorMechanism(
            name='IntegratorMechanism',
            function=SimpleIntegrator(
            ),
        )
        I.reinitialize_when = Never()

        #  returns previous_value + rate*variable + noise
        # so in this case, returns 10.0
        I.execute(10)
        assert np.allclose(I.value, 10.0)
        assert np.allclose(I.output_state.value, 10.0)

        # reinitialize function
        I.function_object.reinitialize(5.0)
        assert np.allclose(I.function_object.value, 5.0)
        assert np.allclose(I.value, 10.0)
        assert np.allclose(I.output_states[0].value, 10.0)

        # reinitialize function without value spec
        I.function_object.reinitialize()
        assert np.allclose(I.function_object.value, 0.0)
        assert np.allclose(I.value, 10.0)
        assert np.allclose(I.output_states[0].value, 10.0)

        # reinitialize mechanism
        I.reinitialize(4.0)
        assert np.allclose(I.function_object.value, 4.0)
        assert np.allclose(I.value, 4.0)
        assert np.allclose(I.output_states[0].value, 4.0)

        I.execute(1)
        assert np.allclose(I.value, 5.0)
        assert np.allclose(I.output_states[0].value, 5.0)

        # reinitialize mechanism without value spec
        I.reinitialize()
        assert np.allclose(I.function_object.value, 0.0)
        assert np.allclose(I.value, 0.0)
        assert np.allclose(I.output_states[0].value, 0.0)
    def test_FHN_valid(self):
        I = IntegratorMechanism(name="I",
                function=FHNIntegrator())
        I.reinitialize_when = Never()
        I.execute(1.0)

        assert np.allclose([[0.05127053]], I.value[0])
        assert np.allclose([[0.00279552]], I.value[1])
        assert np.allclose([[0.05]], I.value[2])

        I.function_object.reinitialize(0.01, 0.02, 0.03)

        assert np.allclose(0.01, I.function_object.value[0])
        assert np.allclose(0.02, I.function_object.value[1])
        assert np.allclose(0.03, I.function_object.value[2])

        assert np.allclose([[0.05127053]], I.value[0])
        assert np.allclose([[0.00279552]], I.value[1])
        assert np.allclose([[0.05]], I.value[2])

        assert np.allclose([[0.05127053]], I.output_states[0].value)

        I.execute(1.0)

        assert np.allclose([[0.06075727]], I.value[0])
        assert np.allclose([[0.02277156]], I.value[1])
        assert np.allclose([[0.08]], I.value[2])

        assert np.allclose([[0.06075727]], I.output_states[0].value)

        # I.reinitialize(new_previous_v=0.01, new_previous_w=0.02, new_previous_time=0.03)
        I.reinitialize(0.01, 0.02, 0.03)

        assert np.allclose(0.01, I.value[0])
        assert np.allclose(0.02, I.value[1])
        assert np.allclose(0.03, I.value[2])

        assert np.allclose(0.01, I.output_states[0].value)