当前位置: 首页>>代码示例>>Python>>正文


Python ConcreteModel.v方法代码示例

本文整理汇总了Python中pyomo.environ.ConcreteModel.v方法的典型用法代码示例。如果您正苦于以下问题:Python ConcreteModel.v方法的具体用法?Python ConcreteModel.v怎么用?Python ConcreteModel.v使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在pyomo.environ.ConcreteModel的用法示例。


在下文中一共展示了ConcreteModel.v方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_reclassification

# 需要导入模块: from pyomo.environ import ConcreteModel [as 别名]
# 或者: from pyomo.environ.ConcreteModel import v [as 别名]
    def test_reclassification(self):
        m = ConcreteModel()
        m.t = ContinuousSet(bounds=(0, 1))
        m.x = ContinuousSet(bounds=(5, 10))
        m.s = Set(initialize=[1, 2, 3])
        m.v = Var(m.t)
        m.v2 = Var(m.s, m.t)
        m.v3 = Var(m.x, m.t)

        m.dv = DerivativeVar(m.v)
        m.dv2 = DerivativeVar(m.v2, wrt=(m.t, m.t))
        m.dv3 = DerivativeVar(m.v3, wrt=m.x)

        TransformationFactory('dae.finite_difference').apply_to(m, wrt=m.t)

        self.assertTrue(m.dv.type() is Var)
        self.assertTrue(m.dv2.type() is Var)
        self.assertTrue(m.dv.is_fully_discretized())
        self.assertTrue(m.dv2.is_fully_discretized())
        self.assertTrue(m.dv3.type() is DerivativeVar)
        self.assertFalse(m.dv3.is_fully_discretized())

        TransformationFactory('dae.collocation').apply_to(m, wrt=m.x)
        self.assertTrue(m.dv3.type() is Var)
        self.assertTrue(m.dv3.is_fully_discretized())
开发者ID:Pyomo,项目名称:pyomo,代码行数:27,代码来源:test_diffvar.py

示例2: test_bilinear_in_disjuncts

# 需要导入模块: from pyomo.environ import ConcreteModel [as 别名]
# 或者: from pyomo.environ.ConcreteModel import v [as 别名]
 def test_bilinear_in_disjuncts(self):
     m = ConcreteModel()
     m.x = Var([0], bounds=(-3, 8))
     m.y = Var(RangeSet(4), domain=Binary)
     m.z = Var(domain=Integers, bounds=(-1, 2))
     m.constr = Constraint(
         expr=m.x[0] == m.y[1] + 2 * m.y[2] + m.y[3] + 2 * m.y[4] + m.z)
     m.logical = ConstraintList()
     m.logical.add(expr=m.y[1] + m.y[2] == 1)
     m.logical.add(expr=m.y[3] + m.y[4] == 1)
     m.logical.add(expr=m.y[2] + m.y[4] <= 1)
     m.v = Var([1, 2])
     m.v[1].setlb(-2)
     m.v[1].setub(7)
     m.v[2].setlb(-4)
     m.v[2].setub(5)
     m.bilinear = Constraint(
         expr=(m.x[0] - 3) * (m.v[1] + 2) - (m.v[2] + 4) * m.v[1] +
         exp(m.v[1] ** 2) * m.x[0] <= m.v[2])
     m.disjctn = Disjunction(expr=[
         [m.x[0] * m.v[1] <= 4],
         [m.x[0] * m.v[2] >= 6]
     ])
     TransformationFactory('contrib.induced_linearity').apply_to(m)
     self.assertEqual(
         m.disjctn.disjuncts[0].constraint[1].body.polynomial_degree(), 1)
     self.assertEqual(
         m.disjctn.disjuncts[1].constraint[1].body.polynomial_degree(), 1)
开发者ID:Pyomo,项目名称:pyomo,代码行数:30,代码来源:test_induced_linearity.py

示例3: test_invalid_derivative

# 需要导入模块: from pyomo.environ import ConcreteModel [as 别名]
# 或者: from pyomo.environ.ConcreteModel import v [as 别名]
    def test_invalid_derivative(self):
        m = ConcreteModel()
        m.t = ContinuousSet(bounds=(0, 10))
        m.v = Var(m.t)
        m.dv = DerivativeVar(m.v, wrt=(m.t, m.t, m.t))

        with self.assertRaises(DAE_Error):
            TransformationFactory('dae.finite_difference').apply_to(m)
开发者ID:Pyomo,项目名称:pyomo,代码行数:10,代码来源:test_finite_diff.py

示例4: test_fixed_var_out_of_bounds_ub

# 需要导入模块: from pyomo.environ import ConcreteModel [as 别名]
# 或者: from pyomo.environ.ConcreteModel import v [as 别名]
 def test_fixed_var_out_of_bounds_ub(self):
     m = ConcreteModel()
     m.s = RangeSet(2)
     m.v = Var(m.s, bounds=(0, 5))
     m.c = ConstraintList()
     m.c.add(expr=m.v[1] == m.v[2])
     m.c.add(expr=m.v[1] == 6)
     with self.assertRaises(ValueError):
         TransformationFactory('contrib.aggregate_vars').apply_to(m)
开发者ID:Pyomo,项目名称:pyomo,代码行数:11,代码来源:test_var_aggregator.py

示例5: test_initialized_continuous_set

# 需要导入模块: from pyomo.environ import ConcreteModel [as 别名]
# 或者: from pyomo.environ.ConcreteModel import v [as 别名]
    def test_initialized_continuous_set(self):
        m = ConcreteModel()
        m.t = ContinuousSet(initialize=[0, 1, 2, 3, 4])
        m.v = Var(m.t)
        m.dv = DerivativeVar(m.v)

        log_out = StringIO()
        with LoggingIntercept(log_out, 'pyomo.dae'):
            TransformationFactory('dae.finite_difference').apply_to(m, nfe=2)
        self.assertIn('More finite elements', log_out.getvalue())
开发者ID:Pyomo,项目名称:pyomo,代码行数:12,代码来源:test_finite_diff.py

示例6: test_reclassification_collocation

# 需要导入模块: from pyomo.environ import ConcreteModel [as 别名]
# 或者: from pyomo.environ.ConcreteModel import v [as 别名]
    def test_reclassification_collocation(self):
        m = ConcreteModel()
        m.t = ContinuousSet(bounds=(0, 1))
        m.x = ContinuousSet(bounds=(5, 10))
        m.s = Set(initialize=[1, 2, 3])
        m.v = Var(m.t)
        m.v2 = Var(m.s, m.t)
        m.v3 = Var(m.t, m.x)

        def _int1(m, t):
            return m.v[t]

        m.int1 = Integral(m.t, rule=_int1)

        def _int2(m, s, t):
            return m.v2[s, t]

        m.int2 = Integral(m.s, m.t, wrt=m.t, rule=_int2)

        def _int3(m, t, x):
            return m.v3[t, x]

        m.int3 = Integral(m.t, m.x, wrt=m.t, rule=_int3)

        def _int4(m, x):
            return m.int3[x]

        m.int4 = Integral(m.x, wrt=m.x, rule=_int4)

        self.assertFalse(m.int1.is_fully_discretized())
        self.assertFalse(m.int2.is_fully_discretized())
        self.assertFalse(m.int3.is_fully_discretized())
        self.assertFalse(m.int4.is_fully_discretized())

        TransformationFactory('dae.collocation').apply_to(m, wrt=m.t)

        self.assertTrue(m.int1.is_fully_discretized())
        self.assertTrue(m.int2.is_fully_discretized())
        self.assertFalse(m.int3.is_fully_discretized())
        self.assertFalse(m.int4.is_fully_discretized())

        self.assertTrue(m.int1.type() is Integral)
        self.assertTrue(m.int2.type() is Integral)
        self.assertTrue(m.int3.type() is Integral)
        self.assertTrue(m.int4.type() is Integral)

        TransformationFactory('dae.collocation').apply_to(m, wrt=m.x)

        self.assertTrue(m.int3.is_fully_discretized())
        self.assertTrue(m.int4.is_fully_discretized())

        self.assertTrue(m.int1.type() is Expression)
        self.assertTrue(m.int2.type() is Expression)
        self.assertTrue(m.int3.type() is Expression)
        self.assertTrue(m.int4.type() is Expression)
开发者ID:Pyomo,项目名称:pyomo,代码行数:57,代码来源:test_integral.py

示例7: test_disc_second_order_1cp

# 需要导入模块: from pyomo.environ import ConcreteModel [as 别名]
# 或者: from pyomo.environ.ConcreteModel import v [as 别名]
    def test_disc_second_order_1cp(self):
        m = ConcreteModel()
        m.t = ContinuousSet(bounds=(0,1))
        m.t2 = ContinuousSet(bounds=(0,10))
        m.v = Var(m.t, m.t2)
        m.dv = DerivativeVar(m.v, wrt=(m.t, m.t2))
        TransformationFactory('dae.collocation').apply_to(m, nfe=2, ncp=1)

        self.assertTrue(hasattr(m, 'dv_disc_eq'))
        self.assertTrue(len(m.dv_disc_eq) == 4)
        self.assertTrue(len(m.v) == 9)                        
开发者ID:Pyomo,项目名称:pyomo,代码行数:13,代码来源:test_colloc.py

示例8: test_invalid

# 需要导入模块: from pyomo.environ import ConcreteModel [as 别名]
# 或者: from pyomo.environ.ConcreteModel import v [as 别名]
    def test_invalid(self):
        m = ConcreteModel()
        m.t = ContinuousSet(bounds=(0, 1))
        m.x = ContinuousSet(bounds=(5, 10))
        m.s = Set(initialize=[1, 2, 3])
        m.v = Var(m.t)
        m.v2 = Var(m.s, m.t)
        m.v3 = Var(m.x, m.t)
        m.y = Var()

        # Not passing a Var as the first positional argument
        with self.assertRaises(DAE_Error):
            m.ds = DerivativeVar(m.s)

        # Specifying both option aliases
        with self.assertRaises(TypeError):
            m.dv = DerivativeVar(m.v, wrt=m.t, withrespectto=m.t)

        # Passing in Var not indexed by a ContinuousSet
        with self.assertRaises(DAE_Error):
            m.dy = DerivativeVar(m.y)

        # Not specifying 'wrt' when Var indexed by multiple ContinuousSets
        with self.assertRaises(DAE_Error):
            m.dv3 = DerivativeVar(m.v3)

        # 'wrt' is not a ContinuousSet
        with self.assertRaises(DAE_Error):
            m.dv2 = DerivativeVar(m.v2, wrt=m.s)

        with self.assertRaises(DAE_Error):
            m.dv2 = DerivativeVar(m.v2, wrt=(m.t, m.s))

        # Specified ContinuousSet does not index the Var
        with self.assertRaises(DAE_Error):
            m.dv = DerivativeVar(m.v, wrt=m.x)

        with self.assertRaises(DAE_Error):
            m.dv2 = DerivativeVar(m.v2, wrt=[m.t, m.x])

        # Declaring the same derivative twice
        m.dvdt = DerivativeVar(m.v)
        with self.assertRaises(DAE_Error):
            m.dvdt2 = DerivativeVar(m.v)

        m.dv2dt = DerivativeVar(m.v2, wrt=m.t)
        with self.assertRaises(DAE_Error):
            m.dv2dt2 = DerivativeVar(m.v2, wrt=m.t)

        m.dv3 = DerivativeVar(m.v3, wrt=(m.x, m.x))
        with self.assertRaises(DAE_Error):
            m.dv4 = DerivativeVar(m.v3, wrt=(m.x, m.x))
开发者ID:Pyomo,项目名称:pyomo,代码行数:54,代码来源:test_diffvar.py

示例9: test_induced_linear_in_disjunct

# 需要导入模块: from pyomo.environ import ConcreteModel [as 别名]
# 或者: from pyomo.environ.ConcreteModel import v [as 别名]
 def test_induced_linear_in_disjunct(self):
     m = ConcreteModel()
     m.x = Var([0], bounds=(-3, 8))
     m.y = Var(RangeSet(2), domain=Binary)
     m.logical = ConstraintList()
     m.logical.add(expr=m.y[1] + m.y[2] == 1)
     m.v = Var([1])
     m.v[1].setlb(-2)
     m.v[1].setub(7)
     m.bilinear_outside = Constraint(
         expr=m.x[0] * m.v[1] >= 2)
     m.disjctn = Disjunction(expr=[
         [m.x[0] * m.v[1] == 3,
          2 * m.x[0] == m.y[1] + m.y[2]],
         [m.x[0] * m.v[1] == 4]
     ])
     TransformationFactory('contrib.induced_linearity').apply_to(m)
     self.assertEqual(
         m.disjctn.disjuncts[0].constraint[1].body.polynomial_degree(), 1)
     self.assertEqual(
         m.bilinear_outside.body.polynomial_degree(), 2)
     self.assertEqual(
         m.disjctn.disjuncts[1].constraint[1].body.polynomial_degree(), 2)
开发者ID:Pyomo,项目名称:pyomo,代码行数:25,代码来源:test_induced_linearity.py

示例10: test_valid

# 需要导入模块: from pyomo.environ import ConcreteModel [as 别名]
# 或者: from pyomo.environ.ConcreteModel import v [as 别名]
    def test_valid(self):
        m = ConcreteModel()
        m.t = ContinuousSet(bounds=(0, 1))
        m.x = ContinuousSet(bounds=(5, 10))
        m.s = Set()
        m.v = Var(m.t)
        m.dv = DerivativeVar(m.v)
        m.dv2 = DerivativeVar(m.v, wrt=(m.t, m.t))

        self.assertTrue(isinstance(m.dv, Var))
        self.assertTrue(isinstance(m.dv, DerivativeVar))
        self.assertTrue(m.dv._wrt[0] is m.t)
        self.assertTrue(m.dv._sVar is m.v)
        self.assertTrue(m.v._derivative[('t',)]() is m.dv)
        self.assertTrue(m.dv.type() is DerivativeVar)
        self.assertTrue(m.dv._index is m.t)
        self.assertTrue(m.dv2._wrt[0] is m.t)
        self.assertTrue(m.dv2._wrt[1] is m.t)
        self.assertTrue(m.v._derivative[('t', 't')]() is m.dv2)
        self.assertTrue(m.dv.get_state_var() is m.v)
        self.assertTrue(m.dv2.get_state_var() is m.v)
        del m.dv
        del m.dv2
        del m.v

        m.v = Var(m.s, m.t)
        m.dv = DerivativeVar(m.v)
        m.dv2 = DerivativeVar(m.v, wrt=(m.t, m.t))
        self.assertTrue(isinstance(m.dv, Var))
        self.assertTrue(isinstance(m.dv, DerivativeVar))
        self.assertTrue(m.dv._wrt[0] is m.t)
        self.assertTrue(m.dv._sVar is m.v)
        self.assertTrue(m.v._derivative[('t',)]() is m.dv)
        self.assertTrue(m.dv.type() is DerivativeVar)
        self.assertTrue(m.t in m.dv._implicit_subsets)
        self.assertTrue(m.s in m.dv._implicit_subsets)
        self.assertTrue(m.dv2._wrt[0] is m.t)
        self.assertTrue(m.dv2._wrt[1] is m.t)
        self.assertTrue(m.v._derivative[('t', 't')]() is m.dv2)
        del m.dv
        del m.dv2
        del m.v
        del m.v_index
        del m.dv_index
        del m.dv2_index

        m.v = Var(m.x, m.t)
        m.dv = DerivativeVar(m.v, wrt=m.x)
        m.dv2 = DerivativeVar(m.v, wrt=m.t)
        m.dv3 = DerivativeVar(m.v, wrt=(m.t, m.x))
        m.dv4 = DerivativeVar(m.v, wrt=[m.t, m.t])
        self.assertTrue(isinstance(m.dv, Var))
        self.assertTrue(isinstance(m.dv, DerivativeVar))
        self.assertTrue(m.dv._wrt[0] is m.x)
        self.assertTrue(m.dv._sVar is m.v)
        self.assertTrue(m.v._derivative[('x',)]() is m.dv)
        self.assertTrue(m.v._derivative[('t',)]() is m.dv2)
        self.assertTrue(m.v._derivative[('t', 'x')]() is m.dv3)
        self.assertTrue(m.v._derivative[('t', 't')]() is m.dv4)
        self.assertTrue(m.dv.type() is DerivativeVar)
        self.assertTrue(m.x in m.dv._implicit_subsets)
        self.assertTrue(m.t in m.dv._implicit_subsets)
        self.assertTrue(m.dv3._wrt[0] is m.t)
        self.assertTrue(m.dv3._wrt[1] is m.x)
        self.assertTrue(m.dv4._wrt[0] is m.t)
        self.assertTrue(m.dv4._wrt[1] is m.t)
开发者ID:Pyomo,项目名称:pyomo,代码行数:68,代码来源:test_diffvar.py

示例11: test_invalid

# 需要导入模块: from pyomo.environ import ConcreteModel [as 别名]
# 或者: from pyomo.environ.ConcreteModel import v [as 别名]
    def test_invalid(self):
        m = ConcreteModel()
        m.t = ContinuousSet(bounds=(0, 1))
        m.x = ContinuousSet(bounds=(5, 10))
        m.s = Set(initialize=[1, 2, 3])
        m.v = Var(m.t)
        m.v2 = Var(m.s, m.t)
        m.v3 = Var(m.x, m.t)

        def _int(m, t):
            return m.v[t]

        def _int2(m, x, t):
            return m.v3[x, t]

        def _int3(m, s, t):
            return m.v2[s,t]

        # Integrals must be indexed by a ContinuousSet
        with self.assertRaises(ValueError):
            m.int = Integral(rule=_int)

        # Specifying multiple aliases of same option
        with self.assertRaises(TypeError):
            m.int = Integral(m.t, wrt=m.t, withrespectto=m.t, rule=_int)

        # No ContinuousSet specified
        with self.assertRaises(ValueError):
            m.int2 = Integral(m.x, m.t, rule= _int2)

        # 'wrt' is not a ContinuousSet
        with self.assertRaises(ValueError):
            m.int = Integral(m.s, m.t, wrt=m.s, rule=_int2)

        # 'wrt' is not in argument list
        with self.assertRaises(ValueError):
            m.int = Integral(m.t, wrt=m.x, rule=_int)

        # 'bounds' not supported
        with self.assertRaises(DAE_Error):
            m.int = Integral(m.t, wrt=m.t, rule=_int, bounds=(0,0.5))

        # No rule specified
        with self.assertRaises(ValueError):
            m.int = Integral(m.t, wrt=m.t)

            # test DerivativeVar reclassification after discretization

        def test_reclassification_finite_difference(self):
            m = ConcreteModel()
            m.t = ContinuousSet(bounds=(0, 1))
            m.x = ContinuousSet(bounds=(5, 10))
            m.s = Set(initialize=[1, 2, 3])
            m.v = Var(m.t)
            m.v2 = Var(m.s, m.t)
            m.v3 = Var(m.t, m.x)

            def _int1(m, t):
                return m.v[t]

            m.int1 = Integral(m.t, rule=_int1)

            def _int2(m, s, t):
                return m.v2[s, t]

            m.int2 = Integral(m.s, m.t, wrt=m.t, rule=_int2)

            def _int3(m, t, x):
                return m.v3[t, x]

            m.int3 = Integral(m.t, m.x, wrt=m.t, rule=_int3)

            def _int4(m, x):
                return m.int3[x]

            m.int4 = Integral(m.x, wrt=m.x, rule=_int4)

            self.assertFalse(m.int1.is_fully_discretized())
            self.assertFalse(m.int2.is_fully_discretized())
            self.assertFalse(m.int3.is_fully_discretized())
            self.assertFalse(m.int4.is_fully_discretized())

            TransformationFactory('dae.finite_difference').apply_to(m, wrt=m.t)

            self.assertTrue(m.int1.is_fully_discretized())
            self.assertTrue(m.int2.is_fully_discretized())
            self.assertFalse(m.int3.is_fully_discretized())
            self.assertFalse(m.int4.is_fully_discretized())

            self.assertTrue(m.int1.type() is Integral)
            self.assertTrue(m.int2.type() is Integral)
            self.assertTrue(m.int3.type() is Integral)
            self.assertTrue(m.int4.type() is Integral)

            TransformationFactory('dae.finite_difference').apply_to(m, wrt=m.x)

            self.assertTrue(m.int3.is_fully_discretized())
            self.assertTrue(m.int4.is_fully_discretized())

            self.assertTrue(m.int1.type() is Expression)
#.........这里部分代码省略.........
开发者ID:Pyomo,项目名称:pyomo,代码行数:103,代码来源:test_integral.py

示例12: test_valid

# 需要导入模块: from pyomo.environ import ConcreteModel [as 别名]
# 或者: from pyomo.environ.ConcreteModel import v [as 别名]
    def test_valid(self):
        m = ConcreteModel()
        m.t = ContinuousSet(bounds=(0, 1))
        m.x = ContinuousSet(bounds=(5, 10))
        m.s = Set(initialize=[1, 2, 3])
        m.v = Var(m.t)
        m.v2 = Var(m.s, m.t)
        m.v3 = Var(m.t, m.x)

        def _int1(m, t):
            return m.v[t]

        m.int1 = Integral(m.t, rule=_int1)

        def _int2(m, s, t):
            return m.v2[s, t]

        m.int2 = Integral(m.s, m.t, wrt=m.t, rule=_int2)

        def _int3(m, t, x):
            return m.v3[t, x]

        m.int3 = Integral(m.t, m.x, wrt=m.t, rule=_int3)

        def _int4(m, x):
            return m.int3[x]

        m.int4 = Integral(m.x, wrt=m.x, rule=_int4)

        self.assertTrue(isinstance(m.int1, Expression))
        self.assertTrue(isinstance(m.int2, Expression))
        self.assertTrue(isinstance(m.int3, Expression))
        self.assertTrue(isinstance(m.int4, Expression))
        self.assertTrue(m.int1.get_continuousset() is m.t)
        self.assertTrue(m.int2.get_continuousset() is m.t)
        self.assertTrue(m.int3.get_continuousset() is m.t)
        self.assertTrue(m.int4.get_continuousset() is m.x)
        self.assertEqual(len(m.int1), 1)
        self.assertEqual(len(m.int2), 3)
        self.assertEqual(len(m.int3), 2)
        self.assertEqual(len(m.int4), 1)
        self.assertTrue(m.int1.type() is Integral)
        self.assertTrue(m.int2.type() is Integral)
        self.assertTrue(m.int3.type() is Integral)
        self.assertTrue(m.int4.type() is Integral)

        repn = generate_standard_repn(m.int1.expr)
        self.assertEqual(repn.linear_coefs, (0.5, 0.5))
        self.assertTrue(repn.linear_vars[0] is m.v[1])
        self.assertTrue(repn.linear_vars[1] is m.v[0])

        repn = generate_standard_repn(m.int2[1].expr)
        self.assertEqual(repn.linear_coefs, (0.5, 0.5))
        self.assertTrue(repn.linear_vars[0] is m.v2[1, 1])
        self.assertTrue(repn.linear_vars[1] is m.v2[1, 0])

        repn = generate_standard_repn(m.int4.expr)
        self.assertEqual(repn.linear_coefs, (1.25, 1.25, 1.25, 1.25))
        self.assertTrue(repn.linear_vars[0] is m.v3[1, 10])
        self.assertTrue(repn.linear_vars[1] is m.v3[0, 10])
        self.assertTrue(repn.linear_vars[2] is m.v3[1, 5])
        self.assertTrue(repn.linear_vars[3] is m.v3[0, 5])
开发者ID:Pyomo,项目名称:pyomo,代码行数:64,代码来源:test_integral.py


注:本文中的pyomo.environ.ConcreteModel.v方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。