4.1.3. Hypersensitive OCP#

  1. 2023 Devakumar Thammisetty

MPOPT is an open-source Multi-phase Optimal Control Problem (OCP) solver based on pseudo-spectral collocation with customized adaptive grid refinement techniques.

https://mpopt.readthedocs.io/

Download this notebook: hypersensitive.ipynb

Install mpopt from pypi using the following. Disable after first usage

Import mpopt (Contains main solver modules)

[1]:
#!pip install mpopt
from mpopt import mp

4.1.3.1. Defining OCP#

Hypersensitive OCP: https://www.gpops2.com/Examples/HyperSensitive.html

We first create an OCP object and then polulate the object with dynamics, path_constraints, terminal_constraints and objective (running_costs, terminal_costs)

[2]:
ocp = mp.OCP(n_states=1, n_controls=1, n_phases=1)
[3]:
ocp.dynamics[0] = lambda x, u, t: [-x[0] * x[0] * x[0] + u[0]]
ocp.running_costs[0] = lambda x, u, t: 0.5 * (x[0] * x[0] + u[0] * u[0])
ocp.terminal_constraints[0] = lambda xf, tf, x0, t0: [xf[0] - 1.0]

Initial state

[4]:
ocp.x00[0] = 1

Box constraints

[5]:
ocp.lbtf[0] = ocp.ubtf[0] = 1000.0

Scale the time variable

[6]:
ocp.scale_t = 1 / 1000.0
[7]:
ocp.validate()

4.1.3.2. Solve and plot the results in one line#

Lets solve the OCP using following pseudo-spectral approximation * Collocation using Legendre-Gauss-Radau roots * Let’s plot the position and velocity evolution with time starting from 0.

[8]:
mpo, post = mp.solve(ocp, n_segments=5, poly_orders=50, scheme="LGR", plot=True)

******************************************************************************
This program contains Ipopt, a library for large-scale nonlinear optimization.
 Ipopt is released as open source code under the Eclipse Public License (EPL).
         For more information visit http://projects.coin-or.org/Ipopt
******************************************************************************

Total number of variables............................:      501
                     variables with only lower bounds:        0
                variables with lower and upper bounds:        0
                     variables with only upper bounds:        0
Total number of equality constraints.................:      252
Total number of inequality constraints...............:        0
        inequality constraints with only lower bounds:        0
   inequality constraints with lower and upper bounds:        0
        inequality constraints with only upper bounds:        0


Number of Iterations....: 12

                                   (scaled)                 (unscaled)
Objective...............:   1.1498050755273090e+00    1.1498050755273090e+00
Dual infeasibility......:   4.7184478546569153e-16    4.7184478546569153e-16
Constraint violation....:   1.4589005542615039e-14    1.1368683772161603e-13
Complementarity.........:   0.0000000000000000e+00    0.0000000000000000e+00
Overall NLP error.......:   1.4589005542615039e-14    1.1368683772161603e-13


Number of objective function evaluations             = 30
Number of objective gradient evaluations             = 13
Number of equality constraint evaluations            = 42
Number of inequality constraint evaluations          = 0
Number of equality constraint Jacobian evaluations   = 13
Number of inequality constraint Jacobian evaluations = 0
Number of Lagrangian Hessian evaluations             = 12
Total CPU secs in IPOPT (w/o function evaluations)   =      0.066
Total CPU secs in NLP function evaluations           =      0.010

EXIT: Optimal Solution Found.
      solver  :   t_proc      (avg)   t_wall      (avg)    n_eval
       nlp_f  | 340.00us ( 11.33us) 340.41us ( 11.35us)        30
       nlp_g  |   4.85ms (115.38us)   4.78ms (113.76us)        42
    nlp_grad  | 266.00us (266.00us) 265.36us (265.36us)         1
  nlp_grad_f  | 322.00us ( 23.00us) 322.67us ( 23.05us)        14
  nlp_hess_l  | 592.00us ( 49.33us) 595.87us ( 49.66us)        12
   nlp_jac_g  |   2.76ms (197.43us)   2.75ms (196.68us)        14
       total  |  76.82ms ( 76.82ms)  76.14ms ( 76.14ms)         1
../_images/notebooks_hypersensitive_15_1.png

Retrive the solution

x: states, u: Controls, t:time, a:Algebraic variables

[9]:
x, u, t, a = post.get_data()
print(f"Terminal time, state : {t[-1][0]:.4f} vs 1000 (Exact), {x[-1]}")
Terminal time, state : 1000.0000 vs 1000 (Exact), [1.]

4.1.3.3. Solve again with Chebyshev-Gauss-Lobatto (CGL) roots#

[10]:
mpo, post = mp.solve(ocp, n_segments=5, poly_orders=50, scheme="CGL", plot=True)
Total number of variables............................:      501
                     variables with only lower bounds:        0
                variables with lower and upper bounds:        0
                     variables with only upper bounds:        0
Total number of equality constraints.................:      252
Total number of inequality constraints...............:        0
        inequality constraints with only lower bounds:        0
   inequality constraints with lower and upper bounds:        0
        inequality constraints with only upper bounds:        0


Number of Iterations....: 12

                                   (scaled)                 (unscaled)
Objective...............:   1.1406025536022588e+00    1.1406025536022588e+00
Dual infeasibility......:   5.5128124287762148e-13    5.5128124287762148e-13
Constraint violation....:   5.6878507039202523e-12    1.8587797967484221e-11
Complementarity.........:   0.0000000000000000e+00    0.0000000000000000e+00
Overall NLP error.......:   5.6878507039202523e-12    1.8587797967484221e-11


Number of objective function evaluations             = 36
Number of objective gradient evaluations             = 13
Number of equality constraint evaluations            = 49
Number of inequality constraint evaluations          = 0
Number of equality constraint Jacobian evaluations   = 13
Number of inequality constraint Jacobian evaluations = 0
Number of Lagrangian Hessian evaluations             = 12
Total CPU secs in IPOPT (w/o function evaluations)   =      0.061
Total CPU secs in NLP function evaluations           =      0.010

EXIT: Optimal Solution Found.
      solver  :   t_proc      (avg)   t_wall      (avg)    n_eval
       nlp_f  | 425.00us ( 11.81us) 407.26us ( 11.31us)        36
       nlp_g  |   5.59ms (114.14us)   5.58ms (113.88us)        49
    nlp_grad  | 264.00us (264.00us) 263.05us (263.05us)         1
  nlp_grad_f  | 325.00us ( 23.21us) 325.10us ( 23.22us)        14
  nlp_hess_l  | 614.00us ( 51.17us) 617.11us ( 51.43us)        12
   nlp_jac_g  |   2.82ms (201.79us)   2.81ms (201.06us)        14
       total  |  78.41ms ( 78.41ms)  77.65ms ( 77.65ms)         1
../_images/notebooks_hypersensitive_19_1.png
[11]:
x, u, t, a = post.get_data()
print(f"Terminal time, state : {t[-1][0]:.4f} vs 1000s (Exact), {x[-1]}")
Terminal time, state : 1000.0000 vs 1000s (Exact), [1.]

4.1.3.4. Solve again with Legendre-Gauss-Lobatto (LGL) roots#

[12]:
mpo, post = mp.solve(ocp, n_segments=5, poly_orders=50, scheme="LGL", plot=True)
Total number of variables............................:      501
                     variables with only lower bounds:        0
                variables with lower and upper bounds:        0
                     variables with only upper bounds:        0
Total number of equality constraints.................:      252
Total number of inequality constraints...............:        0
        inequality constraints with only lower bounds:        0
   inequality constraints with lower and upper bounds:        0
        inequality constraints with only upper bounds:        0


Number of Iterations....: 12

                                   (scaled)                 (unscaled)
Objective...............:   1.1502075893651909e+00    1.1502075893651909e+00
Dual infeasibility......:   1.0234087882698972e-13    1.0234087882698972e-13
Constraint violation....:   4.7414571594509346e-13    1.3997691894473974e-12
Complementarity.........:   0.0000000000000000e+00    0.0000000000000000e+00
Overall NLP error.......:   4.7414571594509346e-13    1.3997691894473974e-12


Number of objective function evaluations             = 36
Number of objective gradient evaluations             = 13
Number of equality constraint evaluations            = 49
Number of inequality constraint evaluations          = 0
Number of equality constraint Jacobian evaluations   = 13
Number of inequality constraint Jacobian evaluations = 0
Number of Lagrangian Hessian evaluations             = 12
Total CPU secs in IPOPT (w/o function evaluations)   =      0.064
Total CPU secs in NLP function evaluations           =      0.011

EXIT: Optimal Solution Found.
      solver  :   t_proc      (avg)   t_wall      (avg)    n_eval
       nlp_f  | 423.00us ( 11.75us) 431.21us ( 11.98us)        36
       nlp_g  |   6.12ms (124.82us)   6.12ms (124.86us)        49
    nlp_grad  | 264.00us (264.00us) 263.12us (263.12us)         1
  nlp_grad_f  | 326.00us ( 23.29us) 327.97us ( 23.43us)        14
  nlp_hess_l  | 611.00us ( 50.92us) 610.94us ( 50.91us)        12
   nlp_jac_g  |   2.82ms (201.07us)   2.82ms (201.54us)        14
       total  |  77.49ms ( 77.49ms)  76.93ms ( 76.93ms)         1
../_images/notebooks_hypersensitive_22_1.png
[13]:
x, u, t, a = post.get_data()
print(f"Terminal time, state : {t[-1][0]:.4f} vs 1000s (Exact), {x[-1]}")
Terminal time, state : 1000.0000 vs 1000s (Exact), [1.]
[ ]: