A Continuous Formulation for Logical Decisions in Differential Algebraic Systems using Mathematical Programs with Complementarity Constraints

This work presents a methodology to represent logical decisions in differential algebraic equation simulation and constrained optimization problems using a set of continuous algebraic equations. The formulations may be used when state variables trigger a change in process dynamics, and introduces a pseudo-binary decision variable, which is continuous, but should only have valid solutions at values of either zero or one within a finite time horizon. This formulation enables dynamic optimization problems with logical disjunctions to be solved by simultaneous solution methods without using methods such as mixed integer programming. Several case studies are given to illustrate the value of this methodology including nonlinear model predictive control of a chemical reactor using a surge tank with overflow to buffer disturbances in feed flow rate. Although this work contains novel methodologies for solving dynamic algebraic equation (DAE) constrained problems where the system may experience an abrupt change in dynamics that may otherwise require a conditional statement, there remain substantial limitations to this methodology, including a limited domain where problems may converge and the possibility for ill-conditioning. Although the problems presented use only continuous algebraic equations, the formulation has inherent non-smoothness. Hence, these problems must be solved with care and only in select circumstances, such as in simulation or situations when the solution is expected to be near the solver’s initial point.


Introduction
In dynamic optimization, models are ideally formulated as a set of continuous equations with continuous derivatives, so that solutions can be efficiently obtained using gradient-based solution algorithms, such as Newton's method.However, in many systems, the need frequently arises to include operators that may be discontinuous (such as the signum operator) or have discontinuous first derivatives (such as the absolute value operator).The introduction of such discontinuities into a model can have adverse impacts on the solver's ability to efficiently obtain an accurate solution due to the introduction of non-smooth gradients.
Dynamic optimal control problems using Model Predictive Control (MPC) are particularly difficult due to the high dimensionality of a time-dependent optimization problem, that requires model predictions and control actions for every time step.Furthermore, online applications require fast solution times so that control actions can be calculated and recommended within some pre-determined sampling period.The introduction of discontinuities further complicates matters, as some practitioners may use more computationally expensive solution methods, such as incorporating logical if statements into a purely sequential solution method in order to implement such disjunctive constraints.While improvements to Mixed Integer Nonlinear Programming (MINLP) solution methods have been reported [1], more computationally efficient methods exist.
Mathematical programs with equilibrium constraints (MPECs) have been proposed as a method to integrate non-smooth behavior into a set of simultaneous algebraic equations by the inclusion of complementarity conditions [2,3].Complementarity, the requirement that at least one of a pair of variables be at some limit, provides a framework for representing disjunctive behavior using a set of continuous equations.MPECs using complementarity constraints have found use in optimization problems in the fields of structural mechanics [4,5], chemical and process engineering [6][7][8][9], electric power generation [10], climate change [11], traffic networks [12], operations research [13], economics [14], and other fields [15,16].
Complementarity constraints can be used to represent non-smooth or discontinuous operators, such as absolute value, sgn, and min/max [17].This work presents the formulation of a greater than or equal to (≥) and a less than or equal to (≤) operator, which can be used for if/then logic in a process model.The formulation is presented as a set of continuous algebraic equations.The equations are formulated in such a way, however, that only binary (0 or 1) solutions are obtained for certain variables at the solution.These pseudo-binary variables are then used to represent logical conditions within the model.In this paper, pseudo-binary variables are defined as continuous variables that converge to one of two values within a finite time horizon.This work does not present a detailed explanation of the convergence properties of problems with complementarity constraints, but rather puts forward a novel formulation that can be used by practitioners to represent logical statements within a continuous process model.Generally, the use of complementarity conditions in a process model is undesirable.However, in certain circumstances, natural discontinuities in the process require specialized techniques for representing these conditions in the model.

Logical Disjunctions in Optimization
Logical expressions, such as the less than/equal to (≤) operator (or Heaviside function), may be introduced into optimization problems through the use of mixed integer programming, where certain variables are constrained at integer values.A general disjunctive program can be converted to an equivalent MINLP [18][19][20] and solved using various MINLP algorithms [21][22][23].However, one drawback to MINLP formulations is that solution times grow exponentially with an increased number of discrete decisions [6].When considering dynamic optimization problems, where the time domain is typically discretized and a set of decisions is required for each time, optimization problems can become especially large.When a rapid solution is required, converting a large dynamic optimization problem with disjunctions to an MINLP problem may not be a tractable option.Therefore, the ability to embed logical statements or other disjunctive operators as sets of algebraic equations and inequalities while maintaining mathematical continuity allows the problems to be posed as a nonlinear programming (NLP) problem, for which many efficient solvers exist.Even so, specialized solution methods may be required to effectively address issues that arise with complementarity constraints.See [24] for details concerning the feasibility issues inherent in MPCC formulations.
In constrained continuous dynamic simulation, two basic methodologies for solving a finite horizon NLP problem exist: sequential methods and simultaneous methods [25], although other methods, including hybrids of the two (i.e., multiple shooting methods) may also be used [26,27].Sequential and simultaneous methods are briefly introduced in Sections 2.2 and 2.3.

Sequential Solution Method
A sequential method employs a forward-stepping differential algebraic equation (DAE) or ordinary differential equation (ODE) solver, using a Runge-Kutta or similar numerical integration technique.Using this method, inputs at every time step are specified.The DAE solver then integrates forward one step at a time using the pre-specified inputs.The sequential method ensures that the state equations are satisfied at all times, as they are enforced by the DAE solver as integration transpires.Logical statements and other disjunctions are fairly easy to implement when using sequential methods, as the state equations can be altered at any point during the integration.For example, when a state variable reaches some limit that triggers a disjunction, a logical statement can be embedded into the DAE model ensuring that the change will be applied to future output from the model while that particular condition holds.
While sequential methods for solving DAE systems certainly have some advantages, when applied to large-scale optimization problems, these methods are inefficient because they require simulating the model many times with different values of inputs (at each time step) in order to compute numerical approximations of gradient matrices.The solutions from initial values that are not optimal lead to excessive CPU time that is only used for intermediate solutions, although this can be avoided by using sensitivity methods and automatic differentiation [28].The requirement to converge the model equations at every iteration also leads to a challenge for unstable systems.If the specified decision variables produce an unstable response, the iteration may fail to find an adequate search direction for the next iteration [29].It is also difficult to enforce inequality constraints on state (or dependent) variables because the values of these variables at each time step are only obtained by forward integration using a set of pre-determined inputs; therefore, constraints cannot be directly imposed on these variables.

Simultaneous Solution Method
Simultaneous solution methods are frequently used in industry for dynamic optimization and real-time control problems because they help to overcome many of the computational inefficiencies associated with sequential solution methods [30][31][32].Simultaneous solution methods use collocation (more specifically, orthogonal collocation on finite elements [33,34]) to convert a DAE-constrained dynamic optimization problem to an NLP where the objective function is minimized and the constraint equations are solved simultaneously, making the algorithm much more computationally efficient.By comparison, a sequential method requires simulation through the differential constraint equations many times for every set of inputs [35].
The crux of a simultaneous solution method is the conversion of the DAE system to a system of purely algebraic equations using a collocation method.The differential equations are specified in Equation ( 1) with time derivatives given as a function ( f ) of differential state variables (x), algebraic state variables (y), user-controlled inputs (u), and external inputs (p), each of which is a function of τ, a variable representing time in each finite element, normalized to the range [0,1] over the time interval: Conversion of these differential equations is done by representing differential state profiles in time by polynomial approximations, which are generated using Lagrange interpolation polynomials (Ω).These polynomials are formulated to exactly match the value of the derivatives when evaluated at the collocation points (τ i ).This relationship, assuming constant inputs over the time interval, is shown in Equation (2), where the derivatives at discrete time points are approximated as the summation of f evaluated at each collocation point (τ i ) multiplied by the corresponding interpolation polynomial (Ω j ).These additional equations allows the differential equation model to be solved as a nonlinear programming problem where differential terms are simply additional variables of an often large-scale and sparse system of nonlinear equations: ( The Lagrange polynomials are defined as shown in Equation (3) and are of order N C − 1, where N C is the number of collocation points used in the approximation over the time interval [36]: The relationship in Equation ( 2) holds exactly at the collocation points because each polynomial (Ω j ) in Equation ( 3) is formulated to have a value of unity at the corresponding collocation point (τ j ) and a value of zero at all the other collocation points [36]: With state derivatives guaranteed to exactly match at the collocation points, the state variables themselves are approximated by integrating Equation (3): This allows for the state values themselves to be approximated.
where Ωj is the integral of Ω j , which is a polynomial of order N C , x 0 is the value of the state variable at the beginning of the time interval, and w = τ i+1 − τ i is the width of the time interval.
In order to ensure integration accuracy and that Ω is explicitly defined at the right end of the time interval (τ = 1), Radau collocation points are used.The Radau collocation points are derived from Radau quadrature, which is similar to Gaussian quadrature, except that one collocation point is defined explicitly at one end (rather than having all points exclusively in the interior) of the time interval [37].For dynamic optimization applications, the interval is 0 to 1, with the state values at 0 obtained from the previous interval, and with a collocation point set exactly at 1.
With an approximation for a single time interval defined, multiple time intervals can be joined together, with a separate polynomial representing each interval, or finite element.The initial condition for each time interval is given as the final condition of the previous time interval (C 0 -continuity).Other quadrature methods propagate first derivatives (C 1 -continuity) or higher p-order derivative information (C p -continuity) across the interval boundaries [38] to achieve higher accuracy across intervals.Figure 1 illustrates the orthogonal collocation on finite elements discretization scheme.In this discretization scheme the first point represents the inital condition of the finite element, while the final point is the first point of the next finite element.
Each time interval (k) of length w contains N C collocation points.The example in the figure uses N C = 3, but higher or lower orders of approximation also exist.The approximation from finite element k would use the state value from the last collocation point (i = N C ) of element k − 1 as the initial condition, as shown in Equation (7).In Equation (7), the subscripts (i and j) refer to the collocation point and the superscript (k) refers to the finite element number: With the approximation in Equation ( 7) completed, the differential equations are converted into algebraic equations, which can be solved by a nonlinear algebraic equation solver.Therefore, enforcing additional algebraic equality constraints (g) becomes possible, as these equations ( 8) can be included with the algebraic equations in Equation ( 7): Nonlinear inequality constraints can also be included, as can upper and lower bounds on the variables themselves: The ability to directly impose constraints on state variables is one of the advantages of a simultaneous solution method, as opposed to the sequential method.The algebraic formulation of Equations ( 6)-( 12) lends itself quite well to inclusion in an optimization problem which can be converged by an NLP solver.

Embedding MPECs with Complementarity into Simultaneous Equations
One of the disadvantages of a simultaneous solution method compared to a sequential method is that it is much more difficult to embed disjunctive constraints or logical conditions.Because the model is solved as a set of simultaneous algebraic equations, the introduction of disjunctions would make it difficult to solve the equations by standard methods.However, with the ability to enforce algebraic constraints within a differential model, MPCCs, which are formulated as sets of algebraic equations, can be embedded into the model to represent disjunctions.These MPCCs take advantage of a complementarity condition that both constraints are active, one as an equality and the other as an inequality, as shown in Equation ( 8), where ⊥ is the complementarity operator [6,16]: In this work, υ + and υ − are referred to as complementarity variables.The condition in Equation ( 13) can be maintained by using a number of different formulations.For example, Equations ( 14) and ( 15) represent the complementarity condition as an equality constraint and an inequality constraint respectively: These equations require that at least one of the pair υ + and υ − be equal to zero as long as υ + and υ − are individually greater than or equal to zero.Equation ( 15) is preferred over Equation ( 14) when implemented in simultaneous solution methods because it allows greater flexibility to the solver to find solutions [6].To further improve solver performance, an approximation to Equation ( 15) may be used in practice.
where is a very small positive number, indicating that some error in this relationship may be tolerated in order to enhance the convergence properties of interior point NLP methods.This relaxed version of the formulation is a solution technique that may enhance convergence properties, but may result in a suboptimal or possibly infeasible solution.The relaxation in Equation ( 16) is not used in the examples discussed in this work as solutions were obtained with the equality constraint in Equation ( 14).
Using the complementarity condition, several different MPCCs can be formulated to represent some commonly used functions.These sets of equations can be embedded into a DAE model and keep the model continuous and smooth, despite the fact that these operators represent non-smooth or discontinuous operators in standard practice.

Absolute Value Operator
The absolute value operator y = |x| (17) can be alternatively represented in a continuous optimization problem by embedding the following equations into the DAE or algebraic model: In Equation (18b), the complementarity variables are restricted to be nonnegative.Because the complementarity condition Equation (18c) requires that at least one of these variables be zero, Equation (18a) represents the difference between two nonnegative values.When x is positive, υ − must be zero in order to satisfy Equation (18c).υ + is therefore positive and equal to x.Thus, the summation of υ + and υ − in Equation (18d) becomes equal to the absolute value of x.Similarly, for negative x, υ − must be positive and υ + must be zero.The summation of these two nonnegative values Equation (18d), therefore, will always be a positive number equal in magnitude to x [6].

Min/Max Operator
The min and max operators, which select the minimum and maximum value, respectively, of two inputs (x 1 and x 2 ) can also be represented using formulations with complementarity conditions: In this formulation, if x 1 is greater than x 2 , υ + will assume the difference between these values.υ − will be zero in order to satisfy the complementarity condition Equation (20c).The lesser of x 1 and x 2 will therefore be the higher number (x 1 ) minus the difference (υ + ) leaving y to be equal to the min of the two as specified in Equation (20d).The greater number will be the higher number plus υ − , which is zero in this case.Therefore, z will represent the max of the two numbers, as Equation (20e) indicates [6].

Signum Operator
The signum operator gives an output of +1 for positive input and −1 for negative input: This binary behavior can also take on a continuous representation by using an MPCC formulation: As Equation (22) indicates, when x is positive, υ + will also be positive and equal in magnitude to x.Because υ − will be zero, y will have to equal +1 in order to satisfy Equation (22d).Similarly, when x is negative, y will be equal to -1, as a positive value of υ − and a zero value of υ + will enforce this in Equation (22d) [6].

MPEC Formulations with Complementarity to Represent Logical Statements
Because MPCCs provide a continuous formulation that approximates some disjunctive relationships, it is possible to represent some logical behavior within a model using an MPCC formulation similar to the ones previously described .For instance, an MPCC can be used to represent a binary variable, which is 1 when some condition is true and 0 otherwise.This binary variable can then be integrated into the model equations such that certain equations only hold true under the logical conditions dictated by the MPCC.The remainder of this section discusses the development of a continuous approximation of the Heaviside function.Section 4 will discuss the methodology for using the continuous Heaviside function to implement logic into a set of DAEs.

Jump Function
With only a slight modification of Equation ( 22), the MPCC can be constructed so as to produce a 1 for a positive input (x) and a 0 for a negative input.Here, the variable δ is introduced to represent the binary output of this MPCC: This MPCC formulation is very similar to the signum operator, with only a slight modification made in the fourth equation.As Equation (24d) indicates, the output of this MPCC can be customized to yield various constants, depending on the terms added to or subtracted from δ: Using the formulation in Equation ( 24), δ becomes a pseudo-binary variable, one which is continuous but can only assume values of zero or one at the solution for negative or positive values of x, respectively.

Heaviside Function
Careful inspection of Equation ( 24) reveals a major shortcoming.When x = 0, both complementarity variables are simultaneously equal to zero.This means that Equation (24d) will be satisfied by any value of δ, as the system has an infinite number of solutions in this case.The MPCC equations must therefore be modified in order to give the system the discrete switching behavior that is desired with no ambiguity for any value of x: Adding a second complementarity condition to the set of equations is proposed to address the issue of ambiguity when x = 0. Equation (26d) contains a third complementarity variable, υ 0 , and is designed such that υ 0 will take on some finite value when υ + and υ − are simultaneously zero, due to the input, x, being equal to zero, which requires a nonzero initial value for υ 0 : In Equation (26e) a third term is added for the case that only υ 0 is nonzero (which occurs when x = 0).However, some ambiguity still exists in this formulation, namely, that all complementarity variables may simultaneously be zero when x is zero, thereby satisfying Equation (26e), regardless of the value of δ.This is the primary limitation of this MPCC formulation.While this limitation is inherent in this formulation, it can be addressed by properly taking advantage of solver convergence properties when used in simulation and optimization implementations.This is done by squaring υ + and υ − , as in Equation (26d), to ensure that these squared terms converge to zero at a faster rate, leaving υ 0 at some nonzero value.With zero values for υ + and υ − and a finite value for υ 0 , the (1 − δ) term multiplying υ 0 must equal zero, giving δ a value of 1 when x = 0. Changing the δ term in Equation (26e) will obviously affect what δ converges to in this case, meaning that the MPCC can be formulated so that δ takes on some other, user-determined, value.The same holds true for the terms multiplying υ + and υ − if other outputs are desired for positive and negative values for x, respectively.Although we use an active set solver for the examples in this work, it is noted that penalty methods may also lead to ambiguity in the solution, particularly solvers that employ a variable penalty.
An alternate formulation using only equality constraints, and which also suffers from the inherent limitation at x = 0, is used for testing the convergence properties of this logical MPCC when implemented in optimization routines.The non-negativity constraints in Equation (26b) are removed and these constraints are instead enforced by squaring the complementarity variables in the first equation Equation (27a).Note that this is a system of four equations and four unknowns, with x being considered an external input to this system: This system of equations is evaluated for convergence properties using Newton's method for solving systems of nonlinear equations.The system exhibits no issues with convergence for negative and nonnegative values of x, with δ converging to 1 and 0, respectively, as desired.The predominant concern is obtaining a distinct desired solution when x is zero.Newton iterations for this scenario are shown in Figures 2-3.As Figure 2 illustrates, υ + and υ − converge to zero as expected.The other complementarity variable, υ 0 , however, remains at the initial guess value, as the squared terms in Equation (27c) converge to zero in order to satisfy Equation (27a).This finite value for υ 0 , however, forces δ to converge exactly to 1 in order to satisfy Equation (27d), rather than leaving this value ambiguous, as the formulation in Equation ( 24) would have.While this MPCC strategy works for solutions that are near the initial guess values for υ 0 , initializations that are not near the solution may cause υ + and υ − to not converge sufficiently to render a feasible solution.Therefore, implementing this Heaviside function MPCC formulation is subject to well known simultaneous solution method initialization limitations [39].

Continuous Logic in Dynamic Systems
Using the collocation scheme combined with the logical MPCC framework developed in the previous section, dynamic systems of equations with logical conditions can be simulated using only a set of continuous algebraic equations.This is done by embedding a logical MPCC into the DAE system.The pseudo-binary variable, δ, from this MPCC can be multiplied with the model equations, meaning that some equations will hold only when δ = 1.Two simulation examples are used to illustrate how this is done.

Tank with Overflow
A simple example to illustrate the need for representing logic in a DAE model is that of a simple tank with overflow, shown in Figure 4.While the dynamics of this system are trivial, the equations representing the dynamic behavior of the tank change dramatically when the tank reaches the overflow limit.The system, as posed in Equation ( 28), can be represented as a simple ODE combined with a logical expression determining when the tank overflows: Here V is the tank volume, Q in is the flow into the tank, Q out is the flow out of the tank, and Q over is the flow exiting the tank as overflow, when the tank volume exceeds the capacity, V max .While the system in Equation ( 28) is very simple, the logical statement Equation (28b) prevents it from being solved using a standard simultaneous solution method.However, by including the algebraic equations representing the Heaviside function MPCC, this system can be solved using a simultaneous solution method.This DAE system translated into a continuous logic formulation using an MPEC with complementarity constraints is given in Equation ( 29), where Equations (29e)-(29h) represent the additional algebraic equations introduced by the logical MPCC: In this formulation, δ hi is a pseudo-binary variable that converges to one when the tank is full and zero when it is not full.However, δ hi can have values between one and zero as the solver searches for a solution.When the tank is not full, Equation (29b) will ensure that Q over is zero.When the tank is full, Q over will take on whatever value necessary to satisfy the material balance Equation (29a).However, Q over must be restricted to non-negative values in order to prevent negative values of Q over from satisfying Equation (29a) when the tank is not full.The MPCC tests whether the quantity V − V max is greater than or equal to zero.However, in order to enhance convergence properties, V is also restricted by Equation (29d), so that V cannot exceed the limit.Alternatively, this constraint can be imposed solely by the MPCC equations.However, this may lead to poor convergence properties of the system.Convergence is also enhanced in this case by squaring υ + and υ − in Equations (29g)-(29h), forcing the squared terms to converge more quickly so that υ 0 remains near the initial guess in the event that the system is at the volume limit.
To demonstrate the ability of Equation ( 29) to accurately represent a logic-dependent dynamic system, the set of equations with pre-specified inputs (Q in and Q out , which are shown in Figure 5.) is simulated for 10 minutes.The 10 minute time horizon is discretized into one minute intervals and solved using a simultaneous solution method.This is done using a DAE solution package known as APMonitor [40][41][42].This software package allows a user to define a model using both differential and algebraic equations [43,44].The software performs the collocation to convert the differential equations to algebraic equations and the problem is converted to a set of nonlinear algebraic equations.This, and subsequent case studies, use four collocation points in between the discretized time steps in the horizon.See [40] for more details on the APMonitor software.For the optimization example in Section 5, an NLP problem is solved.Because the system is still a continuous set of equations, APMonitor computes the gradient matrices with automatic differentiation, ensuring accuracy and fast solution times.The APOPT solver [45] (which is one of several optional solvers in APMonitor) uses a gradient based, active set optimization algorithm, as opposed to an interior point method such as Interior Point OPTimizer (IPOPT) [46] (which is also one of the optional solvers in APMonitor), and demonstrates good convergence as the problem is solved assuming some set of constraints to be active.This works well with inequality constraints such as Equations ( 29c)-(29d).
The results of the simulation are shown in Figures 5-8.As Figures 5 and 6 illustrate, the overflow (Q over ) remains at zero until the tank fills.Once the tank fills, the logical condition that Q over = 0 is nullified as δ hi = 1, allowing Q over to take on whatever positive value is needed to satisfy Equation (29a).The complementarity variables (Figure 8) are well behaved, with υ − equaling zero when the tank is at the high limit and υ 0 equaling zero when the tank is not at the high limit.The positive complementarity variable (υ + ) is always zero as the system is prevented from exceeding the high limit by Equation (29d).As seen in Figure 8, two of the three complementarity variables must be equal to zero for the formulation to return feasible results.As long as two of the variables are zero, the third variable can take on any positive value.
The results in Figures 5-8 illustrate that logic can be embedded into a dynamic system using only continuous algebraic equations to model the system.While convergence for the formulation in Equation ( 29) is obtained, there are many variations of the MPCC formulation, some of which do not display the same ability to converge consistently.When implementing similarly-formulated MPCCs, it may be necessary to explore various formulations to determine which will be the most robust for the application and choice of solver.

Power Flow System
The logical MPCC's performance is also tested in a more complicated simulation of a power flow system (shown in Figure 9) with a photovoltaic solar panel, a battery, a load (represented by a building), and the electric grid.The elements of many energy systems include a combination of multiple energy producers, cyclical energy demand, and energy storage [47][48][49][50][51][52].This system assumes simple dynamics for the battery Equation (30a).Energy balances are computed around the photovoltaic panel and the load in order to obtain Equations (30b)-(30c), respectively.A logic-based operating strategy is applied in order to specify the system's operation.Using this strategy, the maximum amount of solar power is delivered to the load by using the battery.When solar power available (q PV ) exceeds the demand (q load ), the battery (whose state of charge is represented by E batt and whose initial charge is represented by E • batt ) is charged.When the battery reaches the capacity (E max ), the excess power is delivered to the grid with flow q 3 .Conversely, when the battery is void of charge, power must be imported from the grid to the load with flow q 4 .This logic is specified in Equations (30d)-30e.The variables q 1 and q 2 represent the power delivered to and extracted from the battery, respectively: Conversion of the model to continuous form requires two sets of logical MPCC equations representing the logical decisions of Equations 30d-30e.This requires two sets of pseudo-binary (δ) and complementarity variables (υ), which are assigned the subscripts hi and lo, corresponding to the full Equation (30d) and empty Equation (30e) battery charge conditions, respectively.When converted to continuous logic form, Equation (30) becomes Equation ( 31): High limit MPCC equations corresponding to Equation (30e) Low limit MPCC equations corresponding to Equation (30e) The continuous logic formulation for the power flow system is demonstrated using a simulation with pre-determined q pv and q load over a 24-h time horizon, which is shown in Figure 10.Hourly time intervals are used in the simulation.As the figure shows, the supply (q pv ) and demand (q load ) do not perfectly coincide, with the available solar power peaking near midday and the demand peaking later in the afternoon, requiring the system to use battery energy storage in order to maximize the power delivered to the load from the solar panel.As Figures 11-13 illustrate, at the beginning of the day, there is no charge in the battery (indicated by δ lo = 1) and the demand exceeds the load, forcing power to be drawn from the grid.As the solar power picks up, the battery charges until it reaches the capacity (indicated by δ hi = 1).When this occurs, the logic dictates that the excess power be exported from the solar panel to the grid, indicated by the positive values for q 3 in Figure 11.At the end of the day, the solar power is diminished, the battery completely discharges, and power is again imported from the grid.The power flow example again demonstrates the value of using MPCCs to represent logical decisions in a DAE system.Embedding this logic in the form of continuous algebraic equations allows the system to be solved using the simultaneous method, which has been proven to significantly increase computational efficiency as compared to a sequential method.

Continuous Logic in an NMPC Problem
As a demonstration of the value of integrating logic into a simultaneous solution method, a nonlinear model predictive control (NMPC) problem is solved for a continuous stirred tank reactor (CSTR), which carries out the reaction: The objective of the controller is to regulate the concentration of component C (C C ) using the heat input to the reactor (q heat ) and the flow rate of component B (Q B ) as manipulated variables.The system is subject to disturbances in the flow of component A (Q A,in ) and is equipped with a surge tank to buffer out the effects of sudden increases in Q A .However, in the case that the volume of fluid in the surge tank exceeds the tank capacity, the surge tank will overflow and a sudden increase in the flow of A will enter the CSTR as shown in Figure 14.NMPC in this scenario can monitor the level in the surge tank (h) and the flow of A coming into the surge tank so that sudden disturbances due to surge tank overflow can be anticipated and accounted for pre-emptively by the controller.The model requires a built-in logical statement as in Equation ( 28) to represent the tank overflow condition.In the MPC problem, the outflow from the bottom of the surge tank (Q A,out ) is proportional to the square root of the height (h) in the tank Equation (34), with the dynamics of the tank represented by a simple material balance Equation (33).The model requires a built-in logical statement to represent the tank overflow condition Equation (35): The CSTR is assumed to be at constant volume so that the total inlet flow equals the flow out (Q out ) at all times Equation (36): The kinetics in the CSTR are first order in both A and B and the rate law Equation ( 37) has temperature dependence subject to the Arrhenius equation, where R A is the rate of reaction of component A, k 0 is the reaction rate constant, E A is the activation energy, R is the ideal gas constant, T is the temperature in the tank, C A and C B are the concentrations of component A and B, respectively: The CSTR temperature is determined by an energy balance on the tank Equation (38), where q heat is the rate that heat is delivered to the tank, V is the CSTR volume, ρ and C P are the density and the heat capacity, respectively of the fluid in the system, and the subscript 0 refers to the fluid before it enters the tank.The components A, B, and C are all assumed dilute so that their concentrations do not affect the density, heat capacity, or overall material balances of the solution.This assumption also permits neglecting heat of reaction in the energy balance: Material balances on each component are also computed, giving three more differential equations (see Equations ( 39)-( 41)), where C C is the concentration of component C: The MPC problem seeks to minimize deviations from the setpoint for C C subject to disturbances in Q A,in without making drastic control moves.To achieve this trade-off, a quadratic performance index is used where the squared deviations at the end of each time interval are weighted differently (10 for setpoint deviations and 1 for manipulated variable changes) and summed to create a performance index to be minimized.This yields the dynamic optimization problem in Equation (42), which is subject to the system model in Equations 33-41 and inequality constraints on the inputs: Subject to Equations ( 33)-( 41), (42a) A first order hold is used for the manipulated variables (MVs) where the value of these variables is held constant over each time interval.A total of N t time intervals are used in the model prediction.As Figure 14 shows, the controller checks the most recent state measurements (concentrations and temperature in the CSTR and fluid height in the surge tank) and disturbance measurements (flow of A) at each time step in order to update the model and ensure accurate future predictions.The model with built-in logic for surge tank overflow allows the controller to anticipate large influxes of flow and proactively account for this disturbance.
The optimization problem posed in Equation ( 42) is solved using both a sequential and a simultaneous solution method.In this problem, N t = 30 over 1 min time intervals with a control horizon equal to the prediction horizon of 50 min.With two MVs, the optimization problem has 100 degrees of freedom in total.The sequential method version of the problem uses an optimization solver (FMINCON) in MATLAB [53], which takes pre-determined values of the inputs, simulates the system using an explicit ODE integrator (ODE45), computes the objective function and uses this information to construct numeric approximations to the gradient matrices to compute a new search direction for the next iteration.The sequential method also uses if/then logic as in Equation (28b) to describe the changing dynamics of the surge tank.This methodology requires simulating through the entire time horizon of the system model thousands of times in order to generate the gradient matrices and iterate.
The simultaneous version of the problem is solved using APMonitor with the Heaviside function MPCC described in Equation ( 29), which, uses the orthogonal collocation scheme described in Section 4.1.This allows the problem to be expressed entirely as a set of algebraic equations and inequality constraints, which can be solved using NLP methods.The APOPT solver is again used to obtain a solution to this NLP problem.This method does not require multiple simulations of the system model as it solves the constraints of the system simultaneously subject to minimization of the objective function.As opposed to the sequential approach, the simultaneous method converges the equation residuals only once at the optimal solution.It should be noted that other methods do exist for approaching discontinuities in dynamic problems, such as a multiple shooting method, which is somewhat of a hybrid of the two methodologies shown here.The comparison in this NMPC problem is intended to demonstrate that the simultaneous solution method with MPECs to represent logical constraints in the system is a viable method for this particular problem by comparison to a purely sequential method with explicit if/then logic to represent disjunctions that arise over the course of the dynamic simulation.
The MPC problem is solved with the system initially at steady state with Q A,in = Q B,in = 0.5 m 3 /min and C C exactly on setpoint at 3 mol/m 3 .At time t = 0, however, a step change disturbance is introduced, changing Q A,in to 0.8 m 3 /min.The results from each solution method showing the controlled variable (CV) and the MVs are shown in Figure 15.As the figure shows, despite the introduction of a large disturbance, the CV is maintained very near the setpoint in each case.There is little difference in objective function values (1%) when comparing the two methods.The differences may be attributed to choice of solvers (APOPT for simultaneous versus FMINCON for sequential), automatic differentiation (simultaneous) versus finite differences (sequential) for gradients, or differences in discretization with collocation (simultaneous) versus an adaptive step integrator (sequential).For these reasons and others, the two methods arrive at slightly different solutions.Computationally, the sequential method converged with 51 iterations (in 883 sec) with 5, 292 model trajectory solutions that required 16, 393, 388 model derivative evaluations.The number of model intermediate solutions and computational time would significantly decrease if exact sensititivities were computed [54] versus the finite difference approach used in this study to obtain gradients.The simultaneous method completed with 87 iterations (in 5.2 sec) and had 46, 500 model residual evaluations and 17, 400 gradient evaluations.All computational times are with an Intel i7-2760QM CPU operating at 2.4 GHz with 8 GB RAM.
The profiles of some relevant state variables are shown in Figure 16 for the simultaneous solution method.As these plots indicate, the continuous logic formulation produces the desired switching behavior.As the surge tank reaches the overflow condition, the tank overflows but otherwise, Q A,over = 0.In this MPC application, it is invaluable to have the overflow condition represented in the model, as it allows the controller to anticipate large interruptions to the operation of the CSTR.While the disturbance it introduced at t = 0, the major impact is not observed until t = 18 min when the tank overflows.The model, however, allows for this change to be predicted and control moves to be made pre-emptively.As Figure 15 shows, more drastic control moves are made several minutes before the tank overflows.Predicting this occurrence with a logic-embedded model allows the system to effectively maintain the setpoint despite the large change in operating conditions.

Conclusions
This work demonstrates how logical expressions based on the Heaviside function can be used in NMPC and simulation while still taking advantage of the benefits of simultaneous solution methods.The equations, known as MPCCs, can be embedded into a DAE model using continuous algebraic equations.The MPCCs take advantage of complementarity conditions, requiring that an equality and an inequality constraint be active at all times.The major limitation of this MPCC formulation is its inherent ambiguity when x = 0.This inherent limitation makes the significance of this formulation reliant on solver convergence properties and subject to simultaneous solution method initialization challenges.Two simulation examples have been presented to demonstrate the viability of using MPCCs to represent these logical decisions.The examples, as presented, demonstrate rapid and accurate convergence, illustrating how a logical operating scheme can be simulated using an efficient simultaneous solution method.
In addition to simulation, an NMPC problem is also solved using the formulation developed in this work.The simultaneous solution method combined with the continuous logic formulation is compared to a sequential method using simple if/then logic.The results show that each of the the methods produce adequate solutions.The simultaneous method with continuous logic is faster in obtaining a solution, but a more sophisticated implementation of the sequential method would likely yield comparible solutions times.The continuous logic formulation allows implementation of logical statements into a model without having to use the less efficient sequential method for real-time NMPC or dynamic optimization calculations.The model including the dynamics and the logical statements are implemented as a continuous system of algebraic equations, which can be solved with efficient NLP solvers.
While the examples posed in this work demonstrate the potential of using MPCCs for logical decisions, this nascent topic requires much more research to be a viable method for solving optimization problems with such decisions.One of the key challenges to overcome is the non-convexity that is characteristic of many problems with logical decisions, which can cause issues with convergence due to hidden non-smoothness in the formulation or ill conditioning.Furthermore, future work on this formulation must include a study of the mathematical properties of logical MPCCs to provide a better understanding of how these problems are handled by various solvers and what can be done to further enhance performance.In particular, in the examples in this paper, the logical conditions are dependent on pre-determined inputs.Optimality is more difficult to obtain when the logical statements depend on the decision variables, with the optimizer typically finding a feasible solution and stopping.This issue is one that requires further understanding of how a solver deals with logic dependent on decision variables.This paper presents the concept of using MPCCs to represent logical decisions when using a simultaneous solution method so that this concept may be explored for other applications.

Figure 1 .
Figure1.A schematic illustrating the orthogonal collocation on finite elements discretization with a first-order hold assumed for inputs (u) in each element (k).The differential state variables (x) are approximated at each of the collocation points, denoted by i.The points are represented using different shapes and colors, which help distinguish one finite element from another.

Figure 2 .
Figure 2. A plot showing the convergence of the Heaviside function MPCC when x = 0.As the plot shows, δ converges to 1 as desired.

Figure 4 .
Figure 4.A schematic showing how the dynamic equations representing a simple tank change when the tank overflows.(a) is with no overflow; and (b) is when there is overflow.

Figure 5 .
Figure5.Flow rates in and out of the tank overflow system.Q in and Q out are the model inputs.Q over is a dependent variable, subject to the logical condition of the tank being at the overflow limit.

Figure 6 .
Figure 6.Tank volume with a high limit (V max ) of 10 m 3 .If the tank volume reaches this limit, overflow may ensue.

Figure 7 .
Figure 7.The pseudo-binary variable, δ hi , which is a continuous variable that takes on values of 1 (tank full) and 0 (tank empty) at the solution.

Figure 8 .
Figure 8. Complementarity variables used in the tank overflow system.

Figure 9 .
Figure 9. Schematic for the power flow example with photovoltaic panel, battery, electric grid, and a load (represented by the building) with the corresponding flows defined between these elements.

Figure 10 .
Figure 10.Inputs to the power flow model with q pv (the electric power flow entering the photovoltaic panel) and q load (the power demand of the building).

Figure 11 .
Figure 11.Flows in the power network illustrating the viability of the continuous logic MPCC formulation.

Figure 12 .
Figure 12.State of charge (kWh) of the battery with an upper limit of 2 kWh.

Figure 14 .
Figure 14.A schematic showing the MPC scheme of a CSTR and surge tank with overflow.

Figure 15 .
Figure 15.Results from the CSTR with surge tank nonlinear MPC problem showing the solution from the sequential method (blue solid line) with the simultaneous method (red dashed line), where C C is the controlled variable with a setpoint change from 3 to 4 mol/m 3 (a), Q B and q heat are manipulated variables subject to a zero-order hold.

Figure 16 .
Figure 16.Results of the CSTR MPC problem showing other differential and algebraic state variables with time including the compositions of A and B (a); height of fluid in the surge tank (b); and flow from the surge tank (c).