Ejemplo n.º 1
0
    def test_connect_outer_composition_to_all_input_nodes_in_inner_comp(self):

        inner1 = Composition(name="inner")
        A1 = TransferMechanism(name="A1", function=Linear(slope=2.0))
        B1 = TransferMechanism(name="B1", function=Linear(slope=3.0))

        inner1.add_linear_processing_pathway([A1, B1])

        inner2 = Composition(name="inner2")
        A2 = TransferMechanism(name="A2")
        B2 = TransferMechanism(name="B2")
        C2 = TransferMechanism(name="C2")

        inner2.add_nodes([A2, B2, C2])

        outer1 = Composition(name="outer1")
        outer1.add_nodes([inner1, inner2])

        # Spec 1: add projection *node in* inner1 --> inner 2 (implies first InputPort -- corresponding to A2)
        outer1.add_projection(sender=B1, receiver=inner2)
        # Spec 2:  add projection *node in* inner1 --> *node in* inner2
        outer1.add_projection(sender=B1, receiver=B2)
        # Spec 3: add projection inner1 --> *node in* inner2
        outer1.add_projection(sender=inner1, receiver=C2)
        eid = "eid"
        outer1.run(inputs={inner1: [[1.]]}, context=eid)

        assert np.allclose(A1.parameters.value.get(eid), [[2.0]])
        assert np.allclose(B1.parameters.value.get(eid), [[6.0]])

        for node in [A2, B2, C2]:
            assert np.allclose(node.parameters.value.get(eid), [[6.0]])
Ejemplo n.º 2
0
    def test_TimeInterval_no_dependencies(self, conditions, termination_conds):
        comp = Composition()
        comp.add_nodes([self.A, self.B, self.C])
        comp.scheduler.add_condition_set(conditions)
        time_step_abs_value = comp.scheduler._get_absolute_consideration_set_execution_unit(
            termination_conds)

        list(comp.scheduler.run(termination_conds=termination_conds))

        for node, cond in conditions.items():
            executions = [
                comp.scheduler.execution_timestamps[
                    comp.default_execution_id][i].absolute for i in range(
                        len(comp.scheduler.execution_list[
                            comp.default_execution_id])) if node in
                comp.scheduler.execution_list[comp.default_execution_id][i]
            ]

            for i in range(1, len(executions)):
                interval = (executions[i] - executions[i - 1])

                if cond.repeat is not None:
                    assert interval == cond.repeat
                else:
                    assert interval == time_step_abs_value

            if cond.start is not None:
                if cond.start_inclusive:
                    assert cond.start in executions
                else:
                    assert cond.start + time_step_abs_value in executions
    def test_alias_equivalence_for_modulates_and_projections(self):
        inputs = [1123, 941, 43, 311, 21]
        Tx1 = TransferMechanism()
        Ty1 = TransferMechanism()
        G1 = GatingMechanism(gating_signals=[GatingSignal(modulates=Tx1)])
        comp1 = Composition()
        comp1.add_nodes([Tx1, Ty1, G1])
        comp1.add_linear_processing_pathway([Tx1, Ty1, G1, Tx1])
        comp1.run(inputs={Tx1: inputs})

        Tx2 = TransferMechanism()
        Ty2 = TransferMechanism()
        G2 = GatingMechanism(gating_signals=[GatingSignal(projections=Tx2)])
        comp2 = Composition()
        comp2.add_nodes([Tx2, Ty2, G2])
        comp2.add_linear_processing_pathway([Tx2, Ty2, G2, Tx2])
        comp2.run(inputs={Tx2: inputs})
        assert comp1.results == comp2.results
 def test_alias_equivalence_for_modulates_and_projections(self):
     inputs = [1, 9, 4, 3, 2]
     comp1 = Composition()
     tMech1 = TransferMechanism()
     tMech2 = TransferMechanism()
     cMech1 = ControlMechanism(
         control_signals=ControlSignal(modulates=(SLOPE, tMech2)),
         objective_mechanism=ObjectiveMechanism(monitor=(RESULT, tMech2)))
     comp1.add_nodes([tMech1, tMech2, cMech1])
     comp1.add_linear_processing_pathway([cMech1, tMech1, tMech2])
     comp1.run(inputs=inputs)
     comp2 = Composition()
     tMech3 = TransferMechanism()
     tMech4 = TransferMechanism()
     cMech2 = ControlMechanism(
         control_signals=ControlSignal(projections=(SLOPE, tMech4)),
         objective_mechanism=ObjectiveMechanism(monitor=(RESULT, tMech4)))
     comp2.add_nodes([tMech3, tMech4, cMech2])
     comp2.add_linear_processing_pathway([cMech2, tMech3, tMech4])
     comp2.run(inputs=inputs)
     assert comp1.results == comp2.results
Ejemplo n.º 5
0
    def test_connect_outer_composition_to_only_input_node_in_inner_comp_option2(
            self):
        inner1 = Composition(name="inner")

        A1 = TransferMechanism(name="A1", function=Linear(slope=2.0))

        B1 = TransferMechanism(name="B1", function=Linear(slope=3.0))

        inner1.add_linear_processing_pathway([A1, B1])

        inner2 = Composition(name="inner2")

        A2 = TransferMechanism(name="A2", function=Linear(slope=2.0))

        B2 = TransferMechanism(name="B2", function=Linear(slope=3.0))

        inner2.add_linear_processing_pathway([A2, B2])

        outer = Composition(name="outer")
        outer.add_nodes([inner1, inner2])
        outer.add_projection(sender=inner1, receiver=A2)

        # CRASHING WITH: FIX 6/1/20
        # subprocess.CalledProcessError: Command '['dot', '-Tpdf', '-O', 'outer']' returned non-zero exit status 1.
        # outer.show_graph(show_node_structure=True,
        #                  show_nested=True)

        # comp1:  input = 5.0   |  mechA: 2.0*5.0 = 10.0    |  mechB: 3.0*10.0 = 30.0    |  output = 30.0
        # comp2:  input = 30.0  |  mechA2: 2.0*30.0 = 60.0  |  mechB2: 3.0*60.0 = 180.0  |  output = 180.0
        # comp3:  input = 5.0   |  output = 180.0

        res = outer.run(inputs={inner1: [[5.]]})
        assert np.allclose(res, [[[180.0]]])

        assert np.allclose(inner1.output_port.parameters.value.get(outer),
                           [30.0])
        assert np.allclose(inner2.output_port.parameters.value.get(outer),
                           [180.0])
        assert np.allclose(outer.output_port.parameters.value.get(outer),
                           [180.0])
Ejemplo n.º 6
0
    def test_connect_outer_composition_to_only_input_node_in_inner_comp_option3(
            self):

        inner1 = Composition(name="inner")

        A1 = TransferMechanism(name="A1", function=Linear(slope=2.0))

        B1 = TransferMechanism(name="B1", function=Linear(slope=3.0))

        inner1.add_linear_processing_pathway([A1, B1])

        inner2 = Composition(name="inner2")

        A2 = TransferMechanism(name="A2", function=Linear(slope=2.0))

        B2 = TransferMechanism(name="B2", function=Linear(slope=3.0))

        inner2.add_linear_processing_pathway([A2, B2])

        outer = Composition(name="outer")
        outer.add_nodes([inner1, inner2])
        outer.add_projection(sender=B1, receiver=A2)

        # comp1:  input = 5.0   |  mechA: 2.0*5.0 = 10.0    |  mechB: 3.0*10.0 = 30.0    |  output = 30.0
        # comp2:  input = 30.0  |  mechA2: 2.0*30.0 = 60.0  |  mechB2: 3.0*60.0 = 180.0  |  output = 180.0
        # comp3:  input = 5.0   |  output = 180.0

        res = outer.run(inputs={inner1: [[5.]]})
        assert np.allclose(res, [[[180.0]]])

        assert np.allclose(inner1.output_port.parameters.value.get(outer),
                           [30.0])
        assert np.allclose(inner2.output_port.parameters.value.get(outer),
                           [180.0])
        assert np.allclose(outer.output_port.parameters.value.get(outer),
                           [180.0])