How To Solve Absolute Value Abs() Objective With Python Gekko?
An optimization problem with a squared objective solves successfully with IPOPT in Python Gekko. from gekko import GEKKO import numpy as np m = GEKKO() x = m.Var(); y = m.Param(3.2
Solution 1:
You can use m.abs2 instead, It takes into account the issue with the derivative and should solve the issue.
Solution 2:
Here is one possible solution using gekko's binary switch variable:
from gekko import GEKKO
import numpy as np
m = GEKKO()
y = m.Param(3.2)
x = m.Var()
#intermediate
difference = m.Intermediate(x - y)
f = m.if3(difference, -difference, difference)
m.Obj(f)
m.solve()
print(x.value[0],y.value[0])
Returns: 3.2 3.2
m.if3(condition, x1, x2)
takes value as a condition, and returns x1
if condition >= 0
or x1
if condition < 0
.
There are various functions to get around this problem in the logical functions section of the documentation, including m.abs2
, m.abs3
, and m.if2
.
The type 2 functions use MPCC to solve and will continue using IPOPT. The type 3 functions will change to APOPT automatically.
https://github.com/BYU-PRISM/GEKKO/blob/master/docs/model_methods.rsthttps://gekko.readthedocs.io/en/latest/model_methods.html#logical-functions
Post a Comment for "How To Solve Absolute Value Abs() Objective With Python Gekko?"