简体   繁体   English

Gekko Solver 不满足 Constraints 给出错误的解决方案

[英]Gekko Solver does not satisfy Constraints gives wrong solution

I was using Gekko solver for optimizing function, but it gives wrong solution even in simple problems where it also not satisfying the given constraints.我正在使用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: Output:

2.0
6.0
224.0

Adding a check at the end reveals that Gekko finds a correct solution within the requested tolerance although the default solver is IPOPT that finds a continuous solution even when integer=True is requested.在末尾添加检查表明 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

Try switching to the MINLP solver APOPT with m.options.SOLVER=1 .尝试使用m.options.SOLVER=1切换到 MINLP 求解器 APPT。 Inequalities < and <= are equivalent in Gekko because it is a numerical solution.不等式<<=在 Gekko 中是等价的,因为它是一个数值解。

2.0
6.0
224.0
a+b>7 8.0
a^2+b^2<40 40.0

If it is < or > in the mathematical sense that 7 and 40 are not allowable then shift up or down one integer on the constraints such as to >=8 and <=39 .如果在数学意义上<>不允许 7 和 40 ,则在诸如>=8<=39等约束条件下向上或向下移动一个 integer 。

m.Equation([a + b >= 8, \
            a**2 + b**2 <= 39])

Results are correct:结果正确:

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)

Is there something else missing here?这里还缺少什么吗? Why is there a claim of no feasible solution?为什么有人声称没有可行的解决方案?

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM