You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Gekko结合APOPT求解器无法优化分段线性(PWL)函数至最优值的问题求助

Gekko结合APOPT求解器无法优化分段线性(PWL)函数至最优值的问题求助

我最近碰到个头疼的问题:用APOPT求解器优化一个无约束的单分段线性函数时,没法得到预期的最优值,这可把我搞懵了。我感觉自己对model.pwl的用法还有没搞懂的地方,但相关资料真的不好找。

下面是我写的最小复现示例:

model = GEKKO(remote=False)
model.options.SOLVER = 1
model.solver_options = ["minlp_as_nlp 0"]

x = model.sos1([0, 1, 2, 3, 4]) # 换成model.Var(lb=0, ub=4)结果也一样
pwl = model.Var()

model.pwl(x, pwl, [0, 1, 2, 3, 4], [30, 30.1, 30.2, 30.3, 30.4], bound_x=True)

model.Minimize(pwl)

model.solve(display=True)

print(x.value)
print(pwl.value)
print(model.options.objfcnval)

运行后得到的输出是:

----------------------------------------------------------------
 APMonitor, Version 1.0.3
 APMonitor Optimization Suite
 ----------------------------------------------------------------


 --------- APM Model Size ------------
 Each time step contains
   Objects      :            1
   Constants    :            0
   Variables    :            2
   Intermediates:            0
   Connections  :            2
   Equations    :            1
   Residuals    :            1

 Piece-wise linear model pwl1points:            5
 Number of state variables:             12
 Number of total equations: -            5
 Number of slack variables: -            0
 ---------------------------------------
 Degrees of freedom       :              7

 ----------------------------------------------
 Steady State Optimization with APOPT Solver
 ----------------------------------------------

 Iter    Objective  Convergence
    0  3.39503E+01  3.01000E+01
    1  3.22900E+01  1.00000E-10
    2  3.22000E+01  2.22045E-16
    4  3.22000E+01  0.00000E+00
 Successful solution

 ---------------------------------------------------
 Solver         :  APOPT (v1.0)
 Solution time  :   3.819999999541324E-002 sec
 Objective      :    32.2000000000000
 Successful solution
 ---------------------------------------------------


2.0
30.2
32.2

这结果完全出乎我的意料,明眼人都能看出来这个分段线性函数的最小值是30,但求解器却给出了32.2的目标函数值,对应的x是2.0,pwl变量值是30.2,这明显不对啊。

备注:内容来源于stack exchange,提问作者iHowell

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.14 12:30:29