MShoot¶
-
class
mshoot.
MShoot
(cfun)¶ Bases:
object
-
static
cfun
(ux, *args)¶ Cost function applied to all intervals.
ux contains free inputs and adjoint states.
-
extend_bounds
(bounds, n)¶ Unfold scalar bounds to constant vectors. All scalar bounds are copied to n-element arrays.
Parameters: bounds – list(tuple) Paran n: int Returns: array, indexing: [variable][lo/up][interval]
-
static
f_eqcons
(ux, *args)¶ Equality constraints for adjoint variables. SLSQP tries to keep all elements of discont equal to 0.
ux contains free inputs and adjoint states.
-
static
jac
(ux, *args)¶ Calculate Jacobian of the cost function.
ux contains free inputs and adjoint states.
-
norm_ubounds
(ubounds, unominal)¶ Normalize bounds formatted as: [(lo1, hi1), (lo2, hi2), …, (lon, hin)]
Return in the same format.
-
optimize
(model, inp, free, ubounds, xbounds, x0, uguess=None, unominal=None, ynominal=None, join=1, maxiter=50)¶ Multiple shooting in which the problem is transcribed into a single sparse NLP with adjoint variable continuity conditions.
ubounds has to be a list of 2-tuples of floats (one tuple per free input).
xbounds has to be a list of 2-tuples of floats or vectors (one tuple per state). Floats are used for constant bounds. Vectors can be used to define time-varying bounds. The size of a vector must be N+1, where N is the number of rows in inp.
join can be used to solve on a coarser mesh. By default join equals to 1, meaning each interval is treated separately. On larger problems it may be usefull to merge intervals though (less adjoint variables).
Return a tuple with 2 DataFrames: udf with optimized free inputs and xdf with resulting adjoint states. Note that xdf has one more row than udf (for final state).
If x0 doesn’t lie in the feasible region, it will be automatically adjusted.
The shape of uguess should be (n_intervals, n_free_inputs). The number of intervals equals to
inp.index.size - 1
.Parameters: - model – SimModel, control model
- inp – DataFrame, fixed inputs, index with time
- free – list, names of free inputs
- ubounds – list of tuples, free input bounds
- xbounds – list of tuples, state bounds
- x0 – 1D array, initial state
- uguess – 2D array, initial guess for free inputs
- ynominal – list, nominal values of outputs (for regularization)
- join – int, number of intervals to join (default 1)
- maxiter – int, maximum number of iterations (default 50)
Returns: udf, xdf (two DataFrames with optimized inputs and states)
-
slice_bounds
(bounds, nrows)¶ Slice bounds into
n = steps // (nrows-1)
parts.Each bound must be given as a tuple of vectors. Each vector element represents the bound for a specific time.
Return:
list(list(tuple(float, float))) | | | ^ lo. ^ up. bound | | ^ variable | ^ period ^ list of periodsParameters: - bounds – list(tuple(vector, vector))
- nrows – int
Returns: list(list(tuple(float, float)))
-
slice_inputs
(inp, nrows)¶ Slice inp into even adjacent parts with nrows each.
Parameters: - inp – DataFrame
- nrows – int
Returns: list of DataFrames
-
test_input
(u, unames, model, inp, x0)¶ Test input u. u is assumed to be time-varying, but is given as a flattened array (vector) of length:
len(unames) * inp.index.size
.Parameters: - u – vector, flattened inputs
- unames – list(str)
- model – SimModel
- inp – DataFrame
- x0 – vector
Returns: ydf (DataFrame), xdf (DataFrame)
-
test_input_const
(u, unames, model, inp, x0)¶ Test input u. u is assumed to be constant, so must be given as a 1D array (vector) of length
len(unames)
.Parameters: - u – vector, constant inputs
- unames – list(str)
- model – SimModel
- inp – DataFrame
- x0 – vector
Returns: ydf (DataFrame), xdf (DataFrame)
-
trim_to_bounds
(xt, bounds)¶ Returns xt if lies within bounds, otherwise returns the nearest bound.
Parameters: - xt – vector, state at time t
- bounds – list(tuple), bounds for each state
Returns: vector
-
x0_within_bounds
(x0, bounds)¶ Return a tuple of two tuples, each containing one bool per state. The booleans are True is respective bounds are not violated.
The first tuple refers to the lower bound. The second tuple refers to the upper bound.
Parameters: - xt – vector, state at time t
- bounds – list(tuple), bounds for each state
Returns: tuple(tuple(bool, …), tuple(bool, …))
-
static