Skip to content

Commit f0563b7

Browse files
authored
Addition of three new examples under optimial control. (#370)
* added three examples * Three optimal control problem examples - (1) Profit maximization model for economic optimization against fishing population, (2) Reformulated hanging chain problem from Constrained Optimization Set demonstrating physical constraints, (3) Jennings benchmark with minimal final time objective.
1 parent 40c4008 commit f0563b7

File tree

3 files changed

+220
-0
lines changed

3 files changed

+220
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# # Fishing Optimal Control
2+
# We will solve an optimal control problem to maximize profit
3+
# constrained by fish population
4+
# # Problem Statement and Model
5+
# In this system out input is the rate of fishing ``u``, and the profit
6+
# ``J`` will be defined in the objective to maximize.
7+
# Our profit objective is represented as:
8+
# ```math
9+
# \begin{aligned}
10+
# &&\max_{u(t)} J(t) \\
11+
# &&&&&J = \int_0^{10} \left(E - \frac{c}{x}\right) u U_{max} \, dt \\
12+
# &&\text{s.t.} &&& \frac{dx}{dt}= rx(t)\left(1 - \frac{x(t)}{k}\right) - uU_{max}, t \in [0,10] \\
13+
# &&&&&x(0) = 70 \\
14+
# &&&&&0 \leq u(t) \leq 1 \\
15+
# &&&&&E = 1, \; c = 17.5, \; r = 0.71, \; k = 80.5, \; U_{max} = 20 \\
16+
# &&&&&J(0) = 0 \\
17+
# \end{aligned}
18+
# ```
19+
# # Model Definition
20+
# First we must import ``InfiniteOpt`` and other packages.
21+
using InfiniteOpt, Ipopt, Plots;
22+
# Next we specify an array of initial conditions as well as problem variables.
23+
x0 = 70
24+
E, c, r, k, Umax = 1, 17.5, 0.71, 80.5, 20;
25+
# We initialize the infinite model and opt to use the Ipopt solver
26+
m = InfiniteModel(Ipopt.Optimizer);
27+
# Now let's specify variables. ``u`` is as our fishing rate.
28+
# ``x`` will be used to model the fish population in response to ``u``
29+
# the infinite parameter ``t`` that will span over 10 years.
30+
@infinite_parameter(m, t in [0,10],num_supports=100)
31+
@variable(m, 1 <= x, Infinite(t))
32+
@variable(m, 0 <= u <= 1, Infinite(t));
33+
# ``J`` represents profit over time.
34+
@variable(m, J, Infinite(t));
35+
# Specifying the objective to maximize profit ``J``:
36+
@objective(m, Max, J(10));
37+
# Define the ODEs which serve as our system model.
38+
@constraint(m, (J,t) == (E-c/x) * u * Umax)
39+
@constraint(m, (x,t) == r * x *(1 - x/k) - u*Umax);
40+
# Set our initial conditions.
41+
@constraint(m, x(0) == x0)
42+
@constraint(m, J(0) == 0);
43+
# # Problem Solution
44+
# Optimize the model:
45+
optimize!(m)
46+
# Extract the results.
47+
ts = value(t)
48+
u_opt = value(u)
49+
x_opt = value(x)
50+
J_opt = value(J);
51+
52+
p1 = plot(ts, [x_opt, J_opt] ,
53+
label=["Fish Pop" "Profit"],
54+
title="State Variables")
55+
p2 = plot(ts, u_opt,
56+
label = "Rate",
57+
title = "Rate vs Time")
58+
plot(p1,p2 ,layout=(2,1), size=(800,600));
59+
60+
# ### Maintenance Tests
61+
# These are here to ensure this example stays up to date.
62+
using Test
63+
@test termination_status(m) == MOI.LOCALLY_SOLVED
64+
@test has_values(m)
65+
@test u_opt isa Vector{<:Real}
66+
@test J_opt isa Vector{<:Real}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# # Hanging Chain Problem
2+
# We will solve a reformulated version of the Hanging Chain Problem
3+
# from the Constrained Optimization Problem Set.
4+
# # Problem Statement and Model
5+
# In this problem, we seek to minimize the potential energy of a chain of
6+
# length ``L`` suspended between points ``a`` and ``b``.
7+
# The potential energy constrained by length is represented by:
8+
# ```math
9+
# \begin{gathered}
10+
# \int_0^1 x(1 + (x')^2)^{1/2} \, dt
11+
# \end{gathered}
12+
# ```
13+
# Our optimization problem is defined as follows:
14+
# ```math
15+
# \begin{aligned}
16+
# &&\min_{x,u} x_2(t_f)\\
17+
# &&\text{s.t.} &&& \quad \dot{x}_1= u\\
18+
# &&&&&\dot{x}_2 = x_1(1+u^2)^{1/2}\\
19+
# &&&&&\dot{x}_3 = (1+u^2)^{1/2}\\
20+
# &&&&&x(t_0) = (a,0,0)^T\\
21+
# &&&&&x_1(t_f) = b\\
22+
# &&&&&x_3(t_f) = L\\
23+
# &&&&&x(t) \in [0,10]\\
24+
# &&&&&u(t) \in [-10,20]\\
25+
# \end{aligned}
26+
# ```
27+
28+
# # Model Definition
29+
# First we must import ``InfiniteOpt`` and other packages.
30+
using InfiniteOpt, Ipopt, Plots;
31+
# Next we specify an array of initial conditions as well as problem variables.
32+
a, b, L = 1, 3, 4
33+
x0 = [a, 0, 0]
34+
xtf = [b, NaN, L];
35+
# We initialize the infinite model and opt to use the Ipopt solver
36+
m = InfiniteModel(Ipopt.Optimizer);
37+
# t is specified as ``\ t \in [0,1]``. The bounds are arbitrary for this problem:
38+
@infinite_parameter(m, t in [0,1], num_supports = 100);
39+
# Now let's specify variables. ``u`` is our controller variable.
40+
@variable(m, 0 <= x[1:3] <= 10, Infinite(t))
41+
@variable(m, -10 <= u <= 20, Infinite(t));
42+
# Specifying the objective to minimize kinetic energy at the final time:
43+
@objective(m, Min, x[2](1));
44+
# Define the ODEs which serve as our system model.
45+
@constraint(m, (x[1],t) == u)
46+
@constraint(m, (x[2],t) == x[1] * (1 + u^2)^(1/2))
47+
@constraint(m, (x[3],t) == (1 + u^2)^(1/2));
48+
# Set our inital and final conditions.
49+
@constraint(m, [i in 1:3], x[i](0) == x0[i])
50+
@constraint(m, x[1](1) == xtf[1])
51+
@constraint(m, x[3](1) == xtf[3]);
52+
# # Problem Solution
53+
# Optimize the model:
54+
optimize!(m)
55+
# Extract the results.
56+
ts = value(t)
57+
u_opt = value(u)
58+
x1_opt = value(x[1])
59+
x2_opt = value(x[2])
60+
x3_opt = value(x[3])
61+
@show(objective_value(m))
62+
p1 = plot(ts, [x1_opt, x2_opt, x3_opt],
63+
label=["x1" "x2" "x3"],
64+
title="State Variables")
65+
66+
p2 = plot(ts, u_opt,
67+
label="u(t)",
68+
title="Input")
69+
plot(p1, p2, layout=(2,1), size=(800,600));
70+
71+
# ### Maintenance Tests
72+
# These are here to ensure this example stays up to date.
73+
using Test
74+
@test termination_status(m) == MOI.LOCALLY_SOLVED
75+
@test has_values(m)
76+
@test u_opt isa Vector{<:Real}
77+
@test x1_opt isa Vector{<:Real}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# # Minimizing Final Time (Jennings Problem)
2+
# Solve an optimal control problem with a minimal final time.
3+
# Set up and solve the Jennings optimal control benchmark problem.
4+
# # Problem Statement and Model
5+
# When solving differential equations over a variable time interval ``[0,t_f]``,
6+
# we can apply a time-scaling transformation to normalize the interval to``[0,1]``.
7+
# This is achieved by introducing a final time parameter ``t_f``.
8+
# The Jennings optimal control problem divides derivatives by ``t_f``.
9+
# In practice, ``t_f`` appears on the right hand side to avoid any divisions by 0.
10+
# ```math
11+
# \begin{gathered}
12+
# \frac{\frac{dx}{dt}}{t_f} = f(x,u) \\
13+
# \frac{dx}{dt} = t_f f(x,u) \\
14+
# \end{gathered}
15+
# ```
16+
# Our specific problem is defined as the following:
17+
# ```math
18+
# \begin{aligned}
19+
# &&\min_{u(t),t_f} t_f \\
20+
# &&\text{s.t.} &&& \frac{dx_1}{dt}= t_f u, && t \in [0,1] \\
21+
# &&&&&\frac{dx_2}{dt} = t_f \cos(x_1(t)), && t \in [0,1] \\
22+
# &&&&&\frac{dx_3}{dt} = t_f \sin(x_1(t)), && t \in [0,1] \\
23+
# &&&&&x(0) = [\pi/2, 4, 0] \\
24+
# &&&&&x_2(t_f) = 0 \\
25+
# &&&&&x_3(t_f) = 0 \\
26+
# &&&&&-2 \leq u(t) \leq 2
27+
# \end{aligned}
28+
# ```
29+
30+
# # Model Definition
31+
32+
# First we must import ``InfiniteOpt`` and other packages.
33+
using InfiniteOpt, Ipopt, Plots;
34+
# Next we specify an array of initial conditions.
35+
x0 =/2, 4, 0]; # x(0) for x1,x2,x3
36+
# We initialize the infinite model and opt to use the Ipopt solver
37+
m = InfiniteModel(Ipopt.Optimizer);
38+
# Recall t is specified as ``\ t \in [0,1]``:
39+
@infinite_parameter(m, t in [0,1],num_supports= 100)
40+
# Now let's specify descision variables. Notice that ``t_f`` is
41+
# not a function of time and is a singular value.
42+
@variable(m, x[1:3], Infinite(t))
43+
@variable(m, -2 <= u <= 2, Infinite(t))
44+
@variable(m, 0.1 <= tf);
45+
# Specifying the objective to minimize final time:
46+
@objective(m, Min, tf);
47+
# Define the ODEs which serve as our system model.
48+
@constraint(m, (x[1],t) == tf*u)
49+
@constraint(m, (x[2],t) == tf*cos(x[1]))
50+
@constraint(m, (x[3],t) == tf*sin(x[1]));
51+
# Set our inital and final conditions.
52+
@constraint(m, [i in 1:3], x[i](0) == x0[i])
53+
@constraint(m, x[2](1) <=0)
54+
@constraint(m, x[3](1) <= 1e-1);
55+
# # Problem Solution
56+
# Optimize the model:
57+
optimize!(m)
58+
# Extract the results. Notice that we multiply by ``t_f``
59+
# to scale our time.
60+
ts = value(t)*value(tf)
61+
u_opt = value(u)
62+
x1_opt = value(x[1])
63+
x2_opt = value(x[2])
64+
x3_opt = value(x[3]);
65+
# Plot the results
66+
plot(ts, u_opt, label = "u(t)", linecolor = :black, linestyle = :dash)
67+
plot!(ts, x1_opt, linecolor = :blue, linealpha = 0.4, label = "x1")
68+
plot!(ts, x2_opt, linecolor = :green, linealpha = 0.4, label = "x2")
69+
plot!(ts, x3_opt, linecolor = :red, linealpha = 0.4, label = "x3");
70+
71+
# ### Maintenance Tests
72+
# These are here to ensure this example stays up to date.
73+
using Test
74+
@test termination_status(m) == MOI.LOCALLY_SOLVED
75+
@test has_values(m)
76+
@test u_opt isa Vector{<:Real}
77+
@test x1_opt isa Vector{<:Real}

0 commit comments

Comments
 (0)