Representing the Solution
Travel Order: How to Represent?
Ideas?
1. Store whole path (vector of street segments)
between all delivery locations?
– Describes everything
– But time consuming to change
• Re-run path-finding algorithm on each delivery order change
2. Better: store order of delivery locations
– Turn into a full path (vector of street segments) only
when needed
– Faster & easier to change order
Solution Representation
Given 2NSolution
pick up / dropRepresentation
off locations (intersections)
1
5
4
6
7
3
0
11
10
2
Given M courier truck depots (intersections)
vector<int> deliveryOrder;
deliveryOrder = {0, 6, 5, 4, 3, 2, 7, 1}
12
What about Depots?
What
About
the
Depots?
Given 2N pick up / drop off intersections
1
5
4
6
7
3
0
11
10
12
2
Given M courier truck depots (intersections)
Could store:
deliveryOrder = { , 0, 6, 5, 4, 3, 2, 7, 1,
}
Don’t have to: can fill in closest depot to start/end delivery at end
Can Have Repeated Intersections
8 B
1
A B
5
4
6
7
3 A
0
1
0
deliveryOrder = {
Still works!
2
2
, 0, 6, 5, 4, 3, 2, 7, 1, 8, 1,
}
Local Permutations
• Representation
deliveryOrder = {2, 3, 4, 5, 6, 0, 7, 1}
1
5
8
15
5
18
4
6
5
4
3
0
4
2
2
Total travel time = 81
7
20
Local Permutations
• Swap order of two deliveries?
deliveryOrder = {2, 3, 4, 5, 6, 0, 7, 1}
deliveryOrder = {2, 3, 4, 5, 6, 0, 1, 7}
1
5
8
15
14
5
4
6
5
4
3
0
4
2
2
Total travel time = 72
18
23
7
20
7
2-opt
deliveryOrder = {2, 3, 4, 5, 6, 0, 7, 1}
Delete two connections in path
1
5
4
6
7
3
0
2
2-opt
• Reconnect connections differently
deliveryOrder was {2, 3, 4, 5, 6, 0, 7, 1}
deliveryOrder = {2, 5, 4, 3, 6, 0, 7, 1}
General:
1. Pick order for 3 sub-paths
2. Reverse any or all sub-paths
1
3. Check if legal
5
4
6
7
3
0
2
Escaping Local Minima
Say We’re In This State
0
3
1
2
4
7
5
8
6
9
deliveryOrder = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
Local perturbation to improve?
Swap Order of Two Deliveries?
0
3
1
2
4
7
5
8
6
9
2, 2,
3, 4, 5, 6, 7, 8, 9}
deliveryOrder = {0, 1, 3,
No swap of two deliveries can improve!
Stuck in a local minimum
2-Opt?
0
3
1
2
4
7
5
8
6
9
deliveryOrder = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
Path cut into 3 pieces
2-Opt?
0
3
1
2
4
7
5
8
6
9
deliveryOrder = {0, 1, 2, 6,
3, 5,
4, 4,
5, 3,
6, 7, 8, 9}
Reconnected: worse!
2-Opt?
0
3
1
2
Reconnected
differently:
now better!
4
7
5
8
6
9
deliveryOrder = {0, 1, 2, 7,
3, 8,
4, 9,
5, 6, 5,
7, 4,
8, 3}
9}
More powerful permutation operator
can escape more local minima
2-opt: Complexity
• 2N-1 edges/connections between delivery locations
• How many different 2-opts?
– (2N-1) choose 2 edges we could cut
• N = 100 199 * 198 / 2 = ~20,000 options
– 3 sub-paths
• Can order in 6 ways
• 23 options to reverse or not 3 sub-paths
Can reverse or recombine in 48 ways
– Total: ~1,000,000 2-opts
– Fast code can try them all
• If you find an improvement, can try all 2-opts again
– Path has changed a 2-opt that didn’t work before now might
• Could make your own perturbation algorithm
– Maybe 2-opts not best for traveling courier many illegal
• 3-opts?
Heuristic 4: Hill Climbing
• One way to get out of a rut
– Change something!
– Even if it looks like a bad idea at first
• Hill climbing
– Change delivery order
– Even though it increases travel time
• Good idea to save best solution first
– Then try local perturbation around that new
solution
– Maybe you find something better!
– If not, eventually can go back to saved solution
– Lets you explore more options
Exploring the Space
Worse
Travel
Time
Better
Global Minimum
Local Minima
Solution
Really a 200-dimensional space for N = 100. Huge!
Pick a Random Order?
Random Solution
Worse
Travel
Time
Better
Solution
Solution very poor many more bad solutions than
good in this space
Multi-Start?
Best of 4 random orders
Worse
Travel
Time
Better
Solution
A bit better, but still not very good
Space very big, with mostly bad solution points
Greedy Algorithm?
Worse
Greedy
Travel
Time
Better
Solution
Much better!
Still not locally optimal
Local Permutation (Iterative Improvement)?
Worse
Travel
Time
Better
Solution
Search Local Space
Finds Local Minimum!
Combine with Multi-Start?
Worse
Travel
Time
Better
Solution
Yes – Find Many Local Minima
Take Best One
Local Permutations: Can Get Stuck
Worse
Travel
Time
Better
Solution
Can’t Get Out of Local Minimum
More Powerful Local Permutations
Worse
Travel
Time
Better
Solution
Explore Larger Part of Space Less Prone to Getting Stuck
But More Exploration Means More CPU Time Need Balance
Hill Climbing?
Worse
Travel
Time
Better
Solution
Can Get You Unstuck from Local Minima
But Watch CPU Time Need Time to Improve Solution
After It Gets Worse
Simulated Annealing
• Annealing metals
– Cool slowly
– Atoms gradually move to
low energy state
– Strong metal
• Simulated annealing
– Optimization framework that mimics annealing
– Start with poor initial solution & high
temperature (T)
– Perturb solution (move atoms)
• Most perturbations accepted when T high
• Only good ones (reduce cost/energy) accepted
when T approaches 0
Simulated Annealing
S = InitialSolution;
C = Cost (S);
// E.g. travel time
T = high temperature; // big number
while (solution changing) {
How to perturb?
Snew = perturb (S);
Smaller perturbations
Cnew = Cost (S);
as T drops?
C = Cnew - C
if (Cnew < C) ||
{ random(0,1) < e-C/T ) {
S = Snew;
// Update solution
Fast update methods
C = Cnew;
will let you evaluate
}
more perturbations
T = reduceTemp (T);
}
How quickly to reduce T?
© Copyright 2026 Paperzz