From fa7737692b1c22900b5eb4d18678619bc2ffe85a Mon Sep 17 00:00:00 2001 From: Peter Stahlecker Date: Sun, 19 Jan 2025 12:59:40 +0100 Subject: [PATCH 1/6] docstrings changed for solve to match opty --- opty/direct_collocation.py | 59 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/opty/direct_collocation.py b/opty/direct_collocation.py index 4c084b2..76ffd10 100644 --- a/opty/direct_collocation.py +++ b/opty/direct_collocation.py @@ -197,6 +197,65 @@ def __init__(self, obj, obj_grad, equations_of_motion, state_symbols, self.obj_value = [] + # this is added only to correct the docstring, which is a bit different with + # opty compared to cyipopt. + def solve(self, free): + """ + **solve**(x) + + Returns the optimal solution and an info dictionary. + + Solves the posed optimization problem starting at point x. + + Parameters: + + x (array-like, shape(n*N + q*N + r + s, )) + + Initial guess. + + Returns: + + x (array, shape(n*N + q*N + r + s, )) + + optimal solution. + + info (dictionary) + + x: ndarray, shape(n*N + q*N + r + s, ) + + optimal solution + + g: ndarray, shape(n*(N-1) + o, ) + + constraints at the optimal solution + + obj_val: float + + objective value at optimal solution + + mult_g: ndarray, shape(n*(N-1) + o, ) + + final values of the constraint multipliers + + mult_x_L: ndarray, shape(n*N + q*N + r + s, ) + + bound multipliers at the solution + + mult_x_U: ndarray, shape(n*N + q*N + r + s, ) + + bound multipliers at the solution + + status: integer + + gives the status of the algorithm + + status_msg: string + + gives the status of the algorithm as a message + + """ + return super().solve(free) + def _generate_bound_arrays(self): lb = -self.INF * np.ones(self.num_free) ub = self.INF * np.ones(self.num_free) From bf3551dc13ace06160b37cf11425fc55648065ae Mon Sep 17 00:00:00 2001 From: Peter Stahlecker Date: Sun, 19 Jan 2025 13:26:28 +0100 Subject: [PATCH 2/6] tried to correct the error which made it fail a test --- opty/direct_collocation.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/opty/direct_collocation.py b/opty/direct_collocation.py index 76ffd10..7b726ba 100644 --- a/opty/direct_collocation.py +++ b/opty/direct_collocation.py @@ -201,6 +201,7 @@ def __init__(self, obj, obj_grad, equations_of_motion, state_symbols, # opty compared to cyipopt. def solve(self, free): """ + **solve**(x) Returns the optimal solution and an info dictionary. @@ -254,6 +255,7 @@ def solve(self, free): gives the status of the algorithm as a message """ + return super().solve(free) def _generate_bound_arrays(self): From c92117c2470820b6bbc4dc37ab2c60c8106d4638 Mon Sep 17 00:00:00 2001 From: Peter Stahlecker Date: Sun, 19 Jan 2025 13:59:54 +0100 Subject: [PATCH 3/6] next try to solve the error --- opty/direct_collocation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/opty/direct_collocation.py b/opty/direct_collocation.py index 7b726ba..fcd3497 100644 --- a/opty/direct_collocation.py +++ b/opty/direct_collocation.py @@ -202,7 +202,7 @@ def __init__(self, obj, obj_grad, equations_of_motion, state_symbols, def solve(self, free): """ - **solve**(x) + **solve(x)** Returns the optimal solution and an info dictionary. From 790d9c0bbad2fd6fc19aed51b23d3030b8152dc9 Mon Sep 17 00:00:00 2001 From: Peter Stahlecker Date: Sun, 19 Jan 2025 15:21:36 +0100 Subject: [PATCH 4/6] copied from cyipopt code and only changed a few things relevant for opty --- opty/direct_collocation.py | 79 ++++++++++++++++---------------------- 1 file changed, 33 insertions(+), 46 deletions(-) diff --git a/opty/direct_collocation.py b/opty/direct_collocation.py index fcd3497..ce3f7db 100644 --- a/opty/direct_collocation.py +++ b/opty/direct_collocation.py @@ -200,59 +200,46 @@ def __init__(self, obj, obj_grad, equations_of_motion, state_symbols, # this is added only to correct the docstring, which is a bit different with # opty compared to cyipopt. def solve(self, free): - """ - - **solve(x)** - - Returns the optimal solution and an info dictionary. + """Returns the optimal solution and an info dictionary. Solves the posed optimization problem starting at point x. - Parameters: - - x (array-like, shape(n*N + q*N + r + s, )) - - Initial guess. - - Returns: - - x (array, shape(n*N + q*N + r + s, )) - - optimal solution. - - info (dictionary) - - x: ndarray, shape(n*N + q*N + r + s, ) - - optimal solution - - g: ndarray, shape(n*(N-1) + o, ) - - constraints at the optimal solution - - obj_val: float - - objective value at optimal solution - - mult_g: ndarray, shape(n*(N-1) + o, ) - - final values of the constraint multipliers - - mult_x_L: ndarray, shape(n*N + q*N + r + s, ) - - bound multipliers at the solution - - mult_x_U: ndarray, shape(n*N + q*N + r + s, ) - - bound multipliers at the solution + Parameters + ---------- + x : array-like, shape(n*N + q*N + r + s, ) + Initial guess. - status: integer + lagrange : array-like, shape(n*(N-1) + o, ), optional (default=[]) + Initial values for the constraint multipliers (only if warm start option is chosen). - gives the status of the algorithm + zl : array-like, shape(n*N + q*N + r + s, ), optional (default=[]) + Initial values for the multipliers for lower variable bounds (only if warm start option is chosen). - status_msg: string + zu : array-like, shape(n*N + q*N + r + s, ), optional (default=[]) + Initial values for the multipliers for upper variable bounds (only if warm start option is chosen). - gives the status of the algorithm as a message + Returns + ------- + x : :py:class:`numpy.ndarray`, shape `(n*N + q*N + r + s, )` + Optimal solution. + info: :py:class:`dict` with the following entries + + ``x``: :py:class:`numpy.ndarray`, shape `(n*N + q*N + r + s, )` + optimal solution + ``g``: :py:class:`numpy.ndarray`, shape `(n*(N-1) + o, )` + constraints at the optimal solution + ``obj_val``: :py:class:`float` + objective value at optimal solution + ``mult_g``: :py:class:`numpy.ndarray`, shape `(n*(N-1) + o, )` + final values of the constraint multipliers + ``mult_x_L``: :py:class:`numpy.ndarray`, shape `(n*N + q*N + r + s, )` + bound multipliers at the solution + ``mult_x_U``: :py:class:`numpy.ndarray`, shape `(n*N + q*N + r + s, )` + bound multipliers at the solution + ``status``: :py:class:`int` + gives the status of the algorithm + ``status_msg``: :py:class:`str` + gives the status of the algorithm as a message """ From 673b9b09147fbdac0fe57c9ec01cdd3a5f7c087e Mon Sep 17 00:00:00 2001 From: Peter Stahlecker Date: Mon, 20 Jan 2025 09:33:59 +0100 Subject: [PATCH 5/6] corrected signature --- opty/direct_collocation.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/opty/direct_collocation.py b/opty/direct_collocation.py index ce3f7db..666d2c1 100644 --- a/opty/direct_collocation.py +++ b/opty/direct_collocation.py @@ -199,7 +199,7 @@ def __init__(self, obj, obj_grad, equations_of_motion, state_symbols, # this is added only to correct the docstring, which is a bit different with # opty compared to cyipopt. - def solve(self, free): + def solve(self, free, lagrange=[], zl=[], zu=[]): """Returns the optimal solution and an info dictionary. Solves the posed optimization problem starting at point x. @@ -243,7 +243,7 @@ def solve(self, free): """ - return super().solve(free) + return super().solve(free, lagrange=[], zl=[], zu=[]) def _generate_bound_arrays(self): lb = -self.INF * np.ones(self.num_free) From 76c6b3a99d8ad8bf2c898cbab5889266598e7bc0 Mon Sep 17 00:00:00 2001 From: Peter Stahlecker Date: Mon, 20 Jan 2025 10:13:41 +0100 Subject: [PATCH 6/6] corrected errors in signature, removed unnecessary explanations --- opty/direct_collocation.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/opty/direct_collocation.py b/opty/direct_collocation.py index 666d2c1..394a482 100644 --- a/opty/direct_collocation.py +++ b/opty/direct_collocation.py @@ -197,8 +197,6 @@ def __init__(self, obj, obj_grad, equations_of_motion, state_symbols, self.obj_value = [] - # this is added only to correct the docstring, which is a bit different with - # opty compared to cyipopt. def solve(self, free, lagrange=[], zl=[], zu=[]): """Returns the optimal solution and an info dictionary. @@ -243,7 +241,7 @@ def solve(self, free, lagrange=[], zl=[], zu=[]): """ - return super().solve(free, lagrange=[], zl=[], zu=[]) + return super().solve(free, lagrange=lagrange, zl=zl, zu=zu) def _generate_bound_arrays(self): lb = -self.INF * np.ones(self.num_free)