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
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions opty/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,43 @@ def test_invalid_integration_limits(self):
sym.Integral(self.x ** 2, (self.t, 0, 1)), self.state_symbols,
self.input_symbols, self.unknown_symbols, self.N, 1)

def test_variable_time(self):

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
Comment on lines +205 to +214
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.

Copy link
Contributor

@Peter230655 Peter230655 Jan 20, 2025

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.

I have a basic question:
In the current create_objective_function(...) there is a distinction in forming obj, obj_grad depending on the integration method.
But, for example, in the examples-gallery simulation plot_pendulum_swing_up_variable_duration, the gradient is formed as I would naively expect it to be formed.

Can the gradient always be formed as per the method used in plot_pendulum_swing_up_variable_duration, or is this only valid for midpoint euler or is this a (good) approximation, if I understood #30 #31 correctly?
Thanks for any explanations!

Copy link
Member Author

Choose a reason for hiding this comment

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

The examples are a little loose, but it probably doesn't matter too much in the objective calc because both methods have about the same minima.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks!
This would mean one could calculate obj and obj_grad 'naively' without committing a large error?

Copy link
Member Author

Choose a reason for hiding this comment

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

The gradient of the objective has to be the valid gradient within some numerical tolerance. But your choice of solving the integral in the objective does not really matter, as there are numerous integration methods.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks!
The gradient as calculated in the simulation plot_pendulum_swing_up_variable_duration surely is the valid gradient (?)


obj_expr = sym.Integral(self.f1**2 + self.f2**2, self.t)
obj, obj_grad = utils.create_objective_function(
obj_expr, self.state_symbols, self.input_symbols,
self.unknown_symbols, self.N, self.h, time_symbols=self.t)
np.testing.assert_allclose(obj(self.free), expected_obj(self.free))
np.testing.assert_allclose(obj_grad(self.free),
expected_obj_grad(self.free))

def expected_obj(free):
return free[-1]

def expected_obj_grad(free):
grad = np.zeros_like(free)
grad[-1] = 1.0
return grad

obj_expr = sym.Integral(1, self.t)
obj, obj_grad = utils.create_objective_function(
obj_expr, self.state_symbols, self.input_symbols,
self.unknown_symbols, self.N, self.h, time_symbols=self.t)
np.testing.assert_allclose(obj(self.free), expected_obj(self.free))
np.testing.assert_allclose(obj_grad(self.free),
expected_obj_grad(self.free))


def test_parse_free():

Expand Down
Loading