Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add global trajectory parameters #41

Merged
merged 4 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions docs/literate/man/params_in_struct.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# # Add params in NamedTrajectory

# NamedTrajectory.jl support passing parameters as a Tuple when construct a `NamedTrajectory`.

using NamedTrajectories

# First we need to define number of timesteps and timestep
T = 10
dt = 0.1

# then build named tuple of components and data matrices.
components = (
x = rand(3, T),
u = rand(2, T),
Δt = fill(dt, 1, T),
)

# we must specify a timestep and control variable for the trajectory.

timestep = 0.1
control = :u

# some global params as a NamedTuple
params = (
α = rand(1),
β = rand(1)
)

# we can now create a `NamedTrajectory` object with parameters specification.
traj = NamedTrajectory(components; timestep=timestep, controls=control, params=params)
2 changes: 2 additions & 0 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ pages = [
"Quickstart Guide" => "generated/quickstart.md",
"Manual" => [
"generated/man/constructors.md",
"generated/man/params_in_struct.md",
# "generated/man/retrieval.md",
# "generated/man/add_remove.md",
# "generated/man/updating.md",
# "generated/man/times.md",
# "generated/man/operations.md",

],
"Plotting" => "generated/plotting.md",
"Library" => "lib.md"
Expand Down
55 changes: 55 additions & 0 deletions docs/src/generated/man/params_in_struct.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
```@meta
EditURL = "../../../literate/man/params_in_struct.jl"
```

# Add params in NamedTrajectory

NamedTrajectory.jl support passing parameters as a Tuple when construct a `NamedTrajectory`.

````@example params_in_struct
using NamedTrajectories
````

First we need to define number of timesteps and timestep

````@example params_in_struct
T = 10
dt = 0.1
````

then build named tuple of components and data matrices.

````@example params_in_struct
components = (
x = rand(3, T),
u = rand(2, T),
Δt = fill(dt, 1, T),
)
````

we must specify a timestep and control variable for the trajectory.

````@example params_in_struct
timestep = 0.1
control = :u
````

some global params as a NamedTuple

````@example params_in_struct
params = (
α = rand(1),
β = rand(1)
)
````

we can now create a `NamedTrajectory` object with parameters specification.

````@example params_in_struct
traj = NamedTrajectory(components; timestep=timestep, controls=control, params=params)
````

---

*This page was generated using [Literate.jl](https://github.com/fredrikekre/Literate.jl).*

9 changes: 9 additions & 0 deletions src/struct_named_trajectory.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ mutable struct NamedTrajectory{R <: Real}
final::NamedTuple{fnames, <:Tuple{Vararg{AbstractVector{R}}}} where fnames
goal::NamedTuple{gnames, <:Tuple{Vararg{AbstractVector{R}}}} where gnames
components::NamedTuple{cnames, <:Tuple{Vararg{AbstractVector{Int}}}} where cnames
params::NamedTuple{pnames, <:Tuple{Vararg{AbstractVector{R}}}} where pnames
names::Tuple{Vararg{Symbol}}
state_names::Tuple{Vararg{Symbol}}
control_names::Tuple{Vararg{Symbol}}
Expand All @@ -49,6 +50,7 @@ function NamedTrajectory(
initial=(;),
final=(;),
goal=(;),
params=(;),
) where R <: Real
controls = controls isa Symbol ? (controls,) : controls

Expand Down Expand Up @@ -167,6 +169,7 @@ function NamedTrajectory(
final,
goal,
comps,
params,
names,
state_names,
controls
Expand Down Expand Up @@ -221,6 +224,7 @@ function NamedTrajectory(
initial=(;),
final=(;),
goal=(;),
params=(;),
) where R <: Real
controls = (controls isa Symbol) ? (controls,) : controls

Expand Down Expand Up @@ -291,6 +295,7 @@ function NamedTrajectory(
final,
goal,
components,
params,
names,
state_names,
controls
Expand Down Expand Up @@ -328,6 +333,7 @@ function NamedTrajectory(
Z.final,
Z.goal,
Z.components,
Z.params,
Z.names,
Z.state_names,
Z.control_names
Expand Down Expand Up @@ -365,6 +371,7 @@ function NamedTrajectory(
traj.final,
traj.goal,
traj.components,
traj.params,
traj.names,
traj.state_names,
traj.control_names
Expand Down Expand Up @@ -416,6 +423,7 @@ function NamedTrajectory(
initial = NamedTuple([(k => traj.initial[k]) for k ∈ keys(comps) if k ∈ keys(traj.initial)])
final = NamedTuple([(k => traj.final[k]) for k ∈ keys(comps) if k ∈ keys(traj.final)])
goal = NamedTuple([(k => traj.goal[k]) for k ∈ keys(comps) if k ∈ keys(traj.goal)])
params = NamedTuple([(k => traj.params[k]) for k ∈ keys(comps) if k ∈ keys(traj.params)])

return NamedTrajectory(
comps;
Expand All @@ -425,6 +433,7 @@ function NamedTrajectory(
initial=initial,
final=final,
goal=goal,
params
)

end
Expand Down
28 changes: 28 additions & 0 deletions test/test_constructor.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""
test: struct_named_trajectory.jl
"""

@testset "testing constructor" begin
# define number of timesteps and timestep
T = 10
dt = 0.1

components = (
x = rand(3, T),
u = rand(2, T),
Δt = fill(dt, 1, T),
)

timestep = 0.1
control = :u

# some global params as a NamedTuple
params = (
α = rand(1),
β = rand(1)
)

traj = NamedTrajectory(components; timestep=timestep, controls=control, params=params)

@test traj.params == params
end
Loading