Code Monkey home page Code Monkey logo

Comments (10)

AlexanderFabisch avatar AlexanderFabisch commented on July 30, 2024

Hi, can you explain the reasoning for using this equation for the time-to-phase mapping?

According to Ijspeert et al. (2013), the differential equation $\tau \dot{x} = -\alpha_x x$ describes the evolution of the phase variable $x$ (I use the name z in the code). Starting from the initial phase value $x_0 = 1$, the phase value converges monotonically to zero. Instead of using the iterative procedure to calculate the current value of $x$, it is computed directly with the equation in this code.

You should be able to verify this by computing the derivative of the equation given by the function above.

edit: For reference, this is the derivation of the direct solution:

The Python function defines

$$z(t) = (1 - \frac{\alpha_z \Delta t}{\tau})^{\frac{t}{\Delta t}}$$

Then we can compute the derivative

$$z'(t) = \frac{1}{\Delta t} \ln (1 - \frac{\alpha_z \Delta t}{\tau}) \cdot z(t)$$

Let's call $\frac{1}{\Delta t} \ln (1 - \frac{\alpha_z \Delta t}{\tau}) = a(\Delta t)$.

When $\Delta t$ approaches 0, then

$$\lim_{\Delta t \rightarrow 0} a(\Delta t) = -\frac{\alpha_z}{\tau},$$

according to Wolfram Alpha. :)

Hence, combined with the equation before, we can write

$$\tau z'(t) = -\alpha_z \cdot z(t),$$

which is the definition of the phase variable.

from movement_primitives.

AlexanderFabisch avatar AlexanderFabisch commented on July 30, 2024

Also, can you explain the reasoning for the canonical system alpha's calculation? I did not manage to find any literature that explain this

Using the formula from the first code snippet, you can compute the alpha that reaches a specific phase value after the execution time. This is what the function does.

edit: For reference, this is the derivation of the formula.

The direct mapping from time to phase is (just like before)

$$z(t) = (1 - \frac{\alpha_z \Delta t}{\tau})^{\frac{t}{\Delta t}}$$

Let's say we want to reach some constant $z(\tau) = c$ in the end of the DMP. Then we have to find the value of $\alpha_z$.

$$z(\tau) = (1 - \frac{\alpha_z \Delta t}{\tau})^{\frac{\tau}{\Delta t}}$$

The solution is

$$\alpha_z = \frac{\tau}{\Delta t}(1 - c)^{\frac{\Delta t}{\tau}}.$$

The code is a bit different because it uses n_phases $=\frac{\tau}{\Delta t} + 1$ from which we have to subtract 1 instead of the term $\frac{\tau}{\Delta t}$.

from movement_primitives.

jqloh avatar jqloh commented on July 30, 2024

Hi, can you explain the reasoning for using this equation for the time-to-phase mapping?

According to Ijspeert et al. (2013), the differential equation τx˙=−αxx describes the evolution of the phase variable x (I use the name z in the code). Starting from the initial phase value x0=1, the phase value converges monotonically to zero. Instead of using the iterative procedure to calculate the current value of x, it is computed directly with the equation in this code.

You should be able to verify this by computing the derivative of the equation given by the function above.

Using Ijspeert's assumption, shouldn't the solution be an exponential function?
image

from movement_primitives.

jqloh avatar jqloh commented on July 30, 2024

Also, can you explain the reasoning for the canonical system alpha's calculation? I did not manage to find any literature that explain this

Using the formula from the first code snippet, you can compute the alpha that reaches a specific phase value after the execution time. This is what the function does.

Would it be possible to compute the alpha using an exponential time-phase mapping?

from movement_primitives.

AlexanderFabisch avatar AlexanderFabisch commented on July 30, 2024

Would it be possible to compute the alpha using an exponential time-phase mapping?

That's what is done here. I don't think I understood the question.

from movement_primitives.

AlexanderFabisch avatar AlexanderFabisch commented on July 30, 2024

Using Ijspeert's assumption, shouldn't the solution be an exponential function?

I missed this reply. That seems to be indeed a way simpler form to express the same thing. However, I used the other version for such a long time that I am a bit worried about breaking anything, when changing the code. I would have to do an analysis of the numerical stability.

edit: For reference,

$$\lim_{\Delta t \rightarrow 0}(1 - \frac{\alpha_z \Delta t}{\tau})^{\frac{t}{\Delta t}} = \exp(-\frac{\alpha_z}{\tau} t)$$

(I have no proof though.)

from movement_primitives.

AlexanderFabisch avatar AlexanderFabisch commented on July 30, 2024

There are some edge cases, in which the exponential mapping does not behave so nicely:

import numpy as np
from movement_primitives.dmp._canonical_system import canonical_system_alpha, phase
import matplotlib.pyplot as plt

execution_time = 1e3
int_dt = 1e2
alpha_z = canonical_system_alpha(goal_z=1e-10, goal_t=execution_time, start_t=0.0, int_dt=int_dt)
print(alpha_z)
t = np.linspace(0.0, execution_time, 10000)
z = phase(t, alpha_z, goal_t=execution_time, start_t=0.0, int_dt=int_dt)
z2 = np.exp(-alpha_z * t / execution_time)
print(z[-1])
print(z2[-1])

plt.plot(t, z, label="old")
plt.plot(t, z2, ls="--", label=r"$\exp \frac{-\alpha_z t}{\tau}$")
plt.xlabel(r"t ($\tau=" + str(execution_time) + r"$)")
plt.ylabel("$z$")
plt.legend(loc="best")
plt.show()

The exponential mapping approaches the desired target value very slowly and doesn't reach it for some reason.

Figure_1

from movement_primitives.

AlexanderFabisch avatar AlexanderFabisch commented on July 30, 2024

I thought about it again. When I use $\alpha_z = -log(z(\tau))$, the exponential mapping works perfectly. The result is PR #51

from movement_primitives.

jqloh avatar jqloh commented on July 30, 2024

Hi thanks for addressing this issue. May I know how we can pull this update?

from movement_primitives.

AlexanderFabisch avatar AlexanderFabisch commented on July 30, 2024

After cloning the git repository, checkout the branch refactor/time_to_phase_mapping. Let me know if you have any issue with this implementation.

It will soon be merged to main.

from movement_primitives.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.