Python gekko 找不到“options.json";文件 [英] Python gekko cant find "options.json" file

查看:45
本文介绍了Python gekko 找不到“options.json";文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还没有看到与此相关的问题,尽管我做了几次相反的努力,但它还是出现了,所以我希望有人能帮助我了解发生了什么.

I haven't seen a question related to this, yet it pops up despite several of my efforts to the contrary, so I was hoping that someone could help me understand what's going on.

我是 Python 的 Gekko 包的新手,我正在尝试运行非线性求解器来解决正在发生的事情.我收到一条奇怪的错误消息(FileNotFoundError: [Errno 2] No such file or directory: '/var/folders/03/vyh22j3j45j2rqjmnygfw2l80000gn/T/tmpatrk8y36gk_model0/options.json 似乎是程序')/语法而不是数学.这是一个更大的函数/数据/输入中的一个函数,所以我很难提供足够的帮助你复制它 -- 但是 --

I'm new to Python's Gekko package, and I'm trying to run a non-linear solver to the resolve what's going on. I'm getting a weird error message (FileNotFoundError: [Errno 2] No such file or directory: '/var/folders/03/vyh22j3j45j2rqjmnygfw2l80000gn/T/tmpatrk8y36gk_model0/options.json') that seems programmatic/syntactical rather than mathematical. It's one function in a much larger function/data/input, so it's hard for me to provide enough to help you replicate it -- but --

y(即来自 self.get_days_energy(date = date) 的结果)是一个能量值向量,就像来自 self.get_exogenous_inputs_of_day(date) 的外生输入一样.Z 变量是潜在状态变量,A 和 B 分别是潜在状态和外生变量的转移矩阵.此函数的目标是更新上述三个变量.

The y (i.e. the result from self.get_days_energy(date = date)) is a vector of energy values, as is the exogenous inputs from self.get_exogenous_inputs_of_day(date). Z variables are latent state variables, and A and B are transition matrices for latent states and exogenous variables, respectively. The goal of this function is to update the three aforementioned variables.

功能如下:

def daily_weight_fit(self, date):

    """ 
    Update function to the weights of the dynamic system, this will be called 
    once a day after the data has arrived. 
    """

    m = GEKKO(remote = False)
    A = m.Array(
            m.Var, 
            (self.state_weights.shape), 
            value = 1, 
            lb = -10, 
            ub = 10, 
        )
    B = m.Array(
            m.Var, 
            (self.input_weights.shape), 
            value = 1, 
            lb = -100, 
            ub = 100, 
        )

    dates = pd.date_range(start=self.starting_date, end=date)
    date_list = dates.tolist()
    hours = list(range(24))

    y = [[self.get_days_energy(date = date)] for date in dates]
    flat_y = np.reshape(y, -1)
    timesteps = len(flat_y)

    u = [self.get_exogenous_inputs_of_day(date) for date in dates for hour in hours]

    z = m.Array(m.Var, (timesteps, 4), lb = -1, ub = 1)      
    C = np.array([0, 0, 1, 0])

    m.Obj(
        m.sqrt(
            m.sum([(flat_y[i] - z[i][3])**2 for i in range(len(flat_y))])
            )
        )

    for i in range(timesteps - 2):
        state_contribution = np.dot(A, z[i])
        ex_contribution = np.dot(B, u[i])
        for j in range(4):
            m.Equation(z[i + 1][j] == state_contribution[j] + ex_contribution[j])

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

    return A, B, z

这是错误输出:

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


 --------- APM Model Size ------------
 Each time step contains
   Objects      :            1
   Constants    :            0
   Variables    :         1849
   Intermediates:            0
   Connections  :          361
   Equations    :         1793
   Residuals    :         1793

 Number of state variables:           1849
 Number of total equations: -         1793
 Number of slack variables: -            0
 ---------------------------------------
 Degrees of freedom       :             56

 ----------------------------------------------
 Steady State Optimization with APOPT Solver
 ----------------------------------------------

 Iter    Objective  Convergence
    0  4.12076E+08  8.12928E+01
    1  5.38520E+02  1.00000E+00
    2  5.38512E+02  3.75145E-12
    3  5.38450E+02  2.00000E+00
    4  5.38506E+02  1.80710E-01
 NEGATIVE NDF:           -1
    5  1.10270E+07  3.34638E-01
    6  4.89845E+06  1.93472E+00
    7  4.73333E+05  3.99952E+00
    8  3.76178E+05  2.00000E+00
    9  1.69753E+05  1.31302E+00

 Iter    Objective  Convergence
   10  1.90770E+07  5.86504E-01
   11  3.03623E+16  3.34638E-01
   12  9.38385E+11  3.34304E-01
   13  1.14353E+12  1.09011E-02
   14  8.91928E+12  7.65181E-03

Error: 
Program received signal SIGSEGV: Segmentation fault - invalid memory reference.

Backtrace for this error:
#0  0x108c7718d
#1  0x108c7661b
#2  0x7fff661ccf59
#3  0x108a92b6b
apm_mac(33870,0x7fff9e9c1380) malloc: *** error for object 0x7fb371862e00: incorrect checksum for freed object - object was probably modified after being freed.
*** set a breakpoint in malloc_error_break to debug

Program received signal SIGABRT: Process abort signal.

Backtrace for this error:
#0  0x108c7718d
#1  0x108c7661b
#2  0x7fff661ccf59

Error: 'results.json' not found. Check above for additional error details
Traceback (most recent call last):

.... (my own irrelevant function trace)..... 

    m.solve()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/gekko/gekko.py", line 2145, in solve
    self.load_JSON()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/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: '/var/folders/03/vyh22j3j45j2rqjmnygfw2l80000gn/T/tmp42metzl9gk_model0/options.json'

任何提示或建议将不胜感激!John Hedengren,我喜欢你的作品!!

Any tips or advice would be much appreciated! John Hedengren, hi and I love your work!!

推荐答案

您发现了 APOPT 求解器的一个新错误.迭代摘要显示问题正在发散,因此某些变量可能会趋向于无穷大,超出机器精度.

You have discovered a new bug with the APOPT solver. The iteration summary shows that the problem is diverging so some of the variables may be going to Infinity, beyond machine precision.

  1. 第一个选择是尝试不同的求解器,例如带有 m.options.SOLVER=2 的 BPOPT 或带有 m.options.SOLVER=3 的 IPOPT.
  2. 第二种选择是限制您的一些决策变量,或者注意方程或目标函数中的除以零.
  1. A first option is to try a different solver such as BPOPT with m.options.SOLVER=2 or IPOPT with m.options.SOLVER=3.
  2. A second option is to bound some of your decision variables or watch out for divide by zero in your equations or objective function.

每当求解器崩溃时,请考虑将运行目录的内容发送到 m.path 中,以便开发人员修复错误.

Whenever the solver crashes, please consider sending the contents of your run directory in m.path so that developers can fix the bug.

背景信息:options.jsonresults.json 文件由底层 C++/Fortran 可执行文件在本地使用 m=GEKKO(remote=False) 编写或通过网络服务 m=GEKKO(remote=True).在所有情况下,可执行文件都会在本地运行目录 m.path 或远程服务器上以 json 文件的形式生成结果.当可执行文件由于错误而崩溃时,不会生成 json 文件并导致您观察到的错误.您还可以看到求解器在迭代 14 处停止.使用 remote=False,可执行文件从 Gekko bin 目录本地运行(apm for Linux,apm_mac for MacOS,或 apm.exe for Windows)但在运行目录 m.path 中生成文件.

Background Info: The options.json or results.json files are written by the underlying C++/Fortran executable either locally with m=GEKKO(remote=False) or through the web-service m=GEKKO(remote=True). In all cases, the executable produces the results as json files either in the local run directory m.path or else on the remote server. When the executable crashes due to a bug, the json files are not produced and it leads to the error that you observed. You can also see the solver stops at iteration 14. With remote=False, the executable runs locally from the Gekko bin directory (apm for Linux, apm_mac for MacOS, or apm.exe for Windows) but produces the files in the run directory m.path.

这篇关于Python gekko 找不到“options.json";文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆