You can read the coefs and then do any computation, remove the constraint and add a new one.
If I use the
zoo example
from docplex.mp.model import Model
mdl = Model(name='buses')
nbbus40 = mdl.integer_var(name='nbBus40')
nbbus30 = mdl.integer_var(name='nbBus30')
mdl.add_constraint(nbbus40*40 +nbbus30*30 >= 300, 'kids')
mdl.add_constraint(nbbus40*nbbus40*40 + nbbus30*nbbus30*30 <= 1800, 'kidsquad')
mdl.minimize(nbbus40*500 + nbbus30*400)
mdl.solve()
for v in mdl.iter_integer_vars():
print(v," = ",v.solution_value)
qc = mdl.find_matching_quadratic_constraints('kidsquad')[0]
rhs= qc.rhs
coef40=qc.lhs.get_quadratic_coefficient(nbbus40)
coef30=qc.lhs.get_quadratic_coefficient(nbbus30)
print("qc.rhs = ",qc.rhs)
print(coef40," and ",coef30," are the coefs")
coef40=1.2*coef40
coef30=1.2*coef30
mdl.remove_constraint(qc)
mdl.add_constraint(nbbus40*nbbus40*coef40+nbbus30*nbbus30*coef30<=rhs)
mdl.solve()
for v in mdl.iter_integer_vars():
print(v," = ",v.solution_value)
mdl.export("c:\\temp\\buses.lp")
gives
nbBus40 = 6.0
nbBus30 = 2.0
qc.rhs = 1800
40 and 30 are the coefs
nbBus40 = 3.0
nbBus30 = 6.0
and the exported lp is
Minimize
obj: 500 nbBus40 + 400 nbBus30
Subject To
kids: 40 nbBus40 + 30 nbBus30 >= 300
qc1: [ 48 nbBus40^2 + 36 nbBus30^2 ] <= 1800
Bounds
Generals
nbBus40 nbBus30
End
------------------------------
[Alex] [Fleischer]
[Data and AI Technical Sales]
[IBM]
------------------------------
Original Message:
Sent: Thu September 01, 2022 06:44 PM
From: Wei Su
Subject: How to modify coefficients of a quadratic constraint and re-optimizer?
In the attached .sav file, there is a simple quadratic constrained optimization problem.
from docplex.mp.model_reader import ModelReader
mdl = ModelReader.read('portfolio_miqp.sav')
Reads it in.
qc = mdl.find_matching_quadratic_constraints('risk')[0]
Will extract the quadratic constraint.
lhs = qs.lhs
Will get the left hand side expression. And it looks like this:
>>> lhs
docplex.mp.quad.QuadExpr(0.100frac_treasury^2+19frac_hardware^2-4frac_hardware*frac_theater+8frac_hardware*frac_telecom+2frac_hardware*frac_brewery+2frac_hardware*frac_highways+2frac_hardware*frac_cars+frac_hardware*frac_bank+20frac_hardware*frac_software+10frac_hardware*frac_electronics+28frac_theater^2+2frac_theater*frac_telecom+4frac_theater*frac_brewery+2frac_theater*frac_highways+2frac_theater*frac_cars-4frac_theater*frac_software-2frac_theater*frac_electronics+22frac_telecom^2+2frac_telecom*frac_highways+4frac_telecom*frac_cars+6frac_telecom*frac_software+8frac_telecom*frac_electronics+4frac_brewery^2-3frac_brewery*frac_highways-4frac_brewery*frac_cars-2frac_brewery*frac_bank+2frac_brewery*frac_software+2frac_brewery*frac_electronics+3.500frac_highways^2+4frac_highways*frac_cars+frac_highways*frac_bank+2frac_highways*frac_software+3frac_highways*frac_electronics+5frac_cars^2+frac_cars*frac_bank+2frac_cars*frac_software+5frac_cars*frac_electronics+frac_bank^2+frac_bank*frac_software+frac_bank*frac_electronics+25frac_software^2+16frac_software*frac_electronics+16frac_electronics^2)
Now, I want to pick all the terms with a specific variable, say fraction_hardware, and increases their coefficients by 20%. For example, I want to change
8frac_hardware*frac_telecom
to
9.6frac_hardware*frac_telecom
and do the optimization. But it seems very hard to me. Does anyone know a good way to achieve this purpose? If you have any out-of-the-box solution, that is also great. Please do share. I have been stuck on this for quite a while. Any help would be great. Thanks!
Wei
------------------------------
Wei Su
------------------------------
#DecisionOptimization