Benchmarking neural Lyapunov methods
To facilitate comparison of different neural Lyapunov specifications, optimizers, hyperparameters, etc., we provide the benchmark
function.
Through its arguments, users may specify how a neural Lyapunov problem, the neural network structure, the physics-informed neural network discretization strategy, and the optimization strategy used to solve the problem. After solving the problem in the specified manner, the dynamical system is simulated (users can specify an ODE solver in the arguments, as well) and classification by the neural Lyapunov function is compared to the simulation results. The benchmark
function returns a confusion matrix for the resultant neural Lyapunov classifier, the training time, and samples with labels, so that users can compare accuracy and computation speed of various methods.
NeuralLyapunov.benchmark
— Functionbenchmark(dynamics, bounds, spec, chain, strategy, opt; <keyword_arguments>)
benchmark(dynamics, lb, ub, spec, chain, strategy, opt; <keyword_arguments>)
Evaluate the specified neural Lyapunov method on the given system. Return a NamedTuple
containing the confusion matrix, optimization time, and other metrics listed below.
Train a neural Lyapunov function as specified, then discretize the domain using a grid discretization and use the neural Lyapnov function to and the provided classifier
to predict whether grid points are in the region of attraction of the provided fixed_point
. Finally, simulate the system from each grid point and check if the trajectories reach the fixed point. Return a confusion matrix for the neural Lyapunov classifier using the results of the simulated trajectories as ground truth. Additionally return the time it took for the optimization to run.
To use multiple solvers, users should supply a vector of optimizers in opt
. The first optimizer will be used, then the problem will be remade with the result of the first optimization as the initial guess. Then, the second optimizer will be used, and so on. Supplying a vector of Pair
s in optimization_args
will use the same arguments for each optimization pass, and supplying a vector of such vectors will use potentially different arguments for each optimization pass.
Positional Arguments
dynamics
: the dynamical system being analyzed, represented as anODESystem
or the functionf
such thatẋ = f(x[, u], p, t)
; either way, the ODE should not depend on time and onlyt = 0.0
will be used. (For an example of whenf
would have au
argument, seeadd_policy_search
.)bounds
: an array of domains, defining the training domain by bounding the states (and derivatives, when applicable) ofdynamics
; only used whendynamics isa ODESystem
, otherwise uselb
andub
.lb
andub
: the training domain will be $[lb_1, ub_1]×[lb_2, ub_2]×...$; not used whendynamics isa ODESystem
, then usebounds
.spec
: aNeuralLyapunovSpecification
defining the Lyapunov function structure, as well as the minimization and decrease conditions.chain
: a vector of Lux/Flux chains with a d-dimensional input and a 1-dimensional output corresponding to each of the dependent variables, where d is the length ofbounds
orlb
andub
. Note that this specification respects the order of the dependent variables as specified in the PDESystem. Flux chains will be converted to Lux internally by NeuralPDE usingNeuralPDE.adapt(FromFluxAdaptor(false, false), chain)
.strategy
: determines which training strategy will be used. See the NeuralPDE Training Strategy documentation for more details.opt
: optimizer to use in training the neural Lyapunov function.
Keyword Arguments
n
: number of samples used for evaluating the neural Lyapunov classifier.sample_alg
: sampling algorithm used for generating the evaluation data; defaults toLatinHypercubeSample(rng)
; see the QuasiMonteCarlo.jl docs for more information.classifier
: function of $V(x)$, $V̇(x)$, and $x$ that predicts whether $x$ is in the region of attraction; when constructing the confusion matrix, a point is predicted to be in the region of attraction ifclassifier
orendpoint_check
returnstrue
; defaults to(V, V̇, x) -> V̇ < 0
.fixed_point
: the equilibrium being analyzed; defaults to the origin.p
: the values of the parameters of the dynamical system being analyzed; defaults toSciMLBase.NullParameters()
; not used whendynamics isa ODESystem
, then use the default parameter values ofdynamics
.state_syms
: an array of theSymbol
representing each state; not used whendynamics isa ODESystem
(in that case, the symbols fromdynamics
are used); ifdynamics
is anODEFunction
or anODEInputFunction
, the symbols stored there are used, unless overridden here; if not provided here and cannot be inferred,[:state1, :state2, ...]
will be used.parameter_syms
: an array of theSymbol
representing each parameter; not used whendynamics isa ODESystem
(in that case, the symbols fromdynamics
are used); ifdynamics
is anODEFunction
or anODEInputFunction
, the symbols stored there are used, unless overridden here; if not provided here and cannot be inferred,[:param1, :param2, ...]
will be used.policy_search::Bool
: whether or not to include a loss term enforcingfixed_point
to actually be a fixed point; defaults tofalse
; whendynamics isa ODESystem
, the value is inferred by the presence of unbound inputs and whendynamics
is anODEFunction
or anODEInputFunction
, the value is inferred by the type ofdynamics
.optimization_args
: arguments to be passed into the optimization solver, as a vector ofPair
s. For more information, see the Optimization.jl docs.simulation_time
: simulation end time for checking if trajectory from a point reaches equilibriumode_solver
: differential equation solver used in simulating the system for evaluation. For more information, see the DifferentialEquations.jl docs.ode_solver_args
: arguments to be passed into the differential equation solver. For more information, see the DifferentialEquations.jl docs.ensemble_alg
: controls how the evaluation simulations are handled; defaults toEnsembleThreads()
, which uses multithreading (local parallelism only); see the DifferentialEquations.jl docs for more information.endpoint_check
: function of the endpoint of a simulation that returnstrue
when the endpoint is approximately the fixed point andfalse
otherwise; defaults to(x) -> ≈(x, fixed_point; atol=atol)
.atol
: absolute tolerance used in the default value forendpoint_check
.init_params
: initial parameters for the neural network; defaults tonothing
, in which case the initial parameters are generated usingLux.initialparameters
andrng
.init_states
: initial states for the neural network; defaults tonothing
, in which case the initial states are generated usingLux.initialstates
andrng
.rng
: random number generator used to generate initial parameters and states, as well as in the default sampling algorithm; defaults to aStableRNG
with seed0
.
Output Fields
confusion_matrix
: confusion matrix of the neural Lyapunov classifier.data
: aDataFrame
containing the following columns:- "Initial State": initial state of the simulation.
- "Final State": end state of the simulation.
- "V": value of the Lyapunov function at the initial state.
- "dVdt": value of the Lyapunov decrease function at the initial state.
- "Predicted in RoA": whether
classifier
predicted that the initial state is in the region of attraction. - "Actually in RoA": whether the endpoint of the simulation is approximately equal to
fixed_point
(as determined byendpoint_check
). - "Classification": classification of each point, either "TP" (true positive), "TN" (true negative), "FP" (false positive), or "FN" (false negative).
training_time
: time taken to train the neural Lyapunov function.θ
: the parameters of the neural Lyapunov function.phi
: the neural network, represented asphi(x, θ)
if the neural network has a single output, or aVector
of the same with one entry per neural network output (to be used withget_numerical_lyapunov_function
).V
: the neural Lyapunov function.V̇
: the Lyapunov decrease function.