How to define maximum of Intermediate and another value in Python Gekko, when using sequential solver?

风格不统一 提交于 2021-01-04 05:40:13

问题


In a modeling framework for solving systems of differential equations with GEKKO that I am writing I want to calculate the maximum of a parameter (an external forcing) and an integer value (e.g. 0) and use the result in the model equations.

The crucial problem is that for passing the parameter through vectorized functions, the parameter currently is wrapped as an Intermediate equation. This is why a vectorized maximum function, such as built-in max and np.maximum return TypeError: object of type 'int' has no len()

I want to use a sequential solver (e.g. IMODE == 4) so the GEKKO max2 and max3 functions also do not work, as shown below.

Essentially the use case boils down to this minimal example:

from gekko import GEKKO
import numpy as np
import matplotlib.pyplot as plt  

m = GEKKO()
m.time = np.arange(0,20)

y = m.Var(value=5)
forcing = m.Intermediate(m.Param(value=np.arange(-5,15)))

@np.vectorize
def value_above_zero(var):
    return m.max2(var, 0)

forcing_above_0 = value_above_zero(forcing)

m.Equation(y.dt()==-forcing_above_0*y)
m.options.IMODE=4
m.solve(disp=False)

which gives this error when trying to solve:

Exception:  @error: Degrees of Freedom
 * Error: DOF must be zero for this mode
 STOPPING...

Is there some way to define the max of an intermediate equation and some other value using GEKKO, when using a sequential solver? Or is there perhaps another way to wrap the parameter so that np.vectorize does not unpack the time-discretized values of the forcing (i.e. the array in the gekko parameter)?

Any help is very much appreciated!


回答1:


IMODE=6 gives a good solution for this problem. It is still a simulation but allows the additional degrees of freedom. IMODE=4 checks that the number of equations and variables are equal. Both the m.max2() and m.max3() use additional degrees of freedom to solve the problem as shown here.

from gekko import GEKKO
import numpy as np
import matplotlib.pyplot as plt  

m = GEKKO()
m.time = np.arange(0,20)

y = m.Var(value=5)
forcing = m.Intermediate(m.Param(value=np.arange(-5,15)))

@np.vectorize
def value_above_zero(var):
    return m.max2(var, 0)

forcing_above_0 = value_above_zero(forcing)

m.Equation(y.dt()==-forcing_above_0*y)
m.options.IMODE=6
m.solve(disp=False)


来源:https://stackoverflow.com/questions/65067711/how-to-define-maximum-of-intermediate-and-another-value-in-python-gekko-when-us

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!