Simple optoy demo

In [1]:
%matplotlib inline

In [2]:
from pylab import *
from optoy import *

s  = state(name="speed",init=1)
p  = state(name="position")

u  = control(name="throttle")

tf = var(name="final time",init=1)

s.dot = u - s
p.dot = s

smax = 1-sin(2*pi*p)/2

constraints = [

# Boundary conditions
s.start == 0,
p.start == 0,
p.end   == 1,

# Path constraints
u       <=  1,
s       <= smax
]

print ocp(tf, constraints, T=tf,verbose=True,N=50)

plot(value(u),'b');
plot(value(s),'g');
plot(value(smax),'r');

(1, 1)

******************************************************************************
This program contains Ipopt, a library for large-scale nonlinear optimization.
Ipopt is released as open source code under the Eclipse Public License (EPL).
******************************************************************************

This is Ipopt version 3.11.9, running with linear solver mumps.
NOTE: Other linear solvers might be more efficient (see Ipopt documentation).

Number of nonzeros in equality constraint Jacobian...:      453
Number of nonzeros in inequality constraint Jacobian.:      150
Number of nonzeros in Lagrangian Hessian.............:      151

Total number of variables............................:      153
variables with only lower bounds:        0
variables with lower and upper bounds:        0
variables with only upper bounds:        0
Total number of equality constraints.................:      103
Total number of inequality constraints...............:      100
inequality constraints with only lower bounds:        0
inequality constraints with lower and upper bounds:        0
inequality constraints with only upper bounds:      100

iter    objective    inf_pr   inf_du lg(mu)  ||d||  lg(rg) alpha_du alpha_pr  ls
0  1.0000000e+00 1.00e+00 7.28e-01  -1.0 0.00e+00    -  0.00e+00 0.00e+00   0
1  1.2199308e+00 9.04e-01 8.40e+00  -1.7 4.27e+00    -  1.01e-02 9.58e-02h  1
2  1.4838594e+00 8.27e-01 7.69e+00  -1.7 3.10e+00    -  1.18e-01 8.52e-02h  4
3  1.5772587e+00 8.00e-01 7.44e+00  -1.7 2.85e+00    -  8.09e-02 3.28e-02h  5
4  1.7441419e+00 7.45e-01 6.93e+00  -1.7 2.44e+00    -  3.72e-02 6.84e-02h  4
5  1.4760314e+00 3.81e-01 1.69e+01  -1.7 1.57e+00    -  3.29e-02 4.89e-01F  1
6  1.6580267e+00 2.06e-01 1.05e+01  -1.7 1.27e+00    -  8.42e-02 4.58e-01h  1
7  2.0307307e+00 3.99e-03 7.74e+00  -1.7 5.32e-01    -  1.54e-01 1.00e+00h  1
8  2.0732984e+00 7.16e-05 6.23e-01  -1.7 3.39e-01    -  8.50e-01 1.00e+00h  1
9  2.2711672e+00 6.09e-04 8.44e-01  -1.7 2.12e-01    -  5.44e-01 1.00e+00f  1
iter    objective    inf_pr   inf_du lg(mu)  ||d||  lg(rg) alpha_du alpha_pr  ls
10  2.5765409e+00 1.57e-03 4.68e-01  -1.7 4.06e-01    -  8.13e-01 1.00e+00f  1
11  2.7228868e+00 3.97e-03 8.30e-02  -1.7 1.51e+00    -  1.00e+00 1.00e+00h  1
12  2.1743414e+00 3.33e-03 7.05e-02  -2.5 5.49e-01    -  1.00e+00 1.00e+00f  1
13  2.0287249e+00 3.01e-03 1.57e-02  -2.5 1.30e+00    -  1.00e+00 1.00e+00h  1
14  1.9673778e+00 1.97e-03 5.29e-02  -3.8 1.79e+00    -  8.59e-01 5.18e-01h  1
15  1.9202748e+00 1.26e-03 5.07e-02  -3.8 1.24e+00    -  9.61e-01 7.98e-01h  1
16  1.9112536e+00 5.05e-05 1.04e-03  -3.8 2.89e-01    -  1.00e+00 1.00e+00h  1
17  1.9063687e+00 2.59e-05 4.20e-02  -5.7 1.63e-01    -  8.61e-01 6.66e-01h  1
18  1.9042678e+00 4.76e-06 3.76e-03  -5.7 5.90e-02    -  8.91e-01 8.94e-01h  1
19  1.9040256e+00 3.40e-08 3.53e-06  -5.7 7.67e-03    -  1.00e+00 1.00e+00f  1
iter    objective    inf_pr   inf_du lg(mu)  ||d||  lg(rg) alpha_du alpha_pr  ls
20  1.9039352e+00 4.01e-09 2.98e-05  -8.6 2.25e-03    -  9.82e-01 9.90e-01h  1
21  1.9039342e+00 4.06e-13 4.07e-11  -8.6 2.45e-05    -  1.00e+00 1.00e+00f  1

Number of Iterations....: 21

(scaled)                 (unscaled)
Objective...............:   1.9039342485569817e+00    1.9039342485569817e+00
Dual infeasibility......:   4.0684922497639088e-11    4.0684922497639088e-11
Constraint violation....:   4.0634162701280729e-13    4.0634162701280729e-13
Complementarity.........:   2.5114027187434402e-09    2.5114027187434402e-09
Overall NLP error.......:   2.5114027187434402e-09    2.5114027187434402e-09

Number of objective function evaluations             = 39
Number of objective gradient evaluations             = 22
Number of equality constraint evaluations            = 39
Number of inequality constraint evaluations          = 39
Number of equality constraint Jacobian evaluations   = 22
Number of inequality constraint Jacobian evaluations = 22
Number of Lagrangian Hessian evaluations             = 21
Total CPU secs in IPOPT (w/o function evaluations)   =      0.028
Total CPU secs in NLP function evaluations           =      0.140

EXIT: Optimal Solution Found.
proc           wall      num           mean             mean
time           time     evals       proc time        wall time
eval_f     0.012 [s]      0.012 [s]    39       0.30 [ms]        0.30 [ms]
eval_grad_f     0.009 [s]      0.009 [s]    23       0.38 [ms]        0.38 [ms]
eval_g     0.014 [s]      0.014 [s]    39       0.35 [ms]        0.35 [ms]
eval_jac_g     0.043 [s]      0.043 [s]    24       1.80 [ms]        1.80 [ms]
eval_h     0.057 [s]      0.057 [s]    22       2.57 [ms]        2.57 [ms]
main loop     0.171 [s]      0.171 [s]
1.90393424856

In [3]:
value(u)

Out[3]:
[DMatrix(1),
DMatrix(1),
DMatrix(1),
DMatrix(1),
DMatrix(1),
DMatrix(1),
DMatrix(1),
DMatrix(1),
DMatrix(1),
DMatrix(1),
DMatrix(1),
DMatrix(1),
DMatrix(1),
DMatrix(1),
DMatrix(1),
DMatrix(1),
DMatrix(1),
DMatrix(1),
DMatrix(0.999999),
DMatrix(0.243004),
DMatrix(0.378936),
DMatrix(0.566987),
DMatrix(0.765166),
DMatrix(0.982053),
DMatrix(1),
DMatrix(1),
DMatrix(1),
DMatrix(1),
DMatrix(1),
DMatrix(1),
DMatrix(1),
DMatrix(1),
DMatrix(1),
DMatrix(1),
DMatrix(1),
DMatrix(1),
DMatrix(1),
DMatrix(1),
DMatrix(1),
DMatrix(1),
DMatrix(1),
DMatrix(1),
DMatrix(1),
DMatrix(1),
DMatrix(1),
DMatrix(1),
DMatrix(1),
DMatrix(0.999999),
DMatrix(0.999999),
DMatrix(0.999997)]
In [4]:
constraints = [
s.start == 0,
p.start == 0,
p.end   == 1,
u <=  1,
s <= smax ]

print ocp(tf, constraints, T=tf,verbose=True,N=50)

plot(value(u),'b');
plot(value(s),'g');
plot(value(smax),'r');

(1, 1)
This is Ipopt version 3.11.9, running with linear solver mumps.
NOTE: Other linear solvers might be more efficient (see Ipopt documentation).

Number of nonzeros in equality constraint Jacobian...:      453
Number of nonzeros in inequality constraint Jacobian.:      150
Number of nonzeros in Lagrangian Hessian.............:      151

Total number of variables............................:      153
variables with only lower bounds:        0
variables with lower and upper bounds:        0
variables with only upper bounds:        0
Total number of equality constraints.................:      103
Total number of inequality constraints...............:      100
inequality constraints with only lower bounds:        0
inequality constraints with lower and upper bounds:        0
inequality constraints with only upper bounds:      100

iter    objective    inf_pr   inf_du lg(mu)  ||d||  lg(rg) alpha_du alpha_pr  ls
0  1.0000000e+00 1.00e+00 7.28e-01  -1.0 0.00e+00    -  0.00e+00 0.00e+00   0
1  1.2199308e+00 9.04e-01 8.40e+00  -1.7 4.27e+00    -  1.01e-02 9.58e-02h  1
2  1.4838594e+00 8.27e-01 7.69e+00  -1.7 3.10e+00    -  1.18e-01 8.52e-02h  4
3  1.5772587e+00 8.00e-01 7.44e+00  -1.7 2.85e+00    -  8.09e-02 3.28e-02h  5
4  1.7441419e+00 7.45e-01 6.93e+00  -1.7 2.44e+00    -  3.72e-02 6.84e-02h  4
5  1.4760314e+00 3.81e-01 1.69e+01  -1.7 1.57e+00    -  3.29e-02 4.89e-01F  1
6  1.6580267e+00 2.06e-01 1.05e+01  -1.7 1.27e+00    -  8.42e-02 4.58e-01h  1
7  2.0307307e+00 3.99e-03 7.74e+00  -1.7 5.32e-01    -  1.54e-01 1.00e+00h  1
8  2.0732984e+00 7.16e-05 6.23e-01  -1.7 3.39e-01    -  8.50e-01 1.00e+00h  1
9  2.2711672e+00 6.09e-04 8.44e-01  -1.7 2.12e-01    -  5.44e-01 1.00e+00f  1
iter    objective    inf_pr   inf_du lg(mu)  ||d||  lg(rg) alpha_du alpha_pr  ls
10  2.5765409e+00 1.57e-03 4.68e-01  -1.7 4.06e-01    -  8.13e-01 1.00e+00f  1
11  2.7228868e+00 3.97e-03 8.30e-02  -1.7 1.51e+00    -  1.00e+00 1.00e+00h  1
12  2.1743414e+00 3.33e-03 7.05e-02  -2.5 5.49e-01    -  1.00e+00 1.00e+00f  1
13  2.0287249e+00 3.01e-03 1.57e-02  -2.5 1.30e+00    -  1.00e+00 1.00e+00h  1
14  1.9673778e+00 1.97e-03 5.29e-02  -3.8 1.79e+00    -  8.59e-01 5.18e-01h  1
15  1.9202748e+00 1.26e-03 5.07e-02  -3.8 1.24e+00    -  9.61e-01 7.98e-01h  1
16  1.9112536e+00 5.05e-05 1.04e-03  -3.8 2.89e-01    -  1.00e+00 1.00e+00h  1
17  1.9063687e+00 2.59e-05 4.20e-02  -5.7 1.63e-01    -  8.61e-01 6.66e-01h  1
18  1.9042678e+00 4.76e-06 3.76e-03  -5.7 5.90e-02    -  8.91e-01 8.94e-01h  1
19  1.9040256e+00 3.40e-08 3.53e-06  -5.7 7.67e-03    -  1.00e+00 1.00e+00f  1
iter    objective    inf_pr   inf_du lg(mu)  ||d||  lg(rg) alpha_du alpha_pr  ls
20  1.9039352e+00 4.01e-09 2.98e-05  -8.6 2.25e-03    -  9.82e-01 9.90e-01h  1
21  1.9039342e+00 4.06e-13 4.07e-11  -8.6 2.45e-05    -  1.00e+00 1.00e+00f  1

Number of Iterations....: 21

(scaled)                 (unscaled)
Objective...............:   1.9039342485569817e+00    1.9039342485569817e+00
Dual infeasibility......:   4.0684922497639088e-11    4.0684922497639088e-11
Constraint violation....:   4.0634162701280729e-13    4.0634162701280729e-13
Complementarity.........:   2.5114027187434402e-09    2.5114027187434402e-09
Overall NLP error.......:   2.5114027187434402e-09    2.5114027187434402e-09

Number of objective function evaluations             = 39
Number of objective gradient evaluations             = 22
Number of equality constraint evaluations            = 39
Number of inequality constraint evaluations          = 39
Number of equality constraint Jacobian evaluations   = 22
Number of inequality constraint Jacobian evaluations = 22
Number of Lagrangian Hessian evaluations             = 21
Total CPU secs in IPOPT (w/o function evaluations)   =      0.032
Total CPU secs in NLP function evaluations           =      0.128

EXIT: Optimal Solution Found.
proc           wall      num           mean             mean
time           time     evals       proc time        wall time
eval_f     0.011 [s]      0.011 [s]    39       0.28 [ms]        0.28 [ms]
eval_grad_f     0.008 [s]      0.008 [s]    23       0.33 [ms]        0.33 [ms]
eval_g     0.012 [s]      0.012 [s]    39       0.32 [ms]        0.32 [ms]
eval_jac_g     0.042 [s]      0.042 [s]    24       1.75 [ms]        1.75 [ms]
eval_h     0.055 [s]      0.055 [s]    22       2.49 [ms]        2.48 [ms]
main loop     0.160 [s]      0.159 [s]
1.90393424856

In [ ]: