@@ -639,35 +639,9 @@ def _print_statistics(self, reset=True):
639639 if reset :
640640 self ._reset_statistics (self .log )
641641
642- # Create an input/output system implementing an MPC controller
643- def _create_mpc_iosystem (self , dt = True ):
644- """Create an I/O system implementing an MPC controller"""
645- def _update (t , x , u , params = {}):
646- coeffs = x .reshape ((self .system .ninputs , - 1 ))
647- if self .basis :
648- # Keep the coeffecients unchanged
649- # TODO: could compute input vector, shift, and re-project (?)
650- self .initial_guess = coeffs
651- else :
652- # Shift the basis elements by one time step
653- self .initial_guess = np .hstack (
654- [coeffs [:, 1 :], coeffs [:, - 1 :]]).reshape (- 1 )
655- res = self .compute_trajectory (u , print_summary = False )
656- return res .inputs .reshape (- 1 )
657-
658- def _output (t , x , u , params = {}):
659- if self .basis :
660- # TODO: compute inputs from basis elements
661- raise NotImplementedError ("basis elements not implemented" )
662- else :
663- inputs = x .reshape ((self .system .ninputs , - 1 ))
664- return inputs [:, 0 ]
665-
666- return ct .NonlinearIOSystem (
667- _update , _output , dt = dt ,
668- inputs = self .system .nstates , outputs = self .system .ninputs ,
669- states = self .system .ninputs * \
670- (self .timepts .size if self .basis is None else self .basis .N ))
642+ #
643+ # Optimal control computations
644+ #
671645
672646 # Compute the optimal trajectory from the current state
673647 def compute_trajectory (
@@ -762,6 +736,41 @@ def compute_mpc(self, x, squeeze=None):
762736 res = self .compute_trajectory (x , squeeze = squeeze )
763737 return res .inputs [:, 0 ]
764738
739+ # Create an input/output system implementing an MPC controller
740+ def create_mpc_iosystem (self ):
741+ """Create an I/O system implementing an MPC controller"""
742+ # Check to make sure we are in discrete time
743+ if self .system .dt == 0 :
744+ raise ControlNotImplemented (
745+ "MPC for continuous time systems not implemented" )
746+
747+ def _update (t , x , u , params = {}):
748+ coeffs = x .reshape ((self .system .ninputs , - 1 ))
749+ if self .basis :
750+ # Keep the coeffecients unchanged
751+ # TODO: could compute input vector, shift, and re-project (?)
752+ self .initial_guess = coeffs
753+ else :
754+ # Shift the basis elements by one time step
755+ self .initial_guess = np .hstack (
756+ [coeffs [:, 1 :], coeffs [:, - 1 :]]).reshape (- 1 )
757+ res = self .compute_trajectory (u , print_summary = False )
758+
759+ # New state is the new input vector
760+ return res .inputs .reshape (- 1 )
761+
762+ def _output (t , x , u , params = {}):
763+ # Start with initial guess and recompute based on input state (u)
764+ self .initial_guess = x
765+ res = self .compute_trajectory (u , print_summary = False )
766+ return res .inputs [:, 0 ]
767+
768+ return ct .NonlinearIOSystem (
769+ _update , _output , dt = self .system .dt ,
770+ inputs = self .system .nstates , outputs = self .system .ninputs ,
771+ states = self .system .ninputs * \
772+ (self .timepts .size if self .basis is None else self .basis .N ))
773+
765774
766775# Optimal control result
767776class OptimalControlResult (sp .optimize .OptimizeResult ):
@@ -952,7 +961,7 @@ def solve_ocp(
952961# Create a model predictive controller for an optimal control problem
953962def create_mpc_iosystem (
954963 sys , horizon , cost , constraints = [], terminal_cost = None ,
955- terminal_constraints = [], dt = True , log = False , ** kwargs ):
964+ terminal_constraints = [], log = False , ** kwargs ):
956965 """Create a model predictive I/O control system
957966
958967 This function creates an input/output system that implements a model
@@ -1001,15 +1010,14 @@ def create_mpc_iosystem(
10011010 :func:`OptimalControlProblem` for more information.
10021011
10031012 """
1004-
10051013 # Set up the optimal control problem
10061014 ocp = OptimalControlProblem (
10071015 sys , horizon , cost , trajectory_constraints = constraints ,
10081016 terminal_cost = terminal_cost , terminal_constraints = terminal_constraints ,
10091017 log = log , ** kwargs )
10101018
10111019 # Return an I/O system implementing the model predictive controller
1012- return ocp ._create_mpc_iosystem ( dt = dt )
1020+ return ocp .create_mpc_iosystem ( )
10131021
10141022
10151023#
0 commit comments