Gekko returns 整数变量的非整数结果
Gekko returns a non-integer result for an integer variable
我正在尝试使用 Gekko 解决非线性优化问题。尽管将其中一个变量定义为整数,Gekko 仍然 returns 是一个浮点数。我尝试编写一个随机的、更简单的优化问题(如下所示),同样的事情发生了。为什么会这样?如何让 Gekko 求解整数值?
from gekko import Gekko
m = Gekko()
x1 = m.Var(value=2, ub=30, name="x1")
x2 = m.Var(value=1, lb=0, name="x2")
x3 = m.Var(value=4, name="x3", integer=True)
i1 = m.Intermediate(2*x1/x2)
i2 = m.Intermediate(x3**2)
m.Equation(x1 >= x2)
m.Equation(i1 >= 1)
m.Equation(i2 <= 28)
m.Maximize(x1**2 + x2**2 + x3**3)
m.solve()
print("Results:")
print(f"{x1[0]=}")
print(f"{x2[0]=}")
print(f"{x3[0]=}")
Gekko 结果:
x1 = 30.0
x2 = 30.00000031
x3 = 5.2915026231
将求解器从默认 IPOPT
更改为 APOPT
以获得整数解。
m.options.SOLVER=1 # 1=APOPT, 2=BPOPT, 3=IPOPT
这里是关于 solver selection and additional details in the Gekko documentation 的更多信息。修改后的代码产生一个整数解:
----------------------------------------------
Steady State Optimization with APOPT Solver
----------------------------------------------
Iter: 1 I: 0 Tm: 0.00 NLPi: 5 Dpth: 0 Lvs: 2 Obj: -1.95E+03 Gap: NaN
Iter: 2 I: -1 Tm: 0.00 NLPi: 1 Dpth: 1 Lvs: 1 Obj: -1.95E+03 Gap: NaN
--Integer Solution: -1.92E+03 Lowest Leaf: -1.92E+03 Gap: 0.00E+00
Iter: 3 I: 0 Tm: 0.00 NLPi: 2 Dpth: 1 Lvs: 1 Obj: -1.92E+03 Gap: 0.00E+00
Successful solution
---------------------------------------------------
Solver : APOPT (v1.0)
Solution time : 1.590000000214786E-002 sec
Objective : -1925.00000000000
Successful solution
---------------------------------------------------
Results:
x1[0]=30.0
x2[0]=30.0
x3[0]=5.0
我正在尝试使用 Gekko 解决非线性优化问题。尽管将其中一个变量定义为整数,Gekko 仍然 returns 是一个浮点数。我尝试编写一个随机的、更简单的优化问题(如下所示),同样的事情发生了。为什么会这样?如何让 Gekko 求解整数值?
from gekko import Gekko
m = Gekko()
x1 = m.Var(value=2, ub=30, name="x1")
x2 = m.Var(value=1, lb=0, name="x2")
x3 = m.Var(value=4, name="x3", integer=True)
i1 = m.Intermediate(2*x1/x2)
i2 = m.Intermediate(x3**2)
m.Equation(x1 >= x2)
m.Equation(i1 >= 1)
m.Equation(i2 <= 28)
m.Maximize(x1**2 + x2**2 + x3**3)
m.solve()
print("Results:")
print(f"{x1[0]=}")
print(f"{x2[0]=}")
print(f"{x3[0]=}")
Gekko 结果:
x1 = 30.0
x2 = 30.00000031
x3 = 5.2915026231
将求解器从默认 IPOPT
更改为 APOPT
以获得整数解。
m.options.SOLVER=1 # 1=APOPT, 2=BPOPT, 3=IPOPT
这里是关于 solver selection and additional details in the Gekko documentation 的更多信息。修改后的代码产生一个整数解:
----------------------------------------------
Steady State Optimization with APOPT Solver
----------------------------------------------
Iter: 1 I: 0 Tm: 0.00 NLPi: 5 Dpth: 0 Lvs: 2 Obj: -1.95E+03 Gap: NaN
Iter: 2 I: -1 Tm: 0.00 NLPi: 1 Dpth: 1 Lvs: 1 Obj: -1.95E+03 Gap: NaN
--Integer Solution: -1.92E+03 Lowest Leaf: -1.92E+03 Gap: 0.00E+00
Iter: 3 I: 0 Tm: 0.00 NLPi: 2 Dpth: 1 Lvs: 1 Obj: -1.92E+03 Gap: 0.00E+00
Successful solution
---------------------------------------------------
Solver : APOPT (v1.0)
Solution time : 1.590000000214786E-002 sec
Objective : -1925.00000000000
Successful solution
---------------------------------------------------
Results:
x1[0]=30.0
x2[0]=30.0
x3[0]=5.0