torchdiffeq icon indicating copy to clipboard operation
torchdiffeq copied to clipboard

How to return the maximum/minimum/mean value of the trajectory x(t) solved by ODE solver?

Open zhuqunxi opened this issue 3 years ago • 5 comments

Hi Tianqi, do you know How to return the maximum/minimum/mean value of the trajectory x(t) solved by ODE solver?

zhuqunxi avatar Dec 16 '20 07:12 zhuqunxi

Hmm. Mean should be easy because you just need to integrate over x(t) (i.e. store in as a part of the state), assuming what you want is something like 1/T int_0^T x(t) dt.

Max/min are probably as hard as optimizing a non-convex problem (in 1d).

We don't support this at the moment (because it requires a lot of memory), but perhaps returning a dense output instead of x(t) evaluated at user-input t values could be a good idea. Then you can use another optimization method on this dense output to find the max/min.

rtqichen avatar Jan 06 '21 18:01 rtqichen

Oh, how about this. Let x(t) follow dx(t)/dt = f(t, x(t)).

Let m(t) be the minimum of x(t) in the interval 0 to t. Then m(t) should have the ODE

dm(t)/dt = (x(t) < m(t)) * f(t, m(t))

So whenever x(t) < m(t), m(t) will be perturbed by an infinitesimal amount f. I'm not sure if our solvers can actually solve this properly though.

rtqichen avatar Jan 06 '21 19:01 rtqichen

Just chipping into say that a few other (mutually independent) approaches for min/max also come to mind:

  1. Define an event that detects when (dx(t)/dt=)f(t, x(t)) = 0. Plus some extra handling to restart the ODE, include the start/end of the ODE as possible extrema etc.
  2. Once they're in, use callbacks (#134) and track a running min/max of the numerically computed state.
  3. Keep a running min/max of the input x the vector field is evaluated at, with some extra checking to see if steps have been accepted/rejected (based on whether t updates), so that you don't track xs from rejected steps.

I think any of the above should work.

patrick-kidger avatar Jan 06 '21 19:01 patrick-kidger

Hmm. Mean should be easy because you just need to integrate over x(t) (i.e. store in as a part of the state), assuming what you want is something like 1/T int_0^T x(t) dt.

Max/min are probably as hard as optimizing a non-convex problem (in 1d).

We don't support this at the moment (because it requires a lot of memory), but perhaps returning a dense output instead of x(t) evaluated at user-input t values could be a good idea. Then you can use another optimization method on this dense output to find the max/min.

That's what have thought about and it definitely may cost a lot of memory. Thanks for the help -_-.

zhuqunxi avatar Jan 07 '21 03:01 zhuqunxi

Just chipping into say that a few other (mutually independent) approaches for min/max also come to mind:

  1. Define an event that detects when (dx(t)/dt=)f(t, x(t)) = 0. Plus some extra handling to restart the ODE, include the start/end of the ODE as possible extrema etc.
  2. Once they're in, use callbacks (#134) and track a running min/max of the numerically computed state.
  3. Keep a running min/max of the input x the vector field is evaluated at, with some extra checking to see if steps have been accepted/rejected (based on whether t updates), so that you don't track xs from rejected steps.

I think any of the above should work.

Oh, it should be an efficient way to do. I'll try in the following days. Thanks (●’◡’●).

zhuqunxi avatar Jan 07 '21 03:01 zhuqunxi