Skip to content
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

Variable time support in create_objective_function. #205

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

moorepants
Copy link
Member

No description provided.

@Peter230655
Copy link
Contributor

Does this mean, that create_objective_function will soon be able to handle variable h? :-)

@moorepants
Copy link
Member Author

Only if I can figure it out.

@Peter230655
Copy link
Contributor

Peter230655 commented Aug 5, 2024

Only if I can figure it out.

I feel like you know it already! Surely will avoid these ugly expressions like in my ball/disc simulation.
If you want to minimize h, the only sensible expression to minimize would be weight * h, weight > 0.0 ?

@moorepants
Copy link
Member Author

A general solution to this needs to support this cost function:
image

(general optimal control cost function)

Creating the discrete version of that and the discrete gradient from a general SymPy expression is not so simple.

There are past open issues about this: #30 #31

@Peter230655
Copy link
Contributor

Just to make sure I understand it correctly:

  • E and F are functions, differentiable in all their arguments.
  • x is the state vector of the system.
  • u is the control (input) vector.
  • J[x(.), u(.), t_0, t_f] is to be minimized.

A necessary condition for a minimum is that grad_(x, u, t_f)(J) = 0.

@tjstienstra
Copy link
Contributor

There are past open issues about this: #30 #31

See also #190

@moorepants
Copy link
Member Author

Yes, I was starting to implement your suggestion but I think it was missing taking the derivative wrt to h and I started getting hung up on the solution. I basically copied your code and started trying to make it work, but haven't gotten it to yet.

@tjstienstra
Copy link
Contributor

but I think it was missing taking the derivative wrt to h

I would expect that you can just add it to the objective_grad computation, the list of symbols w.r.t. which the jacobian is computed. However, I don't remember testing the solution (in depth), but it is nice to see that you have already written some tests.

Comment on lines +205 to +214
def expected_obj(free):
f = free[2*self.N:-1]
return free[-1]*np.sum(f**2)

def expected_obj_grad(free):
f = free[2*self.N:-1]
grad = np.zeros_like(free)
grad[2*self.N:-1] = 2.0*free[-1]*free[2*self.N:-1]
grad[-1] = np.sum(f**2)
return grad
Copy link
Contributor

@tjstienstra tjstienstra Aug 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should take into account that this is backward Euler, so the first term falls out, see test_backward_single_input.
If free is [x(t), v(t), f1(t), f2(t), c, k, m, h] then the objective should be (f1_vals[1:]**2 + f2_vals[1:]**2).sum() * h_val.
Similarly, the gradient should be a stack of zeros(2*N+1), 2*h_val*f1_vals[1:], [0] 2*h_val*f2_vals[1:], [0, 0, 0, (f1_vals[1:]**2 + f2_vals[1:]**2).sum()]

P.S. quickly wrote out the equations on my phone so would advise checking them.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think I assumed any specific integration routine in the manually created objective functions.

@moorepants moorepants changed the title Added unit tests for variable time support in create_objective_function. Variable time support in create_objective_function. Aug 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants