本文整理汇总了Python中pyomo.core.ConcreteModel类的典型用法代码示例。如果您正苦于以下问题:Python ConcreteModel类的具体用法?Python ConcreteModel怎么用?Python ConcreteModel使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ConcreteModel类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: test_reciprocal
def test_reciprocal(self):
m = ConcreteModel()
m.x = Var(bounds=(1, 2), initialize=1)
m.y = Var(bounds=(2, 3), initialize=2)
mc_expr = mc(m.x / m.y)
self.assertEqual(mc_expr.lower(), 1 / 3)
self.assertEqual(mc_expr.upper(), 1)
开发者ID:mskarha,项目名称:pyomo,代码行数:7,代码来源:test_mcpp.py
示例2: test_var
def test_var(self):
m = ConcreteModel()
m.x = Var(bounds=(-1, 1), initialize=3)
mc_var = mc(m.x)
self.assertEqual(mc_var.lower(), -1)
self.assertEqual(mc_var.upper(), 1)
m.no_ub = Var(bounds=(0, None), initialize=3)
output = StringIO()
with LoggingIntercept(output, 'pyomo.contrib.mcpp', logging.WARNING):
mc_var = mc(m.no_ub)
self.assertIn("Var no_ub missing upper bound.",
output.getvalue().strip())
self.assertEqual(mc_var.lower(), 0)
self.assertEqual(mc_var.upper(), 500000)
m.no_lb = Var(bounds=(None, -3), initialize=-1)
output = StringIO()
with LoggingIntercept(output, 'pyomo.contrib.mcpp', logging.WARNING):
mc_var = mc(m.no_lb)
self.assertIn("Var no_lb missing lower bound.",
output.getvalue().strip())
self.assertEqual(mc_var.lower(), -500000)
self.assertEqual(mc_var.upper(), -3)
m.no_val = Var(bounds=(0, 1))
output = StringIO()
with LoggingIntercept(output, 'pyomo.contrib.mcpp', logging.WARNING):
mc_var = mc(m.no_val)
mc_var.subcv()
self.assertIn("Var no_val missing value.",
output.getvalue().strip())
self.assertEqual(mc_var.lower(), 0)
self.assertEqual(mc_var.upper(), 1)
开发者ID:mskarha,项目名称:pyomo,代码行数:31,代码来源:test_mcpp.py
示例3: test_linear_expression
def test_linear_expression(self):
m = ConcreteModel()
m.x = Var(bounds=(1, 2), initialize=1)
with self.assertRaises(NotImplementedError):
mc_expr = mc(quicksum([m.x, m.x], linear=True))
self.assertEqual(mc_expr.lower(), 2)
self.assertEqual(mc_expr.upper(), 4)
开发者ID:mskarha,项目名称:pyomo,代码行数:7,代码来源:test_mcpp.py
示例4: test_clone_without_expression_components
def test_clone_without_expression_components(self):
m = ConcreteModel()
m.x = Var(initialize=5)
m.y = Var(initialize=3)
m.e = Expression(expr=m.x**2 + m.x - 1)
base = m.x**2 + 1
test = clone_without_expression_components(base, {})
self.assertIs(base, test)
self.assertEqual(base(), test())
test = clone_without_expression_components(base, {id(m.x): m.y})
self.assertEqual(3**2+1, test())
base = m.e
test = clone_without_expression_components(base, {})
self.assertIsNot(base, test)
self.assertEqual(base(), test())
self.assertIsInstance(base, _ExpressionData)
self.assertIsInstance(test, EXPR.SumExpression)
test = clone_without_expression_components(base, {id(m.x): m.y})
self.assertEqual(3**2+3-1, test())
base = m.e + m.x
test = clone_without_expression_components(base, {})
self.assertIsNot(base, test)
self.assertEqual(base(), test())
self.assertIsInstance(base, EXPR.SumExpression)
self.assertIsInstance(test, EXPR.SumExpression)
self.assertIsInstance(base.arg(0), _ExpressionData)
self.assertIsInstance(test.arg(0), EXPR.SumExpression)
test = clone_without_expression_components(base, {id(m.x): m.y})
self.assertEqual(3**2+3-1 + 3, test())
开发者ID:Pyomo,项目名称:pyomo,代码行数:32,代码来源:test_util.py
示例5: test_active_parent_block
def test_active_parent_block(self):
m = ConcreteModel()
m.d1 = Block()
m.d1.sub1 = Disjunct()
m.d1.sub2 = Disjunct()
m.d1.disj = Disjunction(expr=[m.d1.sub1, m.d1.sub2])
with self.assertRaises(GDP_Error):
TransformationFactory('gdp.reclassify').apply_to(m)
开发者ID:Pyomo,项目名称:pyomo,代码行数:8,代码来源:test_reclassify.py
示例6: test_lmtd
def test_lmtd(self):
m = ConcreteModel()
m.x = Var(bounds=(0.1, 500), initialize=33.327)
m.y = Var(bounds=(0.1, 500), initialize=14.436)
m.z = Var(bounds=(0, 90), initialize=22.5653)
mc_expr = mc(m.z - (m.x * m.y * (m.x + m.y) / 2) ** (1/3))
self.assertAlmostEqual(mc_expr.convex(), -407.95444629965016)
self.assertAlmostEqual(mc_expr.lower(), -499.99999999999983)
开发者ID:mskarha,项目名称:pyomo,代码行数:8,代码来源:test_mcpp.py
示例7: test_improved_bounds
def test_improved_bounds(self):
m = ConcreteModel()
m.x = Var(bounds=(0, 100), initialize=5)
improved_bounds = ComponentMap()
improved_bounds[m.x] = (10, 20)
mc_expr = mc(m.x, improved_var_bounds=improved_bounds)
self.assertEqual(mc_expr.lower(), 10)
self.assertEqual(mc_expr.upper(), 20)
开发者ID:mskarha,项目名称:pyomo,代码行数:8,代码来源:test_mcpp.py
示例8: test_fixed_var
def test_fixed_var(self):
m = ConcreteModel()
m.x = Var(bounds=(-50, 80), initialize=3)
m.y = Var(bounds=(0, 6), initialize=2)
m.y.fix()
mc_expr = mc(m.x * m.y)
self.assertEqual(mc_expr.lower(), -100)
self.assertEqual(mc_expr.upper(), 160)
开发者ID:Pyomo,项目名称:pyomo,代码行数:8,代码来源:test_mcpp.py
示例9: makeDisjunctWithRangeSet
def makeDisjunctWithRangeSet():
m = ConcreteModel()
m.x = Var(bounds=(0, 1))
m.d1 = Disjunct()
m.d1.s = RangeSet(1)
m.d1.c = Constraint(rule=lambda _: m.x == 1)
m.d2 = Disjunct()
m.disj = Disjunction(expr=[m.d1, m.d2])
return m
开发者ID:Pyomo,项目名称:pyomo,代码行数:9,代码来源:models.py
示例10: test_mc_2d
def test_mc_2d(self):
m = ConcreteModel()
m.x = Var(bounds=(pi / 6, pi / 3), initialize=pi / 4)
m.e = Expression(expr=cos(pow(m.x, 2)) * sin(pow(m.x, -3)))
mc_ccVals, mc_cvVals, aff_cc, aff_cv = make2dPlot(m.e.expr, 50)
self.assertAlmostEqual(mc_ccVals[1], 0.6443888590411435)
self.assertAlmostEqual(mc_cvVals[1], 0.2328315489072924)
self.assertAlmostEqual(aff_cc[1], 0.9674274332870583)
self.assertAlmostEqual(aff_cv[1], -1.578938503009686)
开发者ID:mskarha,项目名称:pyomo,代码行数:9,代码来源:test_mcpp.py
示例11: test_mc_3d
def test_mc_3d(self):
m = ConcreteModel()
m.x = Var(bounds=(-2, 1), initialize=-1)
m.y = Var(bounds=(-1, 2), initialize=0)
m.e = Expression(expr=m.x * pow(exp(m.x) - m.y, 2))
ccSurf, cvSurf, ccAffine, cvAffine = make3dPlot(m.e.expr, 30)
self.assertAlmostEqual(ccSurf[48], 11.5655473482574)
self.assertAlmostEqual(cvSurf[48], -15.28102124928224)
self.assertAlmostEqual(ccAffine[48], 11.565547348257398)
self.assertAlmostEqual(cvAffine[48], -23.131094696514797)
开发者ID:mskarha,项目名称:pyomo,代码行数:10,代码来源:test_mcpp.py
示例12: test_indexedvar_noindextemplate
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)
scenario_tree.linkInInstances({'Scenario1': model})
root = scenario_tree.findRootNode()
self.assertEqual(len(root._variable_ids), 12)
self.assertEqual(len(root._standard_variable_ids), 6)
self.assertEqual(len(root._derived_variable_ids), 6)
for name in ("x", "y", "z", "q"):
for index in model.s:
self.assertEqual(
(name,index) in root._name_index_to_id, True)
开发者ID:SemanticBeeng,项目名称:pyomo,代码行数:34,代码来源:test_scenariotree.py
示例13: test_fixed_var
def test_fixed_var(self):
m = ConcreteModel()
m.x = Var(bounds=(-50, 80), initialize=3)
m.y = Var(bounds=(0, 6), initialize=2)
m.y.fix()
mc_expr = mc(m.x * m.y)
self.assertEqual(mc_expr.lower(), -100)
self.assertEqual(mc_expr.upper(), 160)
self.assertEqual(
str(mc_expr),
"[ -1.00000e+02 : 1.60000e+02 ] [ 6.00000e+00 : 6.00000e+00 ] [ ( 2.00000e+00) : ( 2.00000e+00) ]")
开发者ID:mskarha,项目名称:pyomo,代码行数:11,代码来源:test_mcpp.py
示例14: test_deactivated_parent_block
def test_deactivated_parent_block(self):
m = ConcreteModel()
m.d1 = Block()
m.d1.sub1 = Disjunct()
m.d1.sub2 = Disjunct()
m.d1.disj = Disjunction(expr=[m.d1.sub1, m.d1.sub2])
m.d1.deactivate()
TransformationFactory('gdp.reclassify').apply_to(m)
self.assertIs(m.d1.type(), Block)
self.assertIs(m.d1.sub1.type(), Block)
self.assertIs(m.d1.sub2.type(), Block)
开发者ID:Pyomo,项目名称:pyomo,代码行数:11,代码来源:test_reclassify.py
示例15: test_active_parent_disjunct_target
def test_active_parent_disjunct_target(self):
m = ConcreteModel()
m.d1 = Disjunct()
m.d1.sub1 = Disjunct()
m.d1.sub2 = Disjunct()
m.d1.disj = Disjunction(expr=[m.d1.sub1, m.d1.sub2])
TransformationFactory('gdp.bigm').apply_to(m, targets=m.d1.disj)
m.d1.indicator_var.fix(1)
TransformationFactory('gdp.reclassify').apply_to(m)
self.assertIs(m.d1.type(), Block)
self.assertIs(m.d1.sub1.type(), Block)
self.assertIs(m.d1.sub2.type(), Block)
开发者ID:Pyomo,项目名称:pyomo,代码行数:12,代码来源:test_reclassify.py
示例16: test_trig
def test_trig(self):
m = ConcreteModel()
m.x = Var(bounds=(pi / 4, pi / 2), initialize=pi / 4)
mc_expr = mc(tan(atan((m.x))))
self.assertAlmostEqual(mc_expr.lower(), pi / 4)
self.assertAlmostEqual(mc_expr.upper(), pi / 2)
m.y = Var(bounds=(0, sin(pi / 4)), initialize=0)
mc_expr = mc(asin((m.y)))
self.assertEqual(mc_expr.lower(), 0)
self.assertAlmostEqual(mc_expr.upper(), pi / 4)
m.z = Var(bounds=(0, cos(pi / 4)), initialize=0)
mc_expr = mc(acos((m.z)))
self.assertAlmostEqual(mc_expr.lower(), pi / 4)
self.assertAlmostEqual(mc_expr.upper(), pi / 2)
开发者ID:mskarha,项目名称:pyomo,代码行数:14,代码来源:test_mcpp.py
示例17: test_singletonvar_wildcardtemplate
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)
scenario_tree.linkInInstances({'Scenario1': model})
root = scenario_tree.findRootNode()
self.assertEqual(len(root._variable_ids), 4)
self.assertEqual(len(root._standard_variable_ids), 2)
self.assertEqual(len(root._derived_variable_ids), 2)
for name in ("x", "y", "z", "q"):
for index in [None]:
self.assertEqual(
(name,index) in root._name_index_to_id, True)
开发者ID:SemanticBeeng,项目名称:pyomo,代码行数:33,代码来源:test_scenariotree.py
示例18: select_tear_mip_model
def select_tear_mip_model(self, G):
"""
Generate a model for selecting tears from the given graph
Returns
-------
model
bin_list
A list of the binary variables representing each edge,
indexed by the edge index of the graph
"""
model = ConcreteModel()
bin_list = []
for i in range(G.number_of_edges()):
# add a binary "torn" variable for every edge
vname = "edge%s" % i
var = Var(domain=Binary)
bin_list.append(var)
model.add_component(vname, var)
# var containing the maximum number of times any cycle is torn
mct = model.max_cycle_tears = Var()
_, cycleEdges = self.all_cycles(G)
for i in range(len(cycleEdges)):
ecyc = cycleEdges[i]
# expression containing sum of tears for each cycle
ename = "cycle_sum%s" % i
expr = Expression(expr=sum(bin_list[i] for i in ecyc))
model.add_component(ename, expr)
# every cycle must have at least 1 tear
cname_min = "cycle_min%s" % i
con_min = Constraint(expr=expr >= 1)
model.add_component(cname_min, con_min)
# mct >= cycle_sum for all cycles, thus it becomes the max
cname_mct = mct.name + "_geq%s" % i
con_mct = Constraint(expr=mct >= expr)
model.add_component(cname_mct, con_mct)
# weigh the primary objective much greater than the secondary
obj_expr = 1000 * mct + sum(var for var in bin_list)
model.obj = Objective(expr=obj_expr, sense=minimize)
return model, bin_list
开发者ID:Pyomo,项目名称:pyomo,代码行数:49,代码来源:decomposition.py
示例19: makeTwoTermDisj
def makeTwoTermDisj():
m = ConcreteModel()
m.a = Var(bounds=(2, 7))
m.x = Var(bounds=(4, 9))
def d_rule(disjunct, flag):
m = disjunct.model()
if flag:
disjunct.c1 = Constraint(expr=m.a == 0)
disjunct.c2 = Constraint(expr=m.x <= 7)
else:
disjunct.c = Constraint(expr=m.a >= 5)
m.d = Disjunct([0, 1], rule=d_rule)
m.disjunction = Disjunction(expr=[m.d[0], m.d[1]])
return m
开发者ID:Pyomo,项目名称:pyomo,代码行数:15,代码来源:models.py
示例20: makeThreeTermDisj_IndexedConstraints
def makeThreeTermDisj_IndexedConstraints():
m = ConcreteModel()
m.I = [1, 2, 3]
m.x = Var(m.I, bounds=(0, 10))
def c_rule(b, i):
m = b.model()
return m.x[i] >= i
def d_rule(d, j):
m = d.model()
d.c = Constraint(m.I[:j], rule=c_rule)
m.d = Disjunct(m.I, rule=d_rule)
m.disjunction = Disjunction(expr=[m.d[i] for i in m.I])
return m
开发者ID:Pyomo,项目名称:pyomo,代码行数:15,代码来源:models.py
注:本文中的pyomo.core.ConcreteModel类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论