Why Gurobi cannot find the solution to a simple Quadratic Optimization Problem?
Answered
import gurobipy as gp
from gurobipy import GRB
import numpy as np
import scipy.sparse as sp
# Create a new model
m = gp.Model("matrix1")
# Create variables
x = m.addMVar(shape=7, vtype=GRB.CONTINUOUS, name="x")
# Set objective
obj = np.array([1, 0.1, 0.8, 0.3, 0.9,0.4,0.4])*0.01 # expected monthly returns
m.setObjective(obj @ x , GRB.MAXIMIZE) #50bps per trade; 50bps per month for shorting
Sigma_psd = np.diag(np.array([30, 0.2, 20, 16, 32, 28, 19])*0.0001)
# Define the quadratic constraint; 5% per month for the portfolio
V = (0.05) ** 2
# Add the quadratic constraint to the model
m.addConstr(x @ Sigma_psd @ x == V, name="quadratic_constraint")
# Optimize model
m.optimize()
weights = x.X
print("Weights from Gurobi:", ", ".join(f"{w:g}" for w in weights))
print(f"Obj: {m.ObjVal:g}")
Sigma_inv = np.linalg.inv(Sigma_psd) # Inverse of Sigma
weights = np.dot(Sigma_inv, obj) # Dot product of inv(Sigma) and obj
weights = np.sqrt(V/np.dot(weights.T, np.dot(Sigma_psd, weights)))*weights
print("Correct Weights:", ", ".join(f"{w:g}" for w in weights))
print(f"Obj under Correct Weights: {np.dot(weights.T, obj):g}")
0

Hi,
I solved your model using Gurobi version 11.0.3.Optimal solution found (tolerance 1.00e04) Best objective 1.661528493733e02, best bound 1.661528493733e02, gap 0.0000%
Note that the default lower bound value for the variables is 0.
Regards
Ahmed0 
Ahmed,
Thanks a lot. I didn't realize that the default lower bound value is 0. Once I relax it, Gurobi finds the correct answer. Thanks!
Zhongjin
0
Please sign in to leave a comment.
Comments
2 comments