本文整理汇总了Python中pyomo.environ.ConcreteModel.x方法的典型用法代码示例。如果您正苦于以下问题:Python ConcreteModel.x方法的具体用法?Python ConcreteModel.x怎么用?Python ConcreteModel.x使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyomo.environ.ConcreteModel
的用法示例。
在下文中一共展示了ConcreteModel.x方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_GDP_nonlinear_objective
# 需要导入模块: from pyomo.environ import ConcreteModel [as 别名]
# 或者: from pyomo.environ.ConcreteModel import x [as 别名]
def test_GDP_nonlinear_objective(self):
m = ConcreteModel()
m.x = Var(bounds=(-1, 10))
m.y = Var(bounds=(2, 3))
m.d = Disjunction(expr=[
[m.x + m.y >= 5], [m.x - m.y <= 3]
])
m.o = Objective(expr=m.x ** 2)
SolverFactory('gdpopt').solve(
m, strategy='LOA',
mip_solver=mip_solver,
nlp_solver=nlp_solver
)
self.assertAlmostEqual(value(m.o), 0)
m = ConcreteModel()
m.x = Var(bounds=(-1, 10))
m.y = Var(bounds=(2, 3))
m.d = Disjunction(expr=[
[m.x + m.y >= 5], [m.x - m.y <= 3]
])
m.o = Objective(expr=-m.x ** 2, sense=maximize)
SolverFactory('gdpopt').solve(
m, strategy='LOA',
mip_solver=mip_solver,
nlp_solver=nlp_solver
)
self.assertAlmostEqual(value(m.o), 0)
示例2: test_is_feasible_function
# 需要导入模块: from pyomo.environ import ConcreteModel [as 别名]
# 或者: from pyomo.environ.ConcreteModel import x [as 别名]
def test_is_feasible_function(self):
m = ConcreteModel()
m.x = Var(bounds=(0, 3), initialize=2)
m.c = Constraint(expr=m.x == 2)
self.assertTrue(is_feasible(m, GDPoptSolver.CONFIG()))
m.c2 = Constraint(expr=m.x <= 1)
self.assertFalse(is_feasible(m, GDPoptSolver.CONFIG()))
m = ConcreteModel()
m.x = Var(bounds=(0, 3), initialize=2)
m.c = Constraint(expr=m.x >= 5)
self.assertFalse(is_feasible(m, GDPoptSolver.CONFIG()))
m = ConcreteModel()
m.x = Var(bounds=(3, 3), initialize=2)
self.assertFalse(is_feasible(m, GDPoptSolver.CONFIG()))
m = ConcreteModel()
m.x = Var(bounds=(0, 1), initialize=2)
self.assertFalse(is_feasible(m, GDPoptSolver.CONFIG()))
m = ConcreteModel()
m.x = Var(bounds=(0, 1), initialize=2)
m.d = Disjunct()
with self.assertRaisesRegexp(NotImplementedError, "Found active disjunct"):
is_feasible(m, GDPoptSolver.CONFIG())
示例3: test_set_expr_undefined_univariate
# 需要导入模块: from pyomo.environ import ConcreteModel [as 别名]
# 或者: from pyomo.environ.ConcreteModel import x [as 别名]
def test_set_expr_undefined_univariate(self):
"""Test expr= option (univariate expression)"""
model = ConcreteModel()
model.x = Var()
ans = model.x >= 0
ans = ans <= 1
model.c = Constraint(expr=ans)
#self.assertRaises(ValueError, model.c)
self.assertEqual(model.c(),None)
model.x = 2
self.assertEqual(model.c(), 2)
self.assertEqual(value(model.c.body), 2)
示例4: test_simple_unsat_model
# 需要导入模块: from pyomo.environ import ConcreteModel [as 别名]
# 或者: from pyomo.environ.ConcreteModel import x [as 别名]
def test_simple_unsat_model(self):
m = ConcreteModel()
m.x = Var()
m.c1 = Constraint(expr=1 == m.x)
m.c2 = Constraint(expr=2 == m.x)
m.o = Objective(expr=m.x)
self.assertFalse(satisfiable(m))
示例5: test_induced_linearity_case2
# 需要导入模块: from pyomo.environ import ConcreteModel [as 别名]
# 或者: from pyomo.environ.ConcreteModel import x [as 别名]
def test_induced_linearity_case2(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.b = Var(bounds=(-2, 7))
m.c = Var()
m.bilinear = Constraint(
expr=(m.x[0] - 3) * (m.b + 2) - (m.c + 4) * m.b +
exp(m.b ** 2) * m.x[0] <= m.c)
TransformationFactory('contrib.induced_linearity').apply_to(m)
xfrmed_blk = m._induced_linearity_info.x0_b_bilinear
self.assertSetEqual(
set(xfrmed_blk.valid_values), set([1, 2, 3, 4, 5]))
select_one_repn = generate_standard_repn(
xfrmed_blk.select_one_value.body)
self.assertEqual(
ComponentSet(select_one_repn.linear_vars),
ComponentSet(xfrmed_blk.x_active[i] for i in xfrmed_blk.valid_values))
示例6: test_unique_component_name
# 需要导入模块: from pyomo.environ import ConcreteModel [as 别名]
# 或者: from pyomo.environ.ConcreteModel import x [as 别名]
def test_unique_component_name(self):
m = ConcreteModel()
m.x = 5
m.y = Var()
name = unique_component_name(m, 'z')
self.assertEqual(name, 'z')
name = unique_component_name(m, 'x')
self.assertEqual(len(name), 3)
self.assertEqual(name[:2], 'x_')
self.assertIn(name[2], '0123456789')
name = unique_component_name(m, 'y')
self.assertEqual(len(name), 3)
self.assertEqual(name[:2], 'y_')
self.assertIn(name[2], '0123456789')
name = unique_component_name(m, 'component')
self.assertEqual(len(name), 11)
self.assertEqual(name[:10], 'component_')
self.assertIn(name[10], '0123456789')
for i in range(10):
setattr(m, 'y_%s' % i, 0)
name = unique_component_name(m, 'y')
self.assertEqual(len(name), 4)
self.assertEqual(name[:2], 'y_')
self.assertIn(name[2], '0123456789')
self.assertIn(name[3], '0123456789')
示例7: test_linear
# 需要导入模块: from pyomo.environ import ConcreteModel [as 别名]
# 或者: from pyomo.environ.ConcreteModel import x [as 别名]
def test_linear(self):
m = ConcreteModel()
m.x = Var()
m.c = Constraint(expr=5*m.x == 10)
calculate_variable_from_constraint(m.x, m.c)
self.assertEqual(value(m.x), 2)
示例8: test_multiple_obj
# 需要导入模块: from pyomo.environ import ConcreteModel [as 别名]
# 或者: from pyomo.environ.ConcreteModel import x [as 别名]
def test_multiple_obj(self):
m = ConcreteModel()
m.x = Var()
m.o = Objective(expr=m.x)
m.o2 = Objective(expr=m.x)
with self.assertRaisesRegexp(RuntimeError, "multiple active objectives"):
SolverFactory('multistart').solve(m)
示例9: test_None_key
# 需要导入模块: from pyomo.environ import ConcreteModel [as 别名]
# 或者: from pyomo.environ.ConcreteModel import x [as 别名]
def test_None_key(self):
"""Test keys method"""
model = ConcreteModel()
model.x = Var()
model.c = Constraint(expr=model.x == 1)
self.assertEqual(list(model.c.keys()),[None])
self.assertEqual(id(model.c),id(model.c[None]))
示例10: test_multiple_objectives
# 需要导入模块: from pyomo.environ import ConcreteModel [as 别名]
# 或者: from pyomo.environ.ConcreteModel import x [as 别名]
def test_multiple_objectives(self):
m = ConcreteModel()
m.x = Var()
m.o = Objective(expr=m.x)
m.o2 = Objective(expr=m.x + 1)
with self.assertRaisesRegexp(ValueError, "Model has multiple active objectives"):
SolverFactory('gdpopt').solve(m)
示例11: test_reclassification
# 需要导入模块: from pyomo.environ import ConcreteModel [as 别名]
# 或者: from pyomo.environ.ConcreteModel import x [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())
示例12: test_bilinear_in_disjuncts
# 需要导入模块: from pyomo.environ import ConcreteModel [as 别名]
# 或者: from pyomo.environ.ConcreteModel import x [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)
示例13: build_model
# 需要导入模块: from pyomo.environ import ConcreteModel [as 别名]
# 或者: from pyomo.environ.ConcreteModel import x [as 别名]
def build_model(self):
m = ConcreteModel()
m.v1 = Var(initialize=1, bounds=(1, 8))
m.v2 = Var(initialize=2, bounds=(0, 3))
m.v3 = Var(initialize=3, bounds=(-7, 4))
m.v4 = Var(initialize=4, bounds=(2, 6))
m.c1 = Constraint(expr=m.v1 == m.v2)
m.c2 = Constraint(expr=m.v2 == m.v3)
m.c3 = Constraint(expr=m.v3 == m.v4)
m.v2.fix()
m.s = RangeSet(5)
m.x = Var(m.s, initialize=5)
m.c = Constraint(m.s)
m.c.add(1, expr=m.x[1] == m.x[3])
m.c.add(2, expr=m.x[2] == m.x[4])
m.c.add(3, expr=m.x[2] == m.x[3])
m.c.add(4, expr=m.x[1] == 1)
m.c.add(5, expr=(2, m.x[5], 3))
m.y = Var([1, 2], initialize={1: 3, 2: 4})
m.c_too = Constraint(expr=m.y[1] == m.y[2])
m.z1 = Var()
m.z2 = Var()
m.ignore_me = Constraint(expr=m.y[1] + m.z1 + m.z2 <= 0)
m.ignore_me_too = Constraint(expr=m.y[1] * m.y[2] == 0)
m.multiple = Constraint(expr=m.y[1] == 2 * m.y[2])
return m
示例14: test_solver_arg
# 需要导入模块: from pyomo.environ import ConcreteModel [as 别名]
# 或者: from pyomo.environ.ConcreteModel import x [as 别名]
def test_solver_arg(self):
m = ConcreteModel()
m.x = Var()
m.c = Constraint(expr=m.x == 2)
m.o = Objective(expr=m.x)
os = StringIO()
m.write(os, format="gams", io_options=dict(solver="gurobi"))
self.assertIn("option lp=gurobi", os.getvalue())
示例15: test_binary_expressions
# 需要导入模块: from pyomo.environ import ConcreteModel [as 别名]
# 或者: from pyomo.environ.ConcreteModel import x [as 别名]
def test_binary_expressions(self):
m = ConcreteModel()
m.x = Var()
m.y = Var()
m.z = Var()
m.c = Constraint(expr=0 <= m.x + m.y - m.z * m.y / m.x + 7)
m.o = Objective(expr=m.x)
self.assertTrue(satisfiable(m))