set_p_fun

Class method.

do_mpc.simulator.Simulator.set_p_fun(self, p_fun)

Method to set the function which gives the values of the parameters. This function must return a CasADi structure which can be obtained with get_p_template().

Example:

In the do_mpc.model.Model we have defined the following parameters:

Theta_1 = model.set_variable('parameter', 'Theta_1')
Theta_2 = model.set_variable('parameter', 'Theta_2')
Theta_3 = model.set_variable('parameter', 'Theta_3')

To integrate the ODE or evaluate the discrete dynamics, the simulator needs to obtain the numerical values of these parameters at each timestep. In the most general case, these values can change, which is why a function must be supplied that can be evaluted at each timestep to obtain the current values.

do-mpc requires this function to have a specific return structure which we obtain first by calling:

p_template = simulator.get_p_template()

The parameter function can look something like this:

p_template['Theta_1'] = 2.25e-4
p_template['Theta_2'] = 2.25e-4
p_template['Theta_3'] = 2.25e-4

def p_fun(t_now):
    return p_template

simulator.set_p_fun(p_fun)

which results in constant parameters.

A more “interesting” variant could be this random-walk:

p_template['Theta_1'] = 2.25e-4
p_template['Theta_2'] = 2.25e-4
p_template['Theta_3'] = 2.25e-4

def p_fun(t_now):
    p_template['Theta_1'] += 1e-6*np.random.randn()
    p_template['Theta_2'] += 1e-6*np.random.randn()
    p_template['Theta_3'] += 1e-6*np.random.randn()
    return p_template
Parameters:p_fun (python function) – A function which gives the values of the parameters
Raises:assert – p must have the right structure
Returns:None
Return type:None

This page is auto-generated. Page source is not available on Github.