Van der Pol Oscillator (Jump)

From mintOC
Revision as of 18:25, 18 January 2016 by FelixMueller (Talk | contribs)

Jump to: navigation, search

This is an implementation of a slightly modified form of the Van der Pol oscillator problem using.

The problem in question can be stated as follows:

Failed to parse (PNG conversion failed; check for correct installation of latex and dvipng (or dvips + gs + convert)): {\begin{array}{llcl}\displaystyle \max _{{x,u}}&x_{3}(t_{f})\\[1.5ex]{\mbox{s.t.}}&{\dot {x}}_{1}(t)&=&(1-x_{2}(t)^{2})x_{1}(t)-x_{2}(t)+u(t)\qquad &\forall t\in [t_{0},t_{f}],\\&{\dot {x}}_{2}(t)&=&x_{1}(t)\qquad &\forall t\in [t_{0},t_{f}],\\&{\dot {x}}_{3}(t)&=&x_{1}(t)^{2}+x_{2}(t)^{2}+u(t)^{2}\qquad &\forall t\in [t_{0},t_{f}],\\[1.5ex]&x(0)&=&(0,1,0)^{T},\\&u(t)&\in &[-0.3,1]\qquad &\forall t\in [t_{0},t_{f}].\end{array}}

where  [t_0, t_f] = [0,5] .

The problem was discretized and the ODEs are solved using the explicit Euler method. Although not necessary in JuMP the code was divided into three parts (following AMPL) - model file, data file and run file. The run file calls the other files and performs additional tasks such as printing results.


Model file ("vdposc_mod.jl"):

#JuMP implementation of Van der Pol oscillator using collocation
#mod file
 
#declaring the model
m = Model(solver=IpoptSolver())
 
#defining variables
@defVar(m, x[ii=1:n_x, tt=1:N])
@defVar(m, L_control <= u[jj = 1:n_u, tt=1:N] <= U_control)
 
#set objective function
@setObjective(m, Min, x[3,N])
 
#setting constraints
#starting values
@addConstraint(m, starting_value[ii=1:n_x], x[ii,1] == x_start[ii])
 
#ODE - solved with explicit euler method (i.e. x_k+1 = x_k + stepsize * f(x_k, t_k))
@addNLConstraint(m, ODE_nonlin[ii=1:1, tt=1:N-1],  x[ii,tt+1] - x[ii,tt] - step_size * ((1 - x[2,tt]^2) * x[1,tt] - x[2,tt] + u[1,tt]) == 0)
@addConstraint(m, ODE[ii=2:n_x, tt=1:N-1],  x[ii,tt+1] - x[ii,tt] - step_size * ode_rhs(time_disc[tt], x[:,tt], u[:,tt])[ii]  >= 0)
@addConstraint(m, ODE[ii=2:n_x, tt=1:N-1],  x[ii,tt+1] - x[ii,tt] - step_size * ode_rhs(time_disc[tt], x[:,tt], u[:,tt])[ii]  <= 0)

Data file ("vdposc_dat.jl"):

#JuMP implementation of Van der Pol oscillator using collocation
#dat file
 
#number of states
n_x = 3;
 
#number of controls
n_u = 1;
 
##discretization
#number of shooting intervals / discretization points
N = 300;
#starting / end time
t_start = 0;
t_end = 5;
#time discretization
time_disc = linspace(t_start,t_end, N+1);
step_size = (t_end - t_start)/N;
 
#starting value
x_start = [0, 1, 0];
 
#bounds for control
L_control = -0.3;
U_control = 1;
 
 
##right hand side of ODE
function ode_rhs(time, state, control)
#give in form f1, f2, f3,...
0,
state[1],
state[1]^2 + state[2]^2 + control[1]^2
end

Run file ("vdposc_run.jl"):

#JuMP implementation of Van der Pol oscillator using collocation
#run file
 
using JuMP;
using Ipopt;
 
 
println("----------------------------------------------------")
println("Time used for data")
@time include("vdposc_dat.jl")
println("----------------------------------------------------")
println("Time used for modeling")
@time include("vdposc_mod.jl")
println("----------------------------------------------------")
println("Time used for solving")
@time solve(m);
 
println("----------------------------------------------------")
println("----------------------------------------------------")
 
 
println("Optimal objective value is: ", getObjectiveValue(m))
println("Optimal Solution is: \n", getValue(x), getValue(u))