How can I obtain random numbers in gekko?

Viewed 49

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 Answers

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.

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.

Related