Main
~~* [[http://apmonitor.com/online/view_pass.php?f=abs4.apm|ABS: Absolute Value Operator (Integer Form)]]~~

~~Attach:if_else_2_~~functions.~~png~~

~~!~~!!! Conditional Statements in Optimization

## Logical Conditions in Optimization

## Main.LogicalConditions History

Hide minor edits - Show changes to output

Changed lines 33-34 from:

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

to:

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

Changed lines 38-39 from:

Each of these techniques are described below with a number of examples~~. The approach taken above with the ABS function used a single binary variable. The ABS function can also be expressed as an MPEC as shown below~~.

to:

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

Deleted lines 41-42:

Added lines 44-49:

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

* [[http://apmonitor.com/online/view_pass.php?f=abs4.apm|ABS: Absolute Value Operator (Integer Form)]]

----

* [[http://apmonitor.com/online/view_pass.php?f=abs4.apm|ABS: Absolute Value Operator (Integer Form)]]

----

Added lines 68-81:

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.

* [[http://apmonitor.com/online/view_pass.php?f=pwl3.apm|Simulate Piecewise Linear Function with Binary Decision Variables]]

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''.

* [[http://apmonitor.com/online/view_pass.php?f=pwl4.apm|Optimize Piecewise Linear Function with Binary Decision Variables]]

Attach:pwl_function.png

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.

----

* [[http://apmonitor.com/online/view_pass.php?f=pwl3.apm|Simulate Piecewise Linear Function with Binary Decision Variables]]

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''.

* [[http://apmonitor.com/online/view_pass.php?f=pwl4.apm|Optimize Piecewise Linear Function with Binary Decision Variables]]

Attach:pwl_function.png

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.

----

Added lines 89-91:

* [[http://apmonitor.com/online/view_pass.php?f=pwl0.apm|Piecewise Linear Function without Object Use (MPEC Form)]]

* [[http://apmonitor.com/online/view_pass.php?f=pwl1.apm|Piecewise Linear Function with PWL Object (MPEC Form)]]

* [[http://apmonitor.com/online/view_pass.php?f=pwl2.apm|Piecewise Linear Function with LOOKUP and PWL Objects (MPEC Form)]]

* [[http://apmonitor.com/online/view_pass.php?f=pwl1.apm|Piecewise Linear Function with PWL Object (MPEC Form)]]

* [[http://apmonitor.com/online/view_pass.php?f=pwl2.apm|Piecewise Linear Function with LOOKUP and PWL Objects (MPEC Form)]]

Changed lines 44-45 from:

to:

----

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)''.

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)''.

Changed lines 51-52 from:

Attach:if_~~elseif_~~else_~~3~~_functions.png

to:

Attach:if_else_2_functions.png

----

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.

----

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.

Added lines 60-62:

Attach:if_elseif_else_3_functions.png

----

----

Changed lines 15-16 from:

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.

to:

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.

Changed lines 40-42 from:

!!!! Logical Conditions with ~~Integer~~ Variables

to:

!!!! Logical Conditions with Binary Variables

* [[http://apmonitor.com/online/view_pass.php?f=abs4.apm|ABS: Absolute Value Operator (Integer Form)]]

Attach:if_else_2_functions.png

* [[http://apmonitor.com/online/view_pass.php?f=if1a.apm|IF: Conditional Statement with Two Functions - Simulate (Integer Form)]]

* [[http://apmonitor.com/online/view_pass.php?f=if1b.apm|IF: Conditional Statement with Two Functions - Optimize (Integer Form)]]

Attach:if_elseif_else_3_functions.png

* [[http://apmonitor.com/online/view_pass.php?f=if2a.apm|IF: Conditional Statement with Three Functions - Simulate (Integer Form)]]

* [[http://apmonitor.com/online/view_pass.php?f=if2b.apm|IF: Conditional Statement with Three Functions - Optimize (Integer Form)]]

* [[http://apmonitor.com/online/view_pass.php?f=abs4.apm|ABS: Absolute Value Operator (Integer Form)]]

Attach:if_else_2_functions.png

* [[http://apmonitor.com/online/view_pass.php?f=if1a.apm|IF: Conditional Statement with Two Functions - Simulate (Integer Form)]]

* [[http://apmonitor.com/online/view_pass.php?f=if1b.apm|IF: Conditional Statement with Two Functions - Optimize (Integer Form)]]

Attach:if_elseif_else_3_functions.png

* [[http://apmonitor.com/online/view_pass.php?f=if2a.apm|IF: Conditional Statement with Three Functions - Simulate (Integer Form)]]

* [[http://apmonitor.com/online/view_pass.php?f=if2b.apm|IF: Conditional Statement with Three Functions - Optimize (Integer Form)]]

Changed line 30 from:

* [[http://en.wikipedia.org/wiki/Signum_function|~~Signum Function~~]]

to:

* Signum Function ([[http://en.wikipedia.org/wiki/Signum_function|What is Signum?]])

Changed lines 5-6 from:

to:

!!! Conditional Statements in Optimization

Changed lines 13-14 from:

* [[http://apmonitor.com/online/view_pass.php?f=abs1.apm|Example 1: Absolute Value Operator]]

to:

* [[http://apmonitor.com/online/view_pass.php?f=abs1.apm|Example 1: Absolute Value Operator Away from the Discontinuity]]

Changed lines 17-22 from:

* [[http://apmonitor.com/online/view_pass.php?f=~~abs1~~.apm|Example 2: Absolute Value Operator ~~in Optimization~~]]

to:

* [[http://apmonitor.com/online/view_pass.php?f=abs2.apm|Example 2: Absolute Value Operator at the Discontinuity]]

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.

* [[http://apmonitor.com/online/view_pass.php?f=abs3.apm|Example 3: Absolute Value away from the Discontinuity]]

* [[http://apmonitor.com/online/view_pass.php?f=abs4.apm|Example 4: Absolute Value at the Discontinuity]]

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.

* [[http://apmonitor.com/online/view_pass.php?f=abs3.apm|Example 3: Absolute Value away from the Discontinuity]]

* [[http://apmonitor.com/online/view_pass.php?f=abs4.apm|Example 4: Absolute Value at the Discontinuity]]

Added line 25:

Changed lines 29-62 from:

* ~~Sign Operator~~

* Most other discontinuous functions

!!!! MPEC: Mathematical Programs with Equilibrium Constraints

Mathematical Programs with Equilibrium Constraints (MPECs) are formulations that can 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~~.

----

!!! SIGN Operator (MPEC Form)

* %list list-page% [[Attach:sign.apm | SIGN Operator Example]]

----

!!! Absolute Value (ABS) Operator

* %list list-page% [[Attach:~~abs.apm | ABS Operator Example]]~~

----

!!! Minimum Selector (MIN) Operator

* %list list-page% [[Attach:min.apm | MIN Operator Example]]

----

!!! Maximum Selector (MAX) Operator

* %list list-page% [[Attach:max.apm | MAX Operator Example]]

* Most other discontinuous

!!!! MPEC: Mathematical Programs with Equilibrium Constraints

Mathematical Programs with Equilibrium Constraints (MPECs) are formulations that can be used to model certain classes of discrete events.

----

!!! SIGN Operator (MPEC Form)

* %list list-page% [[Attach:sign.apm | SIGN Operator Example]]

----

!!! Absolute Value (ABS) Operator

* %list list-page% [[Attach

----

!!! Minimum Selector (MIN) Operator

* %list list-page% [[Attach:min.apm | MIN Operator Example]]

----

!!! Maximum Selector (MAX) Operator

* %list list-page% [[Attach:max.apm | MAX Operator Example

to:

* Piece-wise Linear Functions

* [[http://en.wikipedia.org/wiki/Signum_function|Signum Function]]

* Other discontinuous functions

Two popular methods for reformulation of the conditional statements or operators 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. The approach taken above with the ABS function used a single binary variable. The ABS function can also be expressed as an MPEC as shown below.

!!!! Logical Conditions with Integer Variables

!!!! 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.

* [[http://apmonitor.com/online/view_pass.php?f=abs_mpec.apm|ABS: Absolute Value Operator (MPEC Form)]]

* [[http://apmonitor.com/online/view_pass.php?f=max_mpec.apm|MAX: Maximum Operator (MPEC Form)]]

* [[http://apmonitor.com/online/view_pass.php?f=min_mpec.apm|MIN: Minimum Operator (MPEC Form)]]

* [[http://apmonitor.com/online/view_pass.php?f=sign_mpec.apm|SIGN: Signum Operator (MPEC Form)]]

* [[http://en.wikipedia.org/wiki/Signum_function|Signum Function]]

* Other discontinuous functions

Two popular methods for reformulation of the conditional statements or operators 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. The approach taken above with the ABS function used a single binary variable. The ABS function can also be expressed as an MPEC as shown below.

!!!! Logical Conditions with Integer Variables

!!!! 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.

* [[http://apmonitor.com/online/view_pass.php?f=abs_mpec.apm|ABS: Absolute Value Operator (MPEC Form)]]

* [[http://apmonitor.com/online/view_pass.php?f=max_mpec.apm|MAX: Maximum Operator (MPEC Form)]]

* [[http://apmonitor.com/online/view_pass.php?f=min_mpec.apm|MIN: Minimum Operator (MPEC Form)]]

* [[http://apmonitor.com/online/view_pass.php?f=sign_mpec.apm|SIGN: Signum Operator (MPEC Form)]]

Changed lines 11-30 from:

Minimizing this function can cause a problem for solvers that rely on first and second derivative information to determine search directions ~~for~~ each iteration.

to:

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.

* [[http://apmonitor.com/online/view_pass.php?f=abs1.apm|Example 1: 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.

* [[http://apmonitor.com/online/view_pass.php?f=abs1.apm|Example 2: Absolute Value Operator in Optimization]]

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

* Sign Operator

* Most other discontinuous functions

* [[http://apmonitor.com/online/view_pass.php?f=abs1.apm|Example 1: 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.

* [[http://apmonitor.com/online/view_pass.php?f=abs1.apm|Example 2: Absolute Value Operator in Optimization]]

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

* Sign Operator

* Most other discontinuous functions

Added lines 1-60:

(:title Logical Conditions in Optimization:)

(:keywords if conditions, maximum, minimum, max operator, min operator, sign operator, APMonitor, summation:)

(:description Formulate conditional statements in numerical optimization so that the problem can be solved with gradient based approaches.:)

!!!! 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.

Attach:absolute_value_1st_deriv.png

Minimizing this function can cause a problem for solvers that rely on first and second derivative information to determine search directions for each iteration.

!!!! MPEC: Mathematical Programs with Equilibrium Constraints

Mathematical Programs with Equilibrium Constraints (MPECs) are formulations that can 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.

----

!!! SIGN Operator (MPEC Form)

* %list list-page% [[Attach:sign.apm | SIGN Operator Example]]

----

!!! Absolute Value (ABS) Operator

* %list list-page% [[Attach:abs.apm | ABS Operator Example]]

----

!!! Minimum Selector (MIN) Operator

* %list list-page% [[Attach:min.apm | MIN Operator Example]]

----

!!! Maximum Selector (MAX) Operator

* %list list-page% [[Attach:max.apm | MAX Operator Example]]

----

(:html:)

<div id="disqus_thread"></div>

<script type="text/javascript">

/* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */

var disqus_shortname = 'apmonitor'; // required: replace example with your forum shortname

/* * * DON'T EDIT BELOW THIS LINE * * */

(function() {

var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;

dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js';

(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);

})();

</script>

<noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>

<a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>

(:htmlend:)

(:keywords if conditions, maximum, minimum, max operator, min operator, sign operator, APMonitor, summation:)

(:description Formulate conditional statements in numerical optimization so that the problem can be solved with gradient based approaches.:)

!!!! 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.

Attach:absolute_value_1st_deriv.png

Minimizing this function can cause a problem for solvers that rely on first and second derivative information to determine search directions for each iteration.

!!!! MPEC: Mathematical Programs with Equilibrium Constraints

Mathematical Programs with Equilibrium Constraints (MPECs) are formulations that can 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.

----

!!! SIGN Operator (MPEC Form)

* %list list-page% [[Attach:sign.apm | SIGN Operator Example]]

----

!!! Absolute Value (ABS) Operator

* %list list-page% [[Attach:abs.apm | ABS Operator Example]]

----

!!! Minimum Selector (MIN) Operator

* %list list-page% [[Attach:min.apm | MIN Operator Example]]

----

!!! Maximum Selector (MAX) Operator

* %list list-page% [[Attach:max.apm | MAX Operator Example]]

----

(:html:)

<div id="disqus_thread"></div>

<script type="text/javascript">

/* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */

var disqus_shortname = 'apmonitor'; // required: replace example with your forum shortname

/* * * DON'T EDIT BELOW THIS LINE * * */

(function() {

var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;

dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js';

(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);

})();

</script>

<noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>

<a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>

(:htmlend:)