back

top

Documentation

OclSystem(varsfun, eqfun)

A dynamical system is defined by implementing functions for the system variables and equations, and creating an OclSystem passing the function pointers. You need to implement two functions, one for defining the system variables, and a second one for defining the system equations. The system is created by passing the handles of the two functions to the constructor of OclSystem.

sys = OclSystem(@sysVars,@sysEq);

% Function definitions can be in the same file 
% (if the main script is wrapped by a function) 
% or in separate files:
function sysVars(svh)
  svh.addState('p');
  svh.addState('v');
  svh.addControl('u');  
end
function sysEq(seh,x,z,u,p)
  seh.setODE('p',(1-x.v^2)*x.p-x.v+u.u); 
  seh.setODE('v',x.p);
end

Arguments:

varsfun (function handle)
Function handle to the function that sets up the variables. The function for the variables must have one input argument, no return values, and thus the following siganture: fh(svh) where svh is the OclSystemVarsHandler handler that allows to add variables and parameters.
eqfun (function handle,)
Function handle to the function that sets up the equations. The function for the variables must have five input argument, no return values, and thus the following signature: fh(seh,x,z,u,p) where seh is the OclSystemEqHandler that allows to add ODE and DAE equations, x the states, z the algebraic variables, u the control inputs, p the parameters.

OclSystemVarsHandler()

The system variables handler allows to specify the system variables, its dimensions and bounds.

Methods

addState(id, s, lb=-inf, ub=inf)
Adds a state variable to the system.
Arguments:
id (char)
Name of the state variable
s (int, optional)
Size of the state variable. Scalar, vector, and matrix valued variables are allowed. If a scalar value s is given, the size of the variable will be [s,1]. Defaults to [1,1].
lb=-inf (numeric, optional)
Lower bound on the variable. This value can be overwritten when you specify bounds for OclSolver with solver.setBound. Defaults to -inf.
ub=inf (numeric, optional)
Upper bound on the variable. This value can be overwritten when you specify bounds for OclSolver with solver.setBound. Defaults to inf.
addAlgVar(id, s, lb=-inf, ub=inf)
Adds an algebraic variable to the system.
Arguments:
id (char)
Name of the algebraic variable
s (int, optional)
Size of the algebraic variable. Scalar, vector, and matrix valued variables are allowed. If a scalar value s is given, the size of the variable will be [s,1]. Defaults to [1,1].
lb=-inf (numeric, optional)
Lower bound on the variable. This value can be overwritten when you specify bounds for OclSolver with solver.setBound. Defaults to -inf.
ub=inf (numeric, optional)
Upper bound on the variable. This value can be overwritten when you specify bounds for OclSolver with solver.setBound. Defaults to inf.
addControl(id, s, lb=-inf, ub=inf)
Adds an control input to the system.
Arguments:
id (char)
Name of the control variable
s (int, optional)
Size of the control variable. Scalar, vector, and matrix valued variables are allowed. If a scalar value s is given, the size of the variable will be [s,1]. Defaults to [1,1].
lb=-inf (numeric, optional)
Lower bound on the variable. This value can be overwritten when you specify bounds for OclSolver with solver.setBound. Defaults to -inf.
ub=inf (numeric, optional)
Upper bound on the variable. This value can be overwritten when you specify bounds for OclSolver with solver.setBound. Defaults to inf.
addParameter(id, s, default=[])
Adds a parameter.
Arguments:
id (char)
Name of the parameter
s (int, optional)
Size of the control variable. Scalar, vector, and matrix valued variables are allowed. If a scalar value s is given, the size of the variable will be [s,1]. Defaults to [1,1].
default=[] (numeric, optional)
Default value for the parameter. This value can be overwritten when you specify the parameter for OclSolver with solver.setParameter. Defaults to unbounded.

OclSystemEqHandler()

The system equations handler allows to specify the system equations which can be of ODE and DAE type.

Methods

setODE(id, equation)
Adds a differential equation to the system. Note that for every state variable defined in the variables function, a differential equation must be specified.
Arguments:
id (char)
Name of the state variable for that the differential equation is given.
equation (OclVariable or Matlab matrix)
The equation specifies the derivative of a state variable. Right hand side of the differential equation dot(x) = f(x,z,u,p) for state variable x.
setAlgEquation(equation)
Adds an algebraic equation to the system. Note that in order to be able to simulate the system, the total number of rows of the algebraic equations needs to be equal to the total number/dimension of algebraic variables.
Arguments:
equation (OclVariable or Matlab matrix)
Algebraic equation g in the form g(x,z,u,p)=0

OclOCP(pathcosts=0, arrivalcosts=0, pathconstraints=[], boundaryconditions=[], discretecosts=0)

An optimal control problem is implemented by providing functions for the path costs, arrival costs, path constraints, and boundary conditions. You can pass the function handles as positional arguments or keyword arguments. All function handles are optional. If you do not provide some of the functions, they default to zero cost for the cost functions or an empty constraints array for path constraints and boundary conditions.

ocp = OclOCP('pathcosts', @ocpPathCosts);

% Function definitions can be in the same file 
% (if the main script is wrapped by a function) 
% or in separate files:
function ocpPathCosts(ch,x,z,u,p)
  ch.add( x.p^2 );
  ch.add( x.v^2 );
  ch.add( u.u^2 );
end

Arguments:

pathcosts=0 (function handle, optional)
Function handle to the function that defines the path costs (also called Lagrange cost or intermediate cost). The signature of the function handle is fh(ch,x,z,u,p) where ch is the OclCostHandler, x are the states, z are the algebraic variables, u are the controls, and p are the parameters.
arrivalcosts=0 (function handle, optional)
Function handle to the function that defines the arrival costs (also called Mayer terms). The signature of the function handle is fh(ch,x,T,p) where ch is the OclCostHandler, x are the terminal states, T is the final time, p are the parameters.
pathconstraints=[] (function handle, optional)
Function handle to the function that defines the path constraints. The signature of the function handle is fh(ch,x,t,p) where ch is the OclConstraintHandler, t is the time, p are the parameters.
boundaryconditions=[] (function handle, optional)
Function handle to the function that defines the boundary conditions. The signature of the function handle is fh(ch,x0,xF,p) where ch is the OclConstraintHandler, x0 are the initial states, xF are the terminal states, p are the parameters.
discretecosts=0 (function handle, optional)
Function handle to the function that defines discrete cost. The discrete cost terms can depend on any variable of the discretized optimal control problem which is a non-linear program (NLP). The signature of the function handle is fh(ch, V) where ch is the OclCostHandler, and V of type OclVariable are the nlp variables.

OclCostHandler()

The cost handler allows to add cost terms in a cost function definition.

Methods

add(cost)
Adds a cost term.
Arguments:
cost (OclVariable or Matlab matrix)
Scalar variable containing the cost

OclConstraintHandler()

The constraint handler allows to add constraints to the optimal control problem definition.

Methods

add(lhs, op, rhs)
Adds a constraint to the optimal control problem.
Arguments:
lhs (OclVariable or Matlab matrix)
Left hand side of the constraint equation
op (char)
One of the following operators as a string: ‘<=’, ‘==’, ‘>=’
rhs (OclVariable or Matlab matrix)
Right hand side of the constraint equation

OclSolver(T, system, ocp, options)

Creates a solver object that discretizes the given system and optimal control problem, and calls the underlying optimizer. Before solving set options, parameters, bounds, and the initial guess:

opt = OclOptions();
opt.nlp.controlIntervals = 30;
ocl = OclSolver(10,VanDerPolSystem,VanDerPolOCP,opt);

ocl.setBounds('p', -0.25, inf);
ocl.setInitialBounds('p', 0);

v0 = ocl.getInitialGuess();
v0.states.p = -0.2;

[v,t] = ocl.solve(v0);

% initial guess, solution and times have
% the following structure:
v.states     % state trajectory
v.controls   % control trajectory
v.algVars    % algebraic variable trajectory
v.integrator % integrator variables
t.states     % time points of states
t.controls   % time points of controls

% plotting of control and state p trajectory:
oclPlot(t.controls,v.controls.u)
oclPlot(t.states,v.states.p)

Arguments:

T (numeric)
The end time/horizon length of the optimal control problem. You can alternatively specify a vector of length(T)==N+1 to set the timepoints at which the optimal control problem is discretized. The default discretizatoin is at times linspace(0,1,N+1)*T. If you pass a vector of length(T)==N, the entries of T are the timesteps of the control interval, e.g. T=linspace(0.1,0.1,N). If you specify T=[], the final time of the optimal control problem is free. The endtime is available in the parameters as p.T. The normalized discretization of the control intervals is available in the controls as u.h_normalized. You can set bounds on T and h_normalized as you can do on any other variable. If your system equatiosn are expressed as function of an independent variable other than time, the same holds just that T represents not the end time but the endpoint of the integration over the independent variable.
system (OclSystem)
The system dynamics
ocp (OclOCP)
The optimal control problem
options (struct)
Options struct, can be created with OclOptions()

Returns:

(OclSolver)
A solver object.

Methods

getInitialGuess()
Use this method to retrieve a first initial guess that is generated from the bounds. You can further modify this initial guess to improve the solver performance.
Returns:
(OclVariable)
Structured variable for setting the initial guess
solve(initialGuess)
Calls the solver and starts doing iterations.
Arguments:
initialGuess (OclVariable)
Provide a good initial guess
Returns:
(OclVariable)
The solution of the OCP
(OclVariable)
Time points of the solution
setBounds(id, lb, ub)
Sets a bound on a variable for the whole trajectory. If only the lower bound is given, it will be lb==ub. A bound can be either scalar or a vector with length(lb)==length(ub)==N+1 for states and length(lb)==length(ub)==N for control variables.
Arguments:
id (char)
The variable id
lb (numeric)
The lower bound
ub (numeric,optional)
The upper bound
setInitialBounds(id, lb, ub)
Sets an initial bound on a variable. If only the lower bound is given, it will be lb==ub. A bound can be either scalar or a vector with length(lb)==length(ub)==N+1 for states and length(lb)==length(ub)==N for control variables.
Arguments:
id (char)
The variable id
lb (numeric)
The lower bound
ub (numeric,optional)
The upper bound
setEndBounds(id, lb, ub,optional)
Sets an end bound on a variable. If only the lower bound is given, it will be lb==ub. A bound can be either scalar or a vector with length(lb)==length(ub)==N+1 for states and length(lb)==length(ub)==N for control variables.
Arguments:
id (char)
The variable id
lb (numeric)
The lower bound
ub,optional (numeric)
The upper bound
setParameter(id, lb, ub,optional)
Sets a bound on the parameter with the given name. If only the lower bound is given, it will be lb==ub. A bound can be either scalar or a vector with length(lb)==length(ub)==N+1 for states and length(lb)==length(ub)==N for control variables.
Arguments:
id (char)
The parameter name
lb (numeric)
The lower bound
ub,optional (numeric)
The upper bound

OclOptions()

Creates an options struct for OclSolver. Check the casadi documentation and the ipopt documentation to see which options are available. These options can be set in ‘opt.nlp.ipopt’. The default values are the following:

opt = struct;
opt.solverInterface   = 'casadi';
opt.system_casadi_mx  = false;
opt.controls_regularization = true;
opt.controls_regularization_value = 1e-6;
opt.nlp = struct;
opt.nlp.discretization         = 'collocation';
opt.nlp.controlIntervals       = 20;
opt.nlp.collocationOrder       = 3;
opt.nlp.solver                 = 'ipopt';
opt.nlp.auto_interpolation     = true;
opt.nlp.casadi = struct;
opt.nlp.ipopt = struct;
opt.nlp.ipopt.linear_solver = 'mumps';
opt.nlp.ipopt.hessian_approximation = 'exact';

Returns:

(struct)
the options struct.

OclVariable()

The OclVariable type(or CasadiVariable in the CasADi backend) is the basic structure to retrieve, store, modify structured optimization variables. You can access subvariables by their name like the state trajectory or the control variables.

% v is a solution of an OCP
% p=[px;py;pz] is of size 3x1
% p trajectory is of size 3x1x(N+1) 
% F trajectory is of size 1x1xN
% with N control intervals
p = v.states.p;     % get state p trajectory
F = v.controls.F;   % get control F trajectory

% set all 3x1 p states to the same value
v.states.p = [3;2;1]; 
% set p states 4 and 5 in the trajectory
v.states.p(:,:,4:5) = [1,2,3;4,5,6].'; 
% or (with the same result)
v.states.p(:,:,4:5) = {[1;2;3],[4;5;6]}; 
% or even
v.states.p(:,:,4:5) = {[1,2,3],[4,5,6]}; 

% set all px values of p in state trajectory
v.states.p(1,:,:) = 4;

% plotting of state p trajectory:
plot(t.states.value,v.states.p.value)

Methods

get(id)
Alternative syntax: var.id Gets a sub-variable of a variable. You can use the shorthand notation with the dot operator, e.g.: solution.states.x
Arguments:
id (char)
Name of the state variable
Returns:
(OclVariable)
the sub-variable of the given variable.
set(value)
Alternative syntax: var = value Sets a value to the variable.
Arguments:
value (numeric)
The value to be set. The value either has to be of the same dimension as the variable or if possible it will be repeated in some dimensions to fit the variable. Scalar values will be set to all entries of the variable. You can use the shorthand notation, e.g. initialGuess.states.x = [1,2,3]
slice(dim1, dim2, dim3)
Alternative syntax: var(dim1,dim2,dim3) Gets a slice of a variable. You can slice a variable the same way as you would index a matrix in Matlab/Octave which means linear indexing is also possible.
Arguments:
dim1 (int, :, end)
indizes for the first dimension. The indizes can be scalar, integer arrays, or you can use : or end.
dim2 (int, :, end, optional)
indizes for the second dimension. The indizes can be scalar, integer arrays, or you can use : or end.
dim3 (int, :, end, optional)
indizes for the third dimension. The indizes can be scalar, integer arrays, or you can use : or end.
Returns:
(OclVariable)
the sliced variable.
value()
Get the value of the variable. This is particularly useful if you want to plot the numeric values of the variable, for example for the solution. In system and OCP definition this gives you the underlying symbolic values.
Returns:
(numeric or casadi.SX or casadi.MX or sym)
the underlying value of the variable. The value can be either numeric (for initial guess and solution) or symbolic (in system/ocp definitions).
disp()
Display function of OclVariable. It shows the size of the variable, the names of the children variables, and a part of the value. The output of an OCP initial guess variable looks similar to:
Variable:
  Size: [3681 1]
  Type: OclStructure
  Children: states, integrator, controls, parameters, time
  Value: [100;2;1;0;0;0;0;0;0;0;0;0;0;0;0..]

Simulator(system, options)

Performs simulations of the system by integrating the system dynamics.

simulator = Simulator(PendulumSystem);

x0 = simulator.getStates();
x0.p = [0;1];
x0.v = [-0.5;-1];

p = simulator.getParameters();
p.m = 1;
p.l = 1.5;

t = linspace(0,4,20);

% apply constant force
uVec = simulator.getControlsVec(20);
uVec.F = 10;

[xVec,zVec,uVec] = simulator.simulate(x0,t,uVec,p);

Arguments:

system (OclSystem)
The system dynamics
options (struct, optional)
Options struct, can be created with Simulator.getOptions(). Defaults to empty struct.

Returns:

(Simulator)
the Simulator object.

Methods

getControlsVec(N)
Gives you a structured variable that allows you to specify an open-loop control input by setting values to the variable. The default value for all variables is zero.
Arguments:
N (int)
Number of control intervals
Returns:
(OclVariable)
The control vector.
getStates()
Returns a structured state variable that you can use to pass as the initial state to the simulator. All variable values default to zero.
Returns:
(OclVariable)
The states.
getParameters()
Returns a structured state variable that allows you to set parameters for the simulation. All parameter values default to zero.
Returns:
(OclVariable)
The parameters.
simulate(x0, times, p, uVec)
Simulates the system for the given time interval.
Arguments:
x0 (OclVariable)
The initial state.
times (numeric)
Times vector. The system will be evaluated at the given time points.
p (OclVariable)
The parameters set to numeric values.
uVec (OclVariable, optional)
Controls vector with open-loop controls. Defaults to zero valued controls.
Returns:
(OclVariable)
The state trajectory.
(OclVariable)
The trajectory of algebraic variables.
(OclVariable)
The control inputs applied to the system.