排除pyomo中的解决方案

时间:2018-01-15 17:35:02

标签: pyomo

一般来说是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

如何通过减少搜索空间来包含此信息? 非常感谢。

1 个答案:

答案 0 :(得分: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)