Skip to content

Commit

Permalink
Add plotting to initial guess test function.
Browse files Browse the repository at this point in the history
  • Loading branch information
moorepants committed Feb 19, 2025
1 parent 9d56ba3 commit 1a17afb
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion opty/tests/test_direct_collocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1908,7 +1908,7 @@ def test_attributes_read_only():
with raises(AttributeError):
setattr(test, XX, 5)

def test_linear_initial_guess():
def test_linear_initial_guess(plot=False):
"""Test to check if the initial guess is created correctly."""

x, y, ux, uy = mech.dynamicsymbols('x y ux uy')
Expand Down Expand Up @@ -2037,6 +2037,9 @@ def obj_grad(free):
initial_guess = prob.create_linear_initial_guess()
np.testing.assert_allclose(initial_guess, expected_guess)

if plot:
prob.plot_trajectories(initial_guess)

# A2: np.inf, -np.inf in bounds
bounds[a1] = (-np.inf, 10.0)
bounds[a2] = (-10.0, np.inf)
Expand Down Expand Up @@ -2081,6 +2084,9 @@ def obj_grad(free):
initial_guess = prob.create_linear_initial_guess()
np.testing.assert_allclose(initial_guess, expected_guess)

if plot:
prob.plot_trajectories(initial_guess)

# A4: state instances in instance_constraints
instance_constraints = (
x.func(t0) - 3.0 + ux.func(tf),
Expand Down Expand Up @@ -2118,6 +2124,9 @@ def obj_grad(free):
initial_guess = prob.create_linear_initial_guess()
np.testing.assert_allclose(initial_guess, expected_guess)

if plot:
prob.plot_trajectories(initial_guess)

# B: VARIABLE TIME INTERVAL
# B1: normal
h = sym.symbols('h')
Expand Down Expand Up @@ -2229,6 +2238,9 @@ def obj_grad(free):
initial_guess = prob.create_linear_initial_guess()
np.testing.assert_allclose(initial_guess, expected_guess)

if plot:
prob.plot_trajectories(initial_guess)

# B2: np.inf, -np.inf in bounds
bounds[a1] = (-np.inf, 10.0)
bounds[a2] = (-10.0, np.inf)
Expand All @@ -2252,6 +2264,9 @@ def obj_grad(free):
initial_guess = prob.create_linear_initial_guess()
np.testing.assert_allclose(initial_guess, expected_guess)

if plot:
prob.plot_trajectories(initial_guess)

# B3: no bounds
expected_guess[4*num_nodes: 5*num_nodes] = 0.0
expected_guess[5*num_nodes: 6*num_nodes] = 0.0
Expand All @@ -2273,6 +2288,9 @@ def obj_grad(free):
initial_guess = prob.create_linear_initial_guess()
np.testing.assert_allclose(initial_guess, expected_guess)

if plot:
prob.plot_trajectories(initial_guess)

# B4: state instances in instance_constraints
instance_constraints = (
x.func(t00) - 3.0 + ux.func(tf0),
Expand Down Expand Up @@ -2309,3 +2327,6 @@ def obj_grad(free):

initial_guess = prob.create_linear_initial_guess()
np.testing.assert_allclose(initial_guess, expected_guess)

if plot:
prob.plot_trajectories(initial_guess)

0 comments on commit 1a17afb

Please sign in to comment.