Skip to content

Commit

Permalink
PEP8 cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
murrayrm committed Feb 21, 2021
1 parent 0d15de6 commit a396286
Showing 1 changed file with 19 additions and 21 deletions.
40 changes: 19 additions & 21 deletions control/optimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

__all__ = ['find_optimal_input']


class OptimalControlProblem():
"""Description of a finite horizon, optimal control problem
Expand Down Expand Up @@ -124,7 +125,6 @@ def __init__(
else:
self.terminal_constraints = terminal_constraints


#
# Compute and store constraints
#
Expand Down Expand Up @@ -197,7 +197,7 @@ def __init__(
initial_guess = np.broadcast_to(
initial_guess.reshape(-1, 1),
(self.system.ninputs, self.time_vector.size))
except:
except ValueError:
raise ValueError("initial guess is the wrong shape")

elif initial_guess.shape != \
Expand All @@ -222,7 +222,6 @@ def __init__(
if log:
logging.info("New optimal control problem initailized")


#
# Cost function
#
Expand Down Expand Up @@ -253,7 +252,7 @@ def _cost_function(self, inputs):
else:
if self.log:
logging.debug("calling input_output_response from state\n"
+ str(x))
+ str(x))
logging.debug("initial input[0:3] =\n" + str(inputs[:, 0:3]))

# Simulate the system to get the state
Expand All @@ -266,7 +265,7 @@ def _cost_function(self, inputs):

if self.log:
logging.debug("input_output_response returned states\n"
+ str(states))
+ str(states))

# Trajectory cost
# TODO: vectorize
Expand All @@ -293,7 +292,7 @@ def _cost_function(self, inputs):

# Terminal cost
if self.terminal_cost is not None:
cost += self.terminal_cost(states[:,-1], inputs[:,-1])
cost += self.terminal_cost(states[:, -1], inputs[:, -1])

# Update statistics
self.cost_evaluations += 1
Expand All @@ -307,7 +306,6 @@ def _cost_function(self, inputs):
# Return the total cost for this input sequence
return cost


#
# Constraints
#
Expand Down Expand Up @@ -368,7 +366,7 @@ def _constraint_function(self, inputs):
else:
if self.log:
logging.debug("calling input_output_response from state\n"
+ str(x))
+ str(x))
logging.debug("initial input[0:3] =\n" + str(inputs[:, 0:3]))

# Simulate the system to get the state
Expand All @@ -390,9 +388,9 @@ def _constraint_function(self, inputs):
elif type == opt.LinearConstraint:
# `fun` is the A matrix associated with the polytope...
value.append(
np.dot(fun, np.hstack([states[:,i], inputs[:,i]])))
np.dot(fun, np.hstack([states[:, i], inputs[:, i]])))
elif type == opt.NonlinearConstraint:
value.append(fun(states[:,i], inputs[:,i]))
value.append(fun(states[:, i], inputs[:, i]))
else:
raise TypeError("unknown constraint type %s" %
constraint[0])
Expand All @@ -405,9 +403,9 @@ def _constraint_function(self, inputs):
continue
elif type == opt.LinearConstraint:
value.append(
np.dot(fun, np.hstack([states[:,i], inputs[:,i]])))
np.dot(fun, np.hstack([states[:, i], inputs[:, i]])))
elif type == opt.NonlinearConstraint:
value.append(fun(states[:,i], inputs[:,i]))
value.append(fun(states[:, i], inputs[:, i]))
else:
raise TypeError("unknown constraint type %s" %
constraint[0])
Expand Down Expand Up @@ -448,7 +446,7 @@ def _eqconst_function(self, inputs):
else:
if self.log:
logging.debug("calling input_output_response from state\n"
+ str(x))
+ str(x))
logging.debug("initial input[0:3] =\n" + str(inputs[:, 0:3]))

# Simulate the system to get the state
Expand All @@ -461,7 +459,7 @@ def _eqconst_function(self, inputs):

if self.log:
logging.debug("input_output_response returned states\n"
+ str(states))
+ str(states))

# Evaluate the constraint function along the trajectory
value = []
Expand All @@ -474,9 +472,9 @@ def _eqconst_function(self, inputs):
elif type == opt.LinearConstraint:
# `fun` is the A matrix associated with the polytope...
value.append(
np.dot(fun, np.hstack([states[:,i], inputs[:,i]])))
np.dot(fun, np.hstack([states[:, i], inputs[:, i]])))
elif type == opt.NonlinearConstraint:
value.append(fun(states[:,i], inputs[:,i]))
value.append(fun(states[:, i], inputs[:, i]))
else:
raise TypeError("unknown constraint type %s" %
constraint[0])
Expand All @@ -489,9 +487,9 @@ def _eqconst_function(self, inputs):
continue
elif type == opt.LinearConstraint:
value.append(
np.dot(fun, np.hstack([states[:,i], inputs[:,i]])))
np.dot(fun, np.hstack([states[:, i], inputs[:, i]])))
elif type == opt.NonlinearConstraint:
value.append(fun(states[:,i], inputs[:,i]))
value.append(fun(states[:, i], inputs[:, i]))
else:
raise TypeError("unknown constraint type %s" %
constraint[0])
Expand Down Expand Up @@ -523,7 +521,7 @@ def _eqconst_function(self, inputs):
#
def _reset_statistics(self, log=False):
"""Reset counters for keeping track of statistics"""
self.log=log
self.log = log
self.cost_evaluations, self.cost_process_time = 0, 0
self.constraint_evaluations, self.constraint_process_time = 0, 0
self.eqconst_evaluations, self.eqconst_process_time = 0, 0
Expand Down Expand Up @@ -555,13 +553,13 @@ def _create_mpc_iosystem(self, dt=True):
def _update(t, x, u, params={}):
inputs = x.reshape((self.system.ninputs, self.time_vector.size))
self.initial_guess = np.hstack(
[inputs[:,1:], inputs[:,-1:]]).reshape(-1)
[inputs[:, 1:], inputs[:, -1:]]).reshape(-1)
res = self.compute_trajectory(u, print_summary=False)
return res.inputs.reshape(-1)

def _output(t, x, u, params={}):
inputs = x.reshape((self.system.ninputs, self.time_vector.size))
return inputs[:,0]
return inputs[:, 0]

return ct.NonlinearIOSystem(
_update, _output, dt=dt,
Expand Down

0 comments on commit a396286

Please sign in to comment.