Direct methods for optimal control as implemented ACADO need to discretize possibly continuous-time problem formulations in order to yield discretized optimization problem. For adjusting the way this is done, ACADO offers the data structure "Grid" for specifying discrete time grids. Moreover, for obtaining results along the discretized time horizon, the data structure "VariablesGrid" has been introduced for user-convenience. This tutorial briefly explains how to use these data structures.
A "Grid" is simply an ordered set of time instants, very similar to a vector. However, it differs from a vector in two main respects: first, as time is assumed to proceed in forward direction only, the time instants need to be ordered non-decreasingly. Second, operations performed on a time grid are typically very different from the ones performed on a vector (e.g. a time grid might be shifted by a constant offset but multiplying it with a matrix does not make sense). In most cases, a user wants to setup an equidistant time grid with a given number of time points. This can be done as follows:
Printing the variable "firstGrid" shows that it consist of five time points equally spaced between 0 and 2. The last line accesses and prints the time instant of the third grid point. This grid might be further manipulated, e.g.:
These lines add a sixth grid point at time instant 7. Moreover, the interval length of "firstGrid" is printed, which is defined as the time difference between the last and first grid point. Also specifying non-equidistant grids is simple as demonstrated in the following listing:
These lines set up a "secondGrid" consisting of 3 grid points and assign certain time instants -1, 1, and 5 to them. The Grid data structure offers many more useful features. In order to keep the presentation short, we conclude with illustrating how to merge two time grids:
The data structure VariablesGrid extends the definition of a discrete time grid. Besides the time grid itself, it allows to also store a vector at each grid point. This is particularly useful for presenting discretized trajectories, which consist of a vector sequence of given dimension along a given time grid. Again, we first explain how to setup an equidistant VariablesGrid:
Often it is more convenient to read the data of a VariablesGrid from a txt-file. This can be done as follows:
The file "data.txt" needs to contain exactly one grid point per line, starting with the time instant and followed by the components of the associated vector:
The above listing not only reads a VariablesGrid "gridFromFile" from the file "data.txt" but also shows how "gridFromFile" can be appended to the "equidistantGrid" as defined in the previous listing.
Next example: Plotting Results