Skip to contents

DynamicOptProblem(sys, op, tspan, dt = ) turns a compiled system with inputs (see mtkcompile()'s inputs), costs and constraints into an optimal control problem, transcribed by direct collocation. solve() returns a DynamicOptSolution(). This is the native backend behind the JuMP, InfiniteOpt, CasADi and Pyomo problems of ModelingToolkit (see dynamic_opt_backends).

Usage

DynamicOptProblem(
  sys,
  op = list(),
  tspan,
  dt = NULL,
  steps = NULL,
  tune_parameters = FALSE,
  guesses = list(),
  initial_trajectory = list(),
  nominal_values = list(),
  bounds = list(),
  ...
)

# S3 method for class 'DynamicOptProblem'
solve(
  a,
  b,
  tol = NULL,
  maxiter = NULL,
  inner_maxit = 2000L,
  verbose = FALSE,
  optimizer = c("auto", "interior_point", "native", "optimization"),
  ...
)

Arguments

sys

A compiled System.

op

Operating point: initial values of the states (these become initial constraints), parameter values, and starting values of the inputs and of a free final time.

tspan

c(t0, tf), or list(t0, tf) with tf a parameter.

dt

Grid spacing.

steps

Number of grid points (needed for free final time).

tune_parameters

Also optimize the tunable parameters.

guesses

Starting values of states not fixed by op.

initial_trajectory

Starting trajectories, formulas x ~ expr of the independent variable and parameters.

nominal_values

Typical magnitudes of states, formulas x ~ 10, used to scale the collocation constraints.

bounds

Bounds overriding metadata, pairs list(x, c(lo, hi)).

...

Unused.

a

A DynamicOptProblem.

b

The collocation scheme: a collocation object such as JuMPCollocation(), a name such as "hermite_simpson", or a tableau; by default the scheme of the problem's backend, and the trapezoidal rule for a plain DynamicOptProblem.

tol

Tolerance of the optimality conditions (the constraint violation for the augmented Lagrangian methods).

maxiter

Maximum number of iterations.

inner_maxit

Maximum number of L-BFGS-B iterations per subproblem of the augmented Lagrangian methods.

verbose

Print the progress of the optimizer.

optimizer

"interior_point" is a primal dual interior point method in the manner of Ipopt; "optimization" uses the augmented Lagrangian of the optimization module (AugLag()) and "native" an augmented Lagrangian of this file. "auto" (the default) is the interior point method, with Ipopt's restoration phase; a solve that does not converge has the retcode ConvergenceFailure, as with the JuMP backend of ModelingToolkit.

Value

An object of class DynamicOptProblem.

Details

Costs are expressions of the states and inputs evaluated at given times, EvalAt(1)(x) (written x(1) in Julia), or at the final time, and integral (Lagrange) costs Integral(expr, list(t, t0, tf)) (written Symbolics.Integral(t in (t0, tf))(expr) in Julia), which use the quadrature of the collocation scheme; several costs are summed unless the system has a consolidate function. Constraints are equations or inequalities: those with x(t) hold at every collocation point, those with evaluated variables at their times. Bounds of states, inputs, observed variables and tunable parameters come from their bounds metadata (which may refer to parameters) or from bounds. A value at a time between collocation points is interpolated linearly.

For a free final time, give tspan = list(0, tf) with tf a parameter and steps; the value of tf in op is the initial guess.

solve(prob, b) takes the collocation scheme as b: a collocation object of dynamic_opt_backends, or one of the names "trapezoidal" (the default), "hermite_simpson", "backward_euler", "RadauIIA5", "LagrangeRadau(n)", "LagrangeLegendre(n)", "RK4", or a tableau list(A = , b = , c = ). The solution between the nodes is the collocation polynomial of the scheme.

Examples

t <- t_nounits; D <- D_nounits
x <- Variable("x", t)
u <- Variable("u", t, input = TRUE, bounds = c(-1, 1))
sys <- System(list(D(x) ~ u), t, costs = list(-EvalAt(1)(x)), name = "s")
sys <- mtkcompile(sys, inputs = list(u))
prob <- DynamicOptProblem(sys, list(x ~ 0, u ~ 0), c(0, 1), dt = 0.1)
sol <- solve(prob)
tail(sol$sol[x], 1)
#> [1] 1
# an integral cost with Hermite Simpson collocation
isys <- mtkcompile(System(list(D(x) ~ u), t, name = "q",
                          costs = list(Integral(x^2 + u^2, list(t, 0, 1)))), inputs = list(u))
isol <- solve(DynamicOptProblem(isys, list(x ~ 1, u ~ 0), c(0, 1), dt = 0.1), "hermite_simpson")
isol$model$objective
#> [1] 0.7615943