Pyomo:如何给包括刑的目标的功能

0

的问题

我试图尽量减少成本制造的产品有两个机器。 成本的机为30美元/产品和成本的机B40美元/产品。

有两个限制:

  • 我们必须涵盖的需求的50产品的每月(x+y>=50)
  • 廉价机(A)只能制造40产品的每月(x<=40)

所以我创造了以下Pyomo码:

from pyomo.environ import *
model = ConcreteModel()
model.x = Var(domain=NonNegativeReals)
model.y = Var(domain=NonNegativeReals)

def production_cost(m):
    return 30*m.x + 40*m.y

# Objective
model.mycost = Objective(expr = production_cost, sense=minimize)

# Constraints
model.demand = Constraint(expr = model.x + model.y >= 50)
model.maxA = Constraint(expr = model.x <= 40)

# Let's solve it
results = SolverFactory('glpk').solve(model)

# Display the solution
print('Cost=', model.mycost())
print('x=', model.x())
print('y=', model.y())

它的工作确定,具有明显的解决方案x=40;y=10(成本=1600)

然而,如果我们开始使用的机B,将有一个固定的罚款300美元的费用。

我试着用

def production_cost(m):
  if (m.y > 0):
    return 30*m.x + 40*m.y + 300
  else:
    return 30*m.x + 40*m.y

但我得到的以下错误消息

Rule failed when generating expression for Objective mycost with index
    None: PyomoException: Cannot convert non-constant Pyomo expression (0  <
    y) to bool. This error is usually caused by using a Var, unit, or mutable
    Param in a Boolean context such as an "if" statement, or when checking
    container membership or equality. For example,
        >>> m.x = Var() >>> if m.x >= 1: ...     pass
    and
        >>> m.y = Var() >>> if m.y in [m.x, m.y]: ...     pass
    would both cause this exception.

我不如何执行条件以包括罚款到的目标功能,通过Pyomo代码。

optimization pyomo python
2021-11-22 12:46:07
1

最好的答案

1

由于 m.y 是一个 Var你不能使用 if 发言。 你总是可以使用一个变量使用的二进制 Big M 方法,因为Airsquid所述。 这种方法通常不建议,因为事实证明问题,从LP成MILP,但它是有效的。

你只需要创建一个新的 Binary Var:

model.bin_y = Var(domain=Binary)

然后约束 model.y 是零如果 model.bin_y 是零,否则,任何价值之间的界限。 我用一定的100在这里,但你甚至可以用需求:

model.bin_y_cons = Constraint(expr= model.y <= model.bin_y*100)   

然后,在你的目标只适用新的固定价值为300:

def production_cost(m):
    return 30*m.x + 40*m.y + 300*model.bin_y 

model.mycost = Objective(rule=production_cost, sense=minimize)
2021-11-22 15:22:41

谢谢你@pybegginer,很好地说明:-)我会去深入使用的大M.
Hookstark

对于无限 Vars 你需要使用一个非常大的价值的约束,例如 1E6. 在这样的问题,你会需要仔细检查了解容忍二,因为它可能会发生, bin_y 约。 零,但是 y 仍然大于零:例如,如果限定为1E6和二进制的容忍是1E-6, bin_y 分配给1E-7(非常接近于零),但所得到的约束 y<=5 允许Y要大于零。 无论如何,只是仔细检查这些价值观的时候模型是解决了
pybegginer

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................