Gurobi Python Error: list index out of range
Gurobi Python Error: list index out of range
在 Zeppelin (oracle data studio) 中使用 Gurobi 时,我不断收到列表索引错误。这是代码和错误消息。 Gurobi 的新手,非常感谢任何有关如何解决此问题的有用意见。
将变量(数量)添加到模型(交易)时似乎出现错误
%python
import numpy as np
import pandas as pd
import gurobipy as gp
from gurobipy import *
from rsecommon.analytics.gurobi import *
try:
instance = rseenv.RseGurobiEnv.getInstance()
env = instance.getGurobiEnv()
# Model
deal = Model("Deal Optimization", env)
# Parameters
items = ['item1', 'item2', 'item3', 'item4', 'item5', 'item6', 'item7', 'item8', 'item9', 'item10']
volumes = {'item1':1, 'item2':1, 'item3':2, 'item4':2, 'item5':3, 'item6':3, 'item7':4, 'item8':4, 'item9':5, 'item10':5}
discounts = {'item1':0.05, 'item2':0.04, 'item3':0.01, 'item4':0.08, 'item5':0.06, 'item6':0.02, 'item7':0.01, 'item8':0.09, 'item9':0.06, 'item10':0.09}
min_qty = {'item1':540, 'item2':70, 'item3':220, 'item4':170, 'item5':60, 'item6':850, 'item7':910, 'item8':310, 'item9':480, 'item10':60}
capacity_cap = 5000
# Variables
quantity = deal.addVars(items, ub = (5000/volumes[item] for item in volumes),name='quantity')
# Constraints
deal.addConstr(((gp.quicksum(volumes[item]*quantity[item] for item in volumes) <= capacity_cap )), "Capacity")
# Set objective
deal.setObjective(quantity.prod(discounts), GRB.MAXIMIZE)
# Optimize
deal.optimize()
for v in deal.getVars():
print('%s %g' % (v.varName, v.x))
print('Obj: %g' % deal.objVal)
except GurobiError as e:
print('Error code ' + str(e.errno) + ": " + str(e.message))
except AttributeError:
print('Enco111untered an attribute error')
错误信息:
Traceback (most recent call last):
File "/tmp/zeppelin_python-2988405025033753797.py", line 312, in <module>
exec(code, _zcUserQueryNameSpace)
File "<stdin>", line 15, in <module>
File "model.pxi", line 2496, in gurobipy.Model.addVars
IndexError: list index out of range
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/tmp/zeppelin_python-2988405025033753797.py", line 319, in <module>
raise Exception(traceback.format_exc())
Exception: Traceback (most recent call last):
File "/tmp/zeppelin_python-2988405025033753797.py", line 312, in <module>
exec(code, _zcUserQueryNameSpace)
File "<stdin>", line 15, in <module>
File "model.pxi", line 2496, in gurobipy.Model.addVars
IndexError: list index out of range
尝试在对 Model.addVars 的调用中使用 UB 值的字典:
quantity = deal.addVars(items,
ub = {item:5000/volumes[item] for item in items},
name='quantity')
在 Zeppelin (oracle data studio) 中使用 Gurobi 时,我不断收到列表索引错误。这是代码和错误消息。 Gurobi 的新手,非常感谢任何有关如何解决此问题的有用意见。
将变量(数量)添加到模型(交易)时似乎出现错误
%python
import numpy as np
import pandas as pd
import gurobipy as gp
from gurobipy import *
from rsecommon.analytics.gurobi import *
try:
instance = rseenv.RseGurobiEnv.getInstance()
env = instance.getGurobiEnv()
# Model
deal = Model("Deal Optimization", env)
# Parameters
items = ['item1', 'item2', 'item3', 'item4', 'item5', 'item6', 'item7', 'item8', 'item9', 'item10']
volumes = {'item1':1, 'item2':1, 'item3':2, 'item4':2, 'item5':3, 'item6':3, 'item7':4, 'item8':4, 'item9':5, 'item10':5}
discounts = {'item1':0.05, 'item2':0.04, 'item3':0.01, 'item4':0.08, 'item5':0.06, 'item6':0.02, 'item7':0.01, 'item8':0.09, 'item9':0.06, 'item10':0.09}
min_qty = {'item1':540, 'item2':70, 'item3':220, 'item4':170, 'item5':60, 'item6':850, 'item7':910, 'item8':310, 'item9':480, 'item10':60}
capacity_cap = 5000
# Variables
quantity = deal.addVars(items, ub = (5000/volumes[item] for item in volumes),name='quantity')
# Constraints
deal.addConstr(((gp.quicksum(volumes[item]*quantity[item] for item in volumes) <= capacity_cap )), "Capacity")
# Set objective
deal.setObjective(quantity.prod(discounts), GRB.MAXIMIZE)
# Optimize
deal.optimize()
for v in deal.getVars():
print('%s %g' % (v.varName, v.x))
print('Obj: %g' % deal.objVal)
except GurobiError as e:
print('Error code ' + str(e.errno) + ": " + str(e.message))
except AttributeError:
print('Enco111untered an attribute error')
错误信息:
Traceback (most recent call last):
File "/tmp/zeppelin_python-2988405025033753797.py", line 312, in <module>
exec(code, _zcUserQueryNameSpace)
File "<stdin>", line 15, in <module>
File "model.pxi", line 2496, in gurobipy.Model.addVars
IndexError: list index out of range
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/tmp/zeppelin_python-2988405025033753797.py", line 319, in <module>
raise Exception(traceback.format_exc())
Exception: Traceback (most recent call last):
File "/tmp/zeppelin_python-2988405025033753797.py", line 312, in <module>
exec(code, _zcUserQueryNameSpace)
File "<stdin>", line 15, in <module>
File "model.pxi", line 2496, in gurobipy.Model.addVars
IndexError: list index out of range
尝试在对 Model.addVars 的调用中使用 UB 值的字典:
quantity = deal.addVars(items,
ub = {item:5000/volumes[item] for item in items},
name='quantity')