Example #1
0
    def test_singletonblock_noindextemplate(self):
        st_model = CreateConcreteTwoStageScenarioTreeModel(1)
        st_model.StageVariables['Stage1'].add("b1")
        st_model.StageDerivedVariables['Stage1'].add("b2")
        st_model.NodeVariables['RootNode'].add("b3")
        st_model.NodeDerivedVariables['RootNode'].add("b4")
        st_model.StageCost['Stage1'] = "FirstStageCost"
        st_model.StageCost['Stage2'] = "SecondStageCost"

        scenario_tree = ScenarioTree(scenariotreeinstance=st_model)
        self.assertEqual(len(scenario_tree.stages), 2)
        self.assertEqual(len(scenario_tree.nodes), 2)
        self.assertEqual(len(scenario_tree.scenarios), 1)

        model = self._get_block_model()

        root = scenario_tree.findRootNode()
        root_nonant_names = _get_names(_get_nonant_list(model, root))
        root_derived_nonant_names = _get_names(
            _get_derived_nonant_list(model, root))
        assert len(root_nonant_names) == 6
        assert len(root_derived_nonant_names) == 6

        for name in ("b1.x", "b3.x"):
            assert name in root_nonant_names
        for name in ("b1.X", "b3.X"):
            var = model.find_component(name)
            for vardata in var.values():
                assert vardata.name in root_nonant_names
        for name in ("b2.x", "b4.x"):
            assert name in root_derived_nonant_names
        for name in ("b2.X", "b4.X"):
            var = model.find_component(name)
            for vardata in var.values():
                assert vardata.name in root_derived_nonant_names
Example #2
0
    def test_singletonvar_wildcardtemplate(self):
        st_model = CreateConcreteTwoStageScenarioTreeModel(1)
        st_model.StageVariables['Stage1'].add("x[*]")
        st_model.StageDerivedVariables['Stage1'].add("y[*]")
        st_model.NodeVariables['RootNode'].add("z[*]")
        st_model.NodeDerivedVariables['RootNode'].add("q[*]")
        st_model.StageCost['Stage1'] = "FirstStageCost"
        st_model.StageCost['Stage2'] = "SecondStageCost"

        scenario_tree = ScenarioTree(scenariotreeinstance=st_model)
        self.assertEqual(len(scenario_tree.stages), 2)
        self.assertEqual(len(scenario_tree.nodes), 2)
        self.assertEqual(len(scenario_tree.scenarios), 1)

        model = ConcreteModel()
        model.x = Var()
        model.y = Var()
        model.z = Var()
        model.q = Var()
        model.FirstStageCost = Expression(expr=0.0)
        model.SecondStageCost = Expression(expr=0.0)
        model.obj = Objective(expr=0.0)

        root = scenario_tree.findRootNode()
        root_nonant_names = _get_names(_get_nonant_list(model, root))
        root_derived_nonant_names = _get_names(
            _get_derived_nonant_list(model, root))
        assert len(root_nonant_names) == 2
        assert len(root_derived_nonant_names) == 2

        for name in ("x", "z"):
            assert name in root_nonant_names

        for name in ("y", "q"):
            assert name in root_derived_nonant_names
Example #3
0
    def test_indexedblock_wildcardtemplate(self):
        st_model = CreateConcreteTwoStageScenarioTreeModel(1)
        st_model.StageVariables['Stage1'].add("B1[*]")
        st_model.StageDerivedVariables['Stage1'].add("B2[*]")
        st_model.NodeVariables['RootNode'].add("B3[*]")
        st_model.NodeDerivedVariables['RootNode'].add("B4[*]")
        st_model.StageCost['Stage1'] = "FirstStageCost"
        st_model.StageCost['Stage2'] = "SecondStageCost"

        scenario_tree = ScenarioTree(scenariotreeinstance=st_model)
        self.assertEqual(len(scenario_tree.stages), 2)
        self.assertEqual(len(scenario_tree.nodes), 2)
        self.assertEqual(len(scenario_tree.scenarios), 1)

        model = self._get_block_model()

        root = scenario_tree.findRootNode()
        root_nonant_names = _get_names(_get_nonant_list(model, root))
        root_derived_nonant_names = _get_names(
            _get_derived_nonant_list(model, root))
        assert len(root_nonant_names) == 12
        assert len(root_derived_nonant_names) == 12

        for name in (
                "B1[1].x",
                "B1[2].x",
                "B3[1].x",
                "B3[2].x",
        ):
            assert name in root_nonant_names

        for name in (
                "B1[1].X",
                "B1[2].X",
                "B3[1].X",
                "B3[2].X",
        ):
            var = model.find_component(name)
            for vardata in var.values():
                assert vardata.name in root_nonant_names

        for name in (
                "B2[1].x",
                "B2[2].x",
                "B4[1].x",
                "B4[2].x",
        ):
            assert name in root_derived_nonant_names

        for name in (
                "B2[1].X",
                "B2[2].X",
                "B4[1].X",
                "B4[2].X",
        ):
            var = model.find_component(name)
            for vardata in var.values():
                assert vardata.name in root_derived_nonant_names
Example #4
0
    def test_indexedvar_noindextemplate(self):
        st_model = CreateConcreteTwoStageScenarioTreeModel(1)
        st_model.StageVariables['Stage1'].add("x")
        st_model.StageDerivedVariables['Stage1'].add("y")
        st_model.NodeVariables['RootNode'].add("z")
        st_model.NodeDerivedVariables['RootNode'].add("q")
        st_model.StageCost['Stage1'] = "FirstStageCost"
        st_model.StageCost['Stage2'] = "SecondStageCost"

        scenario_tree = ScenarioTree(scenariotreeinstance=st_model)
        self.assertEqual(len(scenario_tree.stages), 2)
        self.assertEqual(len(scenario_tree.nodes), 2)
        self.assertEqual(len(scenario_tree.scenarios), 1)

        model = ConcreteModel()
        model.s = Set(initialize=[1, 2, 3])
        model.x = Var(model.s)
        model.y = Var(model.s)
        model.z = Var(model.s)
        model.q = Var(model.s)
        model.FirstStageCost = Expression(expr=0.0)
        model.SecondStageCost = Expression(expr=0.0)
        model.obj = Objective(expr=0.0)

        root = scenario_tree.findRootNode()
        root_nonant_names = _get_names(_get_nonant_list(model, root))
        root_derived_nonant_names = _get_names(
            _get_derived_nonant_list(model, root))
        assert len(root_nonant_names) == 6
        assert len(root_derived_nonant_names) == 6

        for name in ("x", "z"):
            indexed_var = model.find_component(name)
            for index in model.s:
                var = indexed_var[index]
                assert var.name in root_nonant_names
        for name in ("y", "q"):
            indexed_var = model.find_component(name)
            for index in model.s:
                var = indexed_var[index]
                assert var.name in root_derived_nonant_names