Reputation: 317
I 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')
Upvotes: 0
Views: 823
Reputation: 51
Hi I think what you are trying to do is not supported by gurobi yet. Not at least as a quadratic programming.
First you have your variables in the denominator which is not advised / supported directly
Second what you are defining is not a quadratic problem. It is a polynomial problem. As far as I know gurobi currently supports only quadratic programs with expressions such as y*y
This is unconstrained problem so I wonder why you need gurobi. Scientific solvers deal with these problem pretty well using gradient decent, Newton and so on methods
I hope this helps
Upvotes: 2