def test_previous_value_persistence_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)

        S.run(inputs={T: 2.0}, num_trials=2)
        # Trial 3
        # integration: 0.9*0.595 + 0.1*2.0 + 0.0 = 0.7355  --->  previous value = 0.7355
        # linear fn: 0.7355*1.0 = 0.7355
        # Trial 4
        # integration: 0.9*0.7355 + 0.1*2.0 + 0.0 = 0.86195  --->  previous value = 0.86195
        # linear fn: 0.86195*1.0 = 0.86195

        assert np.allclose(T.integrator_function.previous_value, 0.86195)
Exemplo n.º 2
0
    def test_save_state_before_simulations(self):

        A = TransferMechanism(name='A',
                              integrator_mode=True,
                              integration_rate=0.2)

        B = IntegratorMechanism(name='B',
                                function=DriftDiffusionIntegrator(rate=0.1))
        C = TransferMechanism(name='C')

        P = Process(pathway=[A, B, C])
        S = System(processes=[P], reinitialize_mechanisms_when=Never())

        S.run(inputs={A: [[1.0], [1.0]]})

        run_1_values = [A.value, B.value[0], C.value]

        # "Save state" code from EVCaux

        # Get any values that need to be reinitialized for each run
        reinitialization_values = {}
        for mechanism in S.stateful_mechanisms:
            # "save" the current state of each stateful mechanism by storing the values of each of its stateful
            # attributes in the reinitialization_values dictionary; this gets passed into run and used to call
            # the reinitialize method on each stateful mechanism.
            reinitialization_value = []

            if isinstance(mechanism.function_object, Integrator):
                for attr in mechanism.function_object.stateful_attributes:
                    reinitialization_value.append(
                        getattr(mechanism.function_object, attr))
            elif hasattr(mechanism, "integrator_function"):
                if isinstance(mechanism.integrator_function, Integrator):
                    for attr in mechanism.integrator_function.stateful_attributes:
                        reinitialization_value.append(
                            getattr(mechanism.integrator_function, attr))

            reinitialization_values[mechanism] = reinitialization_value

        # Allow values to continue accumulating so that we can set them back to the saved state
        S.run(inputs={A: [[1.0], [1.0]]})

        run_2_values = [A.value, B.value[0], C.value]

        S.run(inputs={A: [[1.0], [1.0]]},
              reinitialize_values=reinitialization_values)

        run_3_values = [A.value, B.value[0], C.value]

        assert np.allclose(run_2_values, run_3_values)
        assert np.allclose(
            run_1_values,
            [np.array([[0.36]]),
             np.array([[0.056]]),
             np.array([[0.056]])])
        assert np.allclose(run_2_values, [
            np.array([[0.5904]]),
            np.array([[0.16384]]),
            np.array([[0.16384]])
        ])
    def test_switch_mode(self):
        T = TransferMechanism(integrator_mode=True)
        P = Process(pathway=[T])
        S = System(processes=[P])
        integrator_function = T.integrator_function
        T.reinitialize_when = Never()
        # T starts with integrator_mode = True; confirm that T behaves correctly
        S.run({T: [[1.0], [1.0], [1.0]]})
        assert np.allclose(T.value, [[0.875]])

        assert T.integrator_mode is True
        assert T.integrator_function is integrator_function

        # Switch integrator_mode to False; confirm that T behaves correctly
        T.integrator_mode = False

        assert T.integrator_mode is False
        assert T.integrator_function is None

        S.run({T: [[1.0], [1.0], [1.0]]})
        assert np.allclose(T.value, [[1.0]])

        # Switch integrator_mode BACK to True; confirm that T picks up where it left off
        T.integrator_mode = True

        assert T.integrator_mode is True
        assert T.integrator_function is integrator_function

        S.run({T: [[1.0], [1.0], [1.0]]})
        assert np.allclose(T.value, [[0.984375]])
Exemplo n.º 4
0
    def test_buffer_as_function_of_origin_mech_in_system(self):
        P = ProcessingMechanism(function=Buffer(
            default_variable=[[0.0]], initializer=[[0.0]], history=3))

        process = Process(pathway=[P])
        system = System(processes=[process])
        P.reinitialize_when = Never()
        full_result = []

        def assemble_full_result():
            full_result.append(P.value)

        result = system.run(inputs={P: [[1.0], [2.0], [3.0], [4.0], [5.0]]},
                            call_after_trial=assemble_full_result)
        # only returns index 0 item of the deque on each trial  (output state value)
        assert np.allclose(result,
                           [[[0.0]], [[0.0]], [[1.0]], [[2.0]], [[3.0]]])

        # stores full mechanism value (full deque) on each trial
        expected_full_result = [
            np.array([[0.], [1.]]),
            np.array([[0.], [1.], [2.]]),
            np.array([[[1.]], [[2.]], [[3.]]]),  # Shape change
            np.array([[[2.]], [[3.]], [[4.]]]),
            np.array([[[3.]], [[4.]], [[5.]]])
        ]
        for i in range(5):
            assert np.allclose(expected_full_result[i], full_result[i])
    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)
Exemplo n.º 6
0
    def __init__(
        self,
        system=None,
        composition=None,
        graph=None,
        condition_set=None,
        termination_conds=None,
    ):
        '''
        :param self:
        :param composition: (Composition) - the Composition this scheduler is scheduling for
        :param condition_set: (ConditionSet) - a :keyword:`ConditionSet` to be scheduled
        '''
        self.condition_set = condition_set if condition_set is not None else ConditionSet(
        )

        self.default_execution_id = uuid.uuid4()
        # stores the in order list of self.run's yielded outputs
        self.execution_list = {self.default_execution_id: []}
        self.clocks = {self.default_execution_id: Clock()}
        self.consideration_queue = []
        self.default_termination_conds = {
            TimeScale.RUN: Never(),
            TimeScale.TRIAL: AllHaveRun(),
        }
        self.termination_conds = termination_conds

        if system is not None:
            self.nodes = [m for m in system.execution_list]
            self._init_consideration_queue_from_system(system)
        elif composition is not None:
            self.nodes = [
                vert.component
                for vert in composition.graph_processing.vertices
            ]
            self._init_consideration_queue_from_graph(
                composition.graph_processing)
        elif graph is not None:
            try:
                self.nodes = [vert.component for vert in graph.vertices]
                self._init_consideration_queue_from_graph(graph)
            except AttributeError:
                self.consideration_queue = list(toposort(graph))
                self.nodes = []
                for consideration_set in self.consideration_queue:
                    for node in consideration_set:
                        self.nodes.append(node)
        else:
            raise SchedulerError(
                'Must instantiate a Scheduler with either a System (kwarg system) '
                'or a graph dependency dict (kwarg graph)')

        self.counts_total = {}
        self.counts_useable = {}
        self._init_counts(execution_id=self.default_execution_id)
        self.date_creation = datetime.datetime.now()
        self.date_last_run_end = None
Exemplo n.º 7
0
    def test_reinitialize_run(self):

        L = LCA(name="L",
                function=Linear,
                initial_value=0.5,
                integrator_mode=True,
                leak=0.1,
                competition=0,
                self_excitation=1.0,
                time_step_size=1.0,
                noise=0.0)
        P = Process(name="P", pathway=[L])
        S = System(name="S", processes=[P])

        L.reinitialize_when = Never()
        assert np.allclose(L.integrator_function.previous_value, 0.5)
        assert np.allclose(L.initial_value, 0.5)
        assert np.allclose(L.integrator_function.initializer, 0.5)

        S.run(inputs={L: 1.0},
              num_trials=2,
              initialize=True,
              initial_values={L: 0.0})

        # Integrator fn: previous_value + (rate*previous_value + new_value)*time_step_size + noise*(time_step_size**0.5)

        # Trial 1    |   variable = 1.0 + 0.0
        # integration: 0.5 + (0.1*0.5 + 1.0)*1.0 + 0.0 = 1.55
        # linear fn: 1.55*1.0 = 1.55
        # Trial 2    |   variable = 1.0 + 1.55
        # integration: 1.55 + (0.1*1.55 + 2.55)*1.0 + 0.0 = 4.255
        #  linear fn: 4.255*1.0 = 4.255
        assert np.allclose(L.integrator_function.previous_value, 4.255)

        L.integrator_function.reinitialize(0.9)

        assert np.allclose(L.integrator_function.previous_value, 0.9)
        assert np.allclose(L.value, 4.255)

        L.reinitialize(0.5)

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

        S.run(inputs={L: 1.0}, num_trials=2)
        # Trial 3    |   variable = 1.0 + 0.5
        # integration: 0.5 + (0.1*0.5 + 1.5)*1.0 + 0.0 = 2.05
        # linear fn: 2.05*1.0 = 2.05
        # Trial 4    |   variable = 1.0 + 2.05
        # integration: 2.05 + (0.1*2.05 + 3.05)*1.0 + 0.0 = 5.305
        #  linear fn: 5.305*1.0 = 5.305
        assert np.allclose(L.integrator_function.previous_value, 5.305)
        assert np.allclose(L.initial_value, 0.5)
        assert np.allclose(L.integrator_function.initializer, 0.5)
Exemplo n.º 8
0
    def test_is_finished_stops_system(self):
        D = DDM(name='DDM', function=DriftDiffusionIntegrator(threshold=10.0))
        P = Process(pathway=[D])
        S = System(processes=[P], reinitialize_mechanisms_when=Never())
        S.run(inputs={D: 2.0},
              termination_processing={TimeScale.TRIAL: WhenFinished(D)})

        # decision variable's value should match threshold
        assert D.value[0] == 10.0
        # it should have taken 5 executions (and time_step_size = 1.0)
        assert D.value[1] == 5.0
    def test_transfer_mech_inputs_list_of_floats(self, benchmark):

        T = TransferMechanism(
            name='T',
            default_variable=[0 for i in range(VECTOR_SIZE)],
            integration_rate=1.0,
            integrator_mode=True
        )
        T.reinitialize_when = Never()
        val = benchmark(T.execute, [10.0 for i in range(VECTOR_SIZE)])
        assert np.allclose(val, [[10.0 for i in range(VECTOR_SIZE)]])
    def test_transfer_mech_array_var_normal_array_noise2(self, benchmark):

        T = TransferMechanism(
            name='T',
            default_variable=[0 for i in range(VECTOR_SIZE)],
            function=Linear(),
            noise=[5.0 for i in range(VECTOR_SIZE)],
            integration_rate=1.0,
            integrator_mode=True
        )
        T.reinitialize_when = Never()
        val = benchmark(T.execute, [0 for i in range(VECTOR_SIZE)])
        assert np.allclose(val, [[5.0 for i in range(VECTOR_SIZE)]])
    def test_transfer_mech_array_var_normal_len_1_noise(self):

        T = TransferMechanism(
            name='T',
            default_variable=[0, 0, 0, 0],
            function=Linear(),
            noise=NormalDist(),
            integration_rate=1.0,
            integrator_mode=True
        )
        T.reinitialize_when = Never()
        val = T.execute([0, 0, 0, 0])
        assert np.allclose(val, [[0.41059850193837233, 0.144043571160878, 1.454273506962975, 0.7610377251469934]])
Exemplo n.º 12
0
    def test_LCA_length_1(self):

        T = TransferMechanism(function=Linear(slope=1.0))
        L = LCA(
            function=Linear(slope=2.0),
            self_excitation=3.0,
            leak=0.5,
            competition=
            1.0,  #  competition does not matter because we only have one unit
            time_step_size=0.1)
        P = Process(pathway=[T, L])
        S = System(processes=[P])
        L.reinitialize_when = Never()
        #  - - - - - - - Equations to be executed  - - - - - - -

        # new_transfer_input =
        # previous_transfer_input
        # + (leak * previous_transfer_input_1 + self_excitation * result1 + competition * result2 + outside_input1) * dt
        # + noise

        # result = new_transfer_input*2.0

        # recurrent_matrix = [[3.0]]

        #  - - - - - - - - - - - - - -  - - - - - - - - - - - -

        results = []

        def record_execution():
            results.append(L.value[0][0])

        S.run(inputs={T: [1.0]},
              num_trials=3,
              call_after_trial=record_execution)

        # - - - - - - - TRIAL 1 - - - - - - -

        # new_transfer_input = 0.0 + ( 0.5 * 0.0 + 3.0 * 0.0 + 0.0 + 1.0)*0.1 + 0.0    =    0.1
        # f(new_transfer_input) = 0.1 * 2.0 = 0.2

        # - - - - - - - TRIAL 2 - - - - - - -

        # new_transfer_input = 0.1 + ( 0.5 * 0.1 + 3.0 * 0.2 + 0.0 + 1.0)*0.1 + 0.0    =    0.265
        # f(new_transfer_input) = 0.265 * 2.0 = 0.53

        # - - - - - - - TRIAL 3 - - - - - - -

        # new_transfer_input = 0.265 + ( 0.5 * 0.265 + 3.0 * 0.53 + 0.0 + 1.0)*0.1 + 0.0    =    0.53725
        # f(new_transfer_input) = 0.53725 * 2.0 = 1.0745

        assert np.allclose(results, [0.2, 0.53, 1.0745])
    def test_transfer_mech_array_var_normal_array_noise(self):

        T = TransferMechanism(
            name='T',
            default_variable=[0, 0, 0, 0],
            function=Linear(),
            noise=[NormalDist(), NormalDist(), NormalDist(), NormalDist()],
            integration_rate=1.0,
            integrator_mode=True
        )
        T.reinitialize_when = Never()
        val = T.execute([0, 0, 0, 0])
        expected = [0.7610377251469934, 0.12167501649282841, 0.44386323274542566, 0.33367432737426683]
        for i in range(len(val[0])):
            assert val[0][i] ==  expected[i]
Exemplo n.º 14
0
    def test_termination_conditions_reset(self):
        A = IntegratorMechanism(name='A',
                                default_variable=[0],
                                function=SimpleIntegrator(rate=.5))

        B = TransferMechanism(
            name='B',
            default_variable=[0],
            function=Linear(slope=2.0),
        )

        p = Process(default_variable=[0], pathway=[A, B], name='p')

        s = System(processes=[p],
                   name='s',
                   reinitialize_mechanisms_when=Never())
        term_conds = {TimeScale.TRIAL: AfterNCalls(B, 2)}
        stim_list = {A: [[1]]}

        sched = Scheduler(system=s)
        sched.add_condition(B, EveryNCalls(A, 2))
        s.scheduler_processing = sched

        s.run(inputs=stim_list, termination_processing=term_conds)

        # A should run four times
        terminal_mech = B
        expected_output = [
            numpy.array([4.]),
        ]

        for i in range(len(expected_output)):
            numpy.testing.assert_allclose(expected_output[i],
                                          terminal_mech.output_values[i])

        s.run(inputs=stim_list, )

        # A should run an additional two times
        terminal_mech = B
        expected_output = [
            numpy.array([6.]),
        ]

        for i in range(len(expected_output)):
            numpy.testing.assert_allclose(expected_output[i],
                                          terminal_mech.output_values[i])
    def test_previous_value_persistence_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)

        T.execute(1.0)
        # 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)
    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])
    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)
    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
Exemplo n.º 20
0
    def test_LCA_length_2(self):

        T = TransferMechanism(function=Linear(slope=1.0), size=2)
        L = LCA(function=Linear(slope=2.0),
                size=2,
                self_excitation=3.0,
                leak=0.5,
                competition=1.0,
                time_step_size=0.1)

        P = Process(pathway=[T, L])
        S = System(processes=[P])
        L.reinitialize_when = Never()
        #  - - - - - - - Equations to be executed  - - - - - - -

        # new_transfer_input =
        # previous_transfer_input
        # + (leak * previous_transfer_input_1 + self_excitation * result1 + competition * result2 + outside_input1) * dt
        # + noise

        # result = new_transfer_input*2.0

        # recurrent_matrix = [[3.0]]

        #  - - - - - - - - - - - - - -  - - - - - - - - - - - -

        results = []

        def record_execution():
            results.append(L.value[0])

        S.run(inputs={T: [1.0, 2.0]},
              num_trials=3,
              call_after_trial=record_execution)

        # - - - - - - - TRIAL 1 - - - - - - -

        # new_transfer_input_1 = 0.0 + ( 0.5 * 0.0 + 3.0 * 0.0 - 1.0*0.0 + 1.0)*0.1 + 0.0    =    0.1
        # f(new_transfer_input_1) = 0.1 * 2.0 = 0.2

        # new_transfer_input_2 = 0.0 + ( 0.5 * 0.0 + 3.0 * 0.0 - 1.0*0.0 + 2.0)*0.1 + 0.0    =    0.2
        # f(new_transfer_input_2) = 0.2 * 2.0 = 0.4

        # - - - - - - - TRIAL 2 - - - - - - -

        # new_transfer_input = 0.1 + ( 0.5 * 0.1 + 3.0 * 0.2 - 1.0*0.4 + 1.0)*0.1 + 0.0    =    0.225
        # f(new_transfer_input) = 0.265 * 2.0 = 0.45

        # new_transfer_input_2 = 0.2 + ( 0.5 * 0.2 + 3.0 * 0.4 - 1.0*0.2 + 2.0)*0.1 + 0.0    =    0.51
        # f(new_transfer_input_2) = 0.1 * 2.0 = 1.02

        # - - - - - - - TRIAL 3 - - - - - - -

        # new_transfer_input = 0.225 + ( 0.5 * 0.225 + 3.0 * 0.45 - 1.0*1.02 + 1.0)*0.1 + 0.0    =    0.36925
        # f(new_transfer_input) = 0.36925 * 2.0 = 0.7385

        # new_transfer_input_2 = 0.51 + ( 0.5 * 0.51 + 3.0 * 1.02 - 1.0*0.45 + 2.0)*0.1 + 0.0    =    0.9965
        # f(new_transfer_input_2) = 0.9965 * 2.0 = 1.463

        assert np.allclose(results,
                           [[0.2, 0.4], [0.45, 1.02], [0.7385, 1.993]])
Exemplo n.º 21
0
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, MEAN, VARIANCE],
                               name='Reward')
    Decision = DDM(
        function=BogaczEtAl(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, )

    RewardPrediction = mySystem.execution_list[3]
    InputPrediction = mySystem.execution_list[4]

    # 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 = [
        # Decision Output | Second Trial
        (Decision.output_states[0].value, np.array(1.0)),

        # Input Prediction Output | Second Trial
        (InputPrediction.output_states[0].value, np.array(0.1865)),

        # RewardPrediction Output | Second Trial
        (RewardPrediction.output_states[0].value, np.array(15.0)),

        # --- Decision Mechanism ---
        #    Output State Values
        #       decision variable
        (Decision.output_states[DECISION_VARIABLE].value, np.array([1.0])),
        #       response time
        (Decision.output_states[RESPONSE_TIME].value, np.array([3.84279648])),
        #       upper bound
        (Decision.output_states[PROBABILITY_UPPER_THRESHOLD].value,
         np.array([0.81637827])),
        #       lower bound
        # (round(float(Decision.output_states['DDM_probability_lowerBound'].value),3), 0.184),

        # --- Reward Mechanism ---
        #    Output State Values
        #       transfer mean
        (Reward.output_states[RESULT].value, np.array([15.])),
        #       transfer_result
        (Reward.output_states[MEAN].value, np.array(15.0)),
        #       transfer variance
        (Reward.output_states[VARIANCE].value, np.array(0.0)),

        # System Results Array
        #   (all intermediate output values of system)
        (results_array, expected_results_array),

        # System Simulation Results Array
        #   (all simulation output values 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))