Slack Variable Tutorial
Main.SlackVariables History
Hide minor edits - Show changes to markup
Slack Variables

$$\begin{align}\min \quad & cost_{total}\\\mathrm{subject\;to} \quad & supply<b\\& cost_{total}=(supply-2)^2\end{align}$$
Gekko (Python) Solution
(:source lang=python:)
- Solve slack variable problem
- Minimize total_cost
- Subject to supply < b
from gekko import GEKKO
b = 5 m = GEKKO(remote=False) supply = m.Var() total_cost = m.Var()
m.Equation(supply<b) m.Equation(total_cost==(supply-2)**2) m.Minimize(total_cost)
m.solve()
print(supply.value[0]) print(total_cost.value[0]) (:sourceend:)
APMonitor Solution

Inequality Constraints in APM
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:)
Inequality Constraints
In Gekko Optimization Suite and the APMonitor Modeling Language, inequality constraints are automatically translated into equality constraints with slack variables. In APMonitor, 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 are 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.
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.
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:)<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.