KeyError 0 - Gurobi - Jupyter Notebook
AnsweredI have KeyError 0 in knapsack problem. Could you please help about this issue?
My code details below;
import gurobipy as gp
from gurobipy import GRB
import pandas as pd
v=pd.read_excel('C:\\Users\\INCE\\Desktop\\gurobidata.xlsx',usecols=[1])
v
w=pd.read_excel('C:\\Users\\INCE\\Desktop\\gurobidata.xlsx',usecols=[2])
w
c=1000
N=len(w)
from gurobipy import *
knapsack_model=Model('knapsack')
x=knapsack_model.addVars(N,vtype=GRB.BINARY,name='x')
obj_fn=sum(v[i]*x[i] for i in range (N))
knapsack_model.setObjective(obj_fn,GRB.MAXIMIZE)
knapsack_model.addConstr(sum(w[i]*x[i] for i in range (N))<=c)
knapsack_model.update()
knapsack_model.setParam('OutputFlag',False)
knapsack_model.optimize()
print('Optimization is done. Objective Function Value: %.2f' %knapsack_model.objVal)
for v in knapsack_model.getVars():
print ('%s: %g' %(v.varName,v.x))
0
-
Official comment
This post is more than two years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum, or try Gurobot, our chatbot interface offering instant, expert-level support. -
Your data v and w cannot be used in this way.
If gurobidata.xlsx is a table with headings 'weight' and 'capacity', you could do the following
df=pd.read_excel('gurobidata.xlsx')
v = df['weight'].tolist()
w = df['capacity'].tolist()0
Post is closed for comments.
Comments
2 comments