本文整理汇总了Python中pyomo.environ.ConcreteModel.int2方法的典型用法代码示例。如果您正苦于以下问题:Python ConcreteModel.int2方法的具体用法?Python ConcreteModel.int2怎么用?Python ConcreteModel.int2使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyomo.environ.ConcreteModel
的用法示例。
在下文中一共展示了ConcreteModel.int2方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_reclassification_collocation
# 需要导入模块: from pyomo.environ import ConcreteModel [as 别名]
# 或者: from pyomo.environ.ConcreteModel import int2 [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)
示例2: test_invalid
# 需要导入模块: from pyomo.environ import ConcreteModel [as 别名]
# 或者: from pyomo.environ.ConcreteModel import int2 [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)
#.........这里部分代码省略.........
示例3: test_valid
# 需要导入模块: from pyomo.environ import ConcreteModel [as 别名]
# 或者: from pyomo.environ.ConcreteModel import int2 [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])