Modifying trajectories
Modifying existing trajectories can be useful for a variety of reasons. Sometimes, you may want to change the values of the states, controls, or other components of the trajectory. Other times, you may want to add or remove components from the trajectory.
using NamedTrajectories
Create a random trajectory with 5 time steps, a state variable x
of dimension 3, and a control variable u
of dimension 2
traj = rand(NamedTrajectory, 5)
traj.names
(:x, :u)
Add a new state variable y
to the trajectory. Notice this is in-place.
y_data = rand(4, 5)
add_component!(traj, :y, y_data)
traj.names
(:x, :u, :y)
Remove the state variable y
from the trajectory. This is not in place.
restored_traj = remove_component(traj, :y)
restored_traj.names
(:x, :u)
Adding suffixes
Another common operation is to add or remove a suffix from the components of a trajectory. This can be useful when you want to create a modified version of a trajectory that is related to the original trajectory in some way, or when you want to create a new trajectory that is a combination of two or more existing trajectories.
For now, these tools are used to create a new trajectory.
Add a suffix "_new" to the state variable x
modified_traj = add_suffix(traj, "_modified")
modified_traj.names
(:x_modified, :y_modified, :u_modified)
The modified trajectory contains the same data
modified_traj.x_modified == traj.x
true
Merging trajectories
You can also merge two or more trajectories into a single trajectory. This can be useful when you want to combine data. Mergining trajectories is like taking a direct sum of the underlying data.
Merge the original trajectory with the modified trajectory
merged_traj = merge(traj, modified_traj)
merged_traj.names |> println
(:x, :y, :x_modified, :y_modified, :u, :u_modified)
You can also extract a specific suffix from the components of a trajectory
extracted_traj = get_suffix(merged_traj, "_modified")
extracted_traj.names
(:x_modified, :y_modified, :u_modified)
If you want the original names, you can remove the suffix
original_traj = get_suffix(merged_traj, "_modified", remove=true)
original_traj.names
(:x, :y, :u)
Merging with conflicts
If there are any conflicting symbols, you can specify how to resolve the conflict.
conflicting_traj = rand(NamedTrajectory, 5)
traj.names, conflicting_traj.names
((:x, :u, :y), (:x, :u))
In this case, keep the u
data from the first trajectory and the x
data from the second trajectory
merged_traj = merge(traj, conflicting_traj; merge_names=(u=1, x=2,))
println(merged_traj.u == traj.u, ", ", merged_traj.u == conflicting_traj.u)
println(merged_traj.x == traj.x, ", ", merged_traj.x == conflicting_traj.x)
true, false
false, true
Merged names
merged_traj.names
(:y, :x, :u)
This page was generated using Literate.jl.