Search code examples
pythondrake

How to use a decision variable as a time index?


I am writing a mathematical program and want to let the solver choose the optimal time to switch between two simulation phases. At that specific time, I am writing a constraint for the states of one model instance to be equal the states of a second model instance.

I am trying to achieve something like:

t_switch = prog.NewContinuousVariables(1, name="t_switch")
prog.AddConstraint(eq(q_A[t_switch[0],0], q_B[t_switch[0],0]))

but this returns an IndexError since t_switch is of pydrake type symbolic.Variable, but I need to use an integer as an index.

Any thoughts on how to use a symbolic variable as an index in another constraint, or of an alternate way to solve this problem?


Solution

  • I wouldn't suggest to treat the time index as a decision variable; instead you can always fix the trajectory to switch at a specific time index, but set the time duration as decision variable. This allows you to change the total timing of each phase.