优化求和函数 - GEKKO
Optimizing a Summation function - GEKKO
我刚开始学习优化,但在为下面的问题找到最优值时遇到了一些问题。
注意:这只是我想到的一个随机问题,没有实际应用。
问题:
)
其中 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
值是错误的。是什么导致了这个问题?我的配方有问题吗?如果您能指点我更多关于 APOPT 求解器输出中的各种符号(如 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) to get the options of 2, 4, or 6. To select y
as 1, 2, or 3 I calculated all possible values and then used a binary selector yb
to choose one. There is a constraint that only one of them can be used with m.sum(yb)==1
. There are gekko examples, documentation, and a 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. The iteration summary describes the branch and bound progress 的更多信息。 Iter
=迭代次数,Tm
=求解NLP的时间,NLPi
=NLP迭代次数,Dpth
=分支树的深度,Lvs
=候选叶数,Obj
=NLP 解决方案objective,Gap
=整数解决方案与最佳非整数解决方案之间的差距。
我刚开始学习优化,但在为下面的问题找到最优值时遇到了一些问题。 注意:这只是我想到的一个随机问题,没有实际应用。
问题:
其中 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
值是错误的。是什么导致了这个问题?我的配方有问题吗?如果您能指点我更多关于 APOPT 求解器输出中的各种符号(如 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) to get the options of 2, 4, or 6. To select y
as 1, 2, or 3 I calculated all possible values and then used a binary selector yb
to choose one. There is a constraint that only one of them can be used with m.sum(yb)==1
. There are gekko examples, documentation, and a 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. The iteration summary describes the branch and bound progress 的更多信息。 Iter
=迭代次数,Tm
=求解NLP的时间,NLPi
=NLP迭代次数,Dpth
=分支树的深度,Lvs
=候选叶数,Obj
=NLP 解决方案objective,Gap
=整数解决方案与最佳非整数解决方案之间的差距。