PYOMO:使用集合和参数定义数据集以解决优化问题
PYOMO: Defining dataset using Sets and Parameters for solving an optimization problem
我正在尝试将一些数据转化为优化问题的 PYOMO 模型。
materials = ['steel', 'alum', 'carbon', 'cheese']
每个 material 都有 2 个属性 - 密度和电导率,它们的值定义如下。
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}
objective 函数计算 2 个矩形板的重量,如下所示:
Objective function = Area_1 * thickness_1 * density_1 + Area_2 * thickness_2 * density_2
其中Area_1、thickness_1、density_1分别为板1的面积、厚度和密度。
每个板的面积和厚度是固定的。但是 密度值取决于求解器选择的 material 以获得最佳结果 。该模型还具有如下定义的约束:
(conductivity_1/thickness_1) + (conductivity_2/thickness_2) => 22
因此,当求解器为板选择密度值时,它也必须选择相同的电导率值material。
如果有人能帮助我解决这个问题,我将不胜感激。如果您有不同的想法来解决这个问题,我也欢迎您。谢谢。
这是我认为可以满足您所有问题的示例模型。
一旦你将第二个索引设置为板块 P = {1, 2, 3}
在这种情况下为 3 个板块,那么我们需要对我们的决策变量进行双重索引以表示 material [=13 的分配=] 到盘子 p
。在这个例子中,4 materials,3 个盘子。
此处可能存在许多其他约束变体,但我添加的约束条件总体上回答了您关于电导率的问题。请注意,我还添加了一个约束以确保为每个盘子分配 1 个且仅 1 个 material。根据模型中的其他约束,您 may/may 不需要它,但它可以很好地防止虚假答案。这也是在 pyomo
.
中使用函数 - 规则组合的“for every”约束样式的示例
结果……一个铝制奶酪三明治……:)
# 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' : 40.8,
'alum' : 30.1,
'carbon': 42.4,
'cheese': 15.3}
price = { 'steel' : 2.3,
'alum' : 3.5,
'carbon': 5.8,
'cheese': 6.0}
# t area
plate_dims = { 1: (10, 150),
2: (12.5, 200),
3: (8, 125)}
mdl = pyo.ConcreteModel('material selector')
# SETS (used to index the decision variable and the parameters)
mdl.M = pyo.Set(initialize=materials)
mdl.P = pyo.Set(initialize=plate_dims.keys())
# VARIABLES
mdl.x = pyo.Var(mdl.M, mdl.P, domain=pyo.Binary) # select material M for plate P
# PARAMETERS
mdl.density = pyo.Param(mdl.M, initialize=density)
mdl.conductivity = pyo.Param(mdl.M, initialize=conductivity)
mdl.price = pyo.Param(mdl.M, initialize=price)
mdl.p_thickness = pyo.Param(mdl.P, initialize= {k:v[0] for k,v in plate_dims.items()})
mdl.p_area = pyo.Param(mdl.P, initialize= {k:v[1] for k,v in plate_dims.items()})
# OBJ (minimize total density)
mdl.obj = pyo.Objective(expr=sum(mdl.x[m, p] * mdl.p_thickness[p]
* mdl.p_area[p] * mdl.density[m]
for m in mdl.M for p in mdl.P))
# CONSTRAINTS
# minimum conductivity
mdl.c1 = pyo.Constraint(expr=sum(mdl.x[m, p] * mdl.conductivity[m]/mdl.p_thickness[p]
for m in mdl.M for p in mdl.P) >= 5.0)
# must populate all plates with 1 material
def c2(model, plate):
return sum(mdl.x[m, plate] for m in mdl.M) == 1
mdl.c2 = pyo.Constraint(mdl.P, rule=c2)
# solve it
solver = pyo.SolverFactory('glpk')
result = solver.solve(mdl)
mdl.display()
产量:
Model material selector
Variables:
x : Size=12, Index=x_index
Key : Lower : Value : Upper : Fixed : Stale : Domain
('alum', 1) : 0 : 0.0 : 1 : False : False : Binary
('alum', 2) : 0 : 0.0 : 1 : False : False : Binary
('alum', 3) : 0 : 1.0 : 1 : False : False : Binary
('carbon', 1) : 0 : 0.0 : 1 : False : False : Binary
('carbon', 2) : 0 : 0.0 : 1 : False : False : Binary
('carbon', 3) : 0 : 0.0 : 1 : False : False : Binary
('cheese', 1) : 0 : 1.0 : 1 : False : False : Binary
('cheese', 2) : 0 : 1.0 : 1 : False : False : Binary
('cheese', 3) : 0 : 0.0 : 1 : False : False : Binary
('steel', 1) : 0 : 0.0 : 1 : False : False : Binary
('steel', 2) : 0 : 0.0 : 1 : False : False : Binary
('steel', 3) : 0 : 0.0 : 1 : False : False : Binary
Objectives:
obj : Size=1, Index=None, Active=True
Key : Active : Value
None : True : 3600.0
Constraints:
c1 : Size=1
Key : Lower : Body : Upper
None : 5.0 : 6.516500000000001 : None
c2 : Size=3
Key : Lower : Body : Upper
1 : 1.0 : 1.0 : 1.0
2 : 1.0 : 1.0 : 1.0
3 : 1.0 : 1.0 : 1.0
我正在尝试将一些数据转化为优化问题的 PYOMO 模型。
materials = ['steel', 'alum', 'carbon', 'cheese']
每个 material 都有 2 个属性 - 密度和电导率,它们的值定义如下。
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}
objective 函数计算 2 个矩形板的重量,如下所示:
Objective function = Area_1 * thickness_1 * density_1 + Area_2 * thickness_2 * density_2
其中Area_1、thickness_1、density_1分别为板1的面积、厚度和密度。
每个板的面积和厚度是固定的。但是 密度值取决于求解器选择的 material 以获得最佳结果 。该模型还具有如下定义的约束:
(conductivity_1/thickness_1) + (conductivity_2/thickness_2) => 22
因此,当求解器为板选择密度值时,它也必须选择相同的电导率值material。
如果有人能帮助我解决这个问题,我将不胜感激。如果您有不同的想法来解决这个问题,我也欢迎您。谢谢。
这是我认为可以满足您所有问题的示例模型。
一旦你将第二个索引设置为板块 P = {1, 2, 3}
在这种情况下为 3 个板块,那么我们需要对我们的决策变量进行双重索引以表示 material [=13 的分配=] 到盘子 p
。在这个例子中,4 materials,3 个盘子。
此处可能存在许多其他约束变体,但我添加的约束条件总体上回答了您关于电导率的问题。请注意,我还添加了一个约束以确保为每个盘子分配 1 个且仅 1 个 material。根据模型中的其他约束,您 may/may 不需要它,但它可以很好地防止虚假答案。这也是在 pyomo
.
结果……一个铝制奶酪三明治……:)
# 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' : 40.8,
'alum' : 30.1,
'carbon': 42.4,
'cheese': 15.3}
price = { 'steel' : 2.3,
'alum' : 3.5,
'carbon': 5.8,
'cheese': 6.0}
# t area
plate_dims = { 1: (10, 150),
2: (12.5, 200),
3: (8, 125)}
mdl = pyo.ConcreteModel('material selector')
# SETS (used to index the decision variable and the parameters)
mdl.M = pyo.Set(initialize=materials)
mdl.P = pyo.Set(initialize=plate_dims.keys())
# VARIABLES
mdl.x = pyo.Var(mdl.M, mdl.P, domain=pyo.Binary) # select material M for plate P
# PARAMETERS
mdl.density = pyo.Param(mdl.M, initialize=density)
mdl.conductivity = pyo.Param(mdl.M, initialize=conductivity)
mdl.price = pyo.Param(mdl.M, initialize=price)
mdl.p_thickness = pyo.Param(mdl.P, initialize= {k:v[0] for k,v in plate_dims.items()})
mdl.p_area = pyo.Param(mdl.P, initialize= {k:v[1] for k,v in plate_dims.items()})
# OBJ (minimize total density)
mdl.obj = pyo.Objective(expr=sum(mdl.x[m, p] * mdl.p_thickness[p]
* mdl.p_area[p] * mdl.density[m]
for m in mdl.M for p in mdl.P))
# CONSTRAINTS
# minimum conductivity
mdl.c1 = pyo.Constraint(expr=sum(mdl.x[m, p] * mdl.conductivity[m]/mdl.p_thickness[p]
for m in mdl.M for p in mdl.P) >= 5.0)
# must populate all plates with 1 material
def c2(model, plate):
return sum(mdl.x[m, plate] for m in mdl.M) == 1
mdl.c2 = pyo.Constraint(mdl.P, rule=c2)
# solve it
solver = pyo.SolverFactory('glpk')
result = solver.solve(mdl)
mdl.display()
产量:
Model material selector
Variables:
x : Size=12, Index=x_index
Key : Lower : Value : Upper : Fixed : Stale : Domain
('alum', 1) : 0 : 0.0 : 1 : False : False : Binary
('alum', 2) : 0 : 0.0 : 1 : False : False : Binary
('alum', 3) : 0 : 1.0 : 1 : False : False : Binary
('carbon', 1) : 0 : 0.0 : 1 : False : False : Binary
('carbon', 2) : 0 : 0.0 : 1 : False : False : Binary
('carbon', 3) : 0 : 0.0 : 1 : False : False : Binary
('cheese', 1) : 0 : 1.0 : 1 : False : False : Binary
('cheese', 2) : 0 : 1.0 : 1 : False : False : Binary
('cheese', 3) : 0 : 0.0 : 1 : False : False : Binary
('steel', 1) : 0 : 0.0 : 1 : False : False : Binary
('steel', 2) : 0 : 0.0 : 1 : False : False : Binary
('steel', 3) : 0 : 0.0 : 1 : False : False : Binary
Objectives:
obj : Size=1, Index=None, Active=True
Key : Active : Value
None : True : 3600.0
Constraints:
c1 : Size=1
Key : Lower : Body : Upper
None : 5.0 : 6.516500000000001 : None
c2 : Size=3
Key : Lower : Body : Upper
1 : 1.0 : 1.0 : 1.0
2 : 1.0 : 1.0 : 1.0
3 : 1.0 : 1.0 : 1.0