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.


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