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

removed two lines each at around line 1200 and 1600 to allow one eom … #287

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
5 changes: 0 additions & 5 deletions opty/direct_collocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1181,9 +1181,6 @@ def constraints(state_values, specified_values, constant_values,

"""

if state_values.shape[0] < 2:
raise ValueError('There should always be at least two states.')

assert state_values.shape == (self.num_states,
self.num_collocation_nodes)
# n x N - 1
Expand Down Expand Up @@ -1562,8 +1559,6 @@ def constraints_jacobian(state_values, specified_values,
- n*(N - 1) : number of constraints

"""
if state_values.shape[0] < 2:
raise ValueError('There should always be at least two states.')

# Each of these arrays are shape(n, N - 1). The x_adjacent is
# either the previous value of the state or the next value of
Expand Down
60 changes: 59 additions & 1 deletion opty/tests/test_direct_collocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1516,7 +1516,7 @@ def test_known_and_unknown_order():

def test_for_algebraic_eoms():
"""
If algebraic equations of motion are given to Problem, a ValueError should
If only algebraic equations of motion are given to Problem, a ValueError should
be raised. This a a test for this
"""

Expand Down Expand Up @@ -1566,3 +1566,61 @@ def test_for_algebraic_eoms():
)

assert excinfo.type is ValueError

def test_one_eom_only():
"""
Only one differential equation should work. This tests for the corrrect
shape of the constraints and jacobian.

"""
# Equations of motion.
t = mech.dynamicsymbols._t
y, u = mech.dynamicsymbols('y u')

eom = sym.Matrix([-y.diff(t) - y**3 + u])

t0, tf = 0.0, 10.0
num_nodes = 100
interval_value = (tf - t0)/(num_nodes - 1)

state_symbols = (y, )
specified_symbols = (u,)

# Specify the objective function and form the gradient.
obj_func = sym.Integral(y**2 + u**2, t)
obj, obj_grad = create_objective_function(
obj_func,
state_symbols,
specified_symbols,
tuple(),
num_nodes,
node_time_interval=interval_value
)

# Specify the symbolic instance constraints.
instance_constraints = (
y.func(t0) - 1,
y.func(tf) - 1.5,
)

# Create the optimization problem and set any options.
prob = Problem(
obj,
obj_grad,
eom,
state_symbols,
num_nodes,
interval_value,
instance_constraints=instance_constraints,
)

initial_guess = np.zeros(prob.num_free)
initial_guess[0] = 1.0
initial_guess[num_nodes-1] = 1.5

# assert that prob.constraints and prob.jacobian have the correct shape.
length = 1*(num_nodes-1) + 2
assert prob.constraints(initial_guess).shape == (length,)

length = (2*1 + 1 + 0 + 0) * (1*(num_nodes-1)) + 2
assert prob.jacobian(initial_guess).shape == (length,)
Loading