Discrete Optimization



By Prof. Seungchul Lee
http://iai.postech.ac.kr/
Industrial AI Lab at POSTECH

Table of Contents

1. Optimization

An important tool in 1) engineering problem solving and 2) decision science

  • peolple optimize
  • nature optimizes

3 key components

  1. objective
  2. decision variable or unknown
  3. constraints

Procedures

  1. The process of identifying objective, variables, and constraints for a given problem is known as "modeling"
  2. Once the model has been formulated, optimization algorithm can be used to find its solutions.

In mathematical expression


$$\begin{align} \min_{x} \quad &f(x) \\ \text{subject to} \quad &g_i(x) \leq 0, \qquad i=1,\cdots,m \end{align} $$


Remarks) equivalent


$$\begin{align} \min_{x} f(x) \quad&\leftrightarrow \quad \max_{x} -f(x)\\ \quad g_i(x) \leq 0\quad&\leftrightarrow \quad -g_i(x) \geq 0\\ h(x) = 0 \quad&\leftrightarrow \quad \begin{cases} h(x) \leq 0 \quad \text{and} \\ h(x) \geq 0 \end{cases} \end{align} $$

1.1. Modeling Example

Manufacturer produces two products P and Q with machine A and B.

time of producing each unit of P

  • in machine A: 50 min
  • in machine B: 30 min

time of producing each unit of Q

  • in machine A: 24 min
  • in machine B: 33 min

working plan for a week $\begin{cases} \text{40 hrs of work on machine A} \\ \text{35 hrs of work on machine B} \end{cases}$

The week starts with $\begin{cases} \text{stock of 30 units of P} \\ \text{stock of 90 units of Q} \\ \text{demand of 75 units of P} \\ \text{demand of 95 units of Q} \\ \end{cases}$

Question: how to plan the production to end the week with the maximum stock?

Define decision variables

  • $x$ = unit of P to be produced
  • $y$ = unit of Q to be produced


$$\begin{align} \max_{x} \quad & (30+x-75)+(90+y-95) \\ \text{subject to} \quad & 50x+24y \leq 40 \times 60\\ & 30x + 33y \leq 35 \times 60\\ & x \geq 75-30\\ & y \geq 95-90 \end{align} $$

1.2. General or Standard Forms

1) Linear programming $$\begin{align} \min_{x} \quad & f^Tx \\ \text{subject to} \quad & Ax \leq b\\ & A_{eq}x \leq b_{eq}\\ & LB \leq x \leq UB \end{align} $$


2) Integer programming $$\begin{align} \min_{x} \quad & f^Tx \\ \text{subject to} \quad & Ax \leq b\\ & A_{eq}x \leq b_{eq}\\ & LB \leq x \leq UB\\ & x \in \mathbb{Z} \text{(integer)} \end{align} $$


3) Mixed integer linear programming $$\begin{align} \min_{x,z} \quad & f^T \begin{bmatrix} x \\ z \end{bmatrix} \\ \text{subject to} \quad & A \begin{bmatrix} x \\ z \end{bmatrix} \leq b \\ & A_{eq} \begin{bmatrix} x \\ z \end{bmatrix} = b_{eq}\\ & LB \leq \begin{bmatrix} x \\ z \end{bmatrix} \leq UB \\ & x \in \mathbb{R} \text{(real)} \\ & z \in \mathbb{Z} \end{align} $$


4) Binary integer programming $$\begin{align} \min_{x} \quad & f^Tx \\ \text{subject to} \quad & Ax \leq b \\ & A_{eq}x = b_{eq} \\ & LB \leq x \leq UB \\ & x \in \{0,1\} \end{align} $$


5) Quadratic programming $$\begin{align} \min_{x} \quad & \frac{1}{2}x^THx + f^Tx \\ \text{subject to} \quad & Ax \leq b \\ & A_{eq}x = b_{eq} \\ & LB \leq x \leq UB \end{align} $$

2. Linear Programming (LP)

LP Example 1


$$\begin{align} \max_{x} \quad & x_1 + x_2 \\ \text{subject to} \quad & 2x_1 + x_2 \leq 29 \\ & x_1 + 2x_2 \leq 25 \\ & x_1 \geq 2 \\ & x_2 \geq 5 \end{align} $$


$$\begin{align} \implies \quad \min_{x} \quad & - \begin{bmatrix} 1 & 1 \end{bmatrix} \begin{bmatrix} x_1 \\ x_2 \end{bmatrix} \\ \text{subject to} \quad & \begin{bmatrix} 2 & 1 \\ 1 & 2 \end{bmatrix} \begin{bmatrix} x_1 \\ x_2 \end{bmatrix} \leq \begin{bmatrix} 29 \\ 25 \end{bmatrix} \\ & \begin{bmatrix} 2 \\ 5 \end{bmatrix} \leq \begin{bmatrix} x_1 \\ x_2 \end{bmatrix} \leq \begin{bmatrix} \\ \\ \end{bmatrix} \end{align} $$
In [1]:
f = -[1 1]';
A = [2 1;
    1 2];
b = [29,25]';
lb = [2 5];

linprog(f,A,b,[],[],lb,[])
Out[1]:
Optimization terminated.

ans =

   11.0000
    7.0000

LP example 2


$$\begin{align} \min_{x} \quad & x_1 + x_2 + x_3 + x_4 + x_5 \\ \text{subject to} \quad & x_1 + x_2 = 100 \\ & x_3 + x_4 = 70 \\ & x_2 + x_3 + 2x_4 + 5x_5 = 250 \\ & x_{i} \geq 0 \quad (i = 1,2,\cdots,5) \end{align} $$



$$\begin{align} \implies \quad \min_{x} \quad & -\begin{bmatrix} 1 & 1 & 1 & 1 & 1 \end{bmatrix} \begin{bmatrix} x_1 \\ x_2 \\ x_3 \\ x_4 \\ x_5 \end{bmatrix} \\ \text{subject to} \quad & \begin{bmatrix} 1 & 1 & 0 & 0 & 0 \\ 0 & 0 & 1 & 1 & 0 \\ 0 & 1 & 1 & 2 & 5 \end{bmatrix} \begin{bmatrix} x_1 \\ x_2 \\ x_3 \\ x_4 \\ x_5 \end{bmatrix} = \begin{bmatrix} 100 \\ 70 \\ 250 \end{bmatrix} \\ & \begin{bmatrix} 0 \\ 0 \\ 0 \\ 0 \\ 0 \end{bmatrix} \leq \begin{bmatrix} x_1 \\ x_2 \\ x_3 \\ x_4 \\ x_5 \end{bmatrix} \leq \begin{bmatrix} \\ \\ \\ \\ \\ \end{bmatrix} \end{align} $$
In [2]:
f = -[1 1 1 1 1]';
Aeq = [1 1 0 0 0;
    0 0 1 1 0;
    0 1 1 2 5];
beq = [100 70 250]';
lb = [0 0 0 0 0]';

linprog(f,[],[],Aeq,beq,lb,[])
Out[2]:
Optimization terminated.

ans =

  100.0000
    0.0000
   70.0000
    0.0000
   36.0000

LP example 3

In [3]:
f = -[1 1]';
A = [2 1];
b = log(0.25) - log(pi) + 2*log(2);
lb = [log(0.06) 0]';

x = linprog(f,A,b,[],[],lb,[])

d = exp(x(1))
n = exp(x(2))
Out[3]:
Optimization terminated.

x =

   -2.8134
    4.4821


d =

    0.0600


n =

   88.4194

3. Mixed Integer Linear Programming (MILP)

  • Matlab command: intlinprog (available from MATLAB R2014b)
In [4]:
help intlinprog
Out[4]:
INTLINPROG Mixed integer linear programming.
 
    X = INTLINPROG(f,intcon,A,b) attempts to solve problems of the form
 
             min f'*x    subject to:  A*x  <= b
              x                       Aeq*x = beq
                                      lb <= x <= ub
                                      x(i) integer, where i is in the index
                                      vector intcon (integer constraints)
 
    X = INTLINPROG(f,intcon,A,b) solves the problem with integer variables
    in the intcon vector and linear inequality constraints A*x <= b. intcon
    is a vector of positive integers indicating components of the solution
    X that must be integers. For example, if you want to constrain X(2) and
    X(10) to be integers, set intcon to [2,10].
 
    X = INTLINPROG(f,intcon,A,b,Aeq,beq) solves the problem above while
    additionally satisfying the equality constraints Aeq*x = beq.
 
    X = INTLINPROG(f,intcon,A,b,Aeq,beq,LB,UB) defines a set of lower and
    upper bounds on the design variables, X, so that the solution is in the
    range LB <= X <= UB. Use empty matrices for LB and UB if no bounds
    exist. Set LB(i) = -Inf if X(i) is unbounded below; set UB(i) = Inf if
    X(i) is unbounded above.
 
    X = INTLINPROG(f,intcon,A,b,Aeq,beq,LB,UB,OPTIONS) minimizes with the
    default optimization parameters replaced by values in OPTIONS, an
    argument created with the OPTIMOPTIONS function. See OPTIMOPTIONS for
    details.
 
    X = INTLINPROG(PROBLEM) finds the minimum for PROBLEM. PROBLEM is a
    structure with the vector 'f' in PROBLEM.f, the integer constraints in
    PROBLEM.intcon, the linear inequality constraints in PROBLEM.Aineq and
    PROBLEM.bineq, the linear equality constraints in PROBLEM.Aeq and
    PROBLEM.beq, the lower bounds in PROBLEM.lb, the upper bounds in
    PROBLEM.ub, the options structure in PROBLEM.options, and solver name
    'intlinprog' in PROBLEM.solver.
 
    [X,FVAL] = INTLINPROG(f,intcon,A,b,...) returns the value of the
    objective function at X: FVAL = f'*X.
 
    [X,FVAL,EXITFLAG] = INTLINPROG(f,intcon,A,b,...) returns an EXITFLAG
    that describes the exit condition of INTLINPROG. Possible values of
    EXITFLAG and the corresponding exit conditions are
 
      2  Solver stopped prematurely. Integer feasible point found.
      1  Optimal solution found.
      0  Solver stopped prematurely. No integer feasible point found.
     -2  No feasible point found.
     -3  Root LP problem is unbounded.
 
    [X,FVAL,EXITFLAG,OUTPUT] = INTLINPROG(f,A,b,...) returns a structure
    OUTPUT containing information about the optimization process. OUTPUT
    includes the number of integer feasible points found and the final gap
    between internally calculated bounds on the solution. See the
    documentation for a complete description.
 
    See also LINPROG.

    Reference page in Help browser
       doc intlinprog

MILP Example 1


$$\begin{align} \min_{x} \quad & \begin{bmatrix} -3 & -1 \end{bmatrix} \begin{bmatrix} x_1 \\ x_2 \end{bmatrix} \\ \text{subject to} \quad & \begin{bmatrix} 17 & 11 \end{bmatrix} \begin{bmatrix} x_1 \\ x_2 \end{bmatrix} \leq 83 \\ & \begin{bmatrix} 0 \\ 0 \end{bmatrix} \leq \begin{bmatrix} x_1 \\ x_2 \end{bmatrix} \leq \begin{bmatrix} 3 \\ 7 \end{bmatrix} \\ & x_1, x_2 \in \mathbb{Z} \; \text{(integer)} \end{align} $$
In [5]:
f = [-3 -1]';
intcon = 1:2;
A = [17 11];
b = 83;
LB = [0 0]';
UB = [3 7]';

x = intlinprog(f,intcon,A,b,[],[],LB,UB)
Out[5]:
LP:                Optimal objective value is -11.909091.                                           

Cut Generation:    Applied 1 Gomory cut.                                                            
                   Lower bound is -11.000000.                                                       
                   Relative gap is 0.00%.                                                          


Optimal solution found.

Intlinprog stopped at the root node because the objective value is within a gap tolerance of the optimal value, options.TolGapAbs = 0
(the default value). The intcon variables are integer within tolerance, options.TolInteger = 1e-05 (the default value).


x =

    3.0000
    2.0000

MIP Example 2


$$\begin{align} \min_{x} \quad & \begin{bmatrix} -3 & -2 & -1 \end{bmatrix} \begin{bmatrix} x_1 \\ x_2 \\ x_3 \end{bmatrix} \\ \text{subject to} \quad & \begin{bmatrix} 1 & 1 & 1 \end{bmatrix} \begin{bmatrix} x_1 \\ x_2 \\ x_3 \end{bmatrix} \leq 7 \\ & \begin{bmatrix} 4 & 2 & 1 \end{bmatrix} \begin{bmatrix} x_1 \\ x_2 \\ x_3 \end{bmatrix} = 12 \\ & 0 \leq \begin{bmatrix} x_1 \\ x_2 \\ x_3 \end{bmatrix} \\ & x_3 \in \{0,1\} \end{align} $$
In [6]:
f = [-3; -2; -1];
intcon = 3;
A = [1, 1, 1];
b = 7;
Aeq = [4,2,1];
beq = 12;
lb = zeros(3,1);
ub = [Inf;Inf;1];

x = intlinprog(f,intcon,A,b,Aeq,beq,lb,ub)
Out[6]:
LP:                Optimal objective value is -12.000000.                                           


Optimal solution found.

Intlinprog stopped at the root node because the objective value is within a gap tolerance of the optimal value, options.TolGapAbs = 0
(the default value). The intcon variables are integer within tolerance, options.TolInteger = 1e-05 (the default value).


x =

         0
    5.5000
    1.0000

3.1. Knapsack problems revisit

  • There is a container(knapsack) of capacity C = 20. Further more, there is a set 6 of objects. Each object has a weight and a value.
  • Determine the number of each item to include in a collection so that the total size is less than or equal to 20 and the total value is as large as possible.


items 1 2 3 4 5 6
weights 10 9 4 2 1 20
values 175 90 20 50 10 200


Question: Can we formulate this knapsack problem into a binary integer program?

$\Rightarrow$ decision variables $\quad x_{i} \in \{0,1\} \quad i = 1,\cdots,6$


$$\begin{align} \max_{x} \quad & 175x_1 + 90x_2 + 20x_3 + 50x_4 + 10x_5 + 200x_6 \\ \text{subject to} \quad & 10x_1 + 9x_2 + 4x_3 + 2x_4 + x_5 + 20x_6 \leq 20 \\ \end{align} $$


$\Rightarrow$ intlinprog in Matlab to solve

In [7]:
n = 6;  % # of items
weights = [10 9 4 2 1 20];
values = [175 90 20 50 10 200];
maxWeight = 20;

% in a form of MILP

f = -values;        % max
A = weights;
b = maxWeight;
intcon = 1:n;
lb = zeros(n,1);
ub = ones(n,1);

x = intlinprog(f,intcon,A,b,[],[],lb,ub)
Out[7]:
LP:                Optimal objective value is -305.000000.                                          

Cut Generation:    Applied 1 strong CG cut.                                                         
                   Lower bound is -275.000000.                                                      
                   Relative gap is 0.00%.                                                          


Optimal solution found.

Intlinprog stopped at the root node because the objective value is within a gap tolerance of the optimal value, options.TolGapAbs = 0
(the default value). The intcon variables are integer within tolerance, options.TolInteger = 1e-05 (the default value).


x =

    1.0000
    1.0000
         0
         0
    1.0000
         0

3.2. Factory and warehouse

  • A campany wants to $\begin{cases} \text{1. build new factory in Ulsan, Busan, or both} \\ \text{2. build new warehouse (only one has to be built)} \\ \text{3. warehouse must be built in a city of a new factory} \\ \end{cases} $
  • Available capital is 10. We want to maximize total profit.


Profit Cost to build
1 Build a factory in Ulsan 7 5
2 Build a factory in Busan 5 3
3 Build a warehouse in Ulsan 6 5
4 Build a warehouse in Busan 4 2


  • Form a binary integer programming to find the optimal locations of new factory and warehouse, and solve it using intlinprog in MATLAB.

$\quad\;$ (Hint: define binary decision variables, then use them to define objective function and constraints.)

In [8]:
f = -[7 5 6 4]';
intcon = [1 2 3 4];
A = [5 3 5 2;
    -1 -1 0 0;
    -1 0 1 0;
    0 -1 0 1];
b = [10;
    -1;
    0;
    0];

Aeq = [0 0 1 1];
beq = [1];

lb = [0 0 0 0]';
ub = [1 1 1 1]';
[x fval] = intlinprog(f,intcon,A,b,Aeq,beq,lb,ub)
Out[8]:
LP:                Optimal objective value is -16.000000.                                           


Optimal solution found.

Intlinprog stopped at the root node because the objective value is within a gap tolerance of the optimal value, options.TolGapAbs = 0
(the default value). The intcon variables are integer within tolerance, options.TolInteger = 1e-05 (the default value).


x =

    1.0000
    1.0000
         0
    1.0000


fval =

   -16

3.3. Shortest path

  • Binary integer programming can be applied to finding the shortest path in the graph.
  • Define the binary decision variables and form the binary integer programming. Then you can solve it using intlinprog command in MATLAB



In [9]:
f = [1 4 2 4 8 11 3]';
Aeq = [1 1 0 0 0 0 0
    1 0 -1 0 0 -1 0
    0 1 1 -1 -1 0 0
    0 0 0 1 0 0 -1
    0 0 0 0 1 1 1];
beq = [1 0 0 0 1]';

intcon = 1:7;
lb = zeros(7,1);
ub = ones(7,1);

x = intlinprog(f,intcon,[],[],Aeq,beq,lb,ub)
Out[9]:
LP:                Optimal objective value is 10.000000.                                            


Optimal solution found.

Intlinprog stopped at the root node because the objective value is within a gap tolerance of the optimal value, options.TolGapAbs = 0
(the default value). The intcon variables are integer within tolerance, options.TolInteger = 1e-05 (the default value).


x =

     1
     0
     1
     1
     0
     0
     1

3.4. Robot Path Design

  • A circular mobile robot is to be navigated. The robot should take a path from point A to point B, avoiding the polygonal obstacles (hatched regions).
  • Design a collision free-path the robot can take with the minimum length.


3.5. Vertex cover problem

  • Suppose you want to have an ATM in every street (block, district) of a city of Ulsan. However, each additional ATM machine costs more money. Therefore, we want to have the minimum number of ATM machines at the right location while they are accessible from every street.


In [10]:
n = 8;
f = [1 1 1 1 1 1 1 1]';
intcon = 1:n;
A = -[1 0 1 0 0 0 0 0;
     1 0 0 1 0 0 0 0;
     1 0 0 0 1 0 0 0;
     1 0 0 0 0 1 0 0;
     1 0 0 0 0 0 1 0;
     0 1 0 1 0 0 0 0;
     0 1 0 0 1 0 0 0;
     0 0 1 0 0 0 0 1;
     0 0 0 0 1 0 1 0];
 b = -[1 1 1 1 1 1 1 1 1]';
 lb = zeros(n,1);
 ub = ones(n,1);
 x = intlinprog(f,intcon,A,b,[],[],lb,ub)
Out[10]:
LP:                Optimal objective value is 4.000000.                                             

Cut Generation:    Applied 1 zero-half cut.                                                         
                   Lower bound is 4.000000.                                                         
                   Relative gap is 0.00%.                                                          


Optimal solution found.

Intlinprog stopped at the root node because the objective value is within a gap tolerance of the optimal value, options.TolGapAbs = 0
(the default value). The intcon variables are integer within tolerance, options.TolInteger = 1e-05 (the default value).


x =

     1
     1
     0
     0
     0
     0
     1
     1

3.6. Coloring problems

  • Find the minimum number of colors that can cover the below Ulsan map without conflicting the adjacent rule.
  • Coloring problem: color the vertext of a graph in such a way that adjacent vertices are differently colored, while minimizing the number of colors.




  • Define ILP decision variables


$$\begin{align} & y_k = \begin{cases} 1 \quad \text{if color k is used} \\ 0 \quad \text{if color k is not used} \end{cases} \\ & x_{ik} = \begin{cases} 1 \quad \text{if vertex i gets color k} \\ 0 \quad \text{otherwise} \end{cases} \end{align} $$


$$\begin{align} \min \quad & \sum \limits_{k=1}^5 y_k \\ \text{subject to} \quad & \sum \limits_{k=1}^5 x_{ik} = 1 \\ & x_{ik} + x_{jk} \leq 1 \quad \; \text{for } E_{ij} \in E \text{ and } 1 \leq k \leq 5 \\ & x_{ik} \leq y_k \quad \quad \quad \text{for } 1 \leq i \leq 5 \text{ and } 1\leq k \leq 5 \\ & x_{ik}, y_{k} \in \{0,1\} \end{align} $$
In [11]:
%% coloring problem using binary integer programming
% Actually number of colours is at most 5
% Therefore we restrict our number of colours to be 5

% vertex V1:울주군, V2:중구, V3:북구, V4:남구, V5:동구


% edges (or adjacent)
E = [1,2; 
     1,3; 
     1,4; 
     2,3; 
     2,4; 
     3,4; 
     3,5; 
     4,5];
 
%% equality constraints
A1 = zeros(5, 30);
for i = 1:5
    A1(i,1+(i-1)*5:i*5) = 1;
end

b1 = ones(5,1);

%% inequality 
A2 = zeros(length(E)*5, 30);
for k = 1:5
    for i = 1:length(E)
       A2(i+length(E)*(k-1),5*(E(i,1)-1)+k) = 1; 
       A2(i+length(E)*(k-1),5*(E(i,2)-1)+k) = 1;
    end
end
b2 = ones(length(E)*5, 1);

A3 = zeros(25, 30);
for k = 1:5
    for i = 1:5
        A3(i+5*(k-1),5*(i-1)+k) = 1;   % x_ik
        A3(i+5*(k-1),25+k) = -1;       % -y_k
    end
end
b3 = zeros(25, 1);

%% plug-in to intlinprog standard form
f = [zeros(1,25), ones(1,5)]';
intcon = 1:30;

Aeq = A1;
beq = b1;

A = [A2; A3];
b = [b2; b3];

lb = zeros(30, 1);
ub = ones(30, 1);

solution = intlinprog(f,intcon,A,b,Aeq,beq,lb,ub);
x = solution(1:25);
X = [];
for i = 1:5
    X(i,:) = x(1+(i-1)*5:5+(i-1)*5);
end
y = solution(26:30)
Out[11]:
LP:                Optimal objective value is 1.000000.                                             

Cut Generation:    Applied 2 clique cuts.                                                           
                   Lower bound is 1.000000.                                                         

Branch and Bound:

   nodes     total   num int        integer       relative                                          
explored  time (s)  solution           fval        gap (%)                                         
      27      0.01         1   4.000000e+00   4.000000e+01                                          
      79      0.04         1   4.000000e+00   0.000000e+00                                          

Optimal solution found.

Intlinprog stopped because the objective value is within a gap tolerance of the optimal value, options.TolGapAbs = 0 (the default
value). The intcon variables are integer within tolerance, options.TolInteger = 1e-05 (the default value).


y =

     1
     1
     1
     1
     0

3.7. Flat pattern desing of 3D shapes

  • A box with the dimensions specified is to be made by bending and welding a flat pattern cut out of sheet metal. Design the at pattern with minimum welding length that can be made to the box.




$$ x_i = \begin{cases} 1 \quad \text{if two nodes are connected.}\\ 0 \\ \end{cases} $$


Constraints

  • No disconnected node.
  • Only 6-1 edges are required.


$$\begin{align} \max_{x} \quad & x_1 + x_2 + x_3 + x_4 + 2x_5 + 2x_6 + 2x_7 + 2x_8 + 3x_9 + 3x_{10} + 3x_{11} + 3x_{12} \\ \text{subject to} \quad & x_1 + x_2 + x_5 + x_8 \leq 1 \\ & x_2 + x_4 + x_{10} + x_{12} \leq 1 \\ & x_3 + x_4 + x_6 + x_7 \leq 1 \\ & x_1 + x_3 + x_9 + x_{11} \leq 1 \\ & x_5 + x_6 + x_9 + x_{10} \leq 1 \\ & x_7 + x_8 + x_{11} + x_{12} \leq 1 \\ & x_1 + x_2 + x_3 + x_4 + x_5 + x_6 + x_7 + x_8 + x_9 + x_{10} + x_{11} + x_{12} = 5 \end{align} $$
In [12]:
n = 12;
f = -[1 1 1 1 2 2 2 2 3 3 3 3]';
intcon = 1:n;
A = -[1 1 0 0 1 0 0 1 0 0 0 0;
     0 1 0 1 0 0 0 0 0 1 0 1;
     0 0 1 1 0 1 1 0 0 0 0 0;
     1 0 1 0 0 0 0 0 1 0 1 0;
     0 0 0 0 1 1 0 0 1 1 0 0;
     0 0 0 0 0 0 1 1 0 0 1 1];

 b = -ones(6,1);

 Aeq = ones(1,n);
 beq = 5;
 lb = zeros(n,1);
 ub = ones(n,1);

 x = intlinprog(f,intcon,A,b,Aeq,beq,lb,ub)
Out[12]:
LP:                Optimal objective value is -13.000000.                                           


Optimal solution found.

Intlinprog stopped at the root node because the objective value is within a gap tolerance of the optimal value, options.TolGapAbs = 0
(the default value). The intcon variables are integer within tolerance, options.TolInteger = 1e-05 (the default value).


x =

     0
     0
     0
     0
     1
     0
     1
     0
     1
     0
     1
     1

3.8. Traveling salesman problem

  • Starting from his home, a salesman wishes to visit each of (n-1) other cities and return home at minimal cost. He must visit each city exactly once and it cost $c_{ij}$ to travel from city $i$ to city $j$.
  • What route should he select?




Starting from node 1, visit 2,3,4 once and return node 1 at minimal cost.


$$ x_{ij} = \begin{cases} 1 \quad \text{if travel from i to j} \\ 0 \quad \text{otherwise} \end{cases} $$


$$\begin{align} \min_{x} \quad & \sum \limits_{i=1}^4 \sum \limits_{j=1}^4 c_{ij} x_{ij} \\ \text{subject to} \quad & \sum \limits_{j} x_{ij} = 1 \quad \text{for i=1,2,3,4} \\ & \sum \limits_{i} x_{ij} = 1 \quad \text{for j=1,2,3,4} \\ & \sum \limits_{i \in S, j \in \overline{S}} x_{ij} \geq 1 \quad \text{for all prtitions} \; (S,\overline{S}) \quad 2 \leq \vert S \vert \leq m-2 \\ \end{align} $$



$$\begin{align} \\ \min_{x} \quad & 5x_{12} + 10x_{13} + 13x_{14} + 5x_{21} + 8x_{23} + 9x_{24} \\ & + 10x_{31} + 8x_{32} + 7x_{34} + 13x_{41} + 9x_{42} + 7x_{43} \\ \\ \text{subject to} \quad & x_{11} + x_{12} + x_{13} + x_{14} = 1 \\ & x_{21} + x_{22} + x_{23} + x_{24} = 1 \\ & x_{31} + x_{32} + x_{33} + x_{34} = 1 \\ & x_{41} + x_{42} + x_{43} + x_{44} = 1 \\ & x_{11} + x_{21} + x_{31} + x_{41} = 1 \\ & x_{12} + x_{22} + x_{32} + x_{42} = 1 \\ & x_{13} + x_{23} + x_{33} + x_{43} = 1 \\ & x_{14} + x_{24} + x_{34} + x_{44} = 1 \\ & x_{11} = x_{22} = x_{33} = x_{44} = 0 \\ \\ & \text{no sub-closed loop conditions} \\ \\ & x_{23} + x_{24} + x_{13} + x_{14} \geq 1 \\ & x_{31} + x_{32} + x_{41} + x_{42} \geq 1 \\ \\ & x_{21} + x_{24} + x_{31} + x_{34} \geq 1 \\ & x_{12} + x_{13} + x_{42} + x_{43} \geq 1 \\ \\ & x_{21} + x_{23} + x_{41} + x_{43} \geq 1 \\ & x_{12} + x_{14} + x_{32} + x_{34} \geq 1 \\ \end{align} $$
In [13]:
n = 4;
f = [5 10 13 5 8 9 10 8 7 13 9 7]';
intcon = 1:n*(n-1);
lb = zeros(n*(n-1),1);
ub = ones(n*(n-1),1);

Aeq = [1 1 1 0 0 0 0 0 0 0 0 0;
       0 0 0 1 1 1 0 0 0 0 0 0;
       0 0 0 0 0 0 1 1 1 0 0 0;
       0 0 0 0 0 0 0 0 0 1 1 1;
       0 0 0 1 0 0 1 0 0 1 0 0;
       1 0 0 0 0 0 0 1 0 0 1 0;
       0 1 0 0 1 0 0 0 0 0 0 1;
       0 0 1 0 0 1 0 0 1 0 0 0];

beq = [1 1 1 1 1 1 1 1]';

x = intlinprog(f,intcon,[],[],Aeq,beq,lb,ub)
Out[13]:
LP:                Optimal objective value is 24.000000.                                            


Optimal solution found.

Intlinprog stopped at the root node because the objective value is within a gap tolerance of the optimal value, options.TolGapAbs = 0
(the default value). The intcon variables are integer within tolerance, options.TolInteger = 1e-05 (the default value).


x =

     1
     0
     0
     1
     0
     0
     0
     0
     1
     0
     0
     1

what is wrong?

In [14]:
A = -[0 1 1 0 1 1 0 0 0 0 0 0;
      0 0 0 0 0 0 1 1 0 1 1 0];
b = -[1 1]';

x = intlinprog(f,intcon,A,b,Aeq,beq,lb,ub)
Out[14]:
LP:                Optimal objective value is 31.000000.                                            


Optimal solution found.

Intlinprog stopped at the root node because the objective value is within a gap tolerance of the optimal value, options.TolGapAbs = 0
(the default value). The intcon variables are integer within tolerance, options.TolInteger = 1e-05 (the default value).


x =

     1
     0
     0
     0
     0
     1
     1
     0
     0
     0
     0
     1

4. Quadratic Programming (QP)

4.1. Best location at a concert

Question: Find the best location to listen to singer's voice


$$\begin{align} \implies \quad \min \quad & \sqrt{(x_1-3)^{2}+(x_2-3)^{2}} \\ \implies \quad \min \quad & (x_1-3)^{2} + (x_2-3)^{2} \\ \text{subject to} \quad & x_1 + x_2 \leq 5 \\ & x_1 \geq 0 \\ & x_2 \geq 0 \end{align} $$


$$\begin{align} \implies \quad & x_1^{2} - 6x_1 + 9 + x_2^{2} - 6x_2 + 9 \\ & = x_1^{2} + x_2^{2} - 6 x_1 - 6x_2 + 18 \\ & = \frac{1}{2} \begin{bmatrix} x_1 & x_2 \end{bmatrix} \begin{bmatrix} 2 & 0 \\ 0 & 2 \end{bmatrix} \begin{bmatrix} x_1 \\ x_2 \end{bmatrix} - \begin{bmatrix} 6 & 6 \end{bmatrix} \begin{bmatrix} x_1 \\ x_2 \end{bmatrix} + 18 \\ & \begin{bmatrix} 1 & 1 \end{bmatrix} \begin{bmatrix} x_1 \\ x_2 \end{bmatrix} \leq 5 \\ & \begin{bmatrix} 0 \\ 0 \end{bmatrix} \leq \begin{bmatrix} x_1 \\ x_2 \end{bmatrix} \leq \begin{bmatrix} \\ \\ \end{bmatrix} \end{align} $$
In [15]:
H = [2 0;
    0 2];
f = -[6 6]';
A = [1 1];
b = 5;
lb = [0 0]';

x = quadprog(H,f,A,b,[],[],lb,[])
Out[15]:
Minimum found that satisfies the constraints.

Optimization completed because the objective function is non-decreasing in 
feasible directions, to within the default value of the function tolerance,
and constraints are satisfied to within the default value of the constraint tolerance.




x =

    2.5000
    2.5000

4.2. Linear regression

Data fitting or approximation

Given $(x_1,y_1), (x_2,y_2),\cdots,(x_m,y_m)$, find $y_i \approx \theta_1 x_i + \theta_0$

such that $ \min_{\theta_1,\theta_2} \; \sum \limits_{i=1}^m(\hat{y}_i-y_i)^{2}$

Optimization problem: find $\theta_1$ and $\theta_0$ such that minimize total sum of $\text{error}^{2} \; ( \text{error}_i = \big \vert \hat{y}_i-y_i \big \vert )$ + no constraints on $\theta_1$ and $\theta_0$


$$\implies \quad \text{Let} \quad X = \begin{bmatrix} x_1 & 1 \\ x_2 &1 \\ \vdots & \vdots \\ x_m & 1 \end{bmatrix}, Y = \begin{bmatrix} y_1 \\ y_2 \\ \vdots \\ y_m \end{bmatrix} $$


Remark) norm of vector $u = \begin{bmatrix} u_1 \\ u_2 \\ \vdots \\ u_m \end{bmatrix} = \Vert u \Vert = \sqrt{u_1^{2}+u_2^{2}+\cdots+u_m^{2}}$


$$\begin{align} X\Theta & = \begin{bmatrix} x_1 & 1 \\ x_2 & 1 \\ \vdots & \vdots \\ x_m & 1 \end{bmatrix} \begin{bmatrix} \theta_1 \\ \theta_0 \end{bmatrix} = \begin{bmatrix} \theta_1 x_1 + \theta_0 \\ \theta_1 x_2 + \theta_0 \\ \vdots \\ \theta_1 x_m + \theta_0 \end{bmatrix} \\ \\ X \Theta - Y & = \begin{bmatrix} \theta_1 x_1 + \theta_0 - y_1 \\ \theta_1 x_2 + \theta_0 - y_2 \\ \vdots \\ \theta_1 x_m + \theta_0 - y_m \end{bmatrix} \\ \\ \Vert X \Theta - Y \Vert^{2} & = (\theta_1 x_1 + \theta_0 - y_1)^{2} + \cdots + (\theta_1 x_m + \theta_0 - y_m)^{2} \\ & = \min_{\theta_1,\theta_2} \; \sum \limits_{i=1}^m(\hat{y}_i-y_i)^{2} \\ & \implies \min_{\Theta} \Vert X \Theta - Y \Vert^{2} \end{align} $$


$$\begin{align} & (X \Theta - Y)^T(X \Theta - Y) \\ & = (\Theta^T X^T - Y^T)(X \Theta - Y) \\ & = \Theta^T X^T X \Theta - \Theta^T X^T Y - Y^T X \Theta + \mathbf{Y}^T Y \\ \\ \implies & \frac{1}{2} \Theta^T (2 X^T X) \Theta - 2 Y^T X \Theta \end{align} $$


use quadprog($ 2 X^T X$, $- 2 X^T Y$)

In [16]:
% data
x = [0.1 0.4 0.7 1.2 1.3 1.7 2.2 2.8 3.0 4.0 4.3 4.4 4.9]';
y = [0.5 0.9 1.1 1.5 1.5 2.0 2.2 2.8 2.7 3.0 3.5 3.7 3.9]';

m = length(x);
X = [x ones(m,1)];

H = 2*X'*X;
f = -2*X'*y;

theta = quadprog(H,f);
yhat = X*theta;

figure(1),  clf
plot(x,y,'o',x,yhat,'linewidth',2)
Out[16]:
Minimum found that satisfies the constraints.

Optimization completed because the objective function is non-decreasing in 
feasible directions, to within the default value of the function tolerance,
and constraints are satisfied to within the default value of the constraint tolerance.
In [17]:
%%javascript
$.getScript('https://kmahelona.github.io/ipython_notebook_goodies/ipython_notebook_toc.js')