Mental Health and Wellness Resources Health and Wellness (416-978-8030) or Koffler Student Services. Good 2 Talk (1-866-925-5454): 24 hour helpline Other 24/7:Gerstein Centre and Toronto Distress Centre at 416-408-HELP (4357). CAMH’s Center for Addiction and Mental Health at 250 College Street.
M4 Iterative Algorithms, Continued
Solution Representation What About Depots? Solution Representation Given N pick up / drop offs 2 1 3 0 2 3 2 0 1 1 Given M courier truck depots (intersections) Don’t have to store depots Could fill in closest depot to start/end later deliveryOrder = {0, 0, 0, 1, 3, 3, 1, 2, 2, 2}
Local Permutations Representation deliveryOrder = {1, 3, 2, 1, 3, 0, 0, 2} 2 1 8 15 5 2 18 3 5 0 4 20 3 0 4 1 2 Total travel time = 81
Local Permutations Swap order of two deliveries? deliveryOrder = {1, 3, 2, 1, 3, 0, 0, 2} deliveryOrder = {1, 3, 2, 1, 3, 0, 2, 0} 2 1 8 15 14 5 2 18 23 3 5 0 4 20 3 7 0 4 1 2 Total travel time = 72
2-opt deliveryOrder = {1, 3, 2, 1, 3, 0, 0, 2} Delete two connections in path 3 0 0 1 1 2 2 3
2-opt Reconnect connections differently deliveryOrder = {1, 3, 2, 1, 3, 0, 0, 2} deliveryOrder = {1, 1 2, 3, 3, 0, 0, 2} General: Pick order for 3 sub-paths Reverse any or all sub-paths Check if legal 3 0 0 1 1 2 2 3
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?
Heuristics Overview
Really a 200-dimensional space for N = 100. Huge! Exploring the Space Worse Travel Time Local Minima Global Minimum Better Solution Really a 200-dimensional space for N = 100. Huge!
Solution very poor many more bad solutions than good in this space 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? Travel Time Solution 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
Still not locally optimal Greedy Algorithm? Greedy Worse 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 N=100: about 1M 2-opts, but 500M 3-opts Better Solution Explore Larger Part of Space Less Prone to Getting Stuck But More Exploration Means More CPU Time Need Balance
Hill Climbing? Travel Time Solution 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
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
Simulated Annealing Annealing metals Simulated annealing 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) { Snew = perturb (S); Cnew = Cost (S); C = Cnew - C if (Cnew < C) { S = Snew; // Update solution C = Cnew; } T = reduceTemp (T); How to perturb? Smaller perturbations as T drops? || random(0,1) < e-C/T ) { Fast update methods will let you evaluate more perturbations How quickly to reduce T?
Time Limits & Using Multiple CPUs
Managing the Time Limit Can get better results with more CPU time Multi-start: Run algorithm again with a new starting point Keep best Iterative improvement Keep looking for productive changes But 30 s limit to return solution For every problem Regardless of city size & num intersections How many starting points? How many iterative perturbations? Solution: check how much time has passed End optimization just before time limit
Time Limit #include <chrono> // Time utilities #define TIME_LIMIT 30 // m4: 30 second time limit int main ( ) { auto startTime = std::chrono::high_resolution_clock::now(); bool timeOut = false; while (!timeOut) { myOptimizer (); auto currentTime = std::chrono::high_resolution_clock::now(); auto wallClock = std::chrono::duration_cast<chrono::duration<double>> ( currentTime - startTime); // Keep optimizing until within 10% of time limit if (wallClock.count() > 0.9 * TIME_LIMIT) timeOut = true; } ... Inside namespace chrono Static member function: can call without an object Time difference Gives actual elapsed time no matter how many CPUs you are using Time difference in seconds