## Slack Variable Tutorial

## Main.SlackVariables History

Show minor edits - Show changes to markup

In APMonitor Modeling Language, inequality constraints are automatically translated into equality constraints with slack variables. Slack variables can also be defined by starting a variable name with *slk*. When the model is parsed at run-time, any variable beginning with *slk* is automatically assigned a lower value of zero. Alternatively, inequality constraints will be automatically converted to equality constraints with a slack variable.

In APMonitor Modeling Language, inequality constraints are automatically translated into equality constraints with slack variables. Slack variables can also be defined by starting a variable name with *slk*. When the model is parsed at run-time, any variable beginning with *slk* is automatically assigned a lower value of zero. Alternatively, inequality constraints will be automatically converted to equality constraints with a slack variable.

### Logical Conditions in Optimization

Slack variables are also useful for optimization problems with non-smooth or discontinuous functions. The following tutorial demonstrates the use of slack variables in reformulating the ABS() function, IF statements, and others.

(:html:) <iframe width="560" height="315" src="https://www.youtube.com/embed/NPUNixLGlh0" frameborder="0" allowfullscreen></iframe> (:htmlend:)

(:html:)<br><br><br>(:htmlend:)

<br><br>

(:table border=1 width=80% align=left bgcolor=#FFFFFF cellspacing=0:)

(:table border=1 width=100% align=left bgcolor=#FFFFFF cellspacing=0:)

(:html:)<br><br><br><br><br>(:htmlend:)

(:table border=1 width=100% align=left bgcolor=#EEEEEE cellspacing=0:) (:cellnr:)

! Example model with slack variable ! Solve slack variable problem ! Minimize total_cost ! Subject to supply < b Model Parameters b = 5 End Parameters Variables supply total_cost End Variables Equations supply < b total_cost = (supply-2)^2 Minimize total_cost End Equations End Model

(:cellnr:)

(:tableend:)

## Example Problem

### Example Problem

## Inequality Constraints in APM

### Inequality Constraints in APM

(:table border=1 width=50% align=left bgcolor=#EEEEEE cellspacing=0:)

(:table border=1 width=100% align=left bgcolor=#EEEEEE cellspacing=0:)

(:html:)<br>(:htmlend:)

### Example Problem

## Example Problem

## Inequality Constraints in APM

In APMonitor, inequality constraints are automatically translated into equality constraints with slack variables. Slack variables can also be defined by starting a variable name with *slk*. When the model is parsed at run-time, any variable beginning with *slk* is automatically assigned a lower value of zero. Alternatively, inequality constraints will be automatically converted to equality constraints with a slack variable.

(:tableend:)

(:tableend:)

In APMonitor Modeling Language, inequality constraints are automatically translated into equality constraints with slack variables. Slack variables can also be defined by starting a variable name with *slk*. When the model is parsed at run-time, any variable beginning with *slk* is automatically assigned a lower value of zero. Alternatively, inequality constraints will be automatically converted to equality constraints with a slack variable.

Slack variables are defined to transform an inequality expression into an equality expression with an added slack variable. The slack variable is defined by setting a lower bound of zero (>=0).

Slack variables are defined to transform an inequality expression into an equality expression with an added slack variable. The slack variable is defined by setting a lower bound of zero (>0).

slack >= 0

slack > 0

In APMonitor, slack variables can be defined by starting a variable name with *slk*. When the model is parsed at run-time, any variable beginning with *slk* is automatically assigned a lower value of zero. Alternatively, inequality constraints will be automatically converted to equality constraints with a slack variable.

(:html:) <iframe width="560" height="315" src="https://www.youtube.com/embed/jh6BK0BqqIs?rel=0" frameborder="0" allowfullscreen></iframe> (:htmlend:)

In APMonitor, inequality constraints are automatically translated into equality constraints with slack variables. Slack variables can also be defined by starting a variable name with *slk*. When the model is parsed at run-time, any variable beginning with *slk* is automatically assigned a lower value of zero. Alternatively, inequality constraints will be automatically converted to equality constraints with a slack variable.

(:tableend:)

(:tableend:)

(:title Slack Variable Tutorial:) (:keywords slack variables, inequality, slacks, lower bound, transform:) (:description Slack variables are used to transform an inequality expression into an equality expression:)

### Solve Slack Variable Problems Online

APMonitor has an online interface for solving optimization problems. The optimization problem shown below as **Example Problem** can be solved through the web-interface at the following link:

x = b + slack

x = b + slack (:html:)<br>(:htmlend:)

(:table border=2 width=50% align=left bgcolor=#FFFFFF cellspacing=0:)

(:table border=1 width=80% align=left bgcolor=#FFFFFF cellspacing=0:)

In APMonitor, slack variables can be defined by starting a variable name with **slk**. When the model is parsed at run-time, any variable beginning with *slk* is automatically assigned a lower value of zero. Alternatively, inequality constraints will be automatically converted to equality constraints with a slack variable.

In APMonitor, slack variables can be defined by starting a variable name with *slk*. When the model is parsed at run-time, any variable beginning with *slk* is automatically assigned a lower value of zero. Alternatively, inequality constraints will be automatically converted to equality constraints with a slack variable.

In APMonitor, slack variables can be defined by starting a variable name with **'slk**'. When the model is parsed at run-time, any variable beginning with *slk* is automatically assigned a lower value of zero. Alternatively, inequality constraints will be automatically converted to equality constraints with a slack variable.

**slk**. When the model is parsed at run-time, any variable beginning with *slk* is automatically assigned a lower value of zero. Alternatively, inequality constraints will be automatically converted to equality constraints with a slack variable.

(:html:)<br><br><br><br><br><br>(:htmlend:)

(:html:)<br><br><br><br><br>(:htmlend:)

(:html:)<br><br><br><br>(:htmlend:)

(:html:)<br><br><br><br><br><br>(:htmlend:)

(:html:)<br>(:htmlend:)

(:html:)<br><br><br><br>(:htmlend:)

(:table border=1 width=50% align=left bgcolor=#FFFFFF cellspacing=0:)

(:table border=2 width=50% align=left bgcolor=#FFFFFF cellspacing=0:)

(:table border=1 width=50% align=left bgcolor=#EEEEEE cellspacing=0:)

(:table border=1 width=50% align=left bgcolor=#FFFFFF cellspacing=0:)

(:html:)<br>(:htmlend:)

Slack variables are defined by starting a variable name with **'slk**'. Thus, valid slack variable names would include names such as *slk_1*, *slkr*, *slkvar[1]*. When the model is parsed at run-time, any variable beginning with *slk* is automatically assigned a lower value of zero.

Equation inequalities are automatically tranformed to equality expressions for solvers that do not support them.

(:table border=1 width=50% align=left bgcolor=#EEEEEE cellspacing=0:) (:cellnr:) Inequality Constraint Form (:cell:) x > b (:cellnr:) Equality Constraint Form with Slack Variable (:cell:) x = b + slack slack >= 0 (:tableend:)

In APMonitor, slack variables can be defined by starting a variable name with **'slk**'. When the model is parsed at run-time, any variable beginning with *slk* is automatically assigned a lower value of zero. Alternatively, inequality constraints will be automatically converted to equality constraints with a slack variable.

! Example model with slack variable declaration

! Example model with slack variable

Model example Parameters p1 = 5 End Parameters Variables slk_variable v1 < 4 End Variables Equations v1 = p1 - slk_variable End Equations

! Solve slack variable problem ! Minimize total_cost ! Subject to supply < b Model Parameters b = 5 End Parameters Variables supply total_cost End Variables Equations supply < b total_cost = (supply-2)^2 Minimize total_cost End Equations

Solution p1 = 5 slk_variable = 1 v1 = 4

Slack variables are defined to transform a inequality expression into an equality expression with an added slack variable. The slack variable is defined by setting a lower bound of zero (>=0).

Slack variables are defined to transform an inequality expression into an equality expression with an added slack variable. The slack variable is defined by setting a lower bound of zero (>=0).

APMonitor has an online interface for solving optimization problems. The optimization problem shown below can be solved through the web-interface at the following link:

APMonitor has an online interface for solving optimization problems. The optimization problem shown below as **Example Problem** can be solved through the web-interface at the following link:

### Example

### Example Problem

Attach: table50.jpg Slack Variable Solution Online

Slack Variable Solution Online

Attach: table50.jpg Slack Variable Solution Online

APMonitor has an online interface for solving optimization problems. The optimization problem shown below can be solved the web-interface at the following link:

APMonitor has an online interface for solving optimization problems. The optimization problem shown below can be solved through the web-interface at the following link:

### Solve Slack Variable Problems Online

APMonitor has an online interface for solving optimization problems. The optimization problem shown below can be solved the web-interface at the following link:

Slack Variable Solution Online

Slack variables are defined to give *slack* for infeasibilities. Generally, a slack variable is used to turn off an equation in a region where it does not apply. The slack variable is defined by setting a lower bound of zero (>=0) and including the variable in the objective function.

Slack variables are defined by starting a variable name with **'slk**'. Thus, valid slack variable names would include names such as *slk_1*, *slkr*, *slkvar[1]*. When the model is parsed at run-time, any variable beginning with *slk* is automatically assigned a lower value of zero and included in the objective function.

Slack variables are defined to transform a inequality expression into an equality expression with an added slack variable. The slack variable is defined by setting a lower bound of zero (>=0).

Slack variables are defined by starting a variable name with **'slk**'. Thus, valid slack variable names would include names such as *slk_1*, *slkr*, *slkvar[1]*. When the model is parsed at run-time, any variable beginning with *slk* is automatically assigned a lower value of zero.

Equation inequalities are automatically tranformed to equality expressions for solvers that do not support them.

## Slack Variables

Slack variables are defined to give *slack* for infeasibilities. Generally, a slack variable is used to turn off an equation in a region where it does not apply. The slack variable is defined by setting a lower bound of zero (>=0) and including the variable in the objective function.

Slack variables are defined by starting a variable name with **'slk**'. Thus, valid slack variable names would include names such as *slk_1*, *slkr*, *slkvar[1]*. When the model is parsed at run-time, any variable beginning with *slk* is automatically assigned a lower value of zero and included in the objective function.

### Example

(:table border=1 width=50% align=left bgcolor=#EEEEEE cellspacing=0:) (:cellnr:)

! Example model with slack variable declaration Model example Parameters p1 = 5 End Parameters Variables slk_variable v1 < 4 End Variables Equations v1 = p1 - slk_variable End Equations End Model

(:cellnr:)

Solution p1 = 5 slk_variable = 1 v1 = 4

(:tableend:)