def test_checkmark_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(), B, D) comp.add_projection(MappingProjection(), C, 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_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_node(m) m.is_finished_flag = False comp.add_projection(MappingProjection(), A, C) comp.add_projection(MappingProjection(), B, 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_flag = True B.is_finished_flag = True if i == 4: C.is_finished_flag = 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 _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 }))
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)
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_AtPass_underconstrained(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, AtPass(0)) sched.add_condition(B, Always()) sched.add_condition(C, Always()) termination_conds = {} termination_conds[TimeScale.RUN] = AfterNTrials(1) termination_conds[TimeScale.TRIAL] = AfterNCalls(C, 2) output = list(sched.run(termination_conds=termination_conds)) expected_output = [A, B, C, B, C] 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_node(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_node(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=sched, termination_processing=termination_conds ) output = sched.execution_list[comp.default_execution_id] expected_output = [A, A, A, A, A] assert output == pytest.helpers.setify_expected_output(expected_output)
def test_BeforeTimeStep_2(self): comp = Composition() A = TransferMechanism(function=Linear(slope=5.0, intercept=2.0), name='A') B = TransferMechanism(name='B') comp.add_node(A) comp.add_node(B) comp.add_projection(MappingProjection(), A, B) sched = Scheduler(composition=comp) sched.add_condition(A, BeforeTimeStep(2)) sched.add_condition(B, Always()) 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, B, B, B, B, B] 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_node(m) comp.add_projection(MappingProjection(), A, 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)
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 }))