Skip to content

Commit

Permalink
Add tips on minimum-time problems (SleipnirGroup#707)
Browse files Browse the repository at this point in the history
  • Loading branch information
calcmogul authored Jan 21, 2025
1 parent 1af6098 commit bfabfad
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions docs/tips.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,63 @@ restriction. In other words, use `minimize(x ** 2 + y ** 2 + z ** 2)` instead of
Store common subexpressions in intermediate variables and reuse them instead of
writing out the subexpressions each time. This ensures common subexpressions in
the expression tree are only traversed and updated once.

## Minimum-time problems

The obvious problem formulation for minimum-time problems uses one dt shared
across all timesteps.
```python
import jormungandr as jmg

N = 100
T_max = 5.0

problem = jmg.optimization.OptimizationProblem()

x = problem.decision_variable(N + 1)
v = problem.decision_variable(N)

dt = problem.decision_variable()
dt.set_value(T_max / N)
problem.subject_to(dt > 0)
problem.subject_to(dt < T_max / N)

for k in range(N):
problem.subject_to(x[k + 1] == x[k] + v[k] * dt)

problem.minimize(dt)

problem.solve()
```
The nonzero initial value for dt avoids a degenerate case, and the upper bound
prevents the solver exploiting discretization artifacts.

This formulation can have feasibility issues though per section 15.3
"Elimination of variables" of "Numerical Optimization, 2nd Ed.". Instead, we
recommend using a separate dt for each timestep, with them all
equality-constrained.
```python
import jormungandr as jmg

N = 100
T_max = 5.0

problem = jmg.optimization.OptimizationProblem()

x = problem.decision_variable(N + 1)
v = problem.decision_variable(N)

dt = problem.decision_variable(N)
problem.subject_to(dt > 0)
problem.subject_to(dt < T_max / N)
for k in range(N - 1):
problem.subject_to(dt[k] == dt[k + 1])

for k in range(N):
dt[k].set_value(T_max / N)
problem.subject_to(x[k + 1] == x[k] + v[k] * dt[k])

problem.minimize(sum(dt))

problem.solve()
```

0 comments on commit bfabfad

Please sign in to comment.