[英]SciPy Optimization using SLSQP gives solution that does not satisfy constraints
[英]Gekko Solver does not satisfy Constraints gives wrong solution
我正在使用Gekko求解器来优化 function,但即使在不满足给定约束的简单问题中,它也会给出错误的解决方案。
from gekko import GEKKO
m = GEKKO(remote=False)
a = m.Var(value=0, integer=True)
b = m.Var(value=0, integer=True)
# constraints
m.Equation([a + b > 7, a**2 + b**2 < 40])
# Objective function
m.Maximize(a**3 + b**3)
m.solve(disp=False)
print(a.value[0])
print(b.value[0])
max_value = a.value[0]**3 + b.value[0]**3
print(max_value)
Output:
2.0
6.0
224.0
在末尾添加检查表明 Gekko 在请求的容差内找到了正确的解决方案,尽管默认求解器是 IPOPT,即使请求integer=True
也能找到连续的解决方案。
a = a.value[0]; b = b.value[0]
print('a+b>7',a+b)
print('a^2+b^2<40',a**2+b**2)
# 0.71611780666
# 6.2838821838
# 248.50000026418317
# a+b>7 6.99999999046
# a^2+b^2<40 40.00000001289459
尝试使用m.options.SOLVER=1
切换到 MINLP 求解器 APPT。 不等式<
和<=
在 Gekko 中是等价的,因为它是一个数值解。
2.0
6.0
224.0
a+b>7 8.0
a^2+b^2<40 40.0
如果在数学意义上<
或>
不允许 7 和 40 ,则在诸如>=8
和<=39
等约束条件下向上或向下移动一个 integer 。
m.Equation([a + b >= 8, \
a**2 + b**2 <= 39])
结果正确:
a=3.0 b=5.0 Objective: 152.0
a+b>=8 with a+b=8.0 (constraint satisfied)
a^2+b^2=<39 with a^2+b^2=34.0 (constraint satisfied)
这里还缺少什么吗? 为什么有人声称没有可行的解决方案?
from gekko import GEKKO
m = GEKKO(remote=False)
a = m.Var(value=0, integer=True)
b = m.Var(value=0, integer=True)
# constraints
m.Equation([a + b >= 7, \
a**2 + b**2 <= 40])
# Objective function
m.Maximize(a**3 + b**3)
m.options.SOLVER =1
m.solve(disp=True)
print(a.value[0])
print(b.value[0])
max_value = a.value[0]**3 + b.value[0]**3
print(max_value)
a = a.value[0]; b = b.value[0]
print('a+b>7',a+b)
print('a^2+b^2<40',a**2+b**2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.