### Conditional Statements in Optimization

Conditional statements can cause problems for gradient-based optimization algorithms because they are often posed in a form that gives discontinuous functions, first derivatives, or second derivatives. For example, the absolute value operator is a continuous function but gives a discontinuous first derivative at the origin.

Minimizing this function can cause a problem for solvers that rely on first and second derivative information to determine search directions at each iteration. As an exercise, solve the following model that uses the absolute value operator.

The solver can still converge because the solution is away from the discontinuity. In Example 2, the value of *x* is now determined by the optimizer. The objective function to minimize (y+3)^{2} means that the optimal solution is at *x=0* and *y=0*. However, when the solution is at the discontinuity, a gradient based solver will often fail to converge.

In this case, the IPOPT solver reaches the default maximum number of iterations (100) and reports a failure to converge. Now if the problem is reformulated with an additional variable *intb*, the optimizer can find a solution to the same problem that failed with the abs(x) operator. The value of *intb* is a binary variable that can be either *0* or *1*. The value of *intb* is zero when *x<0* and is one when *x>=0*. The following two examples show that this reformulation allows the solver to quickly find a solution either away from or at the discontinuity.

- Example 3: Absolute Value away from the Discontinuity
- Example 4: Absolute Value at the Discontinuity

The above examples demonstrate this concept with the absolute value operator. The same techniques can be applied to:

- If...Else Statements
- Max Function
- Min Function
- Piece-wise Linear Functions
- Signum Function (What is Signum?)
- Other discontinuous functions

Two popular methods for reformulation of the conditional statements is to either use:

- Binary or integer variables
- Mathematical Programs with Equilibrium Constraints (MPECs)

Each of these techniques are described below with a number of examples.

#### Logical Conditions with Binary Variables

The approach taken above with the ABS function used a single binary variable.

The following examples demonstrate the use of conditional statements to switch between different functions. In this case, the function *y=6*exp(-x)+1* if the value of *x<3* else the function *y=x* is chosen when *x>=3*. The simulate example verifies that the correct value is reported with a fixed value of *x*. The second example shows how *x* can be adjusted to find a minimum value of *y* or *f(x)*.

- IF: Conditional Statement with Two Functions - Simulate (Integer Form)
- IF: Conditional Statement with Two Functions - Optimize (Integer Form)

With three functions an if...elseif...else structure may be used. The following two examples show how to implement these conditional statements to achieve continuous first and second derivatives.

- IF: Conditional Statement with Three Functions - Simulate (Integer Form)
- IF: Conditional Statement with Three Functions - Optimize (Integer Form)

Piece-wise Linear (PWL) functions are one method to approximate any nonlinear function. The following example programs demonstrate a piece-wise linear function with binary decision variables. The sum of the binary decision variables is required to be one, meaning that only one of the linear approximations can be active at a time.

The value of *x* is now declared as a decision variable and is adjusted by the optimizer to minimize the objective function that is equal to *y*. By inspection, the optimal value is at *x=2* giving a result of *y=0*.

The example shown above demonstrates a PWL function with one input and one output but it can also be extended to cases with multiple inputs and multiple outputs.

#### MPECs: Mathematical Programs with Equilibrium Constraints

Mathematical Programs with Equilibrium Constraints (MPECs) are formulations that can also be used to model certain classes of discrete events. MPECs can be more efficient than solving mixed integer formulations of the optimization problems because it avoids the combinatorial difficulties of searching for optimal discrete variables.

- ABS: Absolute Value Operator (MPEC Form)
- MAX: Maximum Operator (MPEC Form)
- MIN: Minimum Operator (MPEC Form)
- Piecewise Linear Function without Object Use (MPEC Form)
- Piecewise Linear Function with PWL Object (MPEC Form)
- Piecewise Linear Function with LOOKUP and PWL Objects (MPEC Form)
- SIGN: Signum Operator (MPEC Form)

comments powered by Disqus