程序代写代做代考 algorithm 6G6Z1109: So+ware Agents and

6G6Z1109: So+ware Agents and
Op8misa8on

Term 2, Lecture 6: GA for Travelling
Salesman Problem

Travelling Salesman Problem (TSP)

•  We might also refer to this (in gender-neutral
terms) as the “Delivery Driver Problem”

•  TSP is the commonly-accepted 8tle, so we will
go with that

•  Archetypal NP-complete/NP-hard problem
(depending on formula8on of ques8on)

•  Used as a test-bed for many different
op8misa8on algorithms

TSP formula8on

•  Given a set of ci#es
distributed in space, find
the shortest possible
route that visits all ci8es
only once and then
returns to the start point

•  That is, find a circular
tour of minimum length

Wikipedia

Why is the TSP interes8ng?

•  Apart from being inherently hard (and
therefore challenging), the TSP finds many
applica8ons in various domains such as
– Transporta8on (best way for a school bus to pick
up n children, best way to make deliveries to n
loca8ons)

– Circuit board drilling (minimise “travel 8me” for
drill head to make n holes in a board)

– Crew scheduling, adver8sement loop placement,
etc. etc.

TSP formula8on
•  Some variants of the TSP
include the edges
(corresponding to a set of
roads)

•  We focus on the Euclidean
TSP, which simply
distributes ci8es in 2D
space, and assumes that
there exists a direct
straight road between any
possible pair of ci8es)

Lee Jacobson

TSP hardness

•  Why is the TSP hard?
•  For n ci8es (n>=3), there
are (n-1)!/2 possible tours

•  So, for 20 ci8es, there are
(19*18*17*16…*1)/2
possible tours

•  60,822,550,204,416,000
for this par8cular instance Lee Jacobson

TSP hardness
•  Why (n-1)!/2 possible tours?
•  For n=20, if we start at arbitrary city, A,

we then have 19 to choose from…
•  Once we move to the next city, we then

have 18 to choose from, and so on….
(which gives us the factorial)

•  n-1, because we have one fewer choice
to make (as, by default, we have to
return to the start point)

•  We divide the number of tours by two,
because we don’t care about the
direc#on of the tour (that is, ACDBE is
considered iden8cal to EBDCA)

•  A tour is a permuta#on of ci8es (that is,
an ordering of ci8es)

Lee Jacobson

A

TSP encoding
•  Given that a tour is
simply an ordering
of ci8es, it seems
natural to use a
string of city labels
to represent a
solu8on

•  Each individual,
therefore, starts
with a shuffled
(randomised)
version of the city
list as its genome

TSP evalua8on

•  We then need to calculate the length of the
tour encoded by the list of ci8es

•  This gives us the fitness of the tour (where
lower (ie. shorter) values are considered
bejer)

•  So, for the sequence DBCAE, we would add
together distances D-B, B-C, A-E, then E back
to D

New class, Map, which is a
version of the Graph class

Takes string of city
labels as input

New class, Map, which is a
version of the Graph class

For each pair of city
labels (including “wrap
around to start”)….

New class, Map, which is a
version of the Graph class

Extract the appropriate
nodes from the ordered
list, calculate distance
between them, and add

to running total

New class, Map, which is a
version of the Graph class

Distance between two points

Added to Node class

Calculates absolute distances in X and Y
(that way, it doesn’t majer which
heading the other node is from the
current node)

Then uses Pythagoras’ theorem to
calculate distance

Tiger Algebra

Processing also has a dist()
func8on…

Problem (1): Crossover

Imagine we have two genomes:
ADBCE

DBCEA
Crossing them over using standard one-point
operator will generate illegal tours…
AD|BCE

DB|CEA = ADCEA DBBCE

Visit some ci8es
more than once,
don’t visit all ci8es

Solu8on: new crossover operator

Ordered crossover: take a random subsequence
of ci8es from first parent, then fill in remaining
ci8es in the order in which they appear in the
second parent
P1: IEHDGCFBJA

P2: ABCDEFGHIJ

Ch: AEHDGCFBIJ

Solu8on: new crossover operator

Ordered crossover: take a random subsequence
of ci8es from first parent, then fill in remaining
ci8es in the order in which they appear in the
second parent
P1: IEHDGCFBJA

P2: ABCDEFGHIJ

Ch: DGCFB

Solu8on: new crossover operator

Ordered crossover: take a random subsequence
of ci8es from first parent, then fill in remaining
ci8es in the order in which they appear in the
second parent
P1: IEHDGCFBJA

P2: ABCDEFGHIJ

Ch: DGCFBI

Is I already
in child? N,
so include

Solu8on: new crossover operator

Ordered crossover: take a random subsequence
of ci8es from first parent, then fill in remaining
ci8es in the order in which they appear in the
second parent
P1: IEHDGCFBJA

P2: ABCDEFGHIJ

Ch: DGCFBIJ

Is J already
in child? N,
so include

Solu8on: new crossover operator

Ordered crossover: take a random subsequence
of ci8es from first parent, then fill in remaining
ci8es in the order in which they appear in the
second parent
P1: IEHDGCFBJA

P2: ABCDEFGHIJ

Ch: A DGCFBIJ

Is A already
in child? N,
so include

Solu8on: new crossover operator

Ordered crossover: take a random subsequence
of ci8es from first parent, then fill in remaining
ci8es in the order in which they appear in the
second parent
P1: IEHDGCFBJA

P2: ABCDEFGHIJ

Ch: A DGCFBIJ

Is B already
in child? Y,
so skip

Solu8on: new crossover operator

Ordered crossover: take a random subsequence
of ci8es from first parent, then fill in remaining
ci8es in the order in which they appear in the
second parent
P1: IEHDGCFBJA

P2: ABCDEFGHIJ

Ch: A DGCFBIJ

Is C already
in child? Y,
so skip

Solu8on: new crossover operator

Ordered crossover: take a random subsequence
of ci8es from first parent, then fill in remaining
ci8es in the order in which they appear in the
second parent
P1: IEHDGCFBJA

P2: ABCDEFGHIJ

Ch: A DGCFBIJ

Is D already
in child? Y,
so skip

Solu8on: new crossover operator

Ordered crossover: take a random subsequence
of ci8es from first parent, then fill in remaining
ci8es in the order in which they appear in the
second parent
P1: IEHDGCFBJA

P2: ABCDEFGHIJ

Ch: AE DGCFBIJ

Is E already
in child? N,
so include

Solu8on: new crossover operator

Ordered crossover: take a random subsequence
of ci8es from first parent, then fill in remaining
ci8es in the order in which they appear in the
second parent
P1: IEHDGCFBJA

P2: ABCDEFGHIJ

Ch: AE DGCFBIJ

Is F already
in child? Y,
so skip

Solu8on: new crossover operator

Ordered crossover: take a random subsequence
of ci8es from first parent, then fill in remaining
ci8es in the order in which they appear in the
second parent
P1: IEHDGCFBJA

P2: ABCDEFGHIJ

Ch: AE DGCFBIJ

Is G already
in child? Y,
so skip

Solu8on: new crossover operator

Ordered crossover: take a random subsequence
of ci8es from first parent, then fill in remaining
ci8es in the order in which they appear in the
second parent
P1: IEHDGCFBJA

P2: ABCDEFGHIJ

Ch: AEHDGCFBIJ

Is H already
in child? N,
so include

Problem (2): fitness values

•  If we are using fitness values that can take a wide
absolute range, some fitnesses can come to dominate,
leading to early stagna8on

•  Also, as we saw with 3-colouring, lots of very small
fitness values can lead to zero-sized ma8ng pool

•  Also, situa8on is complicated by the fact that we are
minimising the func8on

•  Solu8on: use a different selec#on operator that allows
us to easily compare fitnesses (to minimise, and to
allow for very small fitnesses) and doesn’t suffer from
excessive selec8ve pressure

Tournament selec8on

•  Essen8ally, sample a random selec8on of n
popula8on members, then just pick the one with
the lowest tour length (ie., the “winner)

•  n is called the tournament size, and can be tuned
like other parameters

•  We can also tune the selec#on pressure of the
operator, by only selec8ng the winner a certain
propor#on of the #me (otherwise, we just select
a random member of the tournament)

•  In prac8ce, this prevents early stagna8on

Guarantees the
first tour we check

will be the
shortest, by
default

Build the
tournament

Pick winner/
return random

individual

Lee Jacobson solu8on=940

However, our solu8on uses a popula8on of 500, rather than 50. Using a popula8on
of 50 tends to generate inferior solu8ons.

= Popula8on size can drama8cally alter performance…

Next lecture

•  Next week: Local search
•  This week’s lab: Implement ordered crossover