Beispiel #1
0
    def _validate_conditions(self):
        unspecified_nodes = []
        for node in self.nodes:
            if node not in self.conditions:
                # determine parent nodes
                node_index = 0
                for i in range(len(self.consideration_queue)):
                    if node in self.consideration_queue[i]:
                        node_index = i
                        break

                if node_index > 0:
                    dependencies = list(self.consideration_queue[i - 1])
                    if len(dependencies) == 1:
                        cond = EveryNCalls(dependencies[0], 1)
                    elif len(dependencies) > 1:
                        cond = All(*[EveryNCalls(x, 1) for x in dependencies])
                    else:
                        raise SchedulerError(
                            f'{self}: Empty consideration set in consideration_queue[{i - 1}]'
                        )
                else:
                    cond = Always()

                self.conditions.add_condition(node, cond)
                unspecified_nodes.append(node)
        if len(unspecified_nodes) > 0:
            logger.info(
                'These nodes have no Conditions specified, and will be scheduled with conditions: {0}'
                .format({
                    node: self.conditions[node]
                    for node in unspecified_nodes
                }))
Beispiel #2
0
    def test_composite_condition_multi(self):
        comp = Composition()
        A = TransferMechanism(function=Linear(slope=5.0, intercept=2.0), name='A')
        B = TransferMechanism(function=Linear(intercept=4.0), name='B')
        C = TransferMechanism(function=Linear(intercept=1.5), name='C')
        for m in [A, B, C]:
            comp.add_node(m)
        comp.add_projection(MappingProjection(), A, B)
        comp.add_projection(MappingProjection(), B, C)
        sched = Scheduler(composition=comp)

        sched.add_condition(A, EveryNPasses(1))
        sched.add_condition(B, EveryNCalls(A, 2))
        sched.add_condition(C, All(
            Any(
                AfterPass(6),
                AfterNCalls(B, 2)
            ),
            Any(
                AfterPass(2),
                AfterNCalls(B, 3)
            )
        )
        )

        termination_conds = {}
        termination_conds[TimeScale.RUN] = AfterNTrials(1)
        termination_conds[TimeScale.TRIAL] = AfterNCalls(C, 3)
        output = list(sched.run(termination_conds=termination_conds))
        expected_output = [
            A, A, B, A, A, B, C, A, C, A, B, C
        ]
        assert output == pytest.helpers.setify_expected_output(expected_output)
Beispiel #3
0
    def test_checkmark2_1(self):
        comp = Composition()
        A = TransferMechanism(function=Linear(slope=5.0, intercept=2.0), name='scheduler-pytests-A')
        B = TransferMechanism(function=Linear(intercept=4.0), name='scheduler-pytests-B')
        C = TransferMechanism(function=Linear(intercept=1.5), name='scheduler-pytests-C')
        D = TransferMechanism(function=Linear(intercept=.5), name='scheduler-pytests-D')
        for m in [A, B, C, D]:
            comp.add_node(m)
        comp.add_projection(MappingProjection(), A, B)
        comp.add_projection(MappingProjection(), A, D)
        comp.add_projection(MappingProjection(), B, D)
        comp.add_projection(MappingProjection(), C, D)

        sched = Scheduler(composition=comp)

        sched.add_condition(A, EveryNPasses(1))
        sched.add_condition(B, EveryNCalls(A, 2))
        sched.add_condition(C, EveryNCalls(A, 2))
        sched.add_condition(D, All(EveryNCalls(B, 2), EveryNCalls(C, 2)))

        termination_conds = {}
        termination_conds[TimeScale.RUN] = AfterNTrials(1)
        termination_conds[TimeScale.TRIAL] = AfterNCalls(D, 1, time_scale=TimeScale.TRIAL)
        output = list(sched.run(termination_conds=termination_conds))

        expected_output = [
            A, set([A, C]), B, A, set([A, C]), B, D
        ]

        assert output == pytest.helpers.setify_expected_output(expected_output)
Beispiel #4
0
    def test_multisource_1(self):
        comp = Composition()
        A1 = TransferMechanism(function=Linear(slope=5.0, intercept=2.0), name='A1')
        A2 = TransferMechanism(function=Linear(slope=5.0, intercept=2.0), name='A2')
        B1 = TransferMechanism(function=Linear(intercept=4.0), name='B1')
        B2 = TransferMechanism(function=Linear(intercept=4.0), name='B2')
        B3 = TransferMechanism(function=Linear(intercept=4.0), name='B3')
        C1 = TransferMechanism(function=Linear(intercept=1.5), name='C1')
        C2 = TransferMechanism(function=Linear(intercept=.5), name='C2')
        for m in [A1, A2, B1, B2, B3, C1, C2]:
            comp.add_node(m)
        comp.add_projection(MappingProjection(), A1, B1)
        comp.add_projection(MappingProjection(), A1, B2)
        comp.add_projection(MappingProjection(), A2, B1)
        comp.add_projection(MappingProjection(), A2, B2)
        comp.add_projection(MappingProjection(), A2, B3)
        comp.add_projection(MappingProjection(), B1, C1)
        comp.add_projection(MappingProjection(), B2, C1)
        comp.add_projection(MappingProjection(), B1, C2)
        comp.add_projection(MappingProjection(), B3, C2)

        sched = Scheduler(composition=comp)

        for m in comp.nodes:
            sched.add_condition(m, Always())

        termination_conds = {}
        termination_conds[TimeScale.RUN] = AfterNTrials(1)
        termination_conds[TimeScale.TRIAL] = All(AfterNCalls(C1, 1), AfterNCalls(C2, 1))
        output = list(sched.run(termination_conds=termination_conds))

        expected_output = [
            set([A1, A2]), set([B1, B2, B3]), set([C1, C2])
        ]
        assert output == pytest.helpers.setify_expected_output(expected_output)
Beispiel #5
0
    def test_triangle_4b(self):
        comp = Composition()
        A = TransferMechanism(function=Linear(slope=5.0, intercept=2.0), name='scheduler-pytests-A')
        B = TransferMechanism(function=Linear(intercept=4.0), name='scheduler-pytests-B')
        C = TransferMechanism(function=Linear(intercept=1.5), name='scheduler-pytests-C')

        for m in [A, B, C]:
            comp.add_node(m)
        comp.add_projection(MappingProjection(), A, B)
        comp.add_projection(MappingProjection(), A, C)

        sched = Scheduler(composition=comp)

        sched.add_condition(A, EveryNPasses(1))
        sched.add_condition(B, EveryNCalls(A, 2))
        sched.add_condition(C, All(WhenFinished(A), AfterNCalls(B, 3)))

        termination_conds = {}
        termination_conds[TimeScale.RUN] = AfterNTrials(1)
        termination_conds[TimeScale.TRIAL] = AfterNCalls(C, 1)
        output = []
        i = 0
        for step in sched.run(termination_conds=termination_conds):
            if i == 10:
                A._is_finished = True
            output.append(step)
            i += 1

        expected_output = [A, A, B, A, A, B, A, A, B, A, A, set([B, C])]
        # pprint.pprint(output)
        assert output == pytest.helpers.setify_expected_output(expected_output)
    def test_four_integrators_mixed(self):
        A = IntegratorMechanism(name='A',
                                default_variable=[0],
                                function=SimpleIntegrator(rate=1))

        B = IntegratorMechanism(name='B',
                                default_variable=[0],
                                function=SimpleIntegrator(rate=1))

        C = IntegratorMechanism(name='C',
                                default_variable=[0],
                                function=SimpleIntegrator(rate=1))

        D = IntegratorMechanism(name='D',
                                default_variable=[0],
                                function=SimpleIntegrator(rate=1))

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

        p1 = Process(default_variable=[0], pathway=[A, D], name='p1')

        q = Process(default_variable=[0], pathway=[B, C], name='q')

        q1 = Process(default_variable=[0], pathway=[B, D], name='q1')

        s = System(processes=[p, p1, q, q1], name='s')

        term_conds = {
            TimeScale.TRIAL: All(AfterNCalls(C, 1), AfterNCalls(D, 1))
        }
        stim_list = {A: [[1]], B: [[1]]}

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

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

        mechs = [A, B, C, D]
        expected_output = [
            [
                numpy.array([2.]),
            ],
            [
                numpy.array([1.]),
            ],
            [
                numpy.array([4.]),
            ],
            [
                numpy.array([3.]),
            ],
        ]

        for m in range(len(mechs)):
            for i in range(len(expected_output[m])):
                numpy.testing.assert_allclose(expected_output[m][i],
                                              mechs[m].get_output_values(s)[i])
Beispiel #7
0
    def test_four_integrators_mixed(self):
        A = IntegratorMechanism(name='A',
                                default_variable=[0],
                                function=SimpleIntegrator(rate=1))

        B = IntegratorMechanism(name='B',
                                default_variable=[0],
                                function=SimpleIntegrator(rate=1))

        C = IntegratorMechanism(name='C',
                                default_variable=[0],
                                function=SimpleIntegrator(rate=1))

        D = IntegratorMechanism(name='D',
                                default_variable=[0],
                                function=SimpleIntegrator(rate=1))

        c = Composition(pathways=[[A, C], [A, D], [B, C], [B, D]])

        term_conds = {
            TimeScale.TRIAL: All(AfterNCalls(C, 1), AfterNCalls(D, 1))
        }
        stim_list = {A: [[1]], B: [[1]]}

        sched = Scheduler(composition=c)
        sched.add_condition(B, EveryNCalls(A, 2))
        sched.add_condition(C, EveryNCalls(A, 1))
        sched.add_condition(D, EveryNCalls(B, 1))
        c.scheduler = sched

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

        mechs = [A, B, C, D]
        expected_output = [
            [
                numpy.array([2.]),
            ],
            [
                numpy.array([1.]),
            ],
            [
                numpy.array([4.]),
            ],
            [
                numpy.array([3.]),
            ],
        ]

        for m in range(len(mechs)):
            for i in range(len(expected_output[m])):
                numpy.testing.assert_allclose(expected_output[m][i],
                                              mechs[m].get_output_values(c)[i])
    def test_three_2_ABCx2(self):
        A = IntegratorMechanism(name='A',
                                default_variable=[0],
                                function=SimpleIntegrator(rate=.5))

        B = IntegratorMechanism(name='B',
                                default_variable=[0],
                                function=SimpleIntegrator(rate=1))

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

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

        q = Process(default_variable=[0], pathway=[B, C], name='q')

        s = System(processes=[p, q], name='s')

        term_conds = {TimeScale.TRIAL: AfterNCalls(C, 2)}
        stim_list = {A: [[1]], B: [[2]]}

        sched = Scheduler(system=s)
        sched.add_condition(C, All(EveryNCalls(A, 1), EveryNCalls(B, 1)))
        s.scheduler_processing = sched

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

        terminal_mechs = [C]
        expected_output = [
            [
                numpy.array([10.]),
            ],
        ]

        for m in range(len(terminal_mechs)):
            for i in range(len(expected_output[m])):
                numpy.testing.assert_allclose(
                    expected_output[m][i],
                    terminal_mechs[m].get_output_values(s)[i])
Beispiel #9
0
    def test_8(self):
        comp = Composition()
        A = TransferMechanism(function=Linear(slope=5.0, intercept=2.0), name='scheduler-pytests-A')
        B = TransferMechanism(function=Linear(intercept=4.0), name='scheduler-pytests-B')
        for m in [A, B]:
            comp.add_node(m)
        comp.add_projection(MappingProjection(), A, B)

        sched = Scheduler(composition=comp)

        sched.add_condition(A, EveryNPasses(1))
        sched.add_condition(B, EveryNCalls(A, 2))

        termination_conds = {}
        termination_conds[TimeScale.RUN] = AfterNTrials(1)
        termination_conds[TimeScale.TRIAL] = All(AfterNCalls(A, 1), AfterNCalls(B, 1))
        output = list(sched.run(termination_conds=termination_conds))

        expected_output = [A, A, B]
        assert output == pytest.helpers.setify_expected_output(expected_output)
Beispiel #10
0
    def _validate_conditions(self):
        unspecified_nodes = []
        for node in self.nodes:
            if node not in self.conditions:
                dependencies = list(self.dependency_dict[node])
                if len(dependencies) == 0:
                    cond = Always()
                elif len(dependencies) == 1:
                    cond = EveryNCalls(dependencies[0], 1)
                else:
                    cond = All(*[EveryNCalls(x, 1) for x in dependencies])

                self.conditions.add_condition(node, cond)
                unspecified_nodes.append(node)
        if len(unspecified_nodes) > 0:
            logger.info(
                'These nodes have no Conditions specified, and will be scheduled with conditions: {0}'
                .format({
                    node: self.conditions[node]
                    for node in unspecified_nodes
                }))
Beispiel #11
0
    def test_three_2_ABC(self):
        A = IntegratorMechanism(name='A',
                                default_variable=[0],
                                function=SimpleIntegrator(rate=.5))

        B = IntegratorMechanism(name='B',
                                default_variable=[0],
                                function=SimpleIntegrator(rate=1))

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

        c = Composition(pathways=[[A, C], [B, C]])

        term_conds = {TimeScale.TRIAL: AfterNCalls(C, 1)}
        stim_list = {A: [[1]], B: [[2]]}

        sched = Scheduler(composition=c)
        sched.add_condition(C, All(EveryNCalls(A, 1), EveryNCalls(B, 1)))
        c.scheduler = sched

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

        terminal_mechs = [C]
        expected_output = [
            [
                numpy.array([5.]),
            ],
        ]

        for m in range(len(terminal_mechs)):
            for i in range(len(expected_output[m])):
                numpy.testing.assert_allclose(
                    expected_output[m][i],
                    terminal_mechs[m].get_output_values(c)[i])
Beispiel #12
0
    def test_six_integrators_threelayer_mixed(self):
        A = IntegratorMechanism(name='A',
                                default_variable=[0],
                                function=SimpleIntegrator(rate=1))

        B = IntegratorMechanism(name='B',
                                default_variable=[0],
                                function=SimpleIntegrator(rate=1))

        C = IntegratorMechanism(name='C',
                                default_variable=[0],
                                function=SimpleIntegrator(rate=1))

        D = IntegratorMechanism(name='D',
                                default_variable=[0],
                                function=SimpleIntegrator(rate=1))

        E = IntegratorMechanism(name='E',
                                default_variable=[0],
                                function=SimpleIntegrator(rate=1))

        F = IntegratorMechanism(name='F',
                                default_variable=[0],
                                function=SimpleIntegrator(rate=1))

        c = Composition(pathways=[[A, C, E], [A, C, F], [A, D, E], [A, D, F],
                                  [B, C, E], [B, C, F], [B, D, E], [B, D, F]])

        term_conds = {
            TimeScale.TRIAL: All(AfterNCalls(E, 1), AfterNCalls(F, 1))
        }
        stim_list = {A: [[1]], B: [[1]]}

        sched = Scheduler(composition=c)
        sched.add_condition(B, EveryNCalls(A, 2))
        sched.add_condition(C, EveryNCalls(A, 1))
        sched.add_condition(D, EveryNCalls(B, 1))
        sched.add_condition(E, EveryNCalls(C, 1))
        sched.add_condition(F, EveryNCalls(D, 2))
        c.scheduler = sched

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

        # Intermediate time steps
        #
        #     0   1   2   3
        #
        # A   1   2   3   4
        # B       1       2
        # C   1   4   8   14
        # D       3       9
        # E   1   8   19  42
        # F               23
        #
        expected_output = {
            A: [
                numpy.array([4.]),
            ],
            B: [
                numpy.array([2.]),
            ],
            C: [
                numpy.array([14.]),
            ],
            D: [
                numpy.array([9.]),
            ],
            E: [
                numpy.array([42.]),
            ],
            F: [
                numpy.array([23.]),
            ],
        }

        for m in expected_output:
            for i in range(len(expected_output[m])):
                numpy.testing.assert_allclose(expected_output[m][i],
                                              m.get_output_values(c)[i])