Optimization in Control and Robotics
Lab 2. Discrete Optimization
Activity 1
The code below is a simple implementation of the Dijkstra()algorithm.
   function [P, di] = Dijkstra(G, s, d)
       % This is an implementation of the Dijkstra´s algorithm, which finds the minimal
       % cost path between two nodes.
         % the inputs of the algorithm are:
         % G: the graph object
         % s: source node index (s in (1:n))
         % d: destination node index (s in (1:n))
         % the outputs of the algorithm are:
         % P: the path starting at s and ending at d
         % di: the minimal distance between s and d in the graph G
         n=size(G.Nodes,1);   %   # of nodes
         S=s;                 %   S is the set of analyzed nodes.
         C=setdiff(1:n,s);    %   C is the rest of nodes
         dist(1:n)=inf;       %   it stores the shortest distance between s and i
         prev(1:n)=nan;       %   it stores the previous node in the shortest path
         dist(s)=0;
         while S(end)~=d
             candidate=neighbors(G,S(end));      % candidate if connected with the last node entered in S
             candidate=setdiff(candidate,S);     % candidate if not already in S
             for i=candidate'
                 distp=dist(S(end))+G.Edges.Weight(findedge(G,S(end),i));
                 if distp<dist(i)
                     dist(i)=distp; prev(i)=S(end);
                 end
             end
             [a,b]=min(dist(C));
             S=[S,C(b)];
             C=setdiff(1:n,S);
         end
         % build the path
         P=d;
         while P(end)~=s
              P=[P,prev(P(end))];
         end
         P=fliplr(P);
         di=dist(d);
   end
   •     Apply it to calculate the shortest path between the two points in the maze of the figure.
    To do this, it is necessary to convert the maze into a graph and apply the Dijkstra
    algorithm. We propose two ways to do this: Break down the maze into square cells and:
           1. Any cell is a node that can be connected to at most 4 nodes (cells). Two nodes
              are connected by an edge if there is not a wall between them. The weights
              of all edges are 1.
           2. Any cell surrounded by at most one wall is a node. In these cells there is more
              than one possible path (crosspaths). The edges in this graph no longer always
              have weight equal 1, but it is the minimum distance of a path in the maze
              connecting the two nodes that does not pass through another node.
    In ATENEA you can find the edges for the first approach (Graph1.txt) and the edges and
    weights for the second approach (Graph2.txt). Apply the Dijkstra algorithm using both
    approaches and compare the results.
•   Compare the previous result            with   the   one    obtained    using    function
    shortestpath()of MATLAB.
•   Translate the shortest path problem using the binary linear programming formulation
    and solve it using function intlinprog() of MATLAB. Compare the results with the
    previous methods.
Activity 2
Let’s consider 15 of the most important cities in US. The table below contains the distance by
road between all pairs of these cities. The data of this table is in the file distances.txt
                  Atlanta
                              Boston
                                           Chicago
                                                         Dallas
                                                                      Denver
                                                                                   Houston
                                                                                               Las Vegas
                                                                                                               Los Angeles
                                                                                                                               Miami
                                                                                                                                           New Orleans
                                                                                                                                                           New York
                                                                                                                                                                          Phoenix
                                                                                                                                                                                        Francisco
                                                                                                                                                                                        San
                                                                                                                                                                                                    Seattle
                                                                                                                                                                                                                  D.C.
                                                                                                                                                                                                                  Washington
Atlanta                     0 1095              715        805        1437               844 1920                 2230           675              499         884           1832           2537 2730                   657
Boston             1095                0        983 1815              1991            1886 2500                   3036 1539                  1541             213           2664           3179 3043                   440
Chicago               715        983                 0     931        1050            1092 1500                   2112 1390                       947         840           1729           2212 2052                   695
Dallas                805 1815                  931               0      801             242 1150                 1425 1332                       504 1604                  1027           1765 2122                 1372
Denver             1437 1991                 1050          801                 0      1032        885             1174 2094                  1305 1780                         836         1266 1373                 1635
Houston               844 1886               1092          242        1032                   0 1525               1556 1237                       365 1675                  1158           1958 2348                 1443
Las Vegas          1920 2500                 1500 1150                   885          1525                 0           289 2640              1805 2486                         294          573 1188                 2568
Los Angeles        2230 3036                 2112 1425                1174            1556        289                        0 2757          1921 2825                         398          403 1150                 2680
Miami                 675 1539               1390 1332                2094            1237 2640                   2757                 0          892 1328                  2359           3097 3389                 1101
New Orleans           499 1541                  947        504        1305               365 1805                 1921           892                     0 1330             1523           2269 2626                 1098
New York              884        213            840 1604              1780            1675 2486                   2825 1328                  1330                     0     2442           3036 2900                   229
Phoenix            1832 2664                 1729 1027                   836          1158        294                  398 2359              1523 2442                              0       800 1482                 2278
San Francisco      2537 3179                 2212 1765                1266            1958        573                  403 3097              2269 3036                         800              0      817           2864
Seattle            2730 3043                 2052 2122                1373            2348 1188                   1150 3389                  2626 2900                      1482            817               0      2755
Washington D.C.       657        440            695 1372              1635            1443 2568                   2680 1101                  1098             229           2278           2864 2755                      0
We want to select a set of scheduled services among these 15 cities so that we can go from any
city to any other city using some of these services (probably making more than one transfer
through other cities).
A scheduled service consists of a road transport from one city to another. Always the return
service exists, that means, for instance, if there is a service from San Francisco to Los Angeles,
there exist also the service from Los Angeles to San Francisco. That is equivalent to consider
the graph as an undirected graph.
Which is the selection that minimize the sum of distances of all services?
       •   Solve the problem by implementing the Prim algorithm.
       •   Solve the problem using the function minspantree() of MATLAB;
       The code below is a simple implementation of the prim()algorithm.
                 function [mcost,E] = prim(D)
                     [n,n] = size(D);         % The matrix is n by n, where n = # nodes.
                     C = [1];    nC=1;        % C= nodes selected. nC= #C
                     E = [];     nE=0         % k is the number of edges selected
                     S = [2:n]';nS=n-1;       % S= nodes not selected. nS=#S
                     mcost=0;
                     while nC < n
                       mincost = Inf;
                       for i=1:nC
                         for j=1:nS
                           ni = C(i); nj = S(j);
                           if D(ni,nj) < mincost
                              mincost = D(ni,nj);
                              u = ni; v = nj;       % Save nodes and edge selected
                           end
                         end
                       end
                       mcost=mcost+mincost;
                       E=[E;[u,v]]; eE=nE+1;        % Add this edge to tree.
                       C=[C;v];nC=nC+1;
                       S=setdiff(S,v); nS=nS-1;
                     end
                 end
Activity 3
Transportation companies face daily problems in logistics. Let’s consider the following ‘simple’
logistics problem:
       An airline cargo company has an airplane that flies from the UK to Spain on a daily basis
       to transport some cargo. Before the flight, it receives bids for deliveries from (many)
       customers. Each bid contains the weight of the cargo item to be delivered, the amount
       costumers are willing to pay and some other observations. The airline is constrained by
       the total amount of weight the plane is allowed to carry. The company must choose a
       subset of the packages (bids) to carry on the plane in order to maximize the total profit,
       taking into account the weight limit that they must respect and all other constraints.
             Bid      Weight      total                      Observations
                      (tons)      price
             1        2           200 €      This bid is compulsory.
             2        15          500 €      Bids 2 and 9 cannot be selected together.
             3        15          600 €      Bid 3 can only be chosen if both bids 2 and 6 are
                                             selected together.
             4      42                1000 €            Bids 4 and 5 must be chosen together, or both
                                                        or none.
             5      15                300 €
             6      10                600 €             It is compulsory to choose exactly two bids out
                                                        of these three bids: 6 7 and 8.
             7      20                800 €
             8      25                800 €
             9      5                 300 €             Bids 2 and 9 cannot be selected together and bid
                                                        9 can only be chosen if bid 8 is already selected.
             10     16                600 €
             11     15                600 €             It has to choose at least one of these two bids.
             12     23                1000 €
       If the airplane capacity is 𝑊 = 100 tons, which bids have to be chosen in order to
       maximize the profit?
Activity 4
Solve the following instance of the minimum cost flow, that is, calculate the flow at each of the
20 directed edges to supply goods from supply nodes 1 and 3 to demand nodes 2 and 4
satisfying the capacity constraints and that minimize the total cost.
       n=9;                        % # nodes
       m=20;                       % # edges
       %                            1 1 1 1 1   1   1    1   1   1   2
       % 1 2 3 4 5    6   7   8   9 0 1 2 3 4   5   6    7   8   9   0     edge
       S=[1 3 3 3 5   5   5   6   6 6 7 7 7 7   7   8    8   9   9   9];   % starting node
       E=[5 7 8 9 4   6   7   2   5 7 2 4 5 6   9   7    9   2   6   8];   % ending node
       C=[3 6 4 5 9   5   4   6   6 4 5 4 7 3   5   5    3   5   2   5];   % cost
       W=[9 4 3 2 4   2   5   4   5 4 5 4 5 2   4   5    4   3   5   3];   % capacity
       % 1 2 3 4      5   6   7   8 9                                      node
       B=[9 -7 6 -8   0   0   0   0 0 ];                                   % supplies and demands
       a) Compute using as short a code as possible the parameters of the optimization
          problem (objective function, matrix and RHS of linear constraints and lower and
          upper bounds).
       b) Check that it is not necessary to use integer constraints, that is, compare the solution
          using integer and real variables.
Activity 5
Use binary integer programming to solve the classic traveling salesman problem. This problem
involves finding the shortest closed tour through a set of points.
             a) Generate a set of 30 random points inside the square of opposite vertices (0,0)
                and (10,10).
             n=30;                       % number of points
             P=zeros(n,2);               % inicialize point's coordinates
             D=zeros(n,n);               % inicialize distance matrix
             rectangle('Position',[0 0 10 10])
             axis([-1 11 -1 11])
             P=rand(n,2)*10;             % generate n random points
             hold on
             plot(P(:,1),P(:,2),'rx')    % draw the points as red crosses
             for i=1:(n-1)               % calculate distance matrix
                 for j=(i+1):n
                     D(i,j)=norm(P(i,:)-P(j,:));
                     D(j,i)=D(i,j);
                 end
             end
             b) Consider 𝑛2 binary variables 𝑥𝑖𝑗 and solve the following binary linear problem:
                                                 𝑛
                                            min ∑ 𝑑𝑖𝑗 · 𝑥𝑖𝑗
                                                𝑖=1
                                        𝑛
                                     st ∑ 𝑥𝑖𝑗 = 1        𝑖 = 1…𝑛
                                        𝑗
                                        𝑛
                                     st ∑ 𝑥𝑖𝑗 = 1        𝑗 = 1…𝑛
                                        𝑖
             c) Add suitable variables and constraint to solve the TSP and depict the solution.