gpt4 book ai didi

python - Gekko 找不到小问题的解决方案

转载 作者:行者123 更新时间:2023-12-03 19:13:15 25 4
gpt4 key购买 nike

我正在使用 python 中的 Gekko 库进行一些测试,并且有一个我知道解决方案的小问题。完整代码如下:

from gekko import GEKKO

P = [[3.0,3.55,5.18,7.9,5.98],
[1.56,1.56,2.48,3.15,2.38],
[1.49,4.96,6.4,9.4,6.5]]

M = [[1,2,3,4,5],
[6,7,8,9,10],
[11,12,13,14,15]]

mm = M
pp = P
c1 = [300,200,150,250,180]
qtde = [10,10,10]
flex = [0.2,0.2,0.2]

m = GEKKO(remote=False)
ni = 3
nj = 5
x = [[m.Var(lb=0,integer=True) for j in range(nj)] for i in range(ni)]

s = 0
expr = []
for i in range(ni):
for j in range(nj):
s += x[i][j]*pp[i][j]
expr.append(s)
s = 0

for i in range(ni):
for j in range(nj):
if mm[i][j] == 0:
m.Equation(x[i][j] == 0)


for i in range(len(flex)):
if flex[i] == 0:
m.Equation(sum([x[i][j] for j in range(nj)]) >= qtde[i])
else:
m.Equation(sum([x[i][j] for j in range(nj)]) >= qtde[i])
m.Equation(sum([x[i][j] for j in range(nj)]) <= (1+flex[i])*qtde[i])


b = m.Array(m.Var,nj,integer=True,lb=0,ub=1)
iv = [None]*nj



for j in range(nj):
iv[j] = m.sum([pp[i][j]*x[i][j] for i in range(ni)])
m.Equation(iv[j] >= b[j]*c1[j])


m.Obj(m.sum(expr))

m.options.SOLVER=1 # switch to APOPT
m.solver_options = ['minlp_gap_tol 1.0e-2',\
'minlp_maximum_iterations 50000',\
'minlp_max_iter_with_int_sol 50000',\
'minlp_branch_method 1',\
'minlp_integer_leaves 2']


m.solve()

for j in range(nj):
m.Equation((1 - b[j])*iv[j] == 0)

m.options.SOLVER=1
m.solve()



代码退出并出现错误: Exception: @error: Solution Not Found .这很奇怪,因为有一个明确的解决方案:
x = [[0,0,12,0,0],
[0,0,12,0,0],
[0,0,12,0,0]]

更奇怪的是,即使我极大地增加了变量 qtde 的值。 (例如, qtde = [40,40,40] ),算法无法找到解决方案。我写约束的方式有什么错误吗?

最佳答案

有时,求解器需要帮助进行更好的初始猜测或选择性边界,以远离有问题的解决方案。这是仅通过一个求解器调用就可以帮助解决问题的方法。

lower = [0,0,4,0,0]
for i in range(ni):
for j in range(nj):
x[i][j].value = 5
x[i][j].lower = lower[j]
x[i][j].upper = 20

我总是收到 infeasible solution如果我将所有生成单元的下限设置为零,则消息。求解器似乎陷入了全零的试验解或当所有都低于某个阈值时。在这种情况下,我必须将中间单元限制在 4 以上才能获得成功的解决方案,而其他单元则为零。这是完整的代码:

from gekko import GEKKO

P = [[3.0,3.55,5.18,7.9,5.98],
[1.56,1.56,2.48,3.15,2.38],
[1.49,4.96,6.4,9.4,6.5]]

M = [[1,2,3,4,5],
[6,7,8,9,10],
[11,12,13,14,15]]

mm = M
pp = P
c1 = [300,200,150,250,180]
qtde = [10,10,10]
flex = [0.2,0.2,0.2]

m = GEKKO(remote=False)
ni = 3
nj = 5
x = [[m.Var(integer=True) for j in range(nj)] for i in range(ni)]

# Fix x at values to check the feasibility of the initial guess
#x = [[m.Param() for j in range(nj)] for i in range(ni)]

lower = [0,0,4,0,0]
for i in range(ni):
for j in range(nj):
x[i][j].value = 5
x[i][j].lower = lower[j]
x[i][j].upper = 20

s = 0
expr = []
for i in range(ni):
for j in range(nj):
s += x[i][j]*pp[i][j]
expr.append(s)
s = 0

for i in range(ni):
for j in range(nj):
if mm[i][j] == 0:
m.Equation(x[i][j] == 0)


for i in range(len(flex)):
if flex[i] == 0:
m.Equation(sum([x[i][j] for j in range(nj)]) >= qtde[i])
else:
m.Equation(sum([x[i][j] for j in range(nj)]) >= qtde[i])
m.Equation(sum([x[i][j] for j in range(nj)]) <= (1+flex[i])*qtde[i])


b = m.Array(m.Var,nj,value=0.5,integer=True,lb=0,ub=1)
iv = [None]*nj



for j in range(nj):
iv[j] = m.sum([pp[i][j]*x[i][j] for i in range(ni)])
m.Equation(iv[j] >= b[j]*c1[j])


m.Obj(m.sum(expr))

for j in range(nj):
m.Equation((1 - b[j])*iv[j] <= 1e-5)

print('Initial guess: ' + str(x))

# solve as NLP first to see iterations
#m.solver_options = ['minlp_as_nlp 1']
#m.options.SOLVER = 1
#m.solve(debug=0)



# solve as MINLP
m.options.SOLVER=1 # switch to APOPT
m.solver_options = ['minlp_gap_tol 1.0e-2',\
'minlp_maximum_iterations 50000',\
'minlp_max_iter_with_int_sol 50000',\
'minlp_branch_method 1',\
'minlp_integer_leaves 2']

m.options.SOLVER=1
m.solve(disp=False)

print('Final solution: ' + str(x))

使用完美的求解器,不需要初始猜测并且可以从 0 设置边界。至 infinity .有些问题更难解决,例如混合整数变量的问题和使用互补条件时的问题。您的问题同时存在,所以我对求解器在没有初始猜测或适当界限的情况下挣扎并不感到惊讶。

关于python - Gekko 找不到小问题的解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61469170/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com