Solving a Neural Lyapunov Problem
NeuralLyapunov.jl represents neural Lyapunov problems as systems of partial differential equations, using the ModelingToolkit.PDESystem
type. Such a PDESystem
can then be solved using a physics-informed neural network through NeuralPDE.jl.
Candidate Lyapunov functions will be trained within a box domain subset of the state space.
NeuralLyapunov.NeuralLyapunovPDESystem
— FunctionNeuralLyapunovPDESystem(dynamics::ODESystem, bounds, spec; <keyword_arguments>)
NeuralLyapunovPDESystem(dynamics::Function, lb, ub, spec; <keyword_arguments>)
Construct a ModelingToolkit.PDESystem
representing the specified neural Lyapunov problem.
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.
Keyword Arguments
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
, then the symbols fromdynamics
are used; ifdynamics isa ODEFunction
, 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
, then the symbols fromdynamics
are used; ifdynamics isa ODEFunction
, 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
; only used whendynamics isa Function && !(dynamics isa ODEFunction)
; whendynamics isa ODEFunction
,policy_search
should not be supplied (as it must be false); whendynamics isa ODESystem
, value inferred by the presence of unbound inputs.name
: the name of the constructedPDESystem
When using NeuralLyapunovPDESystem
, the Lyapuonv function structure, minimization and decrease conditions, and dynamics will all be symbolically traced to generate the resulting PDESystem
equations. In some cases tracing results in more efficient code, but in others it can result in inefficiencies or even errors.
If the generated PDESystem
is then used with NeuralPDE.jl, that library's parser will convert the equations into Julia functions representing the loss, which presents another opportunity for unexpected errors.
Extracting the numerical Lyapunov function
We provide the following convenience function for generating the Lyapunov function after the parameters have been found. If the PDESystem
was solved using NeuralPDE.jl and Optimization.jl, then the argument phi
is a field of the output of NeuralPDE.discretize
and the argument θ
is res.u.depvar
where res
is the result of the optimization.
NeuralLyapunov.get_numerical_lyapunov_function
— Functionget_numerical_lyapunov_function(phi, θ, structure, dynamics, fixed_point;
<keyword_arguments>)
Combine Lyapunov function structure, dynamics, and neural network weights to generate Julia functions representing the Lyapunov function and its time derivative: $V(x), V̇(x)$.
These functions can operate on a state vector or columnwise on a matrix of state vectors.
Positional Arguments
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.θ
: the parameters of the neural network; If the neural network has multiple outputs,θ[:φ1]
should be the parameters of the first neural network output,θ[:φ2]
the parameters of the second (if there are multiple), and so on. If the neural network has a single output,θ
should be the parameters of the network.structure
: aNeuralLyapunovStructure
representing the structure of the neural Lyapunov function.dynamics
: the system dynamics, as a function to be used in conjunction withstructure.f_call
.fixed_point
: the equilibrium point being analyzed by the Lyapunov function.
Keyword Arguments
p
: parameters to be passed intodynamics
; defaults toSciMLBase.NullParameters()
.use_V̇_structure
: whentrue
, $V̇(x)$ is calculated usingstructure.V̇
; whenfalse
, $V̇(x)$ is calculated usingderiv
as $\frac{∂}{∂t} V(x + t f(x))$ at $t = 0$; defaults tofalse
, as it is more efficient in many cases.deriv
: a function for calculating derivatives; defaults to (and expects same arguments as)ForwardDiff.derivative
; only used whenuse_V̇_structure
isfalse
.jac
: a function for calculating Jacobians; defaults to (and expects same arguments as)ForwardDiff.jacobian
; only used whenuse_V̇_structure
istrue
.J_net
: the Jacobian of the neural network, specified as a functionJ_net(phi, θ, state)
; ifisnothing(J_net)
(as is the default),J_net
will be calculated usingjac
; only used whenuse_V̇_structure
istrue
.