def test_two_AAB(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') term_conds = {TimeScale.TRIAL: AfterNCalls(B, 1)} 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) terminal_mech = B expected_output = [ numpy.array([2.]), ] for i in range(len(expected_output)): numpy.testing.assert_allclose(expected_output[i], terminal_mech.output_values[i])
def test_9(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) comp.add_projection(A, MappingProjection(), B) sched = Scheduler(composition=comp) sched.add_condition(A, EveryNPasses(1)) sched.add_condition(B, WhenFinished(A)) termination_conds = {} termination_conds[TimeScale.RUN] = AfterNTrials(1) termination_conds[TimeScale.TRIAL] = AfterNCalls(B, 2) output = [] i = 0 for step in sched.run(termination_conds=termination_conds): if i == 3: A.is_finished = True output.append(step) i += 1 expected_output = [A, A, A, A, B, A, B] assert output == pytest.helpers.setify_expected_output(expected_output)
def test_two_compositions_one_scheduler(self): comp1 = Composition() comp2 = Composition() A = TransferMechanism(function=Linear(slope=5.0, intercept=2.0), name='scheduler-pytests-A') comp1.add_mechanism(A) comp2.add_mechanism(A) sched = Scheduler(composition=comp1) sched.add_condition(A, BeforeNCalls(A, 5, time_scale=TimeScale.LIFE)) termination_conds = {} termination_conds[TimeScale.RUN] = AfterNTrials(6) termination_conds[TimeScale.TRIAL] = AfterNPasses(1) comp1.run(inputs={A: [[0], [1], [2], [3], [4], [5]]}, scheduler_processing=sched, termination_processing=termination_conds) output = sched.execution_list[comp1._execution_id] expected_output = [A, A, A, A, A, set()] # pprint.pprint(output) assert output == pytest.helpers.setify_expected_output(expected_output) comp2.run(inputs={A: [[0], [1], [2], [3], [4], [5]]}, scheduler_processing=sched, termination_processing=termination_conds) output = sched.execution_list[comp2._execution_id] expected_output = [A, A, A, A, A, set()] # pprint.pprint(output) assert output == pytest.helpers.setify_expected_output(expected_output)
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_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 test_linear_ABBCC(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(), B) comp.add_projection(B, MappingProjection(), C) sched = Scheduler(composition=comp) sched.add_condition(A, Any(AtPass(0), EveryNCalls(C, 2))) sched.add_condition(B, Any(JustRan(A), JustRan(B))) sched.add_condition(C, Any(EveryNCalls(B, 2), JustRan(C))) termination_conds = {} termination_conds[TimeScale.RUN] = AfterNTrials(1) termination_conds[TimeScale.TRIAL] = AfterNCalls( C, 4, time_scale=TimeScale.TRIAL) output = list(sched.run(termination_conds=termination_conds)) expected_output = [A, B, B, C, C, A, B, B, C, C] assert output == pytest.helpers.setify_expected_output(expected_output)
def test_triangle_4b(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(), B) comp.add_projection(A, MappingProjection(), 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_invtriangle_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') 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, EveryNPasses(1)) sched.add_condition(B, EveryNCalls(A, 2)) sched.add_condition(C, Any(AfterNCalls(A, 3), AfterNCalls(B, 3))) termination_conds = {} termination_conds[TimeScale.RUN] = AfterNTrials(1) termination_conds[TimeScale.TRIAL] = AfterNCalls( C, 4, time_scale=TimeScale.TRIAL) output = list(sched.run(termination_conds=termination_conds)) expected_output = [ A, set([A, B]), A, C, set([A, B]), C, A, C, set([A, B]), C ] # pprint.pprint(output) assert output == pytest.helpers.setify_expected_output(expected_output)
def test_checkmark2_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(A, MappingProjection(), D) comp.add_projection(B, MappingProjection(), D) comp.add_projection(C, MappingProjection(), 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)
def test_termination_conditions_reset(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_mechanism(m) comp.add_projection(A, MappingProjection(), B) sched = Scheduler(composition=comp) sched.add_condition(B, EveryNCalls(A, 2)) termination_conds = {} termination_conds[TimeScale.RUN] = AfterNTrials(1) termination_conds[TimeScale.TRIAL] = AfterNCalls(B, 2) output = list(sched.run(termination_conds=termination_conds)) expected_output = [A, A, B, A, A, B] assert output == pytest.helpers.setify_expected_output(expected_output) # reset the RUN because schedulers run TRIALs sched.clock._increment_time(TimeScale.RUN) sched._reset_counts_total(TimeScale.RUN) output = list(sched.run()) expected_output = [A, A, B] assert output == pytest.helpers.setify_expected_output(expected_output)
def __init__(self): # core attributes self.graph = Graph() # Graph of the Composition self._graph_processing = None self.mechanisms = [] self.input_mechanisms = {} self._scheduler_processing = None self._scheduler_learning = None # status attributes self.graph_consistent = True # Tracks if the Composition is in a state that can be run (i.e. no dangling projections, (what else?)) self.needs_update_graph = True # Tracks if the Composition graph has been analyzed to assign roles to components self.needs_update_graph_processing = True # Tracks if the processing graph is current with the full graph self.needs_update_scheduler_processing = True # Tracks if the processing scheduler needs to be regenerated self.needs_update_scheduler_learning = True # Tracks if the learning scheduler needs to be regenerated (mechanisms/projections added/removed etc) # helper attributes self.mechanisms_to_roles = OrderedDict() # Create lists to track identity of certain mechanism classes within the # composition. # Explicit classes: self.explicit_input_mechanisms = [ ] # Need to track to know which to leave untouched self.all_input_mechanisms = [] self.explicit_output_mechanisms = [ ] # Need to track to know which to leave untouched self.all_output_mechanisms = [] self.target_mechanisms = [ ] # Do not need to track explicit as they mush be explicit # TBI: update self.sched whenever something is added to the composition self.sched = Scheduler(composition=self)
def test_BeforeNCalls(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, BeforeNCalls(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, A, set(), set()] assert output == pytest.helpers.setify_expected_output( expected_output)
def test_no_termination_conds(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(), B) comp.add_projection(B, MappingProjection(), C) sched = Scheduler(composition=comp) sched.add_condition(A, EveryNPasses(1)) sched.add_condition(B, EveryNCalls(A, 2)) sched.add_condition(C, EveryNCalls(B, 3)) output = list(sched.run()) expected_output = [ A, A, B, A, A, B, A, A, B, C, ] # pprint.pprint(output) assert output == pytest.helpers.setify_expected_output(expected_output)
def test_WhenFinishedAll_2(self): comp = Composition() A = TransferMechanism(function=Linear(slope=5.0, intercept=2.0), name='A') A.is_finished = False B = TransferMechanism(function=Linear(intercept=4.0), name='B') B.is_finished = True 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, EveryNPasses(1)) sched.add_condition(B, EveryNPasses(1)) sched.add_condition(C, WhenFinishedAll(A, B)) termination_conds = {} termination_conds[TimeScale.RUN] = AfterNTrials(1) termination_conds[TimeScale.TRIAL] = AfterNCalls(A, 5) output = list(sched.run(termination_conds=termination_conds)) expected_output = [ set([A, B]), set([A, B]), set([A, B]), set([A, B]), set([A, B]), ] assert output == pytest.helpers.setify_expected_output(expected_output)
def test_6(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(), B) comp.add_projection(B, MappingProjection(), C) sched = Scheduler(composition=comp) sched.add_condition(A, BeforePass(5)) sched.add_condition(B, AfterNCalls(A, 5)) sched.add_condition(C, AfterNCalls(B, 1)) 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, A, A, A, B, C, B, C, B, C] # pprint.pprint(output) assert output == pytest.helpers.setify_expected_output(expected_output)
def test_Any_end_before_one_finished(self): comp = Composition() A = TransferMechanism(function=Linear(slope=5.0, intercept=2.0), name='A') for m in [A]: comp.add_mechanism(m) sched = Scheduler(composition=comp) sched.add_condition(A, EveryNPasses(1)) termination_conds = {} termination_conds[TimeScale.RUN] = AfterNTrials(1) termination_conds[TimeScale.TRIAL] = Any(AfterNCalls(A, 10), AtPass(5)) output = list(sched.run(termination_conds=termination_conds)) expected_output = [A for _ in range(5)] 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_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].output_values[i])
def test_WhileNot_AtPass_in_middle(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, WhileNot( lambda sched: sched.times[TimeScale.RUN][TimeScale.PASS] == 2, sched)) 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, A] assert output == pytest.helpers.setify_expected_output( expected_output)
def scheduler_learning(self): ''' A default `Scheduler` automatically generated by the Composition, used for the `learning <System_Execution_Learning>` phase of execution. :getter: Returns the default learning scheduler, and builds it if it needs updating since the last access. ''' if self.needs_update_scheduler_learning or self._scheduler_learning is None: self._scheduler_learning = Scheduler(graph=self.graph) self.needs_update_scheduler_learning = False return self._scheduler_learning
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].output_values[i])
def test_6_two_trials(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_mechanism(m) comp.add_projection(A, MappingProjection(), B) comp.add_projection(B, MappingProjection(), C) sched = Scheduler(composition=comp) sched.add_condition(A, BeforePass(5)) sched.add_condition(B, AfterNCalls(A, 5)) sched.add_condition(C, AfterNCalls(B, 1)) termination_conds = {} termination_conds[TimeScale.RUN] = AfterNTrials(2) termination_conds[TimeScale.TRIAL] = AfterNCalls(C, 3) comp.run(inputs={A: [[0], [1], [2], [3], [4], [5]]}, scheduler_processing=sched, termination_processing=termination_conds) output = sched.execution_list[comp._execution_id] expected_output = [ A, A, A, A, A, B, C, B, C, B, C, A, A, A, A, A, B, C, B, C, B, C ] # pprint.pprint(output) assert output == pytest.helpers.setify_expected_output(expected_output)
def test_9b(self): comp = Composition() A = TransferMechanism(function=Linear(slope=5.0, intercept=2.0), name='A') A.is_finished = False 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, EveryNPasses(1)) sched.add_condition(B, WhenFinished(A)) 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, 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)
def test_linear_AAB(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) comp.add_projection(A, MappingProjection(), B) sched = Scheduler(composition=comp) sched.add_condition(A, EveryNPasses(1)) sched.add_condition(B, EveryNCalls(A, 2)) termination_conds = {} termination_conds[TimeScale.RUN] = AfterNCalls( B, 2, time_scale=TimeScale.RUN) termination_conds[TimeScale.TRIAL] = AfterNCalls( B, 2, time_scale=TimeScale.TRIAL) output = list(sched.run(termination_conds=termination_conds)) expected_output = [A, A, B, A, A, B] 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_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_create_scheduler_from_system_StroopDemo(self): Color_Input = TransferMechanism(name='Color Input', function=Linear(slope=0.2995)) Word_Input = TransferMechanism(name='Word Input', function=Linear(slope=0.2995)) # Processing Mechanisms (Control) Color_Hidden = TransferMechanism( name='Colors Hidden', function=Logistic(gain=(1.0, ControlProjection)), ) Word_Hidden = TransferMechanism( name='Words Hidden', function=Logistic(gain=(1.0, ControlProjection)), ) Output = TransferMechanism( name='Output', function=Logistic(gain=(1.0, ControlProjection)), ) # Decision Mechanisms Decision = DDM( function=BogaczEtAl( drift_rate=(1.0), threshold=(0.1654), noise=(0.5), starting_point=(0), t0=0.25, ), name='Decision', ) # Outcome Mechanisms: Reward = TransferMechanism(name='Reward') # Processes: ColorNamingProcess = Process( default_variable=[0], pathway=[Color_Input, Color_Hidden, Output, Decision], name='Color Naming Process', ) WordReadingProcess = Process( default_variable=[0], pathway=[Word_Input, Word_Hidden, Output, Decision], name='Word Reading Process', ) RewardProcess = Process( default_variable=[0], pathway=[Reward], name='RewardProcess', ) # System: mySystem = System( processes=[ColorNamingProcess, WordReadingProcess, RewardProcess], controller=EVCControlMechanism, enable_controller=True, # monitor_for_control=[Reward, (PROBABILITY_UPPER_THRESHOLD, 1, -1)], name='EVC Gratton System', ) sched = Scheduler(system=mySystem) integrator_ColorInputPrediction = mySystem.execution_list[7] integrator_RewardPrediction = mySystem.execution_list[8] integrator_WordInputPrediction = mySystem.execution_list[9] objective_EVC_mech = mySystem.execution_list[10] expected_consideration_queue = [ { Color_Input, Word_Input, Reward, integrator_ColorInputPrediction, integrator_WordInputPrediction, integrator_RewardPrediction }, {Color_Hidden, Word_Hidden}, {Output}, {Decision}, {objective_EVC_mech}, ] assert sched.consideration_queue == expected_consideration_queue
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)) p = [ Process(default_variable=[0], pathway=[A, C, E], name='p'), Process(default_variable=[0], pathway=[A, C, F], name='p1'), Process(default_variable=[0], pathway=[A, D, E], name='p2'), Process(default_variable=[0], pathway=[A, D, F], name='p3'), Process(default_variable=[0], pathway=[B, C, E], name='q'), Process(default_variable=[0], pathway=[B, C, F], name='q1'), Process(default_variable=[0], pathway=[B, D, E], name='q2'), Process(default_variable=[0], pathway=[B, D, F], name='q3') ] s = System(processes=p, name='s') term_conds = { TimeScale.TRIAL: All(AfterNCalls(E, 1), AfterNCalls(F, 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)) sched.add_condition(E, EveryNCalls(C, 1)) sched.add_condition(F, EveryNCalls(D, 2)) s.scheduler_processing = sched s.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.output_values[i])