gpt4 book ai didi

optimization - 优化求和函数 - GEKKO

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

我刚开始学习优化,但在寻找以下问题的最佳值时遇到了一些问题。
注意:这只是我想到的一个随机问题,并没有实际应用。

问题:



哪里x可以是列表中的任何值 ([2,4,6]) 和 y介于 1 和 3 之间。

我的尝试:

from gekko import GEKKO
import numpy as np
import math

def prob(x,y,sel):
z = np.sum(np.array(x)*np.array(sel))
cst = 0
i=0
while i <= y.VALUE:
fact = 1
for num in range(2, i + 1): # find the factorial value
fact *= num
cst += (z**i)/fact
i+=1
return cst


m = GEKKO(remote=False)

sel = [2,4,6] # list of possible x values
x = m.Array(m.Var, 3, **{'value':1,'lb':0,'ub':1, 'integer': True})
y = m.Var(value=1,lb=1,ub=3,integer=True)

# switch to APOPT
m.options.SOLVER = 1

m.Equation(m.sum(x) == 1) # restrict choice to one selection

m.Maximize(prob(x,y,sel))
m.solve(disp=True)


print('Results:')
print(f'x: {x}')
print(f'y : {y.value}')
print('Objective value: ' + str(m.options.objfcnval))

结果:

----------------------------------------------------------------
APMonitor, Version 0.9.2
APMonitor Optimization Suite
----------------------------------------------------------------


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

Number of state variables: 4
Number of total equations: - 1
Number of slack variables: - 0
---------------------------------------
Degrees of freedom : 3

----------------------------------------------
Steady State Optimization with APOPT Solver
----------------------------------------------
Iter: 1 I: 0 Tm: -0.00 NLPi: 2 Dpth: 0 Lvs: 0 Obj: -7.00E+00 Gap: 0.00E+00
Successful solution

---------------------------------------------------
Solver : APOPT (v1.0)
Solution time : 0.024000000000000004 sec
Objective : -7.
Successful solution
---------------------------------------------------


Results:
x: [[0.0] [0.0] [1.0]]
y : [1.0]
Objective value: -7.0
x应该是 [0,0,1](即 6)和 y应该是 3 以获得最大值(61)。 x我得到的值是正确的,但由于某种原因 y我得到的值是错误的。是什么导致了这个问题?我的配方有问题吗?此外,如果您能指点我有关 APOT 求解器输出中各种符号(如 Tm、NLPi 等)的更多信息,也会非常有帮助。

最佳答案

这是 gekko 中的解决方案:

x=6.0
y=3.0

您需要使用 gekko 函数来构建函数并以某种方式提出问题,以便方程不会随着变量值的变化而变化。

from gekko import GEKKO
import numpy as np
from scipy.special import factorial

m = GEKKO(remote=False)
x = m.sos1([2,4,6])
yb = m.Array(m.Var,3,lb=0,ub=1,integer=True)
m.Equation(m.sum(yb)==1)
y = m.sum([yb[i]*(i+1) for i in range(3)])
yf = factorial(np.linspace(0,3,4))
obj = x**0/yf[0]
for j in range(1,4):
obj += x**j/yf[j]
m.Maximize(yb[j-1]*obj)
m.solve()
print('x='+str(x.value[0]))
print('y='+str(y.value[0]))
print('Objective='+str(-m.options.objfcnval))

对于您的问题,我使用了 Special Ordered Set (type 1)获得 2、4 或 6 的选项。选择 y作为 1、2 或 3,我计算了所有可能的值,然后使用了二元选择器 yb选择一个。 m.sum(yb)==1 只能使用其中之一。 .有 gekko examples , documentation , 和 short course如果您需要额外的资源,可以使用。
这是求解器输出:

 ----------------------------------------------------------------
APMonitor, Version 0.9.2
APMonitor Optimization Suite
----------------------------------------------------------------


--------- APM Model Size ------------
Each time step contains
Objects : 1
Constants : 0
Variables : 11
Intermediates: 1
Connections : 4
Equations : 10
Residuals : 9

Number of state variables: 11
Number of total equations: - 7
Number of slack variables: - 0
---------------------------------------
Degrees of freedom : 4

----------------------------------------------
Steady State Optimization with APOPT Solver
----------------------------------------------
Iter: 1 I: 0 Tm: 0.00 NLPi: 6 Dpth: 0 Lvs: 0 Obj: -6.10E+01 Gap: 0.00E+00
Successful solution

---------------------------------------------------
Solver : APOPT (v1.0)
Solution time : 0.047799999999999995 sec
Objective : -61.
Successful solution
---------------------------------------------------


x=6.0
y=3.0
Objective=61.0

以下是有关 solver APOPT options 的更多信息.迭代总结描述了 branch and bound progress .是 Iter =迭代次数, Tm = 解决 NLP 的时间, NLPi =NLP 迭代, Dpth = 分支树的深度, Lvs =候选人离开的人数, Obj =NLP 解决方案目标,以及 Gap =整数解与最佳非整数解之间的差距。

关于optimization - 优化求和函数 - GEKKO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61845226/

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