Question: Help with a generic python algorithm for the traveling salesperson coding problem. 6 6 . 1 A Genetic Algorithm for the Traveling Salesperson Problem In
Help with a generic python algorithm for the traveling salesperson coding problem.
A Genetic Algorithm for the Traveling Salesperson Problem
In this lab, you are tasked with writing the critical portions of a genetic algorithm to solve the traveling salesperson problem TSP
Specifically you will write a routine gatsp initialpopulation, distances, generations which is given an initial population
of valid Hamiltonian paths initialpopulation, which is a list of paths tuples a dictionary of distances distances and the
number of generations generations that your algorithm should run. After completing the requisite number of generations, gatsp
should return the best path tuple from the final generation.
Note that a generation is defined as a single iteration of the algorithm parent selection, crossover, selection of the fittest population
using a steady population across generations the same number of children as there are parents If you use a variation of the above
approach, such as creating half the number of children compared to the population size, you'll need to perform twice the number of
iterations generations
Details
A path is a tuple listing all "cities" in the path. It assumed the path is a Hamiltonian circuit that completes by taking an edge from the last
city listed back to the first city listed. Do not list the first city twice at the start and end of the list
We are providing several supporting routines and files:
util.py contains three utility routines. costpath distances will return the cost of a path. As a debugging aid, cost checks that the path and distance dictionary are valid and will return None if not this should cause your code to throw a TypeError if you use the return value Note that cost may not detect all invalid paths. validpathpath confirms the path is internally sound no repetitions bestpathlistofpaths, distances returns the best lowest cost path in listofpaths.
loaddistfilename will load a dictionary of distances from the file named filename and can be imported from loaddist.py The format of filename is individual lines of the form A B distance" where A and B are city names and distance is a positive integer distance. A line causes the distance from A to B and B to A to be loaded into the dictionary. A line starting with # is a comment. Comment lines and blank lines are ignored. loaddist also ensures that the distance from a node to itself is always zero. You can update the A to B B to A distance later in the file so if you want to slightly change the topology aka distances you can just add a custom update to the end of the file. loaddist returns a list of all the city names and a dictionary of distances. loaddist returns None, None if the file did not load properly.
tsppy if run at the command line with a filename, so python tsppy filename will load distances from filename, compute an initial population, and call gatsp to run generations and report if gatsp found a better result than was in the initial population.
initpop.py contains two routines to create an initial population. These routines are called in tsppy One version of the routine, initpoplist dist take a list of cities and the distance matrix and generates a random set of initial paths of appropriate size. The other version, initlousy intentionally creates an initial set of paths with high costs which may be useful for debugging. Instructions for using intlousy are in the file and in comments in tsppy
twoopt.py contains the opt algorithm described in class, designed to use the lab's cost function. You are not required to use opt in your implementation. It is provided solely for your convenience.
We are providing two sample distance files: DISTsimple is a simple city world with a clear best path. DISTbasic is a city world divided up into districts clouds
More Details
If gatsp is given None for any argument it should return None. If generations is not positive, gatsp should return None. You can assume distances and initialpopulation are valid if nonNone.
Performance
We have no bigO performance requirements for this assignment. Rather we will run some tests designed to see if your genetic algorithm finds better results than the best path in the initial population you can assume we'll rig the initial population so a range of better solutions are feasible
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
