def test_change_scheduler(self): comp = Composition() A = TransferMechanism(function=Linear(slope=5.0, intercept=2.0), name='A') B = TransferMechanism(function=Linear(slope=5.0, intercept=2.0), name='B') for m in [A, B]: comp.add_mechanism(m) s1 = Scheduler(composition=comp) s2 = Scheduler(composition=comp) cs = ConditionSet(s1) cs.add_condition(A, Always()) cs.add_condition(B, Always()) assert cs.scheduler is s1 for owner, cond in cs.conditions.items(): assert cond.scheduler is s1 cs.scheduler = s2 assert cs.scheduler is s2 for owner, cond in cs.conditions.items(): assert cond.scheduler is s2
def test_checkmark_1(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') D = TransferMechanism(function=Linear(intercept=.5), name='D') for m in [A, B, C, D]: comp.add_mechanism(m) comp.add_projection(A, MappingProjection(), B) comp.add_projection(B, MappingProjection(), D) comp.add_projection(C, MappingProjection(), D) sched = Scheduler(composition=comp) sched.add_condition(A, Always()) sched.add_condition(B, Always()) sched.add_condition(C, Always()) sched.add_condition(D, Always()) 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 = [set([A, C]), B, D] assert output == pytest.helpers.setify_expected_output(expected_output)
def test_multisource_2(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_mechanism(m) comp.add_projection(A1, MappingProjection(), B1) comp.add_projection(A1, MappingProjection(), B2) comp.add_projection(A2, MappingProjection(), B1) comp.add_projection(A2, MappingProjection(), B2) comp.add_projection(A2, MappingProjection(), B3) comp.add_projection(B1, MappingProjection(), C1) comp.add_projection(B2, MappingProjection(), C1) comp.add_projection(B1, MappingProjection(), C2) comp.add_projection(B3, MappingProjection(), C2) sched = Scheduler(composition=comp) sched.add_condition_set({ A1: Always(), A2: Always(), B1: EveryNCalls(A1, 2), B3: EveryNCalls(A2, 2), B2: All(EveryNCalls(A1, 4), EveryNCalls(A2, 4)), C1: Any(AfterNCalls(B1, 2), AfterNCalls(B2, 2)), C2: Any(AfterNCalls(B2, 2), AfterNCalls(B3, 2)), }) 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([A1, A2]), set([B1, B3]), set([A1, A2]), set([A1, A2]), set([B1, B2, B3]), set([C1, C2]) ] assert output == pytest.helpers.setify_expected_output(expected_output)
def _validate_condition_set(self): unspecified_nodes = [] for node in self.nodes: if node not in self.condition_set: self.condition_set.add_condition(node, Always()) unspecified_nodes.append(node) if len(unspecified_nodes) > 0: logger.info('These nodes have no Conditions specified, and will be scheduled with condition Always: {0}'.format(unspecified_nodes))
def test_WhenFinishedAll_noargs(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_mechanism(m) comp.add_projection(A, MappingProjection(), C) comp.add_projection(B, MappingProjection(), C) sched = Scheduler(composition=comp) sched.add_condition(A, Always()) sched.add_condition(B, Always()) sched.add_condition(C, Always()) termination_conds = {} termination_conds[TimeScale.RUN] = AfterNTrials(1) termination_conds[TimeScale.TRIAL] = WhenFinishedAll() output = [] i = 0 for step in sched.run(termination_conds=termination_conds): if i == 3: A.is_finished = True B.is_finished = True if i == 4: C.is_finished = True output.append(step) i += 1 expected_output = [ set([A, B]), C, set([A, B]), C, set([A, B]), ] assert output == pytest.helpers.setify_expected_output(expected_output)
def test_AfterNCalls(self): comp = Composition() A = TransferMechanism(function=Linear(slope=5.0, intercept=2.0), name='A') B = TransferMechanism(function=Linear(intercept=4.0), name='B') for m in [A, B]: comp.add_mechanism(m) sched = Scheduler(composition=comp) sched.add_condition(A, Always()) sched.add_condition(B, AfterNCalls(A, 3)) termination_conds = {} termination_conds[TimeScale.RUN] = AfterNTrials(1) termination_conds[TimeScale.TRIAL] = AtPass(5) output = list(sched.run(termination_conds=termination_conds)) expected_output = [A, A, set([A, B]), set([A, B]), set([A, B])] assert output == pytest.helpers.setify_expected_output( expected_output)
def test_AfterTrial(self): comp = Composition() A = TransferMechanism(function=Linear(slope=5.0, intercept=2.0), name='A') comp.add_mechanism(A) sched = Scheduler(composition=comp) sched.add_condition(A, Always()) termination_conds = {} termination_conds[TimeScale.RUN] = AfterTrial(4) termination_conds[TimeScale.TRIAL] = AtPass(1) comp.run(inputs={A: range(6)}, scheduler_processing=sched, termination_processing=termination_conds) output = sched.execution_list expected_output = [A, A, A, A, A] assert output == pytest.helpers.setify_expected_output( expected_output)
def test_NWhen_AfterNCalls(self, n, expected_output): comp = Composition() A = TransferMechanism(function=Linear(slope=5.0, intercept=2.0), name='A') B = TransferMechanism(function=Linear(intercept=4.0), name='B') for m in [A, B]: comp.add_mechanism(m) comp.add_projection(A, MappingProjection(), B) sched = Scheduler(composition=comp) sched.add_condition(A, Always()) sched.add_condition(B, NWhen(AfterNCalls(A, 3), n)) termination_conds = {} termination_conds[TimeScale.RUN] = AfterNTrials(1) termination_conds[TimeScale.TRIAL] = AfterNCalls(A, 6) output = list(sched.run(termination_conds=termination_conds)) expected_output = [A if x == 'A' else B for x in expected_output] assert output == pytest.helpers.setify_expected_output( expected_output)