Trying to solve 1/x with Gurobi
AnsweredHello,
I'm new in Gurobi and everything linked to linear programming. I may ask stupid things, forgive me in advance, I tried my best to sort out this problem by myself, and I failed.
I have this equation: f(x) = ax + b / x + c
a = 5 * 10^-8 ; b = 180 ; c = 0,039
x > 0
I want to minimize it.
I know this is a convex function and a solution exists because:
Derivative of f(x) = a - b / x^2 which is negative until x = √(b/a) then positive
Also f''(x) = 2b / x^3, because x>0 f''(x) is always positive.
Because f'(x) = 0 when x = 60000, the solution min = 0.045
On paper, it sounds ok.
On Python with Gurobipy, I typed:
import gurobipy as grbI did it this way because I saw that post here
prob = grb.Model(name="Min lot")
prob.setParam("NonConvex", 2)
x1 = prob.addVar(
vtype=grb.GRB.CONTINUOUS,
lb=1,
name="lot"
)
x2 = prob.addVar(
vtype=grb.GRB.CONTINUOUS,
name="lot^-1"
)
prob.addConstr(x1 * x2 == 1)
prob.setObjective(
(x1 * 0.00000005) + 180 * x2 + 0.039,
grb.GRB.MINIMIZE)
prob.optimize()
print(f"Optimal objective value: {prob.objVal}")
print(f"Solution values: x1={x1.X}, x2={x2.X}")
Gurobi does not support dividing by variables. However, one can model the expression 1x by introducing a continuous variable z and adding the constraint x*z=1
I cannot understand what is incorrect.
The solution I get is:
Optimal solution found (tolerance 1.00e-04)
Best objective 1.800390000500e+02, best bound 1.800390000500e+02, gap 0.0000%
Optimal objective value: 180.03900005
Solution values: x1=1.0, x2=1.0
I hope you could help me to progress. Thank you.
-
Your code produces the correct result when solved with Gurobi 9.5:
Set parameter NonConvex to value 2
Gurobi Optimizer version 9.5.2 build v9.5.2rc0 (win64)
Thread count: 4 physical cores, 8 logical processors, using up to 8 threads
Optimize a model with 0 rows, 2 columns and 0 nonzeros
Model fingerprint: 0xa7816446
Model has 1 quadratic constraint
Coefficient statistics:
Matrix range [0e+00, 0e+00]
QMatrix range [1e+00, 1e+00]
Objective range [5e-08, 2e+02]
Bounds range [1e+00, 1e+00]
RHS range [0e+00, 0e+00]
QRHS range [1e+00, 1e+00]
Continuous model is non-convex -- solving as a MIP
Presolve time: 0.00s
Presolved: 4 rows, 3 columns, 5 nonzeros
Presolved model has 1 bilinear constraint(s)
Variable types: 3 continuous, 0 integer (0 binary)
Root relaxation: objective 3.900010e-02, 0 iterations, 0.00 seconds (0.00 work units)
Nodes | Current Node | Objective Bounds | Work
Expl Unexpl | Obj Depth IntInf | Incumbent BestBd Gap | It/Node Time
H 0 0 90.0390001 0.03900 100% - 0s
H 0 0 0.0450000 0.03900 13.3% - 0s
0 2 0.04200 0 1 0.04500 0.04200 6.67% - 0s
Explored 3 nodes (1 simplex iterations) in 0.02 seconds (0.00 work units)
Thread count was 8 (of 8 available processors)
Solution count 2: 0.045 90.039
Optimal solution found (tolerance 1.00e-04)
Best objective 4.500000000000e-02, best bound 4.499996664406e-02, gap 0.0001%
Optimal objective value: 0.045000000000002996
Solution values: x1=59999.940000756294, x2=1.666668333313991e-05Maybe you are using an old version that did not handle the problem correctly?
Cheers,
Matthias1 -
Hello Matthias,
You were right, I have updated Gurobipy and I get the expected result.
Sorry for disturbing you for that, I should have had updated before posting as good practive but I thought too sure my code was the issue.
Thanks again, I really appreciate your help and time here.
0 -
No worries!
Have a great weekend!
0
Please sign in to leave a comment.
Comments
3 comments