Simulation and Visualization

Simulation and Visualization#

This Section describes how open-loop and closed-loop simulations can be realized and how the results can be plotted. Section Solver and Simulator describes the creation and parameterization of a solver as well as the simulation process including the saving of simulation data. Subsequently, Section Using GRAMPC-S with Matlab/Simulink shows how the results can be plotted in Matlab and how GRAMPC-S can be integrated into Simulink using an S-function.

Solver and Simulator#

Once the approximation of the stochastic OCP has been selected as described in Section Approximation of the stochastic OCP, a GRAMPC object can be created as a solver using the function

GrampcPtr Solver(ProblemDescriptionPtr problemDescription)

where problemDescription is a shared pointer to the stochastic problem description. Before this solver can be used, the initial states and parameters must be passed to it. Since the representation of states and parameters depends on the used propagation method, these must be calculated by the problem description. All problem descriptions provide the function

void compute_x0_and_p0(DistributionPtr state, DistributionPtr param)

for this purpose. The initial states and parameters can then be accessed by the functions

ctypeRNum* x0()
ctypeRNum* p0()

These can be passed directly to the solver with

solver->setparam_real_vector("x0", problem->x0());
solver->setparam_real_vector("p0", problem->p0());

Next, options and parameters of the solver should be selected. For this purpose, the GRAMPC class provides the member functions

void setopt_real(const char* optName, ctypeRNum optValue)
void setopt_int(const char* optName, ctypeInt optValue)
void setopt_string(const char* optName, const char* optValue)
void setopt_real_vector(const char* optName, ctypeRNum* optValue)
void setopt_int_vector(const char* optName, ctypeInt* optValue)

which call the corresponding GRAMPC functions. For details, please refer to the documentation of GRAMPC. Once the solver has been parameterized, it can be called as follows:

solver->run();

The solution found for the OCP can then be read from the solver object. However, if a closed-loop simulation should be implemented, the actual system behavior must also be simulated, which may deviate from the model of the controller. For this purpose, GRAMPC-S provides the Simulator class, which receives the actual system dynamics, the actual initial states and system parameters. The system can either be simulated without noise, where you can choose between the Euler and Heun method for integration, or simulated with an additive Wiener process, where the Euler-Maruyama method is applied. If the writeDataToFile flag is set, the simulator writes the results of the simulation to text files, which can be plotted in Matlab.