"int 'object Is Not Subscriptable"
i'm starting to learn GEKKO. Now, I am solving a knapsak problem to learn, but this time I get the error 'int 'object is not subscriptable'. can you look at this code? what is the
Solution 1:
Here is a modified version that solves the mixed integer problem in gekko.
from gekko import GEKKO
import numpy as np
m = GEKKO(remote=False)
x = m.Array(m.Var,10,lb=0,ub=1,integer=True)
v=np.array([2, 2, 7, 8, 2, 1, 7, 9, 4, 10])
w=np.array([2, 2, 2, 2, 2, 1, 6, 7, 3, 3])
capacity=16for j inrange(10):
m.Maximize(v[j]*x[j])
xw = [x[i]*w[i] for i inrange(10)]
m.Equation(m.sum(xw)<=capacity)
m.options.solver = 1
m.solve()
print('Objective Function: ' + str(-m.options.objfcnval))
print(x)
Your problem formulation was close. You just needed to define a list xw
that you use to form the capacity constraint.
If you want to use a loop instead of a list comprehension then I recommend the following instead of xw = [x[i]*w[i] for i in range(10)]
.
xw = []
for i inrange(10):
xw.append(x[i]*w[i])
Post a Comment for ""int 'object Is Not Subscriptable""