How to replace coefficients and constant terms in the model
AnsweredHi,
I am running a program with many loops so I'm trying to accelerate it. In my original code, I create a new model for each k and each t, as follows (objective and other constraints are omitted):
for k in range(K):
for t in range(T):
model = Model('forward')
x = model.addVars(H, vtype=GRB.BINARY)
aa = model.addVars(H, I, vtype=GRB.CONTINUOUS)
bb = model.addVars(I, vtype=GRB.CONTINUOUS)
cc = model.addVars(I, vtype=GRB.CONTINUOUS)
for i in range(I):
model.addQConstr(4 * omega[i, t, k] + quicksum(aa[h, i] ** 2 for h in range(H)) + bb[i] ** 2 <= cc[i] ** 2, "SOC1")
for h in range(H):
for i in range(I):
model.addConstr(aa[h, i] == 2 * np.sqrt((omega[i, t, k]  omega[i, t, k] * r[h]) * w[i, h]) * x[h], "Constr_aa")
model.optimize()
Based on the code above, it can be concluded that each model I create in the loop does not significantly differ; only the data “omega[i,t,k]” changes, which appear as constant terms and part of coefficients in the constraints.
Therefore, I want to create the model only once at the beginning, and during the loops over t and k, only change the parameter omega. Is there an easy way to achieve this? This way, the time taken to rebuild the model can be eliminated. Many thanks in advance!
Best regards,
Wenjia

Hi Wenjia,
I think the following post may help.
https://support.gurobi.com/hc/enus/community/posts/26483086080273QuadraticConstraintModificationCan you have a read and see if any questions remain?
 Riley
0 
Hi Riley,
Thank you so much for your response. I now understand how to change the coefficients. However, if I want to change the value of “omega” in my SOC1, I can only do so by removing the entire constraint and readding it using addConstr(). Is that correct?
Best,
Wenjia
0 
Hi Wenjia,
Not quite. Essentially you want to reformulate (with auxiliary variables and constraints) so that the omega coefficient appears in a linear constraint as that is the only constraint type that permits coefficients to be changed with the Model.chgCoeff method.
 Riley
0 
Hi Riley,
I think you mean adding a new variable u (with its lb=1, ub=1) and a new variable v, and rewriting SOC1 as:
v + quicksum(aa[h, i] ** 2 for h in range(H)) + bb[i] ** 2 <= cc[i] ** 2
v == 4 * omega[I, t, k] * uthen using Model.chgCoeff() to change the coefficient of u?
Best,
Wenjia
0 
Actually, if these omega values are not coefficients and just constants then you can adjust with the RHS property:
c = model.addConstr(v == 4* omega[i, t, k])
model.addQConstr(v + quicksum(aa[h, i] ** 2 for h in range(H)) + bb[i] ** 2 <= cc[i] ** 2, "SOC1")then you can modify c.RHS to 4*(new value of omega)
and also
c2 = model.addConstr(v2 == omega[i, t, k]  omega[i, t, k] * r[h])
model.addConstr(aa[h, i] == 2 * np.sqrt(v2 * w[i, h]) * x[h], "Constr_aa")then change the value of c2.RHS to a new value
0 
Thank you so much. I really appreciate the time and effort you put into assisting me. Wish you have a great day!
Best,
Wenjia
0
Please sign in to leave a comment.
Comments
6 comments