Lec 33
Lec 33
Prof. G. Srinivasan
Department of Management Studies
Indian Institute of Technology, Madras
Lecture - 39
Bin Packing and Travelling Salesman Problems
(Refer Slide Time: 00:13)
In this lecture, we continue the discussion on the bin packing problem. The application to
transportation model comes from, assuming that we have a central warehouse and we
have to transport to a large number retailers or customers and there is a point to point
transportation, which means a truck will go from here to this and then it will come back
to the starting point and then it will go to the next customer after picking up material
from here and so, on.
So, the distance are time the truck takes from the warehouse, to customer j can be given
as d j or C j or t j, if it is the time taken, so this time would include the loading the
unloading as well as the transportation time. Now given n customers, where customer j
requires a time of t j and if a vehicle is available for T time units, the problem is to find
out, which of these customers are orders will be allotted to each of these vehicles. Such
that, the number of vehicle is minimized and the sum of the t js allotted to each vehicle
will be less than or equal to T.
As mentioned in the previous lecture, this reduces to what is called, a one dimensional
Bin packing problem, where the one dimension is time here, time required by each of the
customers, to be compared with time available with each of the trucks. So, the problem is
to find out the minimum number of trucks, now we explain it through a numerical
illustration.
(Refer Slide Time: 02:42)
So, we would, so we assume that, there are 10 requirements and the time, for each of
these requirements are given in these 10 numbers and then the total time availability is
80. So, the problem is to find out the minimum number of trucks, such that we are able to
meet all the demand, so now t j is given here, now we have to find out, which of these
items, now we can treat each one of these as items that goes into a bin, the bins length is
80 items have lengths 11, 73, 13, 37 etcetera.
So, which of these items will go to which of these bins, such that we minimize the
number of bins, as well as ensure that the length of items that go into a bin does not
exceed the bin length. So we have seen the formulation in the previous lecture. So we
now assume that, Y j equal to 1, if bin j is chosen and X i j equal to 1, if item, i goes to
bin j. Now, we know that with 10 bins, we can definitely meet all the requirements, so
we are going to assume that, there are smaller sub set of Y 1 to Y 10, which we would
like to minimize.
So, the objective is to minimize sigma Y j, where Y j equal to 1, if bin j is chosen. This
depends on the notation, now we are using j for the bin and i for the item, therefore
summation Y j, j equal to 1 to 10. Now, each bin each item is to be associated to only
one bin, so sigma X i j summed over j is equal to 1, for every i, which means each item, i
will go to exactly one bin.
And then for each bin t i X i j is less than or equal to 80 Y j. Now X i j equal to 1, if item
i goes to bin j. So for a for a particular bin, sum of the X i js is the number of items that
are attached to that bin. Sigma t i X i j is the length of sum of the lengths of the items
attached to that bin. So, this is the sum of the lengths of the items attached to bin j and
this should be less than equal to 80 and we can assign i to j only when it chosen, only
when Y j is 1.
Therefore, this constraint less than equal to 80 Y j or capital T Y j will ensure that, the
not only items will be assigned to bins that are chosen, it will also ensure that when items
are assigned a sum of the lengths does not exceed this 80. And then we have Y j and X i
j as binary variables, now this is a binary integer programming formulation, this can be
solved optimally using an I P solver.
But, right now as I mentioned for 10 items, we will have 10 variables here and 100
variables here, so we will have 110 variables and then there will be 10 constraints here,
there will be 10 constraints here, so there will be 20 constraints. Now, this constraint is
for every item this constraint is for every bin, so there will be 110 variables and 20
constraints, so in general, if there are n items, we would have n square plus n variables
and 2 n constraints.
Now let us we can solve this optimally and when we solve this optimally, we would get a
solution, which would be like this. Y 1 equal to 1, Y 1 equal to Y 2 equal to Y 3 equal to
Y 4 equal to 1, which means 4 bins are chosen. So, the item with 73 goes to this bin
items with 11, 13, 37 and 19 go into this bin. The item with 51 and 29 go into this bin
and the item 17, 39 and 23 go to this bin. So the solution will be this will be item number
1, so X 1 1, X 3 1, X 4 1 and X 10 1 and so on.
Now, the sum of these is eleven plus 19 is 30, 43, so 80, 73, 51 plus 29 is 80. 17 plus 39
is 56, 56 plus 23 is 79. So we find a solution with 280s and 79 and a 73. Now, let us
assume, we want to solve this using heuristics, several heuristics are available, we will
see 1 or 2 of them, so most of these heuristics first sort these items in the decreasing
order.
(Refer Slide Time: 10:00)
would not go into this bin, it would not go into this bin, it will go into this bin 23 plus 19
is 42 42 plus 17 is 59, so it will go into this bin.
Now, look at the 13, it would not go into this bin, it would not go into this bin, it would
not go here, now here it is 23 plus 19, 42, 42 plus 17 is 59, 59 nine plus 13 is 72, so it
will go into this bin. And then take the last one, it will not go here, it would not go here,
it would not go here, now let us see this is 72, so it would not go here, so create a 5th bin
with 11. So, now, this method gives us 5 bins, whereas the optimum gives us 4 bins, it is
a heuristic method, it does not guarantee optimum all the time, therefore it would do that.
Now this, this method gives us it is a minimization problem, so a heuristic algorithm
would give, what is a feasible solution to the problem will be an upper bound to a
minimization problem, so the number of bins 5 is on upper bound to the optimum value,
which is 4. Now, this is a very popular heuristic, this is called the next best decreasing
heuristic, next fit decreasing heuristic, it is called the next fit decreasing heuristic,
important thing, it is a decreasing heuristic, because the items are already arranged in
decreasing or non increasing order, therefore, it is a decreasing heuristic.
In fact, this is called the first fit decreasing heuristic, this is called the first fit decreasing
heuristic, which means every item, we see, we take it, we see whether it will go to the
first bin, if not we will see, whether it will go to the next bin and then it will go to the 3rd
and then so on. So, whichever it fits first it will be put there, so it is called a first fit
decreasing heuristic, now we have other heuristics some of them are called best fit
decreasing heuristic and so on.
So, if there is more than 1, where it can fit, now at that point, we try and fit it into the 1
where it fills up to the maximum, that is called a best fit decreasing and then the worst fit
decreasing would be, if there is a choice, put in the bin, where it would actually fill the
minimum after filling. So, like that we can develop different kinds of heuristics for this
particular problem, now one of the advantages of using this heuristic is now here, we
have a solution with 5 bins.
So, we know that, there is a solution with 5 bins and once, we know that, there is a
solution with 5 bins or 5 trucks in this example; we are only interested in solutions,
which are 4 or lower, because we already have a solution with 5. So, using a very simple
heuristic like this, we get 5 bins and if we still want to solve it optimally and we want to
check whether, 4 is possible then we have to come back.
(Refer Slide Time: 16:13)
And solve this optimization problem, but the nice thing about, this is now you be do not
have to define j equal to 1 to 10, it is enough to define j equal to 1 to 4, because we
already have a solution with 5, so it is enough to define j equal to 1 to 4. And now see
that simply define Y j equal to 1, for j equal to 1 to 4 and X i j equal to 1, if item I goes
to bin j, so we will have 4 plus 40, which is 44 variables. And then we will have 14
constraints n square plus n no here it will change now, this one be n square plus n.
Now, here the number of variables Y j will be 4, X i j will be 40, so we will have 44
variables and in terms of constraints, this is for every i, so there are 10 constraints, this is
for every j, there are 4 constraints, so there are 44 variables and 14 constraints, whereas
in the old formulation, if we use 10 bins then we would have 110 variables and 20
constraints.
So, here are a set of problems, where it is actually advantageous to solve it, using a
heuristic first and then based on the heuristic solution, if we approach the optimization,
we observe that the number of variables and constraints in the optimization will come
down.
So, that is one advantage of using this heuristic. At the same time, if we are all right with
the heuristic solution, then we could say that well, this solution would be acceptable to
us. But, if we actually look at this solution and then we can tweak this solution, now that
is not part of the best fit decreasing heuristic, the best fit decreasing heuristic would stop
here, because it is interested in minimizing the number of bins and this heuristic gives 5
bins.
But, then we could do many things, if you look at the loads, it is 73, 80, 76, now this will
become 72 and this is 11, now we can do a few more things by adjusting some of them
by a little adjustment one can try and actually, reduce the number of bins from 5 to 4.
But, if we were to implement this solution particularly for our truck example, then we
observe that one truck is going to be used for 73 minutes another for 80, 76, 72 and 11.
So when we start applying this heuristic to the situation that, we are talking about, we are
also interested in some kind of a load balance among the trucks.
Because, it is also hard to say that, one truck will be utilized only for 11 minutes, while
few trucks are utilized for 80 76 73 and so on, so we also need to have a secondary load
balancing objective, if we were looking at solving this kind of a problem. We can do few
more things, we can actually relate this problem to a traditional machine scheduling
problem, in fact, we can try and do a few more things.
Now, if we want to solve with the better load balance, for 5 bins, we can try something
else, we can now look at something like bin 1 bin 2 bin 3 bin 4 and bin 5, now we will go
back and put this 73 here. Now the next will not go to this bin we will put a 51, we will
put a 39, we will put a 37 and then we will put a 29 and then the next one, we can put a
23, so it will come here, so 29 plus 23 is 52, now 19 will come here, because if we see,
where we assign for example here.
Now at this point, the first 5 go to the first 5 bins, the 6th one is 23, so 51 plus 23 is 74,
39 plus 23 is 62, 37 plus 23 is 60 and 29 plus 23 is 52, so the best place to put that 23 is
here, because the loads will get balanced. Even though, we can put that 23 here, which
will be closer to 80, but then we realize that the loads will be will be unbalanced and then
there will be a load imbalance, therefore, the best thing to do is to put that 23 here, so
that the loads are also balanced as they can be, so we put this 23 here to get 52.
Now the next one is 19, now if we look at the loads are 73, 51, 39, 37 and 52, so this is
the smallest, so we get better load balance by putting this 19 here. Similarly, the 17 will
come here, the 13 will come here, now this 11 cannot come here, because 73 plus 11 is
84. Now we check this is 64, so this can take that 11. This is 56, this is also 56, this is 52,
so this will finally, take that 11 and now the loads for 5 bins will be 73 64 56 56 and 63.
So now, we have a solution, if we were to use this 5 bins, then this is a solution with
better load balance than this, so depending on the problem and depending on more than
one objective, we can use suitable heuristics. Now, the same problem, we can try
something more and see that, if we have 4 trucks or bins, what happens to and if we start
allocating this 10, what happens to the loads. So, 73 will come here 51 will come here,
39 will come here and 37 will come here, so automatically from a load balance point of
view, 29 will go here, because this is the smallest load 37 plus 9, 46 plus 20, 66.
Now, 23 will come here, 39 plus this will give a 62. Now 19, this is 51, so 19 will come
here, 51 plus 19 is 70. 17 cannot come here, because it will exceed, now to put this 17,
17 also will not come, 17 can come here, 51 plus 19 is 70. So, it will 7 plus 17 will
exceed, 39 plus 23 is 62, so 62 can handle is this 17, this is 66, so this will handle get the
17 and this will get this 13. And then we do not have a place to put this 11, because this
the load will exceed 80, if we add 11 here, also it will exceed 80, here the load will be 62
plus 17, 79, it will exceed 80, this will become 66 plus 13, 79, it will again exceed 80.
But, then we have to put this 11 somewhere, so we can put this 11 here and then say that,
this is 73, this is 81, this is 12 plus 7, 19, 79, this is 12 plus 7, 19, 79. Now we realize
that, the optimum solution was able to give us, 80 73 80 79, the heuristic solution gave
us 73 81 79 79. So, using this heuristic solution, now we observe that, we would not be
able to squeeze all the orders in 80 time units, if we have only 4 trucks or if we use this
heuristic.
And if we want to minimize the number of trucks, such that, I can squeeze everything
into 80, the answer is 5 whereas, the optimum is able to squeeze all the 10 items into 4
trucks or 4 bins, such that the length is 80, here it says, it requires an additional length of
81. Now, we can either solve this optimally, this is easily the best thing that, we would
like to have, but in the absence of solving the integer programming optimally and if we
rely on heuristic solution then we have to understand here, there are times, we would get
even the optimum solution through some of the heuristics, we can try several heuristics.
Now, we will able to minimize the number of bins for a given length and then within
that, we may be able to get a load balance and we may also say given certain number of
bin, what is the length of a bin that, I require. Now, it happens to be 81, what is the
minimum length that, I require, now this problem is equivalent to the machine
scheduling problem, it is called a parallel machine scheduling problem. Now, for this
particular example, if I force 4 bins and then apply a load balancing algorithm, it says
that, i may require 81 minutes on one of the trucks or one of the bins whereas, the
optimum is able to do it, within 80 minutes. Right?
We next move to the travelling salesman problem. Now here what we do is we assume
that, there is a central warehouse and then we have a set of customers. Now we - this is
like the equivalence of where we look at milk run. So, we start a truck from the
warehouse and then we meet the requirements of all these customers in one trip, which
means that even, if each of these have a requirement.
The sum of all the requirements is less than or equal to the capacity of the truck or
theoretically or mathematically, we can assume that the truck has a very large capacity,
which is close to infinity and can handle the requirements of all these customers.
Practically, we would say that, we know these requirements and the truck capacity is
higher than the requirements of all of these, in such a case that truck will start from the
warehouse, visit each customer once and only once deliver and it will come back to the
warehouse, now that is called the travelling salesman problem.
Now, if you assume one warehouse and 6 customers, we have 7 points, we can assume
that, we will have a 7 by 7 distance matrix and then starting from W, we want to visit
each customer once and only once and then we come back to the starting point. Now, we
have already seen several aspects of the TSP, in the advanced operations research course,
where I have given the formulation for, the travelling salesman problem. And also I have
explained many 2 or 3 different types of branch and bound algorithms to solve the
travelling salesman problem.
And I have also looked at a lot of heuristics extensively in that course, where we have
studied the travelling salesman problem in detail. What we will do now is will take
another look at the formulation of the T S P. And then we would also look at some
heuristics and I would also try and give you, some of the optimum solutions to the
travelling salesman problem and make it a little brief, because a more elaborate treatment
is available already in the other course.
(Refer Slide Time: 29:59)
Now, we have already seen this formulation of the travelling salesman problem. Now X i
j equal to 1, if the salesman visits city j immediately after visiting city i. Now d i j is the
distance matrix. Now, again in the context of the travelling salesman problem, we will
assume d i i that is distance between a point and itself to be infinity and not 0. So, a
distance between a point and itself, so large that, we does not visit the point immediately
after visiting it, for example, if put d 1 1, d 2 2 etcetera to be infinity or very large value,
the variables X 1 1, X 2 2, X 3 3 etcetera, will not be in the solution.
Now this will ensure that, if salesman is in a particular city X i j equal to 1 summed over
J, which means, if the person is in a particular city, the person has to visit only one city
immediately. So, that will be taken care of by this and if this person is in a particular city
he or she should have come from 1 city, so that is taken care of this. But, then we need
the extra constraints otherwise the T S P will become an assignment problem or if we
have 5 points, including the warehouse.
It may give solutions like this, which means it goes from say 1 to 2 and then 2 to 1 and
then the salesperson suddenly goes 3 to 4, 4 to 5, 5 to 3. Now they will satisfy this, so
this will be like solution to the assignment problem. Now, things like this are called
subtours and these have to be eliminated. The solution to the T S P is always a full tour,
which means you will have a solution like 1 3 4 2 5 1, this is called a full tour 1 to 2, 2 to
1, 3 to 4, 4 to 5, 5 to 3 is called a subtour.
(Refer Slide Time: 33:19)
So, we need this plus we have, what are called subtour elimination constraints. Now, if
there is a subtour let us say, there is a first, we define u i minus u j plus n X i j less than
equal to n minus 1 and then we define it for i equal to 1 to n minus 1, j equal to 2, 3 up to
n and i not equal to j.
(Refer Slide Time: 34:17)
Now, if there is a subtour, let us say let us say, there is a subtour 1 to 2 and 2 to 1 or first
let us assume that, there is a subtour, which is there is a subtour, which is 3 to 4 and then
let us say for a 5 city problem, there is another subtour that is 1 to 5. So, a subtour 3 to 4
will be eliminated here.
Because, there will be a constraint which will be say u 3 minus u 4 plus 5 X 3 4 is less
than or equal to 4 and u 4 minus u 3 plus 5 X 4 3 less than or equal to 4, there will be a
constraint like this. Where I have just written u i minus u j plus n X i j less than equal to
n minus 1, now if this, if such a subtour exist then these 2 constraints have to be satisfied.
Now, we realize that adding vertically, you are you will get, if both are equal to 1, you
will get 10 less than equal to 8, which is not true, therefore this constraint will ensure that
subtours involving say a 2 city subtour or whatever 3 4 4 3 are automatically eliminated.
The same thing will hold, for any for example, if we have 3 4 5 then there will be u 3
minus u 4 plus something u 4 minus u 5 plus something and u 5 minus u 3 plus
something, so vertically adding it will give us an inconsistent set.
Therefore, all these subtours involving this and this are eliminated, now if there are
subtours that are involve city 1, the reason one is important is because of this i equal to 1
2 to n minus 1 j 2 to 3 to something. So, if there is a subtour involving city 1, then that
subtour apparently 1 will get a feeling that, it will not be eliminated by this, for example,
if we start writing for this, then you will get a thing like.
may be n C 2 minus n constraints and so on. So, we will have of the order of n C 2
constraints here, so this will still become a large problem, when the number of cities
increases and this would roughly increase of the order of n square, so we will have a
large number of constraints. And this is a binary formulation plus a mixed formulation,
because this these set of variables are continuous, so we normally do not use this
formulation to solve the travelling salesman problem, instead we use branch and bound
algorithms.
(Refer Slide Time: 38:53)
We use branch and bound algorithms, there are several types of branch and bound
algorithms that are available; the more popular 1 is the 1 by Little and 3 other authors.
Now, this is based on the penalty approach, I have given a detail description of the
Littles branch and bound algorithm in the advanced operations course and hence I am
not repeating the Littles algorithm once again here. So, the reader the viewer could go to
the other lecture and take a look at the Littles algorithm, through which we can solve
this. Now, several heuristic algorithms are very popular for the branch and bound, for the
travelling salesman problem and for the purpose of completion, let me just explain 1 or 2
heuristic algorithms here. So, we take a 5 city T S P and explain through this.
can proceed and then choose the best out of the these solutions, that we have. In this
example, we can show that, this is actually the optimum solution and the nearest
neighbor is able to give the optimum solution right away. Then we start from 1, this is
because of the nature of the numbers that, we have in this particular example.
(Refer Slide Time: 43:17)
Another very popular heuristic is called the 3 opt heuristic, the 3 opt heuristic essentially
does the 3 way exchange, so what we do is if you have a T S P let us say a large T S P.
We have a large T S P. Now, let us assume this is a solution, so 1 2 3 4 5 6 7 8 1 this
need not be the optimum or the best solution, this is a T S P with 8 nodes or 8 cities, now
I take a solution like this and then I take one of these as a b, another pair is p q and the
third pair is u v.
Now, we realize, there are eight nodes, so a b p q u v can be chosen in many ways. We
would also realize this is a b this is p q, but then I left a node and then I chosen u v here.
Now, using this, I can actually create some feasible solutions, for example I can remove
this, I can remove this and I can remove this, now I can do this, I can add these 2, I can
add this and I can retain this, this will be another feasible solution. For example, I can
remove this, I can do this, this could be another solution. So like this we have several
possible solutions.
So, if we have a a b p q u v, then we can actually get 7 more solutions to this, we can
have a b p u q v a, p b q u v a, p b u q v a, q u b p v a, q u p b v a, u q b p v a, u q p b v a,
so where every a b p q u v chosen, we can have 7 solutions. Now, if there are n, we take
a starting solution, so there are n vertices, there are n edges in the solution, so a b, p q, u
v represent 3 out of the n, so they can be chosen in n C 3 ways and then each of these a b,
p q, u v, we can write 7 more solutions from this.
So, if we are given a T S P with say 5 vertices, then 5 C 3 is 5 X 2, which is 10, 10 into
7, we can get 70 solutions out of this. The only thing is because we have 5, then we
realize that these 70 units are not unique, there will be overlap, because when we have a
5 by 5 to chose a b p q u v, then some of them have to overlap. So, a lot of those 70 will
overlap, but then we can still chose the best of them and we can proceed. Whereas, if we
have a problem with 10 vertices, (Refer Slide Time: 47:29)
And now, we realize that we a b p q u v itself can be chosen in 10 C 3 ways, 10 into nine
into 8 by 6 another 7, so for every solution will be able to this, so we have 800 and 12 7s
are 84, we have 840 solutions, which we can get, from a single solution and then we can
keep improving this. So, 3 opt is a very popular method, one can write a simple computer
program, which would do the 3 opt heuristic and then we could get the best values of the
solution.
But then when we apply these ideas particularly to trucks and distribution and so on, we
also need to look at a little beyond this kind of a T S P and then add a few more aspects
to the T S P. Now, this T S P is symmetric, which means distance between i to j is the
same as j to i, but then when we actually apply these on practical problems, we
understand that actually the distances are not the same, because there could be one ways
and there could be different highways, you need to take when, you go from one place to
another, so we have T S P, which are not symmetric.
We also observe that particularly in critical distributions like say mail motor and other
such distributions, one would observe that actually, even if the routes are the same, the
time taken to go from one place to another will depend on the time of the day. When the
travel is made the travel is made early in the day one can reach a little faster and when
we move into peak hours, then we have, we go much slower.
So, there is a speed dimension that come in, there is a distance the speed from, which the
time is to be computed and the speed will depend on the time of the day, because most of
these also at the end of the day result, in problems were certain delivery times have to be
met and so on. So, we have to take into accounts speed of the vehicle also, we also could
have windows, time windows, where delivery to a customer has to be made either, before
a certain time or after a certain time or within a time interval.
So, all these result in handling the travelling salesman problem very effectively, in terms
of the nature of the objective function, in terms of additional constraints, that we may
have, also in terms of getting the data depending on dynamically updating and getting the
data so that the correct data is used and the corresponding T S P are formulated and
solved. The next thing that, we will have to look at is called the vehicle routing problem,
(Refer Slide Time: 50:20)
where we would have a central warehouse, we would have a set of customers, now we
realize that the demand of these customers is such that, we may require more than one
trip, the vehicle capacity cannot handle the demand of all of them. So, we get into a
situation, of which customer goes into, which trip and then how the trips are made; now
that problem is called the vehicle routing problem.
A solution to vehicle routing problem could be, one trip will be like this and the other
trip would be like this, but in general, which cities or customers will go to which vehicle,
such that we minimize the total distance. We use minimum number of vehicles and then
we are able to meet the demand is called a vehicle routing problem, we will see various
aspects of the vehicle routing problem, in the next lecture.