【问题标题】:Rule out solutions in pyomo排除pyomo中的解决方案
【发布时间】:2018-01-15 17:35:01
【问题描述】:

通常是 pyomo 和 python 的新手,我正在尝试为二进制整数编程问题实现一个简单的解决方案。然而,问题很大,但矩阵 x 的大部分值是事先已知的。我一直在试图弄清楚如何“告诉” pyomo 一些值是预先知道的以及它们是什么。

from __future__ import division # converts to float before division
from pyomo.environ import * # Make symbolds used by pyomo known to python

model = AbstractModel() # Declaration of an abstract model, called model

model.users = Set()
model.slots = Set()

model.prices=Param(model.users, model.slots)
model.users_balance=Param(model.users)
model.slot_bounds=Param(model.slots)

model.x = Var(model.users, model.slots, domain=Binary)

# Define the objective function
def obj_expression(model):
    return sum(sum(model.prices[i,j] * model.x[i,j] for i in model.users) 
for j in model.slots)

model.OBJ = Objective(rule=obj_expression, sense=maximize) 

# A user can only be assigned to one slot
def one_slot_rule(model, users):
    return sum(model.x[users,n] for n in model.slots) <= 1

model.OneSlotConstraint = Constraint(model.users, rule=one_slot_rule)

# Certain slots have a minimum balance requirement. 
def min_balance_rule1(model, slots):
    return sum(model.x[n,slots] * model.users_balance[n] for n in 
model.users) >= model.slot_bounds[slots]

model.MinBalanceConstraint1 = Constraint(model.slots, 
rule=min_balance_rule1)

所以我希望能够从我知道 x[i,j] 的某些值为 0 的事实中受益。例如,我有一个额外条件列表

x[1,7] = 0
x[3,6] = 0
x[5,8] = 0

如何包含这些信息以从减少搜索空间中受益? 非常感谢。

【问题讨论】:

  • 为什么要使用抽象模型?您是使用pyomo 命令还是 Python 脚本求解模型?

标签: pyomo


【解决方案1】:

模型构建完成后,您可以执行以下操作:

model.x[1,7].fix(0)
model.x[3,6].fix(0)
model.x[5,8].fix(0)

或者,假设您有一个 Set,model.Arcs,其中包含以下内容:

model.Arcs = Set(initialize=[(1,7), (3,6), (5,8)])

您可以在循环中修复 x 个变量:

for i,j in model.Arcs:
    model.x[i,j].fix(0)

【讨论】:

  • 非常感谢。有没有办法在约束中执行此操作并在我的 .dat 文件中包含 x[i,j]=0 的 (i,j) 集?
  • 模型构建完成后,是否意味着我运行了instance = model.create_instance('mydata.dat')?如果我在创建实例后尝试运行 model.x[1,7].fix(0),则会收到 ValueError: Error retrieving component x[1,7]: The component has not beenConstructed?
  • 是的。你需要这条线model = model.create_instance('mydata.dat')
  • 是的,您可以使用稀疏参数在约束中执行此操作
  • 谢谢贝瑟尼。我正在尝试使用备用参数来执行此操作,因为要指定很多值。我有def sparse_rule(model): return model.x[m,n] == 0 for (m,n) in model.Arcs,但这给了我一个语法错误。不知道我做错了什么。我在 mydata.dat 文件中定义了Set Arcs: = ...
猜你喜欢
  • 2017-02-17
  • 2019-02-10
  • 2016-12-06
  • 1970-01-01
  • 2021-04-23
  • 2013-04-30
  • 2019-11-19
  • 1970-01-01
  • 2020-01-27
相关资源
最近更新 更多