import numpy as np
from docplex.mp.model import Model
rnd = np.random
rnd.seed(1995)
#Number of Objects
N = 12
M = 3
#Gnerate the list of objects indices
Obj = [j for j in range(0, N)]
Knap = [i for i in range(0, M)]
print(Obj)
print(Knap)
#Generate objects weights and save them in W list
W = rnd.randint(5, 50, size=N)
print(W)
#Generate objects profits and save them in P list
P = rnd.randint(10, 100, size=N)
print(P)
#Knapsack Capacity
C = rnd.randint(1, 5, size=M) ### different of m capisties
print(C)
#Create the model
mdl = Model('MKP')
#Define decision variables
x = mdl.binary_var_matrix(Obj,M, name='x')
print(x)
#Add objective function
mdl.maximize(mdl.sum(P[j]*x[j] for j in Obj))
## mdl.maximize(mdl.sum(P[i]*x[j][i] for i in Knap))
#Add capacity constraint
mdl.add_constraints(mdl.sum(W[j]*x[j] for j in Obj) <= C[i] for i in Knap)
mdl.add_constraints(mdl.sum(x[j][i] for i in Knap) <= 1 for j in Obj)
#Write the model on lp file
mdl.export_as_lp("./mkp.lp")
#Define the cpu time limit
mdl.parameters.timelimit = 300
#Solve the model
solution = mdl.solve(log_output=True)
#Print the solution status
print(solution.solve_status)
#Print the solution
print(solution)
Well, how about adding more details. Like, what are you trying to accomplish, how are you doing it, what difficulties you have found with the approach, another approaches you have tried, etc. In summary, without code isn't a good question neither is with just code. Search for the balance!
this is output how can i solve this maximize problem
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
[0, 1, 2]
[41 23 43 10 34 25 33 6 16 14 40 11]
[59 73 50 89 74 66 22 62 42 40 27 10]
[3 2 3]
{(0, 0): docplex.mp.Var(type=B,name='x_0_0'), (0, 1): docplex.mp.Var(type=B,name='x_0_1'), (0, 2): docplex.mp.Var(type=B,name='x_0_2'), (1, 0): docplex.mp.Var(type=B,name='x_1_0'), (1, 1): docplex.mp.Var(type=B,name='x_1_1'), (1, 2): docplex.mp.Var(type=B,name='x_1_2'), (2, 0): docplex.mp.Var(type=B,name='x_2_0'), (2, 1): docplex.mp.Var(type=B,name='x_2_1'), (2, 2): docplex.mp.Var(type=B,name='x_2_2'), (3, 0): docplex.mp.Var(type=B,name='x_3_0'), (3, 1): docplex.mp.Var(type=B,name='x_3_1'), (3, 2): docplex.mp.Var(type=B,name='x_3_2'), (4, 0): docplex.mp.Var(type=B,name='x_4_0'), (4, 1): docplex.mp.Var(type=B,name='x_4_1'), (4, 2): docplex.mp.Var(type=B,name='x_4_2'), (5,
0): docplex.mp.Var(type=B,name='x_5_0'), (5, 1): docplex.mp.Var(type=B,name='x_5_1'), (5, 2): docplex.mp.Var(type=B,name='x_5_2'), (6, 0): docplex.mp.Var(type=B,name='x_6_0'), (6, 1): docplex.mp.Var(type=B,name='x_6_1'), (6, 2): docplex.mp.Var(type=B,name='x_6_2'), (7, 0): docplex.mp.Var(type=B,name='x_7_0'), (7, 1): docplex.mp.Var(type=B,name='x_7_1'), (7, 2): docplex.mp.Var(type=B,name='x_7_2'), (8, 0): docplex.mp.Var(type=B,name='x_8_0'), (8, 1): docplex.mp.Var(type=B,name='x_8_1'), (8, 2): docplex.mp.Var(type=B,name='x_8_2'), (9, 0): docplex.mp.Var(type=B,name='x_9_0'), (9, 1): docplex.mp.Var(type=B,name='x_9_1'), (9, 2): docplex.mp.Var(type=B,name='x_9_2'), (10, 0):
docplex.mp.Var(type=B,name='x_10_0'), (10, 1): docplex.mp.Var(type=B,name='x_10_1'),
(10, 2): docplex.mp.Var(type=B,name='x_10_2'), (11, 0): docplex.mp.Var(type=B,name='x_11_0'), (11, 1): docplex.mp.Var(type=B,name='x_11_1'), (11, 2): docplex.mp.Var(type=B,name='x_11_2')}
Traceback (most recent call last):
File "c:/Users/User/Downloads/Mmmmkp.py", line 49, in <module>
mdl.maximize(mdl.sum(P[j]*x[j] for j in Obj))
File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\docplex\mp\model.py", line 2845, in sum
return self._aggregator.sum(args)
File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\docplex\mp\aggregator.py", line 176, in sum
sum_res = self._sum_with_iter(sum_args)
File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\docplex\mp\aggregator.py", line 199, in _sum_with_iter
for item in args:
File "c:/Users/User/Downloads/Mmmmkp.py", line 49, in <genexpr>
mdl.maximize(mdl.sum(P[j]*x[j] for j in Obj))
KeyError: 0