Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question of time-phase mapping and alpha value for canonical system alpha #49

Closed
jqloh opened this issue Jun 26, 2024 · 10 comments · Fixed by #51
Closed

Question of time-phase mapping and alpha value for canonical system alpha #49

jqloh opened this issue Jun 26, 2024 · 10 comments · Fixed by #51

Comments

@jqloh
Copy link

jqloh commented Jun 26, 2024

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

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

@AlexanderFabisch
Copy link
Member

AlexanderFabisch commented Jun 26, 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.

@AlexanderFabisch
Copy link
Member

AlexanderFabisch commented Jun 26, 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}$.

@jqloh
Copy link
Author

jqloh commented Jun 26, 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

@jqloh
Copy link
Author

jqloh commented Jun 26, 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?

@AlexanderFabisch
Copy link
Member

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.

@AlexanderFabisch
Copy link
Member

AlexanderFabisch commented Jun 26, 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.)

@AlexanderFabisch
Copy link
Member

AlexanderFabisch commented Jun 26, 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

@AlexanderFabisch
Copy link
Member

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

@jqloh
Copy link
Author

jqloh commented Jun 27, 2024

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

@AlexanderFabisch
Copy link
Member

AlexanderFabisch commented Jun 27, 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.

@dfki-ric dfki-ric locked and limited conversation to collaborators Jun 27, 2024
@AlexanderFabisch AlexanderFabisch converted this issue into discussion #52 Jun 27, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
2 participants