当我运行GEKKO优化时,我得到一个“FileNotFoundError”

2024-10-01 04:47:22 发布

您现在位置:Python中文网/ 问答频道 /正文

当我运行GEKKO优化时,我得到一个“FileNotFoundError”,请告诉我如何处理它。 我的代码有问题吗? Y是二进制整数决策变量

#initialize gekko
model = GEKKO(remote=False)
#APOPT is an Mixed Integer Nonlinear Problem solver
model.options.SOLVER = 1
model.time
#optional solver settings with APOPT
model.solver_options = ['minlp_maximum_iterations 500', \
                    # minlp iterations with integer solution
                    'minlp_max_iter_with_int_sol 10', \
                    # treat minlp as nlp
                    'minlp_as_nlp 0', \
                    # nlp sub-problem max iterations
                    'nlp_maximum_iterations 50', \
                    # 1 = depth first, 2 = breadth first
                    'minlp_branch_method 1', \
                    # maximum deviation from whole number
                    'minlp_integer_tol 0.05', \
                    # covergence tolerance
                    'minlp_gap_tol 0.01']

#parameter
X = total_PV_set
k = len(X)
eq = model.Param(value=len(X))
eq1 = model.Param(value=1)

#Decision Variable
# N = model.Var(value=1, lb=1, ub=k, integer=True)
N = 3
Y = model.Array(model.Var, (N, k), lb=0, ub=1, integer=True)
V = model.Array(model.Var, (N, 1))
W = model.Array(model.Var, (N, 1))
vary = model.Array(model.Var, (N, 1))
covary = model.Array(model.Var, (N, 1))

#Constraints
for i in range(N):
    vary_buff = 0
    for j in range(k):
        vary_buff += model.Intermediate(variance(X[j]) * Y[i][j])
    model.Equation(vary[i] == vary_buff)
for i in range(N):
    covary_buff = 0
    for j in range(k):
        for e in range(k-1):
            if j < (e+1):
                covary_buff += model.Intermediate(2*covariance(X[j], X[e+1])*Y[i][j]*Y[i][e+1])
    model.Equation(covary[i] == covary_buff)
for i in range(N):
    model.Equation(V[i] == model.Intermediate(vary[i]+covary[i]))
for i in range(N):
    model.Equation(W[i] == model.Intermediate(model.sum(Y[i][:])))
model.Equation(model.sum(Y) == eq)
for i in range(k):
    model.Equation(model.sum(Y[:, i]) == eq1)


cc = model.Intermediate(model.sum([(W[i]*V[i]) for i in range(N)]))
model.Obj(cc/model.sum(W))

#minimize objective
# model.options.IMODE = 3
# model.options.MEAS_CHK = 0
model.solve()

#Print the results
print ('--- Results of the Optimization Problem ---')
print('Y: '+str(Y))
print('N: '+str(N))
print('V: '+str(V))
print('W: '+str(W))
print('Objective: '+str(model.options.objfcnval))

Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 23:09:28) [MSC v.1916 64 bit (AMD64)] on win32 runfile('C:/Users/chldj/EOJIN/VPP_test.py', wdir='C:/Users/chldj/EOJIN') Backend TkAgg is interactive backend. Turning interactive mode on. C:/Users/chldj/EOJIN/VPP_test.py:91: DeprecationWarning: elementwise comparison failed; this will raise an error in the future. model.Equation(vary[i] == vary_buff) C:/Users/chldj/EOJIN/VPP_test.py:98: DeprecationWarning: elementwise comparison failed; this will raise an error in the future. model.Equation(covary[i] == covary_buff) C:/Users/chldj/EOJIN/VPP_test.py:100: DeprecationWarning: elementwise comparison failed; this will raise an error in the future. model.Equation(V[i] == model.Intermediate(vary[i]+covary[i])) C:/Users/chldj/EOJIN/VPP_test.py:102: DeprecationWarning: elementwise comparison failed; this will raise an error in the future. model.Equation(W[i] == model.Intermediate(model.sum(Y[i][:]))) ---------------------------------------------------------------- APMonitor, Version 0.9.2 APMonitor Optimization Suite ----------------------------------------------------------------

Error: Exception: Access Violation
At line 2391 of file custom_parse.f90
Traceback: not available, compile with -ftrace=frame or -ftrace=full
Error: 'results.json' not found. Check above for additional error details
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "C:\pycharm\PyCharm Community Edition 2019.2.2\helpers\pydev\_pydev_bundle\pydev_umd.py", line 197, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
  File "C:\pycharm\PyCharm Community Edition 2019.2.2\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "C:/Users/chldj/EOJIN/VPP_test.py", line 114, in <module>
    model.solve()
  File "C:\python\lib\site-packages\gekko\gekko.py", line 2145, in solve
    self.load_JSON()
  File "C:\python\lib\site-packages\gekko\gk_post_solve.py", line 13, in load_JSON
    f = open(os.path.join(self._path,'options.json'))
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\chldj\\AppData\\Local\\Temp\\tmpdgnw5ovqgk_model0\\options.json'

这是否意味着解是无穷大的? 我认为第一次迭代的和(W)将是0。因此,它可以使目标函数“无穷大”。 我怎样才能修好它


Tags: theinpyformodelrangeusersbuff
1条回答
网友
1楼 · 发布于 2024-10-01 04:47:22

函数variancecovariance不在Gekko库中。您需要删除这些函数,而不是使用任何Gekko库函数。一些Numpy函数也允许用于矩阵运算,如numpy.dot。您可以使用函数的组合,例如fromGekkonumpy

from gekko import GEKKO
import numpy as np
m = GEKKO()
A = m.Array(m.Var,(4,3))
b = m.Array(m.Param,3,value=1)
x = np.dot(A,b)
[m.Minimize(x[i]**2) for i in range(4)]
m.solve(disp=False)
print(A)

这是一个list of Gekko functions

  • abs(x)绝对值| x |
  • abs2(x)带MPCC的绝对值
  • abs3(x)开关用二进制变量的绝对值
  • acos(x)反余弦,cos^-1(x)
  • ^{
  • Array(type,size)GEKKO对象数组
  • arx自回归外生投入(时间序列)模型
  • ^{
  • ^{
  • atan(x)反切线,tan^-1(x)
  • ^{
  • bspline用于二维数据的bspline
  • cos(x)余弦
  • ^一维数据的{}三次样条
  • erf(x)错误函数
  • erfc(x)互补误差函数
  • exp(x)e^x
  • if3(cond,x1,x2)在x1(cond<;0)和x2(cond>;=0)之间切换
  • log(x)log_e(x),自然对数
  • log10(x)log_10(x),log base 10
  • max2(x1,x2)MPCC的最大值
  • max3(x1,x2)开关二进制变量的最大值
  • min2(x1,x2)具有MPCC的最小值
  • min3(x1,x2)开关二进制变量的最小值
  • ^动力学问题的{}周期(初始=最终)
  • pwl分段线性函数
  • ^带有MPCC的{}符号运算符
  • sign3(x)带二进制变量的signum运算符用于开关
  • sin(x)正弦
  • sinh(x)双曲正弦
  • sqrt(x)平方根
  • state_space连续/离散和稠密/稀疏状态空间
  • sum列表或numpy数组中元素的总和
  • tan(x)切线
  • tanh(x)双曲正切
  • vsum(x)数据方向上单个变量的垂直和

其他函数如variancecovariance不在函数库中

相关问题 更多 >