-
Notifications
You must be signed in to change notification settings - Fork 80
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
trajectory tracking problem #3
Comments
Hi @Charvelau, Thanks! Yes it should. You can see #1 for how to use |
thanks, will try it out and let you know if got any result ^-^ |
Hi @anassinator, I have defined my system and tried the PathQRCost class, the cost function works all right but it's stuck on Currently the x_path and u_path are both zero matrices. There seems to be some thing wrong with Could you help to find where the problem is ? And thanks in advance. |
@Charvelau I think I just fixed the The issue is that you're dividing by zero when Hope this helps! |
Hi @anassinator cheers |
@Charvelau I'm closing this for now |
Dear @anassinator, Qingwei |
Hi @Charvelau, You don't need to create a separate class, that's just to make things clearer. I believe all you need to do is something as follows: # Constrain the inputs.
min_bounds = np.array([0.0, 0.0, 0.0])
max_bounds = np.array([100.0, 20.0, 9.8])
u_constrained_inputs = tensor_constrain(u_inputs, min_bounds, max_bounds)
# Build your dynamics model f using u_constrained_inputs instead of u_inputs.
# You still need to give u_inputs and not u_constrained_inputs to AutoDiffDynamics.
...
# Constrain the solution.
xs, us_unconstrained = ilqr.fit(x0, us_init)
us = constrain(us_unconstrained, min_bounds, max_bounds) Hope this helps. |
Dear @anassinator , it does help. I have updated the constraint, and it is working. And it also converge, but just not the way I am looking forward to, xs is quite far from x_input (just optimized in the sense of the first element of x_input) and us seems to be stuck on the bounds. I will work on parameter tuning. Hope will get a satisfactory result. Thanks very much. Charvelau |
I haven't been able to take a look at your system, so I'm not sure if I understood what you mean exactly -- especially the "first element" part. |
Dear @anassinator, I mean, the first row of x_input, which is [10,5,8,0,0,0] in my case. While actually I expect xs to rack the entire trajectory of x_input. |
Oh! You mean only x0 is correct (which is always the case). Maybe the desired path is infeasible with the constraints set. Otherwise I'd try generating the random path differently -- a random walk for example might converge more easily. |
So maybe I should change my constraints or something. I have change the constraints for u and the fitted xs is the same. Actually I think it may try to get close to the desired x_input, but always only the x0 is tracked |
I have noticed that in the definition of PathQRCost, when calculating x_diff and u_diff, only the i_th elements is considered, will the i loop from zero to N-1 and take a sum? |
Yeah that's summed up here: Lines 193 to 205 in 066814d
Does it work when the inputs are unconstrained? If so, then the problem should be your constraints. |
Yes, it works, roughly. I will check the constraints and initial state for x0 and us_init thoroughly and let you know if got any progress. |
Dear @anassinator, in fact trajectory optimization with both desired x_path and u_path does not need constraints, lol. I mean, if it converge close enough to the desired trajectory, both xs and us will fall into reasonable and feasible region, naturally. What I need to do is to tuning the weighting coefficients and u_init. |
Dear @anassinator Sorry to bother you again, the controller works fine, thanks for your great work. A small issue is that the us seems to be of some high frequency oscillation (see pics below), do you have any clue how to eliminate it, through weight tuning or some other parameters maybe? Thanks a lot. |
Oh nice! Is this with the constraints? And are those graphs of us[0] and us[1]? If so, what are they constrained to? Or is one of those the path? And what's the blue curve? I'm not aware of any way to deal with this off the top of my head, and iLQR doesn't ensure smooth changes in u. There might be a way to take it into account in the cost function. I'm wondering if adding an additional cost term that is proportional to Lines 193 to 205 in 066814d
|
@anassinator The two pics are my us[0] and us[1] without constraints, with desired being blue and fitted be orange, I haven't uploaded the results for xs, but for xs[0], and xs[1], the fitted curve is quite perfect with relative error rate under 2%, while xs[2] also suffers the problem of oscillation. Currently, no constraints on x or u has been applied, as I have said, if an optimal path can be found, of course it will be in feasible domain, right? |
Dear @anassinator , I have added the following code, the idea is that I create a window with length of five, and get the standard deviation of it, then move it forward, and add them up. But it does not work, us seems to be unchanged, just as without the code below.
And also, I am trying to discretize my dynamics with RK4 method, this maybe can help. But the way I am defining it seems to be have some problem. Could you take a look? Thank you very much. I have uploaded it @ https://github.com/Charvelau/test/blob/master/gaitgen.ipynb '#nonlinear dynamics. discritize with RK4x_inputs_dot1=nonldyn(x_inputs,u_inputs) discritize with RK4x_inputs_dot1=nonldyn(x_inputs,u_inputs) Charvelau |
Hey! Sorry for the delay. I'm busy with exams until Monday, so I won't be able to take a look before then. The code you added seems more complicated than I would've expected it to be. I was thinking something more like (haven't actually tested this): # Subtract all next values of us from their previous values
us_diff = us[1:] - us[:-1]
# Compute a scalar cost with some weight vector us_diff_weight
us_diff_cost = us_diff_weight.dot(np.sum(us_diff, axis=1))
return J + ... + us_diff_cost But, it is very possible for this to not affect the results at all now that I think about it, because this will not be taken into account in the cost function's fist/second-order derivatives (the ones in Maybe another way to deal with this instead if the method above fails would be by extending your state such that it is of size
Then you can change PathQRCost's cost function to something like: def l(self, x, u, i, terminal=False):
"""Instantaneous cost function.
Args:
x: Current state [state_size].
u: Current control [action_size]. None if terminal.
i: Current time step.
terminal: Compute terminal cost. Default: False.
Returns:
Instantaneous cost (scalar).
"""
Q = self.Q_terminal if terminal else self.Q
R = self.R
x_diff = x - self.x_path[i]
squared_x_cost = x_diff.T.dot(Q).dot(x_diff)
if terminal:
return squared_x_cost
u_diff = u - self.u_path[i]
squared_u_cost = u_diff.T.dot(R).dot(u_diff)
# Add weighted u - u_prev
u_diff_cost = u_diff_weights.dot(u - x[state_size:])
return squared_x_cost + squared_u_cost + u_diff_cost The first/second-derivative functions ( I'll have to take a look at the discretization thing after my exams :P |
Yeah sure, thanks for your time and good luck with the exams! Charvelau |
Sorry I forgot about this. Is this still an issue? |
Does PathQRcost work with the MPC? I have ran into issues where the model get stuck at a point. |
You will need a version of |
so create replace |
You need to either
In both options, you will need to either keep back-filling |
that worked perfectly!! thankyou |
Hi anassinator, great work and thanks for the contribution. I am wondering if this can handle problems with with one step cost being L(X; u) = (X-X*)TQ(X-X*)+ (u-u*)TR(u-u*), where X* and u* are my target trajectory? @anassinator
The text was updated successfully, but these errors were encountered: