Need help with Objective function
AnsweredI am trying to convert an objective function from scipy to Gurobi as follows but getting "unsupported operand type(s) for ** or pow(): 'gurobipy.LinExpr' and 'float'". Any idea how I could re-write the below? Thanks in advance.
from gurobipy import *
import scipy.optimize as optimize
price = 95.0428
par = 100.0
T = 1.5
coup = 5.75
freq = 2
guess = 0.05
freq = float(freq)
periods = T * freq
coupon = coup / 100. * par / freq
dt = [(i + 1) / freq for i in range(int(periods))]
#coverting the below scipy.optimize to Gurobi
#ytm_func = lambda y: sum([coupon / (1 + y / freq) ** (freq * t) for t in dt]) + (par / (1 + y / freq) ** (freq * T)) - price
#optimize.newton(ytm_func, guess)
m = Model()
y = m.addVar(vtype=GRB.CONTINUOUS, name='y')
m.setObjective(quicksum([coupon / (1 + y / freq) ** (freq * t) for t in dt]) + (par / (1 + y / freq) ** (freq * T)) - price, GRB.MINIMIZE)
m.optimize()
m.printAttr('X')-
Official comment
This post is more than three years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum. Or why not try our AI Gurobot?. -
Hi Suhas,
The scipy.optimize.newton method finds a zero of the given function. This is different from what is meant by the optimization problem in your code. As written, the goal of your optimization problem is to find a nonnegative value of y that minimizes the objective function. The optimal solution to this problem is to make y as large as possible (i.e., the objective function value approaches -95.0428 as y approaches infinity).
To mimic the behavior of scipy.optimize.newton, you should instead add a constraint that the function must equal zero. Then the solver would find a y for which the function evaluates to zero. That said, the current version of Gurobi (8.1.1) can solve convex mixed-integer quadratic programs. This means the objective function and constraints should be linear or convex quadratic. Unfortunately, the problem you consider does not fit this form, so Gurobi will not be able to solve it.
The next version of Gurobi (9.0) will be able to solve general non-convex quadratic programs. Depending on the parameter values, it may be possible to restructure your problem to be of this form.
About the error: the ** and pow() operators aren't supported when building a constraint or objective. Instead, you can write the product of two variables explicitly, e.g.,
m.addConstr(x*x + y*y <= 2)
I hope this helps!
Eli
0 -
Thanks Eli, I understand now.
0 -
Hello Eli,
I have a question about the ** and pow() operators, more specifically the product of more than 2 variables, for example : m.setObjective (x*x*x) . I see that this doesn't work too, even in version: Gurobi (9.0). On the other hand I see that the m.addGenConstrPow() method can be used but I don't understand what exactly it returns.
Any hint will be very helpful.Thank you.
Regards,
Ibtissam
0 -
Hi Ibtissam,
Gurobi 9.0 supports optimization for models with general (convex or non-convex) quadratic constraints and objectives. Unfortunately, because \( x^3 \) is a cubic expression, it is not directly supported by Gurobi. See here for more information on the types of models that Gurobi is able to solve.
Like you mentioned, you could use the Model.addGenConstrPow() method to model the function \( x^3 \). The key difference is that with this method, Gurobi constructs a piecewise-linear approximation of the specified function ( \( x^3 \) ) and optimizes with respect to this approximation. Thus, Gurobi actually solves an approximate version of the problem. You have some control over the quality of this approximation with the attributes FuncPieces, FuncPieceError, etc.
In theory, we can model arbitrary polynomial functions in an extended variable space using only quadratic constraints. For example, if we introduce additional variables \( y \) and \( z \) and add the constraints \( y = x^2 \) and \( z = xy \), we have effectively modeled \( z = x^3 \). However, in reality, numerical errors can be amplified throughout such decompositions, leading to solutions that are inaccurate with respect to the true function we are trying to model.
Eli
0 -
Thank you for your answer Eli.
Regards,
Ibtissam
0
Post is closed for comments.
Comments
6 comments