Difference between revisions of "Lotka Volterra fishing problem (JuMP)"
From mintOC
FelixMueller (Talk | contribs) |
FelixMueller (Talk | contribs) |
||
(4 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
− | JuMP code | + | This is an implementation of the [[Lotka Volterra fishing problem]] using JuMP. |
+ | 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. | ||
− | + | The control function was relaxed, i.e. <math> u(t) \in [0,1] </math>. | |
− | [[Category:Julia/JuMP]] | + | Model file ("lotka_mod.jl"): |
+ | <source lang="AMPL"> | ||
+ | #JuMP implementation of Lotka-Volterra fishing example using collocation | ||
+ | #mod file | ||
+ | |||
+ | #declaring the model | ||
+ | m = Model() | ||
+ | |||
+ | #defining variables | ||
+ | @defVar(m, x[ii=1:n_x, tt=1:N]) | ||
+ | @defVar(m, L_control <= 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)) | ||
+ | @addConstraint(m, ODE[ii=1:n_x, tt=1:N-1], x[ii,tt+1] - x[ii,tt] - step_size * ode_rhs(x[:,tt], u[tt])[ii] == 0) | ||
+ | </source> | ||
+ | |||
+ | Data file ("lotka_dat.jl"): | ||
+ | <source lang="AMPL"> | ||
+ | #JuMP implementation of Lotka-Volterra fishing example using collocation | ||
+ | #dat file | ||
+ | |||
+ | #general parameters | ||
+ | c0 = 0.4; | ||
+ | c1 = 0.2; | ||
+ | |||
+ | #number of states | ||
+ | n_x = 3; | ||
+ | |||
+ | ##discretization | ||
+ | #number of shooting intervals / discretization points | ||
+ | N = 300; | ||
+ | #starting / end time | ||
+ | t_start = 0; | ||
+ | t_end = 12; | ||
+ | #time discretization | ||
+ | time = linspace(t_start,t_end, N+1); | ||
+ | step_size = (t_end - t_start)/N; | ||
+ | |||
+ | #starting value | ||
+ | x_start = [0.5, 0.7, 0]; | ||
+ | |||
+ | #bounds for control | ||
+ | L_control = 0 | ||
+ | U_control = 1; | ||
+ | |||
+ | |||
+ | ##right hand side of ODE | ||
+ | function ode_rhs(state, control) | ||
+ | #give in form f1, f2, f3,... | ||
+ | state[1] - state[1] * state[2] - c0 * state[1] * control, | ||
+ | -state[2] + state[1] * state[2] - c1 * state[2] * control, | ||
+ | (state[1]-1) * (state[1]-1) + (state[2]-1) * (state[2]-1) | ||
+ | end | ||
+ | </source> | ||
+ | |||
+ | Run file ("lotka_run.jl"): | ||
+ | <source lang="AMPL"> | ||
+ | #JuMP implementation of Lotka-Volterra fishing example using collocation | ||
+ | #run file | ||
+ | |||
+ | using JuMP; | ||
+ | using Ipopt; | ||
+ | |||
+ | |||
+ | println("----------------------------------------------------") | ||
+ | println("Time used for data") | ||
+ | @time include("lotka_dat.jl") | ||
+ | println("----------------------------------------------------") | ||
+ | println("Time used for modeling") | ||
+ | @time include("lotka_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)) | ||
+ | </source> | ||
+ | |||
+ | [[Category: Julia/JuMP]] |
Latest revision as of 15:59, 19 January 2016
This is an implementation of the Lotka Volterra fishing problem using JuMP. 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.
The control function was relaxed, i.e. .
Model file ("lotka_mod.jl"):
#JuMP implementation of Lotka-Volterra fishing example using collocation #mod file #declaring the model m = Model() #defining variables @defVar(m, x[ii=1:n_x, tt=1:N]) @defVar(m, L_control <= 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)) @addConstraint(m, ODE[ii=1:n_x, tt=1:N-1], x[ii,tt+1] - x[ii,tt] - step_size * ode_rhs(x[:,tt], u[tt])[ii] == 0)
Data file ("lotka_dat.jl"):
#JuMP implementation of Lotka-Volterra fishing example using collocation #dat file #general parameters c0 = 0.4; c1 = 0.2; #number of states n_x = 3; ##discretization #number of shooting intervals / discretization points N = 300; #starting / end time t_start = 0; t_end = 12; #time discretization time = linspace(t_start,t_end, N+1); step_size = (t_end - t_start)/N; #starting value x_start = [0.5, 0.7, 0]; #bounds for control L_control = 0 U_control = 1; ##right hand side of ODE function ode_rhs(state, control) #give in form f1, f2, f3,... state[1] - state[1] * state[2] - c0 * state[1] * control, -state[2] + state[1] * state[2] - c1 * state[2] * control, (state[1]-1) * (state[1]-1) + (state[2]-1) * (state[2]-1) end
Run file ("lotka_run.jl"):
#JuMP implementation of Lotka-Volterra fishing example using collocation #run file using JuMP; using Ipopt; println("----------------------------------------------------") println("Time used for data") @time include("lotka_dat.jl") println("----------------------------------------------------") println("Time used for modeling") @time include("lotka_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))