You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Update Optimal Control Example Documentation (#380)
* Update Fishing example docs
* Adjust unit test of fishing example to test output values within tolerance
* Update Hanging Chain example docs
* Update Jennings example docs
* Added unit tests to examples (excluding pandemic control)
* Update tolerance to 1E-06
* Minor update
---------
Co-authored-by: pulsipher <[email protected]>
# We will solve an optimal control problem to maximize profit
3
-
# constrained by fish population
3
+
# constrained by fish population.
4
+
4
5
# # Problem Statement and Model
5
-
# In this system out input is the rate of fishing ``u``, and the profit
6
+
# In this system, our input is the fishing rate ``u``, and the profit
6
7
# ``J`` will be defined in the objective to maximize.
7
8
# Our profit objective is represented as:
8
9
# ```math
@@ -16,51 +17,76 @@
16
17
# &&&&&J(0) = 0 \\
17
18
# \end{aligned}
18
19
# ```
19
-
# # Model Definition
20
+
21
+
# # Modeling in InfiniteOpt
20
22
# First we must import ``InfiniteOpt`` and other packages.
21
23
using InfiniteOpt, Ipopt, Plots;
22
-
# Next we specify an array of initial conditions as well as problem variables.
24
+
25
+
# Next we specify our initial conditions and problem variables.
23
26
x0 =70
24
27
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
28
+
29
+
# # Model Initialization
30
+
# We initialize the infinite model with [`InfiniteModel`](@ref) and select Ipopt as our optimizer that will be used to solve it.
26
31
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)
32
+
33
+
# # Infinite Parameter Definition
34
+
# We now define the infinite parameter ``t \in [0, 10]`` to represent time over a 10 year period. We'll also specify 100 equidistant time points.
35
+
@infinite_parameter(m, t in [0, 10], num_supports=100)
36
+
37
+
# # Infinite Variable Definition
38
+
# Now that we have our infinite parameter defined, let's specify our infinite variables:
39
+
# - ``1 \leq x(t)`` : fish population at time ``t``
40
+
# - ``0 \leq u(t) \leq 1`` : fishing rate
41
+
# - ``J(t)`` : profit over time
31
42
@variable(m, 1<= x, Infinite(t))
32
43
@variable(m, 0<= u <=1, Infinite(t));
33
-
# ``J`` represents profit over time.
34
44
@variable(m, J, Infinite(t));
35
-
# Specifying the objective to maximize profit ``J``:
45
+
46
+
# # Objective Definition
47
+
# Now we add the objective using `@objective` to maximize profit ``J`` at the end of the 10 year period:
36
48
@objective(m, Max, J(10));
37
-
# Define the ODEs which serve as our system model.
49
+
50
+
# # Constraint Definition
51
+
# The last step is to add our constraints. First, define the ODEs which serve as our system model.
38
52
@constraint(m, ∂(J,t) == (E-c/x) * u * Umax)
39
53
@constraint(m, ∂(x,t) == r * x *(1- x/k) - u*Umax);
40
-
#Set our initial conditions.
54
+
#We also set our initial conditions for ``x`` and ``J``.
41
55
@constraint(m, x(0) == x0)
42
56
@constraint(m, J(0) ==0);
57
+
43
58
# # Problem Solution
44
-
#Optimize the model:
59
+
#Now we're ready to solve! We can solve the model by invoking `optimize!`:
45
60
optimize!(m)
46
-
# Extract the results.
61
+
62
+
# # Extract and Plot the Results
63
+
# Now we can extract the optimal results and plot them to visualize how the fish population compares to the profit. Note that the values of infinite variables are
64
+
# returned as arrays corresponding to how the supports were used to discretize our model.
65
+
# We can use the `value` function to extract the values of the infinite variables.
47
66
ts =value(t)
48
67
u_opt =value(u)
49
68
x_opt =value(x)
50
69
J_opt =value(J);
51
70
71
+
# Create the plot for the fish population and profit over time.
52
72
p1 =plot(ts, [x_opt, J_opt] ,
53
73
label=["Fish Pop""Profit"],
54
-
title="State Variables")
74
+
title="State Variables");
75
+
# Create the plot for the fishing rate over time.
55
76
p2 =plot(ts, u_opt,
56
-
label ="Rate",
57
-
title ="Rate vs Time")
58
-
plot(p1,p2 ,layout=(2,1), size=(800,600));
77
+
label ="Fishing Rate",
78
+
title ="Fishing Rate vs Time");
79
+
# Visualize the two plots on one figure.
80
+
plot(p1,p2 ,layout=(2,1), size=(800,600))
59
81
60
82
# ### Maintenance Tests
61
83
# These are here to ensure this example stays up to date.
Copy file name to clipboardExpand all lines: docs/src/examples/Optimal Control/Hanging Chain.jl
+39-14Lines changed: 39 additions & 14 deletions
Original file line number
Diff line number
Diff line change
@@ -25,53 +25,78 @@
25
25
# \end{aligned}
26
26
# ```
27
27
28
-
# # Model Definition
28
+
# # Modeling in InfiniteOpt
29
29
# First we must import ``InfiniteOpt`` and other packages.
30
30
using InfiniteOpt, Ipopt, Plots;
31
-
# Next we specify an array of initial conditions as well as problem variables.
31
+
32
+
# Next we specify our initial conditions and problem variables.
32
33
a, b, L =1, 3, 4
33
34
x0 = [a, 0, 0]
34
35
xtf = [b, NaN, L];
35
-
# We initialize the infinite model and opt to use the Ipopt solver
36
+
37
+
# # Model Definition
38
+
# We initialize the infinite model with [`InfiniteModel`](@ref) and select Ipopt as our optimizer that will be used to solve it.
36
39
m =InfiniteModel(Ipopt.Optimizer);
37
-
# t is specified as ``\ t \in [0,1]``. The bounds are arbitrary for this problem:
40
+
41
+
# # Infinite Parameter Definition
42
+
# We define the infinite parameter t as ``\ t \in [0,1]``. The bounds are arbitrary for this problem:
38
43
@infinite_parameter(m, t in [0,1], num_supports =100);
39
-
# Now let's specify variables. ``u`` is our controller variable.
44
+
45
+
# # Infinite Variable Definition
46
+
# Now let's specify variables. We define ``x`` as a vector containing the state variables for position, energy and chain length. ``u`` is our control variable.
40
47
@variable(m, 0<= x[1:3] <=10, Infinite(t))
41
48
@variable(m, -10<= u <=20, Infinite(t));
42
-
# Specifying the objective to minimize kinetic energy at the final time:
49
+
50
+
# # Objective Definition
51
+
# We specify the objective using `@objective` to minimize potential energy at the final time:
43
52
@objective(m, Min, x[2](1));
44
-
# Define the ODEs which serve as our system model.
53
+
54
+
# # Constraint Definition
55
+
# The last step is to add our constraints. First, define the ODEs which serve as our system model.
0 commit comments