Integer Linear Programming
Main.IntegerProgramming History
Hide minor edits - Show changes to output
Added lines 55-64:
---------------------------------------------------
Solver : APOPT (v1.0)
Solution time : 0.0852 sec
Objective : -2.
Successful solution
---------------------------------------------------
Objective: 2.0
x: 2.0
y: 2.0
Changed line 7 from:
{$ \begin{align} & \text{maximize} && \mathbf{c}^\mathrm{T} \mathbf{x}\\ & \text{subject to} && A \mathbf{x} \le \mathbf{b}, \\ & && \mathbf{x} \ge \mathbf{0}, \\ &&& \mathbf{x} \in \mathbb{Z}^n \end{align} $}
to:
{$ \begin{align} & \text{maximize} && \mathbf{c}^\mathrm{T} \mathbf{x}\\ & \text{subject to} && A \mathbf{x} \le \mathbf{b} \\ & && \mathbf{x} \ge \mathbf{0} \\ &&& \mathbf{x} \in \mathbb{Z}^n \end{align} $}
Changed line 80 from:
For large-scale problems, it is more efficient to solve the problem in sparse matrix form. The matrix arguments are passed to Gekko in [[https://en.wikipedia.org/wiki/Sparse_matrix#Coordinate_list_(COO)|coordinate (COO) list]] form. COO list form is ''[[row indices],[values]]'' for a vector and ''[[row indices],[column indices],[values]]'' for a matrix.
to:
For large-scale problems, it is more efficient to solve the problem in sparse matrix form. The matrix arguments are passed to Gekko in [[https://en.wikipedia.org/wiki/Sparse_matrix#Coordinate_list_(COO)|coordinate (COO) list]] form. COO list form is ''[row indices],[values]'' for a vector and ''[row indices],[column indices],[values]'' for a matrix.
Changed lines 80-81 from:
For large-scale problems, it is more efficient to solve the problem in sparse matrix form. The matrix arguments are passed to Gekko in [[https://en.wikipedia.org/wiki/Sparse_matrix#Coordinate_list_(COO)|coordinate (COO) list]] form.
to:
For large-scale problems, it is more efficient to solve the problem in sparse matrix form. The matrix arguments are passed to Gekko in [[https://en.wikipedia.org/wiki/Sparse_matrix#Coordinate_list_(COO)|coordinate (COO) list]] form. COO list form is ''[[row indices],[values]]'' for a vector and ''[[row indices],[column indices],[values]]'' for a matrix.
Deleted line 85:
Changed lines 39-40 from:
Gekko is a Python API to APMonitor.
to:
Gekko is a Python API to APMonitor. The same ILP problem is solved with Gekko.
Changed lines 42-53 from:
to:
from gekko import GEKKO
m = GEKKO()
x,y = m.Array(m.Var,2,integer=True,lb=0)
m.Maximize(y)
m.Equations([-x+y<=1,
3*x+2*y<=12,
2*x+3*y<=12])
m.options.SOLVER = 1
m.solve()
print('Objective: ', -m.options.OBJFCNVAL)
print('x: ', x.value[0])
print('y: ', y.value[0])
m = GEKKO()
x,y = m.Array(m.Var,2,integer=True,lb=0)
m.Maximize(y)
m.Equations([-x+y<=1,
3*x+2*y<=12,
2*x+3*y<=12])
m.options.SOLVER = 1
m.solve()
print('Objective: ', -m.options.OBJFCNVAL)
print('x: ', x.value[0])
print('y: ', y.value[0])
Changed lines 56-65 from:
Nonlinear programming solvers (such as IPOPT) may not return an integer solution because they are designed for continuous variables. Mixed Integer Nonlinear Programming solvers (such as APOPT) are equipped to solve for binary or integer variables. Select the appropriate [[https://apmonitor.com/wiki/index.php/Main/OptionApmSolver|solver option]] to either find an initial solution without integer variables or an integer solution. It is sometimes desirable to find a non-integer solution first because of the often significant reduction in computation time without the integer variables.
!!!! Discrete Variables
Discrete decision variables are those that have only certain levels or quantities that are acceptable at an optimal solution. Examples of discrete variables are binary (e.g. off/on or 0/1), integer (e.g. 4,5,6,7), or general discrete values that are not integer (e.g. 1/4 cm, 1/2 cm, 1 cm).
'''Python Gekko'''
Integer variable '''x1''' and [[https://en.wikipedia.org/wiki/Special_ordered_set|Special Ordered Set]] '''x2''' variables are solved with [[https://gekko.readthedocs.io/en/latest/|Python GEKKO]]. The '''sos1''' Gekko function is used to create the SOS1 variable.
Discrete decision variables are those that have only certain levels or quantities that are acceptable at an optimal solution. Examples of discrete variables are binary (e.g. off/on or 0/1), integer (e.g. 4,5,6,7), or general discrete values that are not integer (e.g. 1/4 cm, 1/2 cm, 1 cm).
'''Python Gekko'''
Integer variable '''x1''' and [[https://en.wikipedia.org/wiki/Special_ordered_set|Special Ordered Set]] '''x2''' variables are solved with [[https://gekko.readthedocs.io/en/latest/|Python GEKKO]]. The '''sos1''' Gekko function is used to create the SOS1 variable
to:
Nonlinear programming solvers (such as IPOPT) may not return an integer solution because they are designed for continuous variables. Mixed Integer Nonlinear Programming solvers (such as APOPT) are equipped to solve for binary or integer variables. It is selected with ''m.options.SOLVER=1''. Select the appropriate [[https://apmonitor.com/wiki/index.php/Main/OptionApmSolver|solver option]] to either find an initial solution without integer variables or an integer solution. It is sometimes desirable to find a non-integer solution first because of the often significant reduction in computation time without the integer variables.
'''Solution in Matrix Form'''
Another representation is matrix form. Gekko function ''qobj'' defines a linear or quadratic objective and ''axb'' defines the {`Ax\leb`} constraint.
'''Solution in Matrix Form'''
Another representation is matrix form. Gekko function ''qobj'' defines a linear or quadratic objective and ''axb'' defines the {`Ax\leb`} constraint.
Changed lines 64-69 from:
m = GEKKO() # create GEKKO model
x1 = m.Var(integer=True,lb=-5,ub=10)
# create Special Ordered Set variable
x2 =m.sos1([0.5, 1.15, 2.6, 5.2])
m.Obj(4*x1**2-4*x2*x1**2+x2**2+x1**2-x1+1)
m.options.SOLVER= 1 # APOPT solver
x1 = m.Var(integer=True
x2 =
m.Obj
m.options.SOLVER
to:
m = GEKKO(remote=False)
c = [0,1]
A = [[-1,1],[3,2],[2,3]]
b = [1,12,12]
z = m.Array(m.Var,2,integer=True,lb=0)
m.qobj(c,x=z,otype='max')
m.axb(A,b,x=z,etype='<=')
m.options.SOLVER = 1
c = [0,1]
A = [[-1,1],[3,2],[2,3]]
b = [1,12,12]
z = m.Array(m.Var,2,integer=True,lb=0)
m.qobj(c,x=z,otype='max')
m.axb(A,b,x=z,etype='<=')
m.options.SOLVER = 1
Changed lines 73-74 from:
print('x1: ' + str(x1.value[0]))
print('x2: ' + str(x2.value[0]))
print('
to:
print('Objective: ', -m.options.OBJFCNVAL)
print('x: ', z[0].value[0])
print('y: ', z[1].value[0])
print('x: ', z[0].value[0])
print('y: ', z[1].value[0])
Changed lines 78-104 from:
to:
'''Solution in Sparse Matrix Form'''
For large-scale problems, it is more efficient to solve the problem in sparse matrix form. The matrix arguments are passed to Gekko in [[https://en.wikipedia.org/wiki/Sparse_matrix#Coordinate_list_(COO)|coordinate (COO) list]] form.
(:source lang=python:)
from gekko import GEKKO
m = GEKKO(remote=False)
c = [[2],[1]]
# [[row indices],[column indices],[values]]
A = [[1,1,2,2,3,3],[1,2,1,2,1,2],[-1,1,3,2,2,3]]
b = [[1,2,3],[1,12,12]]
z = m.Array(m.Var,2,integer=True,lb=0)
m.qobj(c,x=z,otype='max',sparse=True)
m.axb(A,b,x=z,etype='<=',sparse=True)
m.options.SOLVER = 1
m.solve()
print('Objective: ', -m.options.OBJFCNVAL)
print('x: ', z[0].value[0])
print('y: ', z[1].value[0])
(:sourceend:)
'''Additional Resources'''
* [[https://apmonitor.com/do/index.php/Main/DiscreteVariables|Dynamic Optimization with Discrete Variables]]
* [[https://apmonitor.com/me575/index.php/Main/DiscreteOptimization|Discrete Optimization]]
* [[https://apmonitor.com/pdc/index.php/Main/LinearProgramming|Linear Programming]]
* [[Main/IntegerBinaryVariables|Mixed Integer Nonlinear Programming (MINLP)]]
For large-scale problems, it is more efficient to solve the problem in sparse matrix form. The matrix arguments are passed to Gekko in [[https://en.wikipedia.org/wiki/Sparse_matrix#Coordinate_list_(COO)|coordinate (COO) list]] form.
(:source lang=python:)
from gekko import GEKKO
m = GEKKO(remote=False)
c = [[2],[1]]
# [[row indices],[column indices],[values]]
A = [[1,1,2,2,3,3],[1,2,1,2,1,2],[-1,1,3,2,2,3]]
b = [[1,2,3],[1,12,12]]
z = m.Array(m.Var,2,integer=True,lb=0)
m.qobj(c,x=z,otype='max',sparse=True)
m.axb(A,b,x=z,etype='<=',sparse=True)
m.options.SOLVER = 1
m.solve()
print('Objective: ', -m.options.OBJFCNVAL)
print('x: ', z[0].value[0])
print('y: ', z[1].value[0])
(:sourceend:)
'''Additional Resources'''
* [[https://apmonitor.com/do/index.php/Main/DiscreteVariables|Dynamic Optimization with Discrete Variables]]
* [[https://apmonitor.com/me575/index.php/Main/DiscreteOptimization|Discrete Optimization]]
* [[https://apmonitor.com/pdc/index.php/Main/LinearProgramming|Linear Programming]]
* [[Main/IntegerBinaryVariables|Mixed Integer Nonlinear Programming (MINLP)]]
Added lines 34-35:
%width=550px%Attach:integer_programming_solution.png
Changed line 16 from:
Integer variables are declared in APMonitor with the prefix ''int''. The problem is solved with [[Main/MATLAB|MATLAB]], [[Main/JuliaOpt|Julia]], [[Main/PythonApp]] or through the [[http://apmonitor.com/online/view_pass.php|APMonitor Online Interface]].
to:
Integer variables are declared in APMonitor with the prefix ''int''. The problem is solved with [[Main/MATLAB|MATLAB]], [[Main/JuliaOpt|Julia]], [[Main/PythonApp|Python]] or through the [[http://apmonitor.com/online/view_pass.php|APMonitor Online Interface]].
Deleted lines 13-16:
Integer variables are declared in APMonitor with the prefix ''int''.
Changed lines 16-21 from:
Variables
Python Gekko
to:
Integer variables are declared in APMonitor with the prefix ''int''. The problem is solved with [[Main/MATLAB|MATLAB]], [[Main/JuliaOpt|Julia]], [[Main/PythonApp]] or through the [[http://apmonitor.com/online/view_pass.php|APMonitor Online Interface]].
Variables
int_x >= 0
int_y >= 0
End Variables
Intermediates
x = int_x
y = int_y
End Intermediates
Equations
maximize y
-x+y<=1
3*x+2*y<=12
2*x+3*y<=12
End Equations
Variables
int_x >= 0
int_y >= 0
End Variables
Intermediates
x = int_x
y = int_y
End Intermediates
Equations
maximize y
-x+y<=1
3*x+2*y<=12
2*x+3*y<=12
End Equations
Added lines 37-38:
Gekko is a Python API to APMonitor.
Changed lines 40-49 from:
m = GEKKO() # create GEKKO model
# create integer variables
x1 = m.Var(integer=True,lb=-5,ub=10)
x2 = m.Var(integer=True,lb=-1,ub=2)
m.Obj(4*x1**2-4*x2*x1**2+x2**2+x1**2-x1+1)
m.options.SOLVER = 1 # APOPT solver
m.solve()
print('x1: ' + str(x1.value[0]))
print('x2: ' + str(x2.value[0]))
to:
Changed line 12 from:
|| %width=300px%Attach:integer_linear_programming.png || {$\begin{align} \max & \text{ } y \\ -x +y & \leq 1 \\ 3x +2y & \leq 12 \\ 2x +3y & \leq 12 \\ x,y & \ge 0 \\ x,y & \in \mathbb{Z} \end{align}$} ||
to:
|| %width=400px%Attach:integer_linear_programming.png || {$\begin{align} \max & \text{ } y \\ -x +y & \leq 1 \\ 3x +2y & \leq 12 \\ 2x +3y & \leq 12 \\ x,y & \ge 0 \\ x,y & \in \mathbb{Z} \end{align}$} ||
Changed line 12 from:
|| %width=200px%Attach:integer_linear_programming.png || {$\begin{align} \max & \text{ } y \\ -x +y & \leq 1 \\ 3x +2y & \leq 12 \\ 2x +3y & \leq 12 \\ x,y & \ge 0 \\ x,y & \in \mathbb{Z} \end{align}$} ||
to:
|| %width=300px%Attach:integer_linear_programming.png || {$\begin{align} \max & \text{ } y \\ -x +y & \leq 1 \\ 3x +2y & \leq 12 \\ 2x +3y & \leq 12 \\ x,y & \ge 0 \\ x,y & \in \mathbb{Z} \end{align}$} ||
Changed lines 11-13 from:
to:
|| border=0
|| %width=200px%Attach:integer_linear_programming.png || {$\begin{align} \max & \text{ } y \\ -x +y & \leq 1 \\ 3x +2y & \leq 12 \\ 2x +3y & \leq 12 \\ x,y & \ge 0 \\ x,y & \in \mathbb{Z} \end{align}$} ||
|| %width=200px%Attach:integer_linear_programming.png || {$\begin{align} \max & \text{ } y \\ -x +y & \leq 1 \\ 3x +2y & \leq 12 \\ 2x +3y & \leq 12 \\ x,y & \ge 0 \\ x,y & \in \mathbb{Z} \end{align}$} ||
Changed lines 7-20 from:
{$ \begin{align} & \text{maximize} && \mathbf{c}^\mathrm{T} \mathbf{x}\\ & \text{subject to} && A \mathbf{x} \le \mathbf{b}, \\ & && \mathbf{x} \ge \mathbf{0}, \\ & \text{and} && \mathbf{x} \in \mathbb{Z}^n, \end{align} $}
{$\max c^T x$}
{$\mathrm{subject to} Ax \le b$}
{$x \ge 0 $}
{$x \in \mathbb{Z}^n$}
A [[Main/IntegerBinaryVariables|Mixed-Integer LinearProgramming (MILP)]] problem has continuous and integer variables. Mixed-Integer Nonlinear Programming (MINLP) also includes nonlinear equations and requires specialized MINLP solvers such as [[https://apopt.com|APOPT]]. MINLP solvers can also solve MILP or ILP problems although other solvers such as CPLEX, Gurobi, or FICO Xpress are specialized commercial solvers for MILP. APMonitor and [[https://gekko.readthedocs.io/en/latest/|GEKKO]] solve MINLP problems.
!!!! Integer Variables
{$\mathrm{subject to} Ax \le b$}
{$x \ge 0 $}
{$x \in \mathbb{Z}^n$}
A [[Main/IntegerBinaryVariables|Mixed-Integer Linear
!!!! Integer Variables
to:
{$ \begin{align} & \text{maximize} && \mathbf{c}^\mathrm{T} \mathbf{x}\\ & \text{subject to} && A \mathbf{x} \le \mathbf{b}, \\ & && \mathbf{x} \ge \mathbf{0}, \\ &&& \mathbf{x} \in \mathbb{Z}^n \end{align} $}
A [[Main/IntegerBinaryVariables|Mixed-Integer Linear Programming (MILP)]] problem has continuous and integer variables. Mixed-Integer Nonlinear Programming (MINLP) also includes nonlinear equations and requires specialized MINLP solvers such as [[https://apopt.com|APOPT]]. MINLP solvers can also solve MILP or ILP problems although other solvers such as CPLEX, Gurobi, or FICO Xpress are specialized commercial solvers for MILP. APMonitor and [[https://gekko.readthedocs.io/en/latest/|GEKKO]] solve MINLP problems. The following [[https://en.wikipedia.org/wiki/Integer_programming|integer linear programming (ILP) problem]] has a two potential maximum values at (1,2) and (2,2).
%width=400px%Attach:integer_linear_programming.png
{$\begin{align} \max & \text{ } y \\ -x +y & \leq 1 \\ 3x +2y & \leq 12 \\ 2x +3y & \leq 12 \\ x,y & \ge 0 \\ x,y & \in \mathbb{Z} \end{align}$}
'''APMonitor'''
A [[Main/IntegerBinaryVariables|Mixed-Integer Linear Programming (MILP)]] problem has continuous and integer variables. Mixed-Integer Nonlinear Programming (MINLP) also includes nonlinear equations and requires specialized MINLP solvers such as [[https://apopt.com|APOPT]]. MINLP solvers can also solve MILP or ILP problems although other solvers such as CPLEX, Gurobi, or FICO Xpress are specialized commercial solvers for MILP. APMonitor and [[https://gekko.readthedocs.io/en/latest/|GEKKO]] solve MINLP problems. The following [[https://en.wikipedia.org/wiki/Integer_programming|integer linear programming (ILP) problem]] has a two potential maximum values at (1,2) and (2,2).
%width=400px%Attach:integer_linear_programming.png
{$\begin{align} \max & \text{ } y \\ -x +y & \leq 1 \\ 3x +2y & \leq 12 \\ 2x +3y & \leq 12 \\ x,y & \ge 0 \\ x,y & \in \mathbb{Z} \end{align}$}
'''APMonitor'''
Added lines 25-26:
Python Gekko is an interface
Deleted lines 27-28:
{$\begin{align} \max & \text{ } y \\ -x +y & \leq 1 \\ 3x +2y & \leq 12 \\ 2x +3y & \leq 12 \\ x,y & \ge 0 \\ x,y & \in \mathbb{Z} \end{align}$}
Added lines 1-69:
(:title Integer Linear Programming:)
(:keywords integer, ILP, MILP, binary, discrete:)
(:description Integer programming, also known as Integer Linear Programming, is where all of the variables are binary (0 or 1), integer (e.g. integer 0 to 10), or other discrete decision variables in optimization:)
[[https://en.wikipedia.org/wiki/Integer_programming|Integer Linear Programming (ILP)]] is a type of optimization problem where the variables are integer values and the objective function and equations are linear.
{$ \begin{align} & \text{maximize} && \mathbf{c}^\mathrm{T} \mathbf{x}\\ & \text{subject to} && A \mathbf{x} \le \mathbf{b}, \\ & && \mathbf{x} \ge \mathbf{0}, \\ & \text{and} && \mathbf{x} \in \mathbb{Z}^n, \end{align} $}
{$\max c^T x$}
{$\mathrm{subject to} Ax \le b$}
{$x \ge 0 $}
{$x \in \mathbb{Z}^n$}
A [[Main/IntegerBinaryVariables|Mixed-Integer Linear Programming (MILP)]] problem has continuous and integer variables. Mixed-Integer Nonlinear Programming (MINLP) also includes nonlinear equations and requires specialized MINLP solvers such as [[https://apopt.com|APOPT]]. MINLP solvers can also solve MILP or ILP problems although other solvers such as CPLEX, Gurobi, or FICO Xpress are specialized commercial solvers for MILP. APMonitor and [[https://gekko.readthedocs.io/en/latest/|GEKKO]] solve MINLP problems.
!!!! Integer Variables
Integer variables are declared in APMonitor with the prefix ''int''.
'''APMonitor Model File'''
! Integer decision variable (-5 to 10)
Variables
int_v >=-5 <=10
'''Python Gekko'''
{$\begin{align} \max & \text{ } y \\ -x +y & \leq 1 \\ 3x +2y & \leq 12 \\ 2x +3y & \leq 12 \\ x,y & \ge 0 \\ x,y & \in \mathbb{Z} \end{align}$}
(:source lang=python:)
from gekko import GEKKO
m = GEKKO() # create GEKKO model
# create integer variables
x1 = m.Var(integer=True,lb=-5,ub=10)
x2 = m.Var(integer=True,lb=-1,ub=2)
m.Obj(4*x1**2-4*x2*x1**2+x2**2+x1**2-x1+1)
m.options.SOLVER = 1 # APOPT solver
m.solve()
print('x1: ' + str(x1.value[0]))
print('x2: ' + str(x2.value[0]))
(:sourceend:)
Nonlinear programming solvers (such as IPOPT) may not return an integer solution because they are designed for continuous variables. Mixed Integer Nonlinear Programming solvers (such as APOPT) are equipped to solve for binary or integer variables. Select the appropriate [[https://apmonitor.com/wiki/index.php/Main/OptionApmSolver|solver option]] to either find an initial solution without integer variables or an integer solution. It is sometimes desirable to find a non-integer solution first because of the often significant reduction in computation time without the integer variables.
!!!! Discrete Variables
Discrete decision variables are those that have only certain levels or quantities that are acceptable at an optimal solution. Examples of discrete variables are binary (e.g. off/on or 0/1), integer (e.g. 4,5,6,7), or general discrete values that are not integer (e.g. 1/4 cm, 1/2 cm, 1 cm).
'''Python Gekko'''
Integer variable '''x1''' and [[https://en.wikipedia.org/wiki/Special_ordered_set|Special Ordered Set]] '''x2''' variables are solved with [[https://gekko.readthedocs.io/en/latest/|Python GEKKO]]. The '''sos1''' Gekko function is used to create the SOS1 variable.
(:source lang=python:)
from gekko import GEKKO
m = GEKKO() # create GEKKO model
x1 = m.Var(integer=True,lb=-5,ub=10)
# create Special Ordered Set variable
x2 = m.sos1([0.5, 1.15, 2.6, 5.2])
m.Obj(4*x1**2-4*x2*x1**2+x2**2+x1**2-x1+1)
m.options.SOLVER = 1 # APOPT solver
m.solve()
print('x1: ' + str(x1.value[0]))
print('x2: ' + str(x2.value[0]))
(:sourceend:)
See [[https://apmonitor.com/do/index.php/Main/DiscreteVariables|Dynamic Optimization with Discrete Variables]], [[https://apmonitor.com/me575/index.php/Main/DiscreteOptimization|Discrete Optimization]], and [[Main/IntegerBinaryVariables|MINLP Solutions]]
(:keywords integer, ILP, MILP, binary, discrete:)
(:description Integer programming, also known as Integer Linear Programming, is where all of the variables are binary (0 or 1), integer (e.g. integer 0 to 10), or other discrete decision variables in optimization:)
[[https://en.wikipedia.org/wiki/Integer_programming|Integer Linear Programming (ILP)]] is a type of optimization problem where the variables are integer values and the objective function and equations are linear.
{$ \begin{align} & \text{maximize} && \mathbf{c}^\mathrm{T} \mathbf{x}\\ & \text{subject to} && A \mathbf{x} \le \mathbf{b}, \\ & && \mathbf{x} \ge \mathbf{0}, \\ & \text{and} && \mathbf{x} \in \mathbb{Z}^n, \end{align} $}
{$\max c^T x$}
{$\mathrm{subject to} Ax \le b$}
{$x \ge 0 $}
{$x \in \mathbb{Z}^n$}
A [[Main/IntegerBinaryVariables|Mixed-Integer Linear Programming (MILP)]] problem has continuous and integer variables. Mixed-Integer Nonlinear Programming (MINLP) also includes nonlinear equations and requires specialized MINLP solvers such as [[https://apopt.com|APOPT]]. MINLP solvers can also solve MILP or ILP problems although other solvers such as CPLEX, Gurobi, or FICO Xpress are specialized commercial solvers for MILP. APMonitor and [[https://gekko.readthedocs.io/en/latest/|GEKKO]] solve MINLP problems.
!!!! Integer Variables
Integer variables are declared in APMonitor with the prefix ''int''.
'''APMonitor Model File'''
! Integer decision variable (-5 to 10)
Variables
int_v >=-5 <=10
'''Python Gekko'''
{$\begin{align} \max & \text{ } y \\ -x +y & \leq 1 \\ 3x +2y & \leq 12 \\ 2x +3y & \leq 12 \\ x,y & \ge 0 \\ x,y & \in \mathbb{Z} \end{align}$}
(:source lang=python:)
from gekko import GEKKO
m = GEKKO() # create GEKKO model
# create integer variables
x1 = m.Var(integer=True,lb=-5,ub=10)
x2 = m.Var(integer=True,lb=-1,ub=2)
m.Obj(4*x1**2-4*x2*x1**2+x2**2+x1**2-x1+1)
m.options.SOLVER = 1 # APOPT solver
m.solve()
print('x1: ' + str(x1.value[0]))
print('x2: ' + str(x2.value[0]))
(:sourceend:)
Nonlinear programming solvers (such as IPOPT) may not return an integer solution because they are designed for continuous variables. Mixed Integer Nonlinear Programming solvers (such as APOPT) are equipped to solve for binary or integer variables. Select the appropriate [[https://apmonitor.com/wiki/index.php/Main/OptionApmSolver|solver option]] to either find an initial solution without integer variables or an integer solution. It is sometimes desirable to find a non-integer solution first because of the often significant reduction in computation time without the integer variables.
!!!! Discrete Variables
Discrete decision variables are those that have only certain levels or quantities that are acceptable at an optimal solution. Examples of discrete variables are binary (e.g. off/on or 0/1), integer (e.g. 4,5,6,7), or general discrete values that are not integer (e.g. 1/4 cm, 1/2 cm, 1 cm).
'''Python Gekko'''
Integer variable '''x1''' and [[https://en.wikipedia.org/wiki/Special_ordered_set|Special Ordered Set]] '''x2''' variables are solved with [[https://gekko.readthedocs.io/en/latest/|Python GEKKO]]. The '''sos1''' Gekko function is used to create the SOS1 variable.
(:source lang=python:)
from gekko import GEKKO
m = GEKKO() # create GEKKO model
x1 = m.Var(integer=True,lb=-5,ub=10)
# create Special Ordered Set variable
x2 = m.sos1([0.5, 1.15, 2.6, 5.2])
m.Obj(4*x1**2-4*x2*x1**2+x2**2+x1**2-x1+1)
m.options.SOLVER = 1 # APOPT solver
m.solve()
print('x1: ' + str(x1.value[0]))
print('x2: ' + str(x2.value[0]))
(:sourceend:)
See [[https://apmonitor.com/do/index.php/Main/DiscreteVariables|Dynamic Optimization with Discrete Variables]], [[https://apmonitor.com/me575/index.php/Main/DiscreteOptimization|Discrete Optimization]], and [[Main/IntegerBinaryVariables|MINLP Solutions]]