我正在尝试使用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.
发布评论
评论(2)
您链接的文档页面与另一个与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.
您提供的文档链接是针对不同的Gekko软件:
python
pip安装gekko
中的Gekko优化套件在和阅读文档文档。两个软件包都可以分析时间序列数据。
numpy.random.randn()
函数可以与gekko
一起使用。这解决了10次优化问题,其值不同,
p
是从正常的,零分布中采样的。The documentation link that you provided is to different gekko software:
The Gekko Optimization Suite in Python
pip install gekko
is described in the Wikipedia article and in the Read the Docs documentation.Both software packages can analyze time-series data. The
numpy.random.randn()
function can be used withgekko
.This solves the optimization problem 10 times with different values for
p
sampled from a normal, mean-zero distribution.