Public API
This page lists exported symbols of CTModelsJSON, CTModelsJLD, CTModels.Serialization.
From CTModelsJSON
CTModelsJSON [Module]
CTModelsJSON — Module
Weak-dependency extension of CTModels providing JSON3-based serialization.
Loaded automatically when both CTModels and JSON3 are available in the session. Adds methods for CTModels.export_ocp_solution and CTModels.import_ocp_solution dispatching on CTModels.JSON3Tag.
From CTModelsJLD
CTModelsJLD [Module]
CTModelsJLD — Module
Weak-dependency extension of CTModels providing JLD2-based serialization.
Loaded automatically when both CTModels and JLD2 are available in the session. Adds methods for CTModels.export_ocp_solution and CTModels.import_ocp_solution dispatching on CTModels.JLD2Tag.
From CTModels.Serialization
CTModels.Serialization [Module]
CTModels.Serialization — Module
SerializationSerialization module for CTModels.
This module provides functions for importing and exporting optimal control solutions to various formats (JLD2, JSON).
Organisation
- types.jl: Abstract and concrete types for format tags (
CTModels.Serialization.AbstractTag,CTModels.Serialization.JLD2Tag,CTModels.Serialization.JSON3Tag) - export_import.jl: Public API for export/import (
CTModels.Serialization.export_ocp_solution,CTModels.Serialization.import_ocp_solution) - reconstruction_helpers.jl: Internal helpers for solution reconstruction (
_reconstruct_solution_from_data,_extract_time_vector)
Public API
The following functions are exported and accessible as CTModels.function_name():
export_ocp_solution: Export a solution to fileimport_ocp_solution: Import a solution from file
Supported Formats
- JLD2: Binary format (requires
JLD2.jlpackage) - JSON: Text format (requires
JSON3.jlpackage)
Private API
The following are internal utilities (accessible via Serialization.function_name):
__format: Get default format__filename_export_import: Get default filename
See also: CTModels.Serialization.export_ocp_solution, CTModels.Serialization.import_ocp_solution.
AbstractTag [Abstract Type]
CTModels.Serialization.AbstractTag — Type
abstract type AbstractTagAbstract type for export/import functions, used to choose between JSON or JLD extensions.
See also: CTModels.Serialization.JLD2Tag, CTModels.Serialization.JSON3Tag.
JLD2Tag [Struct]
CTModels.Serialization.JLD2Tag — Type
struct JLD2Tag <: CTModels.Serialization.AbstractTagJLD tag for export/import functions.
Fields
No fields (empty struct used as a type tag).
See also: CTModels.Serialization.AbstractTag, CTModels.Serialization.JSON3Tag.
JSON3Tag [Struct]
CTModels.Serialization.JSON3Tag — Type
struct JSON3Tag <: CTModels.Serialization.AbstractTagJSON tag for export/import functions.
Fields
No fields (empty struct used as a type tag).
See also: CTModels.Serialization.AbstractTag, CTModels.Serialization.JLD2Tag.
export_ocp_solution [Function]
CTModels.Serialization.export_ocp_solution — Function
export_ocp_solution(
sol::CTModels.Solutions.AbstractSolution;
format,
filename
)
Export an optimal control solution to a file.
Arguments
sol::AbstractSolution: The solution to export.
Keyword Arguments
format::Symbol=:JLD: Export format, either:JLDor:JSON.filename::String="solution": Base filename (extension added automatically).
Returns
Nothing: This function writes to a file and returns nothing.
Notes
Requires loading the appropriate package (JLD2 or JSON3) before use.
See also: CTModels.Serialization.import_ocp_solution.
export_ocp_solution(
::CTModels.Serialization.JSON3Tag,
sol::CTModels.Solutions.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.jsonextension 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"export_ocp_solution(
::CTModels.Serialization.JLD2Tag,
sol::CTModels.Solutions.Solution;
filename
)
Export an optimal control solution to a .jld2 file using the JLD2 format.
This function serializes and saves a CTModels.Solution object to disk, allowing it to be reloaded later. The solution is discretized to avoid serialization warnings for function objects.
Arguments
::CTModels.JLD2Tag: A tag used to dispatch the export method for JLD2.sol::CTModels.Solution: The optimal control solution to be saved.
Keyword Arguments
filename::String = "solution": Base name of the file. The.jld2extension is automatically appended.
Example
julia> using JLD2
julia> export_ocp_solution(JLD2Tag(), sol; filename="mysolution")
# → creates "mysolution.jld2"Notes
- Functions are discretized on the time grid to avoid JLD2 serialization warnings
- The solution can be perfectly reconstructed via
import_ocp_solution - Uses the same discretization logic as JSON export for consistency
import_ocp_solution [Function]
CTModels.Serialization.import_ocp_solution — Function
import_ocp_solution(
ocp::CTModels.Models.AbstractModel;
format,
filename
) -> CTModels.Solutions.Solution{TimeGridModelType, TimesModelType, StateModelType, ControlModelType, VariableModelType, ModelType, CostateModelType, Float64, DualModelType, CTModels.Solutions.SolverInfos{Any, Dict{Symbol, Any}}} where {TimeGridModelType<:CTModels.Solutions.AbstractTimeGridModel, TimesModelType<:CTModels.Components.AbstractTimesModel, StateModelType<:CTModels.Components.AbstractStateModel, ControlModelType<:CTModels.Components.AbstractControlModel, VariableModelType<:CTModels.Components.AbstractVariableModel, ModelType<:CTModels.Models.AbstractModel, CostateModelType<:Function, DualModelType<:CTModels.Solutions.AbstractDualModel}
Import an optimal control solution from a file.
Arguments
ocp::AbstractModel: The model associated with the solution.
Keyword Arguments
format::Symbol=:JLD: Import format, either:JLDor:JSON.filename::String="solution": Base filename (extension added automatically).
Returns
Solution: The imported solution.
Notes
Requires loading the appropriate package (JLD2 or JSON3) before use.
See also: CTModels.Serialization.export_ocp_solution.
import_ocp_solution(
::CTModels.Serialization.JSON3Tag,
ocp::CTModels.Models.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.jsonextension 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")import_ocp_solution(
::CTModels.Serialization.JLD2Tag,
ocp::CTModels.Models.Model;
filename
)
Import an optimal control solution from a .jld2 file.
This function loads a previously saved CTModels.Solution from disk and reconstructs it using build_solution from the discretized data.
Arguments
::CTModels.JLD2Tag: A tag used to dispatch the import method for JLD2.ocp::CTModels.Model: The associated optimal control problem model.
Keyword Arguments
filename::String = "solution": Base name of the file. The.jld2extension is automatically appended.
Returns
CTModels.Solution: The reconstructed solution object.
Example
julia> using JLD2
julia> sol = import_ocp_solution(JLD2Tag(), model; filename="mysolution")Notes
- The solution is reconstructed from discretized data via
build_solution - This ensures perfect round-trip consistency with the export
- The OCP model from the file is used if the provided one is not compatible