如何在Gekko中获取随机数?

发布于 2025-02-09 05:42:49 字数 575 浏览 2 评论 0 原文

我正在尝试使用Gekko解决Python中的优化问题,其中一个变量在每个时间步骤中都有一个随机值,但是我无法使用返回随机数的Gekko函数。

遵循文档页面( http://tt./gekko/gekko/gekko/docs/user/user -Manual/functions.htm ),函数rnorm返回“来自均值和方差的正态分布的随机数”。我使用了如下所示:

x = m.Var(value=0)
m.Equation(x == 5.*m.rnorm(0, 1))

提供的

m = GEKKO()

,但是我收到以下错误消息:

attributeError:'gekko'对象没有属性'rnorm'

我想知道我是否缺少某些东西,或者是否还有另一种获取随机数的方法。

I am trying to solve an optimization problem in Python, using gekko, where one of the variables takes on a random value at each time step, but I haven't been able to use the gekko function that returns random numbers.

Following the documentation page (http://t-t.dk/gekko/docs/user-manual/functions.htm), the function rnorm returns "a random number from a normal distribution with mean and variance provided". I used it as shown here:

x = m.Var(value=0)
m.Equation(x == 5.*m.rnorm(0, 1))

provided that

m = GEKKO()

but I get the following error message:

AttributeError: 'GEKKO' object has no attribute 'rnorm'

I would like to know if there is something that I am missing or if there is another way to get random numbers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

醉南桥 2025-02-16 05:42:49

您链接的文档页面与另一个与Python中的优化套件不同的软件包关联。我建议查看此页面: https://gekko.readthedocs.io.ire/latestestestestestsesio.io readthedocs.io/en/latestestestest /model_methods.html 用于正确的文档。

至于您关于随机数的问题,我建议使用另一个软件包,例如python的随机或numpy的随机。我不确定如何在不看到更多代码的情况下确切地将其应用于您的问题;您可以做的是为每个时间步有一个随机数,然后在Gekko编写问题时将其添加到某个地方。

The documentation page you linked is associated with another package that isn't the same as the Optimization Suite in Python. I suggest looking at this page: https://gekko.readthedocs.io/en/latest/model_methods.html for the correct documentation.

As for your question about random numbers, I suggest using another package like python's random or numpy's random.normal. I'm not sure how exactly to apply it in your problem without seeing more code; what you could do is have an array of random numbers for each timestep and multiply or add it in somewhere while writing the problem in Gekko.

仅一夜美梦 2025-02-16 05:42:49

您提供的文档链接是针对不同的Gekko软件:

Gekko的时间赛和建模软件是一种免费的开源软件系统,用于管理和分析时间表数据,以及解决和分析大规模经济模型。请参阅gekko主页: www.tt.dk/gekko 。在Gekko版本概述页面上阅读有关不同GEKKO版本的状态的更多信息。

python pip安装gekko 中的Gekko优化套件在阅读文档文档

Gekko是用于机器学习和优化混合和差分代数方程的Python软件包。它与用于线性,二次,非线性和混合整数编程的大尺度求解器(LP,QP,NLP,MILP,MINLP)。操作模式包括参数回归,数据核对,实时优化,动态模拟和非线性预测控制。 Gekko是面向对象的Python库,可促进apmonitor的本地执行。

两个软件包都可以分析时间序列数据。 numpy.random.randn()函数可以与 gekko 一起使用。

from gekko import GEKKO
import numpy as np
m = GEKKO(remote=False)
p = m.Param()
x = m.Var()
m.Equation(x==5*p)

for i in range(10):
    p.value = np.random.randn()
    m.solve(disp=False)
    print(x.value[0],p.value[0])

这解决了10次优化问题,其值不同, p 是从正常的,零分布中采样的。

The documentation link that you provided is to different gekko software:

Gekko Timeseries and Modeling Software is a free and open-source software system for managing and analyzing timeseries data, and for solving and analyzing large-scale economic models. See the Gekko homepage: www.t-t.dk/gekko. Read more about the status of different Gekko versions on the Gekko versions overview page.

The Gekko Optimization Suite in Python pip install gekko is described in the Wikipedia article and in the Read the Docs documentation.

GEKKO is a Python package for machine learning and optimization of mixed-integer and differential algebraic equations. It is coupled with large-scale solvers for linear, quadratic, nonlinear, and mixed integer programming (LP, QP, NLP, MILP, MINLP). Modes of operation include parameter regression, data reconciliation, real-time optimization, dynamic simulation, and nonlinear predictive control. GEKKO is an object-oriented Python library to facilitate local execution of APMonitor.

Both software packages can analyze time-series data. The numpy.random.randn() function can be used with gekko.

from gekko import GEKKO
import numpy as np
m = GEKKO(remote=False)
p = m.Param()
x = m.Var()
m.Equation(x==5*p)

for i in range(10):
    p.value = np.random.randn()
    m.solve(disp=False)
    print(x.value[0],p.value[0])

This solves the optimization problem 10 times with different values for p sampled from a normal, mean-zero distribution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文