I am trying to implemet the subgradient method in Lagragian relaxation to update the multiplier lamda which has three dimension, I have ecountered an Except error, it looks the is bugs in the code. it shows the following error:

---> 39          lamda[i,k] = max(0,lamda[i,k] + b_Ax[i,k])

TypeError: list indices must be integers or slices, not tuple

b = np.ones((I,K))
#Initial data
m=0 #number of iterations to start with in the algorithm
n=0 #iteration count that is incremented when LBb is not imprtoved
N= 3 # Maximum number of iterations count (max iter with improvement)
M= 1 #Maximum number of itereations allowed in the heuristic i.e LR heuristic
LB_b= -math.inf #best lower bound
UB_b= math.inf #best upper bound
pi_m= 1   # step size
epsilon= 0.001 # gap limit
tao= 0.001 #limit for tild_m where tildm goes to zero after some iterations.
beta= 0.5# scalar multiplied by tildm
tild_m= 1  # scalar oaly a role in reducing the step size after some iterations.
lamda = np.zeros((I,K))
# Decision varaibles
x_m = model.addVars(I,J,K,vtype=GRB.BINARY, name ='x_m')
#Lagrange relaxation
for m in range(M):
    #Relaxed porblem_in this case the Lower bound
    z1 =(9*x_m[0,0,0]+ 2*x_m[0,1,0]+ x_m[1,0,1] + 2*x_m[1,1,1] + 3*x_m[2,0,2] + 
    8*x_m[2,1,2] + sum(lamda[i,k]*b[i,k]- lamda[i,k]*sum(x_m[i, j,k] for j in range(J)) 
    for i in range(I) for k in range(K)))
    model.addConstr(6*x_m[0,0,0] +7*x_m[1,0,1] + 9*x_m[2,0,2] <= 13, "const1")
    model.addConstr(8*x_m[0,1,0]+5*x_m[1,1,1]+6*x_m[2,1,2] <= 11, "const2")
    LB_m = model.objVal
    if LB_m > LB_b:
       if n == N:
    b_Ax1 = [b[i,k] - sum(x_m[i,j,k].x  for j in range(J))  for i in range(I) for k in 
    b_Ax = [pi_m*num for num in b_Ax1]
    b_Axr = sum(b[i,k]- sum(x_m[i, j, k].x for j in range(J)) for i in range(I) for k in 
    # the solution of relaxed problem will be feasible to original problem,
    gap = abs(UB_b-LB_b)/ (UB_b)
    if (UB_b-LB_b)/ UB_b <= epsilon:
        for i in range(I):
            for k in range(K):
                lamda[i,k] = max(0,lamda[i,k] + b_Ax[i,k])
    pi_m = pi_m = tild_m*((UB_b-LB_b)/(LA.norm(b_Axr)**2))
  • 1
    $\begingroup$ b_Ax = [pi_m*num for num in b_Ax1] makes bAx a vector instead of a matrix, so you should (i) make b_Ax a matrix, or (ii) access it values by b_Ax[i * I + k]. $\endgroup$ Commented Aug 24, 2023 at 13:22
  • 1
    $\begingroup$ @MatheusDiógenesAndrade Thank you for your comment, what about b_Ax1, is it possible to convert it into a matrix directly while preserving the solution, and getting lamda[i,k] updated. I have faced a problem before with ' b_Ax1 = (b[i,k] - sum(x_m...)', I could not multiply it by a scalar pi_m because it is a generator, and I could transform it into a matrix to update lamda[i,k]. $\endgroup$
    – ABDE
    Commented Aug 24, 2023 at 13:36
  • $\begingroup$ Just do b_Ax1 = np.ones((I, K)) and then populate it. If you could post the entire code, it would be helpful. $\endgroup$ Commented Aug 24, 2023 at 18:55


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.