JSON

Index

Warning

In the examples in the documentation below, the methods are not prefixed by the module name even if they are private.

julia> using CTModels
julia> x = 1
julia> private_fun(x) # throw an error

must be replaced by

julia> using CTModels
julia> x = 1
julia> CTModels.private_fun(x)

However, if the method is reexported by another package, then, there is no need of prefixing.

julia> module OptimalControl
           import CTModels: private_fun
           export private_fun
       end
julia> using OptimalControl
julia> x = 1
julia> private_fun(x)

Documentation

CTModels.export_ocp_solutionMethod
export_ocp_solution(
    ::CTModels.JSON3Tag,
    sol::CTModels.Solution;
    filename
)

Export an optimal control solution to a .json file using the JSON3 format.

This function serializes a CTModels.Solution into a structured JSON dictionary, including all primal and dual information, which can be read by external tools.

Arguments

  • ::CTModels.JSON3Tag: A tag used to dispatch the export method for JSON3.
  • sol::CTModels.Solution: The solution to be saved.

Keyword Arguments

  • filename::String = "solution": Base filename. The .json extension is automatically appended.

Notes

The exported JSON includes the time grid, state, control, costate, objective, solver info, and all constraint duals (if available).

Example

julia> using JSON3
julia> export_ocp_solution(JSON3Tag(), sol; filename="mysolution")
# → creates "mysolution.json"
CTModels.import_ocp_solutionMethod
import_ocp_solution(
    ::CTModels.JSON3Tag,
    ocp::CTModels.Model;
    filename
)

Import an optimal control solution from a .json file exported with export_ocp_solution.

This function reads the JSON contents and reconstructs a CTModels.Solution object, including the discretized primal and dual trajectories.

Arguments

  • ::CTModels.JSON3Tag: A tag used to dispatch the import method for JSON3.
  • ocp::CTModels.Model: The model associated with the optimal control problem. Used to rebuild the full solution.

Keyword Arguments

  • filename::String = "solution": Base filename. The .json extension is automatically appended.

Returns

  • CTModels.Solution: A reconstructed solution instance.

Notes

Handles both vector and matrix encodings of signals. If dual fields are missing or null, the corresponding attributes are set to nothing.

Example

julia> using JSON3
julia> sol = import_ocp_solution(JSON3Tag(), model; filename="mysolution")