具有python字典值的Gurobi目标 [英] Gurobi objective with python dictionary values

查看:199
本文介绍了具有python字典值的Gurobi目标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Gurobi 6.0与Python 2.7结合使用.我很想知道Gurobi是否允许目标函数具有来自具有决策变量索引的字典的值.附加代码:

I am using Gurobi 6.0 with Python 2.7. I am curious to know if Gurobi allows the objective function to have values coming from a dictionary with indices of the decision variables. Attaching the code:

from gurobipy import *

d = {
     (0, 0): 0,
     (0, 1): -5,
     (1, 0): 4,
     (1, 1): 2,
     (2, 0): 0,
     (0, 2): 10
     }

m = Model()
x = m.addVar(vtype=GRB.INTEGER)
y = m.addVar(vtype=GRB.INTEGER)

m.update()
m.addConstr(x + y <= 2)
m.setObjective(d[(x, y)], GRB.MAXIMIZE)
m.optimize()
print m.objVal
print x.x
print y.x

模型的答案是

-5.0

-5.0

-0.0

-0.0

这显然是没有意义的,因为根据给定的数据,max(d [(x,y)])= 10发生在x = 0和y = 2处.这是什么问题? Gurobi甚至允许这样的词典参考吗?甚至允许吗?

which clearly makes no sense because max(d[(x,y)]) = 10 happens at x=0 and y=2 as per the given data. What is the issue here? Does Gurobi even allow such dictionary references ? Is it even allowed?

推荐答案

对于在您的代码中稍微复杂的因果链d[(x,y)]等效于d[(0,1)],因此常数-5最终成为您的目标函数.原因是

For a somewhat complex causal chain d[(x,y)] in your code is equivalent to d[(0,1)], so the constant -5 winds up being your objective function. The reasons are

  • gurobi.Var已定义__hash __
  • gurobi.Var已定义__cmp__.它总是返回一个真实的对象
  • 在您的情况下,x和y的哈希值分别为0和1
  • python字典查找算法解析d [(x, y)]到d [(0,1)]
  • gurobi.Var has __hash__ defined
  • gurobi.Var has __cmp__ defined. It always returns a truthy object
  • In your case, x and y have hash values of 0 and 1
  • The python dictionary lookup algorithm resolves d[(x,y)] to d[(0,1)]

您要执行的操作不适合整数编程框架.将其放入gurobi的最佳方法是添加

What you are trying to do doesn't fit into the integer programming framework. The best way to put this into gurobi is to add indicator variables that x and y take on specific values.

这篇关于具有python字典值的Gurobi目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆