numagic / lumos

scalable accelerated optimal control
MIT License
16 stars 0 forks source link

first try at more automatic collections #57

Open yunlongxu-numagic opened 2 years ago

yunlongxu-numagic commented 2 years ago

attempt to automatically collect names for states, states_dot, and residuals as well and automatically collect the states_dot and residuals values from each submodel

yunlongxu-numagic commented 2 years ago

There are a few issues that we can already see:

1) it could be a bad design if we only automatically collect "outputs" (user still needs to call helper function), and ask the users to manually collect all other returns. Just seems error prone 2) if we automatically collect states names, then it's actually a little bit the other way round: eg, if we have at top level vehicle.vx as state, and then we need to pass that to the vx state entry for the submodel vehicle. So we may also need to implement some extraction helper (and also think bout the decision of what delimiters to use to separate parent and child models) 3) imagine we have a state at top level called vehicle.vx, then the syntax for self.make_vector("states", name=value, ...) no longer works because vehicle.vx is not a valid kwarg value! (interestingly self.make_vector("states", **{"vehicle.vx": value} would actually work) 4) automatic collection of states derivatives would also cause problems where we might want to modify some derivatives before computation, eg: when we have a vehicle_on_track model where it takes derivatives w.r.t. distance, and then we have a vehicle submode, which takes derivatives w.r.t. time, we would want to do the change of variable before we finally output the final top level derivatives. (if we make the changed variable derivatives as additional states derivative at top level, then with automatic derivative collection, we will end up with two sets of duplicate derivatives, of which the time derivatives are the ones we don't want to have!)

yunlongxu-numagic commented 2 years ago

Note, sometimes we can also have models who doesn't want all submodel states to be its states, for example:

we have a 3d rigid body dynamics RigidBody3d which has 12 states, and then we want to build a 2d rigid body RigidBody2d, by reusing the 3d one. In RigidBody2d, we might just wrap around the RigidBody3d, and then feed it constant inputs for those that are only valid for 3d, and only extrat states_dot for outputs that are useful for the 2d case. As such, the 2d model would have fewer states and states derivatives than its submodel.

Of course this is also linked to design decision (eg, here re-using 3d model for 2d special case)