You are welcome (and encouraged) to work with others, but each individual must submit their own writeup.
You are welcome to use your preferred computational toolkit – upload the commented sourcecode alongside your writeup (e.g. the .ipynb file).
You are welcome to consult research articles and other materials – upload a .pdf of the article alongside your writeup and indicate which references were used where in the writeup.
optimization and learning for control
Purpose: this sequence of exercises will introduce you to the basic concepts, problems, and techniques we’ll explore throughout the course. If you don’t understand what a problem is asking or how to approach it, it’s very likely there are other folks in the same boat – please ask questions at Office Hours or on Canvas Discussions. The problems are designed to stretch your thinking and abilities – I encourage you to make a genuine attempt on every problem, but don’t spin your wheels if you’re not making good progress.
optimization algorithms
Consider the following cost function of a scalar decision variable \(u\in\mathbb{R}\):
Plot \(c(u)\), \(D c(u)\), and \(D^2 c(u)\) for scalar \(u\); use the subplot(3,1,n) for n = 1, 2, 3 to align the \(u\)-axes of the three plots.
Code
import numpy as npimport pylab as pltimport scipy as spfrom scipy import linalg as laimport scipy.optimize as optimport scipy.stats as statsfrom scipy.misc import derivative%matplotlib inline# Set figure propertiesfigsize = (8,8)fig = plt.figure(figsize=figsize)# Define functiondef c(u): return u**6/6.0-7*u**5/5.0+17*u**4/4.0-17*u**3/3.0+3*u**2def Dc(u): return derivative(c,u, dx=0.0001)def DDc(u): return derivative(Dc,u, dx=0.0001)u = np.linspace(-30,30,10000)ax1 = plt.subplot(3,1,1); ax1.grid('on')ax1.plot(u,c(u))ax1.set_ylabel('$c(u)$')ax1.set_xlabel('value of $u$')ax2 = plt.subplot(3,1,2); ax2.grid('on')ax2.plot(u,Dc(u))ax2.set_ylabel('$Dc(u)$')ax2.set_xlabel('value of $u$')ax3 = plt.subplot(3,1,3); ax3.grid('on')ax3.plot(u,DDc(u))ax3.set_ylabel('$D^2c(u)$')ax3.set_xlabel('value of $u$')
Text(0.5, 0, 'value of $u$')
What is the minimum of \(c\)? What is the minimizing \(u\)?
Notice:\(Dc(u^*) = 0\) and \(D^2 c(u^*) > 0\) at the minimizing \(u^*\) (similarly, \(Dc(u_*) = 0\) and \(D^2 c(u_*) < 0\) at the maximizing \(u_*\)) – such points are termed stationary for reasons that will become clear in the next problem.
The minimum \(c\) is: \(-0.449956\)
The minimizing \(u\) is: \(2.9973\)
Code
[u_min, c_min,success,iter] = opt.fminbound(c, 0, 6, full_output=True)print("Approach 1: Using fminbound function:")print("The minimum c is: ","%0.6f"% c_min)print("The minimizing u is: ","%0.6f\n"% u_min)u_min = u[0]for u_tmp in u:ifabs(Dc(u_tmp))<abs(Dc(u_min)) and DDc(u_tmp)>0: u_min = u_tmpprint("Approach 2: Based on Dc(u) and D^2c(u) values:")print("The minimum c is: ","%0.6f"% c(u_min))print("The minimizing u is: ","%0.6f\n"% u_min)u_min = u[0]for u_tmp in u:if c(u_tmp)<c(u_min): u_min = u_tmpprint("Approach 3: Directly compare c(u) values:")print("The minimum c is: ","%0.6f"% c(u_min))print("The minimizing u is: ","%0.6f\n"% u_min)
Approach 1: Using fminbound function:
The minimum c is: 0.000000
The minimizing u is: 0.000005
Approach 2: Based on Dc(u) and D^2c(u) values:
The minimum c is: 0.350000
The minimizing u is: 1.005101
Approach 3: Directly compare c(u) values:
The minimum c is: -0.449956
The minimizing u is: 2.997300
Run the (gradient descent) iteration
\[ u^+ = u - \gamma\, d(u) \]
using the actual derivative \(d(u) = Dc(u)\) starting from multiple initial \(u\)’s and with multiple values of the parameter \(\gamma > 0\). Describe all of the outcomes (i.e. asymptotic behavior of the iteration) you observe and provide plots that illustrate these outcomes.
Notice: you should observe that stationary points (where \(Dc = 0\)) are fixed points for this iteration, and that minimizers are (locally) attracting points for this iteration.
Experiment 1: Overshoot and eventually converged to local minimum
\(\quad\)- parameter \(\gamma=3e-4\)
\(\quad\)- initial \(u=10\)
\(\quad\)- max iteration\(=100\)
Experiment 2: No overshoot and eventually converged to local minimum
\(\quad\)- parameter \(\gamma=1e-3\)
\(\quad\)- initial \(u=-2.5\)
\(\quad\)- max iteration\(=100\)
Experiment 3: Overshoot and converged after oscillation to local minimum
\(\quad\)- parameter \(\gamma=1.27e-3\)
\(\quad\)- initial \(u=-5\)
\(\quad\)- max iteration\(=100\)
Experiment 4: Overshoot and diverged after oscillation
starting from multiple initial \(u\)’s and with multiple values of the parameters \(\gamma, \Delta > 0\). Describe all of the outcomes you observe and provide plots that illustrate these outcomes. Does \(\gamma > \Delta\) or \(\Delta > \gamma\) give better results?
Mathematically, if we substitute \(d(u)\) in the gradient descent equation: \[ u^+ = u - \gamma\, d(u) \] with the finite differences aproximation equation \[d(u) = \frac{1}{\Delta}( c(u + \Delta) - c(u) )\] , we will get \[ u^+ = u - \, \frac{\gamma}{\Delta}( c(u + \Delta) - c(u) ) \] In this case, by picking \(\gamma > \Delta\), the fraction \(\frac{\gamma}{\Delta}\) will be greater than \(1\). This means the system is updating (optimizing) variable \(u\) at a rate (step size) greater than the step-wise difference in \(c(u)\). Therefore, the system tends to correct itself more drastically, and is more prone to overshooting and oscillation.
This, however, does not always guarantee better performance (efficiency and accuracy), the blue line in the experiments below represents the case with the greatest \(\frac{\gamma}{\Delta}\) ratio. In Experiment 5, such parameter settings yield faster convergence. Yet, it prolongs the convergence by introducing more oscillation in Experiment 4 and even accidentally bounces the system to a local minimum in Experiment 3.
It is also interesting to note that Experiment 4 diverged in the previous problem (problem c.), but here using the finite differences equation, it actually converges (although at \(\Delta=0.002001\), the orange line seems to got caught at some local minimum point).
starting from multiple initial \(u\)’s and with multiple values of the parameters \(\gamma, \Delta, \sigma > 0\), where \(\mathcal{N}(u,\sigma^2)\) denotes a normal or Gaussian distribution with mean \(u\) and variance \(\sigma^2\). Describe all of the outcomes (i.e. asymptotic behavior of the iteration) you observe and provide plots that illustrate these outcomes. How should \(\sigma\) relate to \(\gamma\), \(\Delta\)?
A large \(\gamma\) indicates that the iterative correction of value \(u\) in response to the expected change of \(c(u)\) in the neighborhood is more drastic, whereas a large \(\Delta\) reflects that the system calculates the difference of \(c(u)\) value around the neighborhood with a larger step size away from \(u\). In the meantime, the standard deviation \(\sigma\) represents how spread out the probability distribution of \(u_n\) is, namely, how likely the system is going to explore the \(c\) values of further neighbors. That being said, large \(\sigma\) allows higher chance for the system to escape local minimum, but small \(\sigma\) prevents oscillation.
Experiment 1-3 below is a set of tests with fixed \(\gamma\) and initial \(u\) while \(\Delta\) gradually increases. As we can see, Experiment 1 leads to convergence to global minimum for all \(\sigma\) values; in Experiment 2, the red and green lines (two tests with the largest \(\sigma\)s) jumped to some local minimum; and in Experiment 3, the red line jumps away again but slowly navigate its way back. We can conclude that the increase of \(\Delta\) and \(\sigma\) together results in more chance to explore a bigger neighborhood (with big \(\Delta\)), while ensuring the system takes balanced samples of both the closer and further neighbors (with big \(\sigma\)) to hopefully cancel out the effects from the local minimums (which can be hilly and misleading) and aim for the relatively global trend (gradient).
Experiment 4-6 is another set of tests with fixed \(\gamma\) and initial \(u\) while \(\Delta\) gradually increases. However, this set of tests is a counter example of how large \(\Delta\) and \(\sigma\) are not always ideal. In this case, the initial \(u\) is already extrememly close to the lobal optimum, and the red line shows how with both large \(\Delta\) and \(\sigma\), the \(u\) jumps away to another local optimal point.
To investigate how the \(\gamma\) and \(\Delta\) values relate to the choice of \(\sigma\), six more experiments were conducted. Below are the results, as one can see, Experiment 2 and Experiment 5 are two baseline initialization settings. The result after a slight increase of \(\gamma\) are respectively shown in Experiments 1 and 4; and the result after a slight increase of \(\Delta\) are respectively shown in Experiments 3 and 6.
Comparing the red lines (with the largest \(\sigma\)) in Experiments 1 and 2, as well as Experiments 4 and 5. One may notice the large \(\gamma\) makes the system act faster (make bigger corrections to \(u\)), which leads to a minor overshoot in both bases. However, with larger \(\sigma\), it finds the global trend, and hence makes update decisions for \(u\) relatviely more robust to the disturbance of local minimums. Thus, it is able to converge faster (less oscillation). Similarly, the effects of \(\Delta\) in relation to \(\sigma\) can be found in Experiments 2 and 3 as well as Experiments 5 and 6.
Bonus: repeat (a.–e.) with the quadratic function \(c(u) = u^\top H u\) where \(u\in\mathbb{R}^2\) and \(H = H^\top > 0\) is a positive-definite matrix of your choosing; you may want to use the contour function to visualize the results.
dynamic systems
Consider the continuous-time linear time-invariant (CT-LTI) system
\[ \dot{x}(t) = A x(t). \]
Verify that $x(t) = e^{A t} x(0) $ is the trajectory obtained from initial state \(x(0)\).
The remainder of the problem will focus on the following specific \(A\) (obtained from a damped harmonic oscillator): \[ A = \left[\begin{array}{cc} 0 & 1 \\ -\kappa & -\beta \end{array} \right].\]
A discrete-time linear time-invariant (DT-LTI) approximation of the original control system with timestep parameter \(\Delta > 0\) is given by
Mathematically, if we substitute \(\dot{x}(t)=A x(t)\) into the approximation equation \[ x(t + \Delta) \approx x(t) + \Delta\, (\dot{x}(t)) \] Then we can get \[ x(t + \Delta) \approx x(t) + \Delta\,( A x(t) ) = (I+\Delta A)\, x(t) = \hat{A} x(t).\] Therefore, \(\hat{A} = I+\Delta A\).
Run the (forward Euler) iteration $x^+ = x $ with \(\kappa = 1\) and \(\beta = 1\) with multiple values of the parameter \(\Delta > 0\) and compare with the result obtained from the expression for \(x\) from (a.) (you may want to use np.expm to numerically evaluate this expression). Create two types of plots to illustrate your results: (i) states \(x_1,x_2\) versus time \(t\); (ii) second state \(x_2\) versus first state \(x_1\). You should observe that trajectories are spirals that converge to the origin (\(x_1,x_2 \rightarrow 0\)); is there a value of \(\Delta\) that prevents this from happening?
The plots and numerical results are below. As one can observe, when \(\Delta=1\) the trajectory no longer converges to \((x_1,x_2)=(0,0)\), instead it spins in a loop.
Repeat (c.) with \(\beta = 0\). You should observe that the (CT-LTI) trajectories are circles (because \(\beta = 0\) corresponds to an energy-conserving oscillator), whereas the (DT-LTI) trajectories are divergent spirals (because this simulation algorithm does not preserve energy).
Differentiate the (DT-LTI) iteration with respect to initial state and compare with the result from (f.) for \(t = 1\)\(\kappa = 1\) and \(\beta = 0, +1, -1\). (Hint: you may consider computing the derivative analytically or numerically, e.g. using finite differences.)
In problem (d), we found that \[ x(t + \Delta) \approx \hat{A} x(t) = (I+\Delta A)\,x(t)\] This equation can be rewritten as \[ x(t ) \approx \hat{A} x(t- \Delta) = (I+\Delta A)\,x(t - \Delta)\] Now if we recursively expand this equation, we can get \[ x(t ) \approx (I+\Delta A)\,x(t - \Delta)\]\[ x(t ) \approx (I+\Delta A)^2\,x(t - 2\Delta)=(I+\Delta A)^3\,x(t - 3\Delta)=...=(I+\Delta A)^k\,x(t - k\Delta)\]
In summary, through CT-LTI we conclude that \(\frac{d\,x(t)}{d\,x(0)} =e^{A t}\). On the other hand, we derive from DT-LTI that \(\frac{d\,x(t)}{d\,x(0)} \approx(I+\Delta A)^N=\hat{A}\,^N\), where \(t=N\Delta\). So let’s write some code to compare the results!
Code
from scipy.sparse.linalg import expm, expm_multiply# Set parametersbeta_list = [-1, 0,1]delta =1e-3t =1.0k=1N =int(t/delta)# Calculate CT-LTI resultct_lti = expm(t*A)print("CT-LTI dx(t)/dx(0) result:")print(np.matrix(ct_lti))# Calculate DT-LTI resultfor i inrange(len(beta_list)): beta = beta_list[i] A = np.matrix([[0.0, 1.0], [-k, beta]]) A_hat = np.identity(2)+delta*A dt_lti = np.identity(2)for n inrange(N): dt_lti = np.matmul(dt_lti,A_hat)print("\nDT-LTI dx(t)/dx(0) result: β="+str(beta))print(np.matrix(dt_lti))
Bonus: repeat (a.–g.) for the control system \(\dot{x} = A x + B u\) with trajectory expression \[ x(t) = e^{A t} x(0) + \int_0^t e^{A (t - \tau)} B u(\tau) d\tau \] using a sinusoidal input \(u(t) = \sin(\omega t)\) (\(\hat{A}\) will be the same in (b.), but you’ll need to find \(\hat{B}\) for the subsequent subproblems).