Logarithmic Optimization Problem
回答済みI am very new in using Gurobi, and I encounter some problem when using "addGenConstrLog".
I try to solve a complicated log-optimal problem, and I first tried a simple version
where w is an unit simplex and X is daily history return.
Below is my code:
import numpy as np
import gurobipy as gp
from gurobipy import GRB
model = gp.Model()
N=2
n=2
w = model.addMVar((n, 1), name="w")
w_t = w.transpose()
R_h = np.array(R_hat)
# R_h=np.array[[ 4.00959308e-03 -1.35401866e-04] [ 7.39726027e-05 7.39726027e-05]]
model.addConstrs((w[i] >= 0 for i in range(n)), name="w_nonnegativity")
model.addConstr(w.sum() == 1, name="w_sum_to_1")
x1 = model.addVar(name="x1")
x2 = model.addVar(name="x2")
y1 = model.addVar(name="y1")
y2 = model.addVar(name="y2")
model.addConstr(x1 == 1 + w_t @ R_h[:, 0])
model.addConstr(y1 == 1 + w_t @ R_h[:, 1])
model.addGenConstrLog(x1, x2, name="log_term1")
model.addGenConstrLog(y1, y2, name="log_term1")
objective = (1/N) * (x2+y2)
model.setObjective(objective, sense=GRB.MAXIMIZE)
model.Params.FuncPieces = 100
model.params.FuncPieceError = 1e-6
model.optimize()
print(f'optimal value w: {w.X}')
-
Hi Xiao,
Can you try using the latest version (v11.0.1) and setting the parameter FuncNonlinear=1 (instead of the ones you are setting)
Cheers,
David0 -
Hi David,
I already use the latest version(v11.0.1) and setting the parameter
model.Params.FuncNonlinear=1
But still got the same results...I wonder if my origin code is wrong?Here's my output:I noticed that there are gap 0.0011%Thank you!0 -
Hi Xiao,
Thanks for the extra output. When creating variables the default lower bound is 0.
Hence that solution is not allowed, if we allow the \(\texttt{y}\) variable to be negative:y1 = model.addVar(name="y1", lb=-1000)
y2 = model.addVar(name="y1", lb=-1000) # Only this one is neededWe get (roughly) the expected result:
Best objective 1.933075925509e-03, best bound 1.933093487611e-03, gap 0.0009%
optimal value w: [[9.99996442e-01], [3.55767355e-06]]Cheers,
David0 -
Hi David,
Thanks a lot !
"When creating variables the default lower bound is 0."
This is very helpful for me.
But I still want to ask why there are nonzero gap?
0 -
Hi Xiao,
This is lower than the default MIPGap (1e-4).
You are right, setting the MIPGap explicitly to 0 we get:
Best objective 1.933093486649e-03, best bound 1.933093486649e-03, gap 0.0000%
optimal value w: [[1.], [0.]]Cheers,
David0
サインインしてコメントを残してください。
コメント
5件のコメント