In [1]:
%matplotlib inline
from pylab import *
from optoy import *

Equations of a ball in freefall:

$\dot{x}=v_x$

$\dot{y}=v_y$

$\dot{v_x}=0$

$\dot{v_y}=-g$

In [2]:
gravity = par()

states = struct(["x","y","vx","vy"])

# Ode model for a ball
#      state   params constant over stage
def f( x,      dt,g   ):
    return dt*vertcat([
        x[2],
        x[3],
        0,
        -g
    ])

simulator = OdeSimulator(f,T=1)
In [3]:
stage1_x0 = var(shape=4) # Freefall
stage1_t  = var()

stage2_x0 = var(shape=4) # Bounce

stage3_x0 = var(shape=4) # Freefall
stage3_t  = var()

stage4_x  = var(shape=4) # End

# For easier indexing using labels
state1 = states(stage1_x0)
state2 = states(stage2_x0)
state3 = states(stage3_x0)
state4 = states(stage4_x)
In [4]:
# Expression obtained by integrating the ode over symbolic input
xf1 = simulator(stage1_x0, stage1_t,gravity )
xf2 = stage2_x0*vertcat([1,1,1,-0.5])
xf3 = simulator(stage3_x0, stage3_t,gravity )
In [5]:
# Objective
f = -state4["x"]

g = [
        state1["vx"]**2 + state1["vy"]**2 == 1, #normalized speed
        state2["y"] == 0, # Ground contact
        stage2_x0 == xf1, # Transition
        stage3_x0 == xf2, # Transition
        state4["y"] ==0, # Ground contact
        stage4_x == xf3, # Transition   
        state1["x"] == 0,
        state1["y"] == 0,
        stage1_t >= 0,
        stage3_t >= 0
]

stage1_x0.init = [0,0,1,1]
stage3_x0.init = [0,0,1,1]

stage1_t.init = stage3_t.init = 2

gravity.value = 1 # [m/s^2]

solver = minimize(f,g,verbose=True)

print value(stage4_x)
******************************************************************************
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
******************************************************************************

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...:       42
Number of nonzeros in inequality constraint Jacobian.:        2
Number of nonzeros in Lagrangian Hessian.............:        8

Total number of variables............................:       18
                     variables with only lower bounds:        0
                variables with lower and upper bounds:        0
                     variables with only upper bounds:        0
Total number of equality constraints.................:       17
Total number of inequality constraints...............:        2
        inequality constraints with only lower bounds:        0
   inequality constraints with lower and upper bounds:        0
        inequality constraints with only upper bounds:        2

iter    objective    inf_pr   inf_du lg(mu)  ||d||  lg(rg) alpha_du alpha_pr  ls
   0 -0.0000000e+00 2.00e+00 2.29e-01  -1.0 0.00e+00    -  0.00e+00 0.00e+00   0
   1 -1.1769348e+00 3.80e-01 6.19e-01  -1.0 1.18e+00    -  1.00e+00 1.00e+00h  1
   2 -1.5143703e+00 8.81e-03 5.51e-02  -1.7 3.37e-01    -  1.00e+00 1.00e+00h  1
   3 -1.5020642e+00 7.06e-04 3.42e-03  -2.5 4.24e-02    -  1.00e+00 1.00e+00h  1
   4 -1.5000133e+00 4.57e-06 2.53e-05  -3.8 3.47e-03    -  1.00e+00 1.00e+00h  1
   5 -1.5000000e+00 3.18e-09 1.86e-08  -5.7 8.29e-05    -  1.00e+00 1.00e+00h  1
   6 -1.5000000e+00 3.85e-13 2.18e-12  -8.6 8.79e-07    -  1.00e+00 1.00e+00h  1

Number of Iterations....: 6

                                   (scaled)                 (unscaled)
Objective...............:  -1.5000000000011544e+00   -1.5000000000011544e+00
Dual infeasibility......:   2.1780355297096321e-12    2.1780355297096321e-12
Constraint violation....:   3.8458125573015423e-13    3.8458125573015423e-13
Complementarity.........:   2.5070550732432494e-09    2.5070550732432494e-09
Overall NLP error.......:   2.5070550732432494e-09    2.5070550732432494e-09


Number of objective function evaluations             = 7
Number of objective gradient evaluations             = 7
Number of equality constraint evaluations            = 7
Number of inequality constraint evaluations          = 7
Number of equality constraint Jacobian evaluations   = 7
Number of inequality constraint Jacobian evaluations = 7
Number of Lagrangian Hessian evaluations             = 6
Total CPU secs in IPOPT (w/o function evaluations)   =      0.020
Total CPU secs in NLP function evaluations           =      0.284

EXIT: Optimal Solution Found.
                   user           real      num           mean             mean
                   time           time     evals       user time        real time
       eval_f     0.006 [s]      0.006 [s]     7       0.92 [ms]        0.92 [ms]
  eval_grad_f     0.044 [s]      0.044 [s]     8       5.47 [ms]        5.47 [ms]
       eval_g     0.007 [s]      0.007 [s]     7       0.99 [ms]        0.99 [ms]
   eval_jac_g     0.062 [s]      0.061 [s]     9       6.84 [ms]        6.81 [ms]
       eval_h     0.182 [s]      0.182 [s]     7      26.05 [ms]       26.02 [ms]
 all previous     0.301 [s]      0.301 [s]
        ipopt     0.009 [s]      0.008 [s]
    main loop     0.310 [s]      0.308 [s]
[1.5, 0, 0.707107, -0.353553]
In [6]:
gravity.value = 2 # [m/s^2]
solver.update()

print value(stage4_x)
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...:       42
Number of nonzeros in inequality constraint Jacobian.:        2
Number of nonzeros in Lagrangian Hessian.............:        8

Total number of variables............................:       18
                     variables with only lower bounds:        0
                variables with lower and upper bounds:        0
                     variables with only upper bounds:        0
Total number of equality constraints.................:       17
Total number of inequality constraints...............:        2
        inequality constraints with only lower bounds:        0
   inequality constraints with lower and upper bounds:        0
        inequality constraints with only upper bounds:        2

iter    objective    inf_pr   inf_du lg(mu)  ||d||  lg(rg) alpha_du alpha_pr  ls
   0 -0.0000000e+00 3.00e+00 3.94e-01  -1.0 0.00e+00    -  0.00e+00 0.00e+00   0
   1 -3.6826555e+00 1.16e+00 1.51e+00  -1.0 3.68e+00    -  1.00e+00 1.00e+00h  1
   2 -9.0403331e-01 3.12e-01 6.58e-01  -1.0 2.78e+00    -  1.00e+00 1.00e+00h  1
   3 -9.3935714e-01 2.79e-01 1.95e-01  -1.7 4.26e-01    -  7.58e-01 1.00e+00h  1
   4 -7.5419675e-01 1.68e-02 3.53e-02  -1.7 1.85e-01    -  1.00e+00 1.00e+00h  1
   5 -7.5029209e-01 2.45e-04 5.52e-04  -2.5 1.70e-02    -  1.00e+00 1.00e+00h  1
   6 -7.5000687e-01 4.45e-06 1.57e-05  -3.8 1.57e-03    -  1.00e+00 1.00e+00h  1
   7 -7.5000002e-01 1.17e-08 4.11e-08  -5.7 7.81e-05    -  1.00e+00 1.00e+00h  1
   8 -7.5000000e-01 1.57e-12 5.52e-12  -8.6 8.89e-07    -  1.00e+00 1.00e+00h  1

Number of Iterations....: 8

                                   (scaled)                 (unscaled)
Objective...............:  -7.5000000000234812e-01   -7.5000000000234812e-01
Dual infeasibility......:   5.5214418522120758e-12    5.5214418522120758e-12
Constraint violation....:   1.5651924201165457e-12    1.5651924201165457e-12
Complementarity.........:   2.5082416129668846e-09    2.5082416129668846e-09
Overall NLP error.......:   2.5082416129668846e-09    2.5082416129668846e-09


Number of objective function evaluations             = 9
Number of objective gradient evaluations             = 9
Number of equality constraint evaluations            = 9
Number of inequality constraint evaluations          = 9
Number of equality constraint Jacobian evaluations   = 9
Number of inequality constraint Jacobian evaluations = 9
Number of Lagrangian Hessian evaluations             = 8
Total CPU secs in IPOPT (w/o function evaluations)   =      0.024
Total CPU secs in NLP function evaluations           =      0.384

EXIT: Optimal Solution Found.
                   user           real      num           mean             mean
                   time           time     evals       user time        real time
       eval_f     0.008 [s]      0.008 [s]     9       0.91 [ms]        0.91 [ms]
  eval_grad_f     0.057 [s]      0.057 [s]    10       5.68 [ms]        5.66 [ms]
       eval_g     0.009 [s]      0.009 [s]     9       1.01 [ms]        1.01 [ms]
   eval_jac_g     0.079 [s]      0.079 [s]    11       7.17 [ms]        7.17 [ms]
       eval_h     0.244 [s]      0.244 [s]     9      27.13 [ms]       27.09 [ms]
 all previous     0.397 [s]      0.397 [s]
        ipopt     0.010 [s]      0.009 [s]
    main loop     0.407 [s]      0.406 [s]
[0.75, 0, 0.707107, -0.353553]
In [6]:
 
In [ ]: