Пример #1
0
    def test_multidim_nested_sum_rule(self):
        m = ConcreteModel()
        m.I = RangeSet(3)
        m.J = RangeSet(3)
        m.JI = m.J * m.I
        m.K = Set(m.I, initialize={1: [10], 2: [10, 20], 3: [10, 20, 30]})
        m.x = Var(m.I, m.J, [10, 20, 30])

        @m.Constraint()
        def c(m):
            return sum(sum(m.x[i, j, k] for k in m.K[i]) for j, i in m.JI) <= 0

        template, indices = templatize_constraint(m.c)
        self.assertEqual(len(indices), 0)
        self.assertEqual(
            template.to_string(verbose=True), "templatesum("
            "templatesum(getitem(x, _2, _1, _3), iter(_3, getitem(K, _2))), "
            "iter(_1, _2, JI))  <=  0")
        self.assertEqual(
            str(template), "SUM(SUM(x[_2,_1,_3] for _3 in K[_2]) "
            "for _1, _2 in JI)  <=  0")
        # Evaluate the template
        self.assertEqual(
            str(resolve_template(template)), 'x[1,1,10] + '
            '(x[2,1,10] + x[2,1,20]) + '
            '(x[3,1,10] + x[3,1,20] + x[3,1,30]) + '
            '(x[1,2,10]) + '
            '(x[2,2,10] + x[2,2,20]) + '
            '(x[3,2,10] + x[3,2,20] + x[3,2,30]) + '
            '(x[1,3,10]) + '
            '(x[2,3,10] + x[2,3,20]) + '
            '(x[3,3,10] + x[3,3,20] + x[3,3,30])  <=  0')
Пример #2
0
    def test_simple_sum_rule(self):
        m = ConcreteModel()
        m.I = RangeSet(3)
        m.J = RangeSet(3)
        m.x = Var(m.I,m.J)
        @m.Constraint(m.I)
        def c(m, i):
            return sum(m.x[i,j] for j in m.J) <= 0

        template, indices = templatize_constraint(m.c)
        self.assertEqual(len(indices), 1)
        self.assertIs(indices[0]._set, m.I)
        self.assertEqual(
            template.to_string(verbose=True),
            "templatesum(getitem(x, _1, _2), iter(_2, J))  <=  0.0"
        )
        self.assertEqual(
            str(template),
            "SUM(x[_1,_2] for _2 in J)  <=  0.0"
        )
        # Evaluate the template
        indices[0].set_value(2)
        self.assertEqual(
            str(resolve_template(template)),
            'x[2,1] + x[2,2] + x[2,3]  <=  0.0'
        )
Пример #3
0
    def test_simple_abstract_rule(self):
        m = AbstractModel()
        m.I = RangeSet(3)
        m.x = Var(m.I)

        @m.Constraint(m.I)
        def c(m, i):
            return m.x[i] <= 0

        # Note: the constraint can be abstract, but the Set/Var must
        # have been constructed (otherwise accessing the Set raises an
        # exception)

        with self.assertRaisesRegex(ValueError, ".*has not been constructed"):
            template, indices = templatize_constraint(m.c)

        m.I.construct()
        m.x.construct()
        template, indices = templatize_constraint(m.c)
        self.assertEqual(len(indices), 1)
        self.assertIs(indices[0]._set, m.I)
        self.assertEqual(str(template), "x[_1]  <=  0")
Пример #4
0
    def test_simple_rule_nonfinite_set(self):
        m = ConcreteModel()
        m.x = Var(Integers, dense=False)

        @m.Constraint(Integers)
        def c(m, i):
            return m.x[i] <= 0

        template, indices = templatize_constraint(m.c)
        self.assertEqual(len(indices), 1)
        self.assertIs(indices[0]._set, Integers)
        self.assertEqual(str(template), "x[_1]  <=  0")
        # Evaluate the template
        indices[0].set_value(2)
        self.assertEqual(str(resolve_template(template)), 'x[2]  <=  0')
Пример #5
0
    def test_simple_rule(self):
        m = ConcreteModel()
        m.I = RangeSet(3)
        m.x = Var(m.I)
        @m.Constraint(m.I)
        def c(m, i):
            return m.x[i] <= 0

        template, indices = templatize_constraint(m.c)
        self.assertEqual(len(indices), 1)
        self.assertIs(indices[0]._set, m.I)
        self.assertEqual(str(template), "x[_1]  <=  0.0")
        # Test that the RangeSet iterator was put back
        self.assertEqual(list(m.I), list(range(1,4)))
        # Evaluate the template
        indices[0].set_value(2)
        self.assertEqual(str(resolve_template(template)), 'x[2]  <=  0.0')