4. Learning Model Predictive Control for quadrotor autonomous and
4.6. Sampled safe set and terminal cost relaxation
4.6.5. Convex piecewise-linear fitting of the terminal cost function
At iteration ๐, the sampled safe set ๐๐๐ contains the following states:
๐๐๐ = {๐ฟ0, ..., ๐ฟ๐} = {๐00, ๐01, ..., ๐๐๐
๐} (4.51)
The terminal cost function ๐๐(๐), if (as in Note 4.5) there are no states in common to multiple trajectories (except for ๐๐), when applied to every state in ๐๐๐ gives:
๐๐(๐๐๐) = {๐๐(๐00), ๐๐(๐01), ..., ๐๐(๐๐๐
๐)} =
= {๐ฝ[0,๐0
0](๐00), ๐ฝ[1,๐0
0](๐01), ..., ๐ฝ[๐๐
๐,๐๐](๐๐๐
๐)} โก
โก {๐ฝ00, ๐ฝ10, ..., ๐ฝ๐๐
๐} โก
โก {๐00, ๐10, ..., ๐๐๐
๐} (4.52)
We then consider the problem of fitting the obtained data:
(๐00, ๐00), ..., (๐๐๐
๐, ๐๐๐
๐) โก (๐1, ๐1), ..., (๐๐, ๐๐) โR๐รR, ๐ = |๐๐๐| (4.53) with a convex piecewise-linear function ๐ โถ R๐ โ R from some set โฑ of candidate functions. With a least-squares fitting criterion, we obtain the problem:
min๐โโฑ
โ๐ฝ (๐)
๐ , ๐ฝ (๐) =
๐
โ
๐=1
(๐(๐๐) โ ๐๐)2 (4.54)
We call โ๐ฝ(๐)๐ the RMSE index (root-mean-square error) of the function ๐ fitting the data. The convex piecewise-linear fitting problem (4.54) is to find the function ๐, from
the given family โฑ of convex piecewise-linear functions, that gives the best (smallest) RMSE value with the given data.
Our main interest is in the case when ๐ (i.e. the state dimension) is relatively small, while ๐ (i.e. the number of data points) can be relatively large (e.g. 104 or more). The methods that will be described in the following, however, work for any values of ๐ and ๐.
Several special cases of the convex piecewise-linear fitting problems (4.54) can be solved exactly.
When โฑ is the set of affine functions, i.e. ๐(๐) = ๐๐๐+๐, the problem (4.54) reduces to an ordinary linear least-squares problem in the function parameters ๐ โ R๐ and ๐ โ R and can be analytically solved. This approach is called parametric convex piecewise-linear fitting.
A less trivial case is, by converse, the non-parametric convex piecewise-linear fitting, in which โฑ is the set of all piecewise-linear functions from R๐ to R, with no other constraint on the form of ๐.
Of course, not all data can be fit well (i.e. with small RMSE) with a convex piecewise-linear function. For example, if the data are samples from a function that has a strong concave curvature, then no convex function can fit it well.
Max-affine functions
For our purposes, we will consider a parametric fitting problem, in which the candidate functions are parametrized by a finite-dimensional vector of coefficients ๐ถ.
We choose as โฑ the set of functions on R๐ with the form:
๐(๐) = max (๐๐1๐ + ๐1, ..., ๐๐๐๐ + ๐๐) (4.55)
We refer to a function of this form as a max-affine function with ๐ terms. Such functions are parameterized by the coefficients vector:
๐ถ = (๐1, ..., ๐๐, ๐1, ..., ๐๐) โR๐(๐+1) (4.56)
Max-affine functions can be visualized as an intersection of ๐ planes in theR๐รR space, of which we take the envelope (which corresponds to the max operation).
4.6. Sampled safe set and terminal cost relaxation
Indeed, any convex piecewise-linear function on R๐ can be expressed as a max-affine function, for some ๐, so this form is in a sense universal.
Our interest, however, is in the case when the number of terms ๐ is relatively small, In this case the max-affine representation (4.55) is compact, in the sense that the number of parameters needed to describe ๐ is much smaller than the number of parameters in the original data set (i.e. ๐(๐ + 1)). The methods that will be described in the following, however, do not require ๐ to be small.
With max-affine functions, the fitting problem (4.54) reduces to the nonlinear least-squares problem:
min๐ถ โ๐ฝ (๐ถ)
๐ , ๐ฝ (๐ถ) =
๐
โ
๐=1
( max
๐=1,...,๐(๐๐๐๐๐+ ๐๐) โ ๐๐)
2
(4.57)
Fitting procedure
In this section, we present an algorithm that solves approximately the ๐-term max-affine fitting problem (4.57) [11].
The heuristic idea behind this algorithm is the following:
โข create ๐ partitions of the data points {(๐1, ๐1), ..., (๐๐, ๐๐)};
โข on each partition ๐ = 1, ..., ๐, solve analytically the linear least-square fitting problem associated to the ๐-th affine functions composing ๐, obtaining a first estimate of the coefficients ๐๐ and ๐๐;
โข update each partition on the base of the current coefficients value;
โข iterate the algorithm until the coefficients converge to a certain value.
The algorithm, therefore, alternates between partitioning the data and carrying out least-squares fits to update the coefficients.
Let ๐๐(๐) for ๐ = 1, ..., ๐, be a partition of the data indices {1, ..., ๐} at the ๐-th iteration of the algorithm:
๐๐(๐) โ {1, ..., ๐}
โ
๐
๐๐(๐) = {1, ..., ๐}, ๐๐(๐)โฉ ๐๐(๐) = โ for ๐ โ ๐ (4.58)
The generation of the initial partitions ๐๐(0) will be analyzed in the next section.
Denoting with ๐(๐)๐ and ๐(๐)๐ the values of the parameters at the ๐-th iteration of the algorithm, we generate the next values, ๐(๐+1)๐ and ๐(๐+1)๐ , from the current partition ๐๐(๐), as explained in the following paragraphs.
For each ๐ = 1, ..., ๐, we carry out a least-squares fit of ๐๐๐๐๐+ ๐๐ to ๐๐, using only the data points with ๐ โ ๐๐(๐). In other words, we take ๐(๐+1)๐ and ๐(๐+1)๐ as values of ๐ and ๐ that minimize
โ
๐โ๐๐(๐)
(๐๐๐๐+ ๐ โ ๐๐)2 (4.59)
which is indeed the linear least-square problem associated to the ๐-th affine function composing ๐, restricted to only the data in the ๐-th partition.
Such problem can be solved analytically, and the pair (๐, ๐) that minimizes (4.59) is given by:
(๐(๐+1)๐
๐๐(๐+1)) = (โ ๐๐๐๐๐ โ ๐๐
โ ๐๐๐ โฃ๐๐(๐)โฃ)
โ1
(โ ๐๐๐๐
โ ๐๐ ) (4.60)
where the sums are over ๐ โ ๐๐(๐).
Using the new values of the coefficients, we update the partition to obtain ๐๐(๐+1), by assigning ๐ to ๐๐(๐+1) if:
๐(๐+1)(๐๐) = max
๐ =1,...,๐(๐(๐+1)๐๐ ๐๐+ ๐๐ (๐+1)) = ๐(๐+1)๐๐ ๐๐+ ๐(๐+1)๐ (4.61)
This means that ๐๐(๐+1)is the set of indices ๐ for which the affine function ๐(๐+1)๐๐ ๐๐+๐๐(๐+1) is the maximum for the data point ๐๐.
During this step of the algorithm, one or more of the sets ๐๐(๐+1) may become empty.
The simplest approach is to drop these empty sets from the partitions, and continue with a smaller value of ๐.
This algorithm is iterated until one of the following conditions is met:
โข Convergence has been reached, which equivalently occurs when:
โ
the coefficients vector ๐ถ(๐) has converged to a steady-state value: ๐ถ(๐+1) = ๐ถ(๐),โ๐ โฅ ๐โฒ;
4.6. Sampled safe set and terminal cost relaxation
โ
the partitions ๐๐(๐) do not change anymore after a certain iteration: ๐๐(๐+1) = ๐๐(๐),โ๐ โฅ ๐โฒ, โ๐ โ {1, ..., ๐}.
โข The maximum number of iterations has been reached: ๐ > ๐๐๐ก๐๐.
Generation of the initial partitions
The choice of the initial partitions ๐๐(0) is a crucial step, since it directly affects the quality of the fitting given by the max-affine interpolating function generated by the algorithm. A bad selection of the initial partitions may lead the algorithm to not converge, or to converge to a piecewise-linear approximation that poorly fits the data (even when the data shows a good convex curvature).
We provide in the following a general approach to generate initial partitions that ensure, in most cases, a good interpolation of the data points [11].
Each partition ๐๐(0), with ๐ = 1, ..., ๐, is generated starting from ๐ randomly chosen points ๐1, ..., ๐๐ โR๐, which are called seed points.
Then, ๐๐(0) is defined as the set of indices ๐ associated to the data points ๐๐ that are closest to ๐๐:
๐๐0 = {๐ โถ |๐๐โ ๐๐| < |๐๐โ ๐๐ |, โ๐ โ ๐}, ๐ = 1, ..., ๐ (4.62)
Specifically, the indices in the partitions ๐๐(0) define the Voronoi sets of data points ๐๐ associated to the seed points ๐๐.
With this method, we generate initial partitions that tessellate the whole subset of R๐ containing the data points {๐๐}๐๐=1. Such partitions are very well suited for max-affine interpolation, since max-affine functions are the envelope of ๐ intersecting planes in R๐ and the fitting procedure locally interpolates the data in each partition with one of the planes composing the function.
The Voronoi sets associated to the initial partitions, which tessellate the set of data points, are depicted in Figure 4.6, showing the case of 20 randomly generated partitions.
The seed points ๐๐ should be randomly generated according to some probability distri-bution that matches the shape of the data points ๐๐. A possible choice is to generate them from a normal distribution with the sample mean ๐๐ฅ and the sample covariance
๐ฎ๐ฅ of the data points:
๐๐ฅ = 1 ๐
๐
โ
๐=1
๐๐, ๐ฎ๐ฅ = 1 ๐
๐
โ
๐=1
๐๐๐๐๐ (4.63)
Note 4.7 Since the seed points are generated from a normal distribution, it may happen that some of them are chosen outside the set of data points and enough far from it such that the related partition is empty. As done in the fitting algorithm, the simplest approach is to drop these empty partitions and continue with a smaller value of ๐.
This method for generating initial partitions can be reformulated to more specific pro-cedure that applies only to 1D data (i.e. ๐ = 1) and provides in most cases better results.
Instead of generating randomly the initial partitions ๐๐(0)from seed points, the partitions are chosen as equally spaced intervals on the ordered set (i.e. from lowest to highest value) of data points {๐ฅ1, ..., ๐ฅ๐} โR:
๐๐(0) = {1 + (๐ โ 1) โ๐
๐ โ , ..., ๐ โ๐
๐โ} , for ๐ = 1, ..., ๐ โ 1 ๐๐(0) = {1 + ๐ โ๐
๐ โ , ..., ๐} (4.64)
With this approach, we generate, as well as in the general method, initial partitions that cover the whole subset ofR containing the data points {๐ฅ๐}๐๐=1. This method applies well only to 1D data for the trivial reason thatR is much easier to subdivide in bounded and connected subsets rather thanR๐, for which instead generating the partitions randomly in more convenient.
Fitting algorithm
The full algorithm for convex piecewise-linear fitting can be written as follows:
Algorithm 4.2 Convex piecewise-linear fitting
Inputs: {(๐1, ๐1), ..., (๐๐, ๐๐)}; ๐; ๐๐๐ก๐๐ (max n. of iterations) Outputs: ๐ถ
(1) Compute the sample mean ๐๐ฅ and sample covariance ๐ฎ๐ฅ of the data points
4.6. Sampled safe set and terminal cost relaxation
{๐๐}๐๐=1 as in (4.63)
(2) Generate the seed points ๐๐, ๐ = 1, ..., ๐, from the distribution ๐ฉ(๐๐ฅ, ๐ฎ๐ฅ) (3) Compute the initial partitions ๐๐(0), ๐ = 1, ..., ๐, as in (4.62)
(4) Remove empty partitions from ๐๐(0), ๐ = 1, ..., ๐, and update the value of ๐ (5) For ๐ = 0, ..., ๐๐๐ก๐๐โ 1:
(2.1) Compute ๐(๐+1)๐ and ๐(๐+1)๐ , ๐ = 1, ..., ๐, as in (4.60) (2.2) Update the partitions to ๐๐(๐+1), ๐ = 1, ..., ๐, as in (4.61)
(2.3) Remove empty partitions from ๐๐(๐+1), ๐ = 1, ..., ๐, and update the value of ๐
(2.4) โข If ๐๐(๐+1) = ๐๐(๐), return:
๐ถ = (๐(๐)1 , ..., ๐(๐)๐ , ๐(๐)1 , ..., ๐(๐)๐ )
โข otherwise, continue (6) Return:
๐ถ = (๐(๐1 ๐๐ก๐๐), ..., ๐(๐๐ ๐๐ก๐๐), ๐(๐1 ๐๐ก๐๐), ..., ๐(๐๐ ๐๐ก๐๐))
Examples
In this section, we will show two examples of application of the convex piecewise-linear fitting algorithm.
We will use it to fit a set of 1D and 2D data points, obtained from 2 convex functions, specifically:
๐1 โถR โ R, ๐1(๐ฅ) = ๐ฆ = 0.1๐ฅ2 (4.65a) ๐2 โถR2โR, ๐2(๐) = ๐ฆ = 0.1(๐ฅ21+ ๐ฅ22) (4.65b)
For function 1, data points are {(๐ฅ๐, ๐ฆ๐)}๐= {(๐ฅ๐, ๐1(๐ฅ๐))}๐, {๐ฅ๐}๐ = [โ5 โถ 0.5 โถ 5].
The algorithm is set up with ๐ = 10, ๐๐๐ก๐๐ = 10; the initial partitions are generated by setting up the MATLAB random number generator with the 'default' seed.
The algorithm returns ๐ถ after only 1 iteration:
๐ถ = ((๐๐)๐, (๐๐)๐)
(๐๐)๐๐ =
โโ
โโ
โโ
โโ
โโ
โโ
โ 0.35 0.95 0.65 0.1
โ0.8
โ0.3
โโ
โโ
โโ
โโ
โโ
โโ
โ
, (๐๐)๐๐ =
โโ
โโ
โโ
โโ
โโ
โโ
โ
โ0.3
โ2.25
โ1.02
โ0.00833
โ1.55
โ0.175
โโ
โโ
โโ
โโ
โโ
โโ
โ
(๐๐)๐ and (๐๐)๐ have 6 < ๐ elements, so 4 empty partitions have been generated and removed.
Figure 4.4 depicts the max-affine interpolating function with respect to data points:
Figure 4.4. Convex piecewise-linear fit of a 1D convex function. Data points are depicted in red, while the max-affine interpolating function is depicted in cyan.
We see that the interpolating function is the envelope of 6 lines, having angular coeffi-cient ๐๐ and intercept ๐๐, for ๐ = 1, ..., 6.
For function 2, data points are {(๐๐, ๐ฆ๐)}๐ = {(๐๐, ๐2(๐๐))}๐, {๐๐}๐ = [โ5 โถ 0.5 โถ 5] ร [โ5 โถ 0.5 โถ 5].
The algorithm is set up with ๐ = 20, ๐๐๐ก๐๐ = 50; the initial partitions are generated by setting up the MATLAB random number generator with the 'default' seed.
The algorithm returns ๐ถ after 16 iterations:
๐ถ = ((๐๐)๐, (๐๐)๐)
4.6. Sampled safe set and terminal cost relaxation
(๐๐)๐๐ =
โโ
โโ
โโ
โโ
โโ
โโ
โโ
โโ
โโ
โโ
โโ
โโ
โโ
โโ
โโ
โโ
โโ
โโ
โโ
โโ
โ
โ
0.688 0.246 0.75 โ0.578
โ0.702 โ0.299 0.439 0.827 0.196 0.342
โ0.757 0.79
โ0.351 0.354 0.31 โ0.246
โ0.863 0.208 0.0964 โ0.777
โ0.65 โ0.8
โ0.147 0.829
โ0.161 โ0.18 0.837 โ0.159 0.908 0.724
0.75 โ0.9
โโ
โโ
โโ
โโ
โโ
โโ
โโ
โโ
โโ
โโ
โโ
โโ
โโ
โโ
โโ
โโ
โโ
โโ
โโ
โโ
โ
โ
, (๐๐)๐๐ =
โโ
โโ
โโ
โโ
โโ
โโ
โโ
โโ
โโ
โโ
โโ
โโ
โโ
โโ
โโ
โโ
โโ
โโ
โโ
โโ
โ
โ
โ1.21
โ2.15
โ1.31
โ2.08
โ0.29
โ2.87
โ0.506
โ0.295
โ1.86
โ1.37
โ2.47
โ1.66
โ0.0165
โ1.72
โ3.27
โ3.34
โโ
โโ
โโ
โโ
โโ
โโ
โโ
โโ
โโ
โโ
โโ
โโ
โโ
โโ
โโ
โโ
โโ
โโ
โโ
โโ
โ
โ
(๐๐)๐ and (๐๐)๐ have 16 < ๐ elements, so 4 empty partitions have been generated and removed.
We see that the interpolating function is the envelope of 16 planes, having coefficients (๐๐๐, ๐๐), for ๐ = 1, ..., 16.
Figure 4.5 depicts the interpolating max-affine function with respect to data points:
Figure 4.5. Convex piecewise-linear fit of a 2D convex function. Data points are depicted in red, while the max-affine interpolating function is depicted in cyan.
Figure 4.6 depicts instead the Voronoi sets associated to the initial partitions:
Figure 4.6. Voronoi sets of the initial partitions. Seed points are depicted in red, while the boundaries of each set are marked with black lines.
As pointed out in Note 4.7, in Figure 4.6 we see only 16 out of 20 generated partitions;
this means that 4 partitions have been generated outside the subset of data points in R2 (i.e. [โ5, 5] ร [โ5, 5]). Indeed, we have seen that (๐๐)๐ and (๐๐)๐ have only 16 elements, meaning that the 4 empty partitions have been removed from ๐๐(0), ๐ = 1, ..., 20.