在 PYOMO 中为 2 个变量定义特定的一组值
Defining specific set of values for 2 variables in PYOMO
我正在尝试使用多个变量分配 material 属性。例如;密度和电导率是 material_1、material_2 和 material_3 的两个决策变量。
我必须输入以下信息:
density of material_1 = 1000
density of material_2 = 2000
density of material_3 = 1500
conductivity of material_1 = 250
conductivity of material_2 = 400
conductivity of material_3 = 100
给出了Pyomo中定义变量的标准格式
下面:
model.variable_1 = Var(bounds=(800,2000))
上面的代码表示variable_1是一个变量,下限=800,上限=2000。
但是我们如何定义具有特定值集而不是边界的变量?
这个想法是将数据值输入优化器,这样 在选择密度值时,它也应该从相同的 material 中选择电导率值。
我们如何将这样的条件强加到 pyomo 框架中?有人可以帮我解决这个问题吗?
因此,如果您只是选择众多选项中的一个,则可以将其设置为整数线性规划。基本要点是我们让下面示例中的二进制变量 x
表示选择 material i
的行为,其中 i
是 [= 集合的成员29=]s.
在你上面的问题中,你似乎很难理解模型中参数的分离概念(价格、密度、电导率等),这些参数的值是固定的来自变量,这是您要建模的决策。
一个比下面稍微高级的模型可能是一个混合模型,您可以在某些约束下采用各种 material 的比例等,这需要将 x
的域更改为为非负实数。这只是模拟选择的二元动作。当然,在像这样微不足道的模型中,您可以使用 list/dictionary 理解或过滤器来解决它,因此使用代数建模确实有点过分,但它是区分您所询问的概念的示例。
# material selection model
import pyomo.environ as pyo
# data
materials = ['steel', 'alum', 'carbon', 'cheese']
density = { 'steel' : 1.2,
'alum' : 0.8,
'carbon': 1.8,
'cheese': 0.7}
conductivity = {'steel' : 6.4,
'alum' : 3.1,
'carbon': 4.4,
'cheese': 0.3}
price = { 'steel' : 2.3,
'alum' : 3.5,
'carbon': 5.8,
'cheese': 6.0}
m = pyo.ConcreteModel('material selector')
# SETS (used to index the decision variable and the parameters)
m.matl = pyo.Set(initialize=materials)
# VARIABLES
m.x = pyo.Var(m.matl, domain=pyo.Binary) # a binary decision variable representing the selection of matl
# PARAMETERS
m.density = pyo.Param(m.matl, initialize=density)
m.conductivity = pyo.Param(m.matl, initialize=conductivity)
m.price = pyo.Param(m.matl, initialize=price)
# OBJ (minimize price)
m.obj = pyo.Objective(expr=sum(m.x[i] * m.price[i] for i in m.matl))
# Constraints
m.c1 = pyo.Constraint(expr=(sum(m.x[i] * m.density[i] for i in m.matl) >= 1.0)) # min density
m.c2 = pyo.Constraint(expr=(sum(m.x[i] * m.conductivity[i] for i in m.matl) <= 5.0)) # max cond.
# solve it
solver = pyo.SolverFactory('glpk')
result = solver.solve(m)
m.display()
产量:
Model material selector
Variables:
x : Size=4, Index=matl
Key : Lower : Value : Upper : Fixed : Stale : Domain
alum : 0 : 0.0 : 1 : False : False : Binary
carbon : 0 : 1.0 : 1 : False : False : Binary
cheese : 0 : 0.0 : 1 : False : False : Binary
steel : 0 : 0.0 : 1 : False : False : Binary
Objectives:
obj : Size=1, Index=None, Active=True
Key : Active : Value
None : True : 5.8
Constraints:
c1 : Size=1
Key : Lower : Body : Upper
None : 1.0 : 1.8 : None
c2 : Size=1
Key : Lower : Body : Upper
None : None : 4.4 : 5.0
我正在尝试使用多个变量分配 material 属性。例如;密度和电导率是 material_1、material_2 和 material_3 的两个决策变量。
我必须输入以下信息:
density of material_1 = 1000
density of material_2 = 2000
density of material_3 = 1500
conductivity of material_1 = 250
conductivity of material_2 = 400
conductivity of material_3 = 100
给出了Pyomo中定义变量的标准格式 下面:
model.variable_1 = Var(bounds=(800,2000))
上面的代码表示variable_1是一个变量,下限=800,上限=2000。
但是我们如何定义具有特定值集而不是边界的变量?
这个想法是将数据值输入优化器,这样 在选择密度值时,它也应该从相同的 material 中选择电导率值。
我们如何将这样的条件强加到 pyomo 框架中?有人可以帮我解决这个问题吗?
因此,如果您只是选择众多选项中的一个,则可以将其设置为整数线性规划。基本要点是我们让下面示例中的二进制变量 x
表示选择 material i
的行为,其中 i
是 [= 集合的成员29=]s.
在你上面的问题中,你似乎很难理解模型中参数的分离概念(价格、密度、电导率等),这些参数的值是固定的来自变量,这是您要建模的决策。
一个比下面稍微高级的模型可能是一个混合模型,您可以在某些约束下采用各种 material 的比例等,这需要将 x
的域更改为为非负实数。这只是模拟选择的二元动作。当然,在像这样微不足道的模型中,您可以使用 list/dictionary 理解或过滤器来解决它,因此使用代数建模确实有点过分,但它是区分您所询问的概念的示例。
# material selection model
import pyomo.environ as pyo
# data
materials = ['steel', 'alum', 'carbon', 'cheese']
density = { 'steel' : 1.2,
'alum' : 0.8,
'carbon': 1.8,
'cheese': 0.7}
conductivity = {'steel' : 6.4,
'alum' : 3.1,
'carbon': 4.4,
'cheese': 0.3}
price = { 'steel' : 2.3,
'alum' : 3.5,
'carbon': 5.8,
'cheese': 6.0}
m = pyo.ConcreteModel('material selector')
# SETS (used to index the decision variable and the parameters)
m.matl = pyo.Set(initialize=materials)
# VARIABLES
m.x = pyo.Var(m.matl, domain=pyo.Binary) # a binary decision variable representing the selection of matl
# PARAMETERS
m.density = pyo.Param(m.matl, initialize=density)
m.conductivity = pyo.Param(m.matl, initialize=conductivity)
m.price = pyo.Param(m.matl, initialize=price)
# OBJ (minimize price)
m.obj = pyo.Objective(expr=sum(m.x[i] * m.price[i] for i in m.matl))
# Constraints
m.c1 = pyo.Constraint(expr=(sum(m.x[i] * m.density[i] for i in m.matl) >= 1.0)) # min density
m.c2 = pyo.Constraint(expr=(sum(m.x[i] * m.conductivity[i] for i in m.matl) <= 5.0)) # max cond.
# solve it
solver = pyo.SolverFactory('glpk')
result = solver.solve(m)
m.display()
产量:
Model material selector
Variables:
x : Size=4, Index=matl
Key : Lower : Value : Upper : Fixed : Stale : Domain
alum : 0 : 0.0 : 1 : False : False : Binary
carbon : 0 : 1.0 : 1 : False : False : Binary
cheese : 0 : 0.0 : 1 : False : False : Binary
steel : 0 : 0.0 : 1 : False : False : Binary
Objectives:
obj : Size=1, Index=None, Active=True
Key : Active : Value
None : True : 5.8
Constraints:
c1 : Size=1
Key : Lower : Body : Upper
None : 1.0 : 1.8 : None
c2 : Size=1
Key : Lower : Body : Upper
None : None : 4.4 : 5.0