Using PWL and Multi-Objectives together
AnsweredIn this post, it is mentioned that setObjectiveN cannot be used together with setPWLObj. However, this week I have attended the Gurobi Days event in Paris and I believe it was mentioned that they can actually be used together if setObjectiveN is called first and setPWLObj is called right after. Doing the opposite, i.e. calling first setPWLObj and then setObjectiveN, would result in Gurobi overwriting setPWLObj with the setObjectiveN (at least this is what was mentioned during the event). Moreover, it was said that using a combination of setObjectiveN and setPWLObj is much faster than using piecewise-linear constraints as mentioned here.
Could you please confirm me that is indeed possible to combine together setObjectiveN and setPWLObj as mentioned above?
-
Hi Tomas,
Piecewise-linear(PWL) objectives are not directly supported with the multi-objective feature. Note that setObjectiveN requires an expression as input and it is not possible to provide a PWL expression.
If you want to work with PWL objectives and multiple objectives, you can apply a simple workaround. You can introduce an auxiliary variable \(z\) and then use the addGenConstrPWL method to add the constraint \(z = \text{PWL function}\). You can then minimize/maximize \(z\) in the respective objective.
Moreover, it was said that using a combination of setObjectiveN and setPWLObj is much faster than using piecewise-linear constraints as mentioned here.
Could you point me to the paragraph where it is said that using setObjectiveN and setPWLObj is much faster than using PWL constraints?
Best regards,
Jaromił0 -
Hi Jaromił,
thanks for your reply. Regarding the workaround using an auxiliary variable z, I am already using that as my current approach to use a piecewise linear objective. However, I was wondering if it is possible to use the setPWLObj instead of the addGenConstrPWL method (with auxiliary variable), together with setObjectiveN. What happens if I first define an objective with setObjectiveN and one line later I define another one with setPWLObj? Will the objective defined with setObjectiveN be overwritten or will Gurobi sum the two objectives?
Could you point me to the paragraph where it is said that using setObjectiveN and setPWLObj is much faster than using PWL constraints?
I cannot unfortunately, as this emerged during a question/discussion after a presentation at the Gurobi Days event in Paris.
Best regards,
Tomas
0 -
Hi Tomas,
I just tried it to confirm it and this is currently not supported. The following simple code
import gurobipy as gp
from gurobipy import GRB
from math import exp
def f(u):
return exp(-u)
def g(u):
return 2 * u * u - 4 * u
m = gp.Model()
y = m.addVar(0,1, name='y')
z = m.addVar(0,1, name='z')
npts = 101
ptu = []
ptf = []
ptg = []
for i in range(npts):
ptu.append(0 + (1 - 0) * i / (npts - 1))
ptf.append(f(ptu[i]))
ptg.append(g(ptu[i]))
m.setObjectiveN(0, 0, 1)
m.setPWLObj(y, ptu, ptf)
m.setObjectiveN(0, 1, 2)
m.setPWLObj(z, ptu, ptg)
m.setPWLObj(y, ptu, ptf)
m.optimize()results in the expected error
gurobipy.GurobiError: Multi-objectives with piecewise-linear terms
Please let me know if this is what you had in mind.
Best regards,
Jaromił0 -
Hi Jaromił,
Thanks again for your reply, this indeed is very clarifying. I think what was said at the Gurobi Days event was that setPWLObj could be used together with setObjective - not setObjectiveN. In that case the combination is possible and it should be faster to implement a PWL cost using setPWLObj rather than defining an auxiliary variable y and setting it equal to the PWL constraints (but once again I have no source for that).
Best regards,
Tomas
0 -
Hi Tomas,
For setObjective your statement is true.
Best regards,
Jaromił0
Please sign in to leave a comment.
Comments
5 comments