2
$\begingroup$

I am trying to update the rhs of a constraint iteratively under a while loop in docplex, however, it does not work properly. As I analyze the output text file, while some constraints are updated as I want, others are not.

z_bar is a list consists of y+1 elements and is updated at each iteration. Constraint set I want to change the RHS of consists of (x+1)*(y+1) constraints. z_bar is related to the j indice, however since each constraint involves i and j indices, I have to update all. What do you think I am doing wrong?

Original constraint set:

# Define sets
X_cbd = [(i, j) for i in range(1, x + 1) for j in range(1, y + 1)]

# Define variables
x_cbd = sub_cbd.binary_var_dict(X_cbd, name="x", lb=0)

for i in range(1, x + 1):
for j in range(1, y + 1):
sub_cbd.add_constraint(x_cbd[i, j] <= z_bar[j], ctname='constraint_name{0}{1}'.format(i, j))

Updating constraint set rhs attempt:

z_bar=[]

for i in range(1, x + 1):
    for j in range(1, y + 1):
        sub_cbd.get_constraint_by_name('constraint_name{0}{1}'.format(i, j)).rhs = z_bar[j]
$\endgroup$
3
  • $\begingroup$ Please check if cts_by_name is set to true in the model definition. $\endgroup$ Commented Feb 6, 2023 at 1:19
  • $\begingroup$ I tried that, but it does not seem to be the issue. z_bar is a list and updated in each iteration, however the rhs of the constraint is not properly updated somehow. $\endgroup$
    – user5245
    Commented Feb 6, 2023 at 7:19
  • $\begingroup$ Try with get_constraint_by_index and see $\endgroup$ Commented Feb 7, 2023 at 0:23

1 Answer 1

1
$\begingroup$

I think the method you pointed out would be as follows:

# model

mdl = Model(name='...')

variables part

constraints like
mdl.add_constraint(LHD <=> rhs, 'name')
mdl.minimize(...)

RHS = []
for i in range(0,N):  
   mdl.get_constraint_by_name("name").rhs=RHS[i]
   mdl.solve()

Also, you can use 'name'.rhs.add(value) to change the RHS coefficient in the model or any desired loop.

$\endgroup$
3
  • $\begingroup$ Hey, thanks for your comment! When I try that, I get the following error: TypeError: '<=' not supported between instances of 'dict' and 'dict'. I think it could have something do with the way I defined variables in the model, however it would take significant effort to update it. Do you know if it is possible to directly fix it from the expression given in the main post rather than changing some definitions? It would be really great. I added the variable definitions to the main post. $\endgroup$
    – user5245
    Commented Feb 6, 2023 at 10:06
  • $\begingroup$ @madetolast, I checked this with the dict form definition of the variables, but the models worked without any issues. I really did not deep into that to check line by line. $\endgroup$
    – A.Omidi
    Commented Feb 6, 2023 at 11:23
  • $\begingroup$ Thanks for the comment! This is very interesting... It still does not work. I debugged the problem and found that somehow it just does not update some indices of z_bar. For instance, z_bar is a list [1,1,1,0,0,0,..0]. 15 elements in total, first 3 elements are 1, rest are 0. When I debugged the for loop, I found that, it updates the first 11 elements, but does not change the rest for j indice. I added how it looks and how it should look to the main post. Thanks again. $\endgroup$
    – user5245
    Commented Feb 6, 2023 at 13:04

Your Answer

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