diff --git a/python/gen/Makefile b/python/gen/Makefile new file mode 100644 index 0000000..1bdd148 --- /dev/null +++ b/python/gen/Makefile @@ -0,0 +1,9 @@ +TSP_FILE_PATH=../../data/a280.tsp +NUM_OF_GENERATIONS=10 +OUTPUT_EVERY_X_GEN=2 +MUTATION_RATE=0.1 +POPULATION_SIZE=100 +TOURNEY_SIZE=25 + +run: + python main.py $(TSP_FILE_PATH) $(NUM_OF_GENERATIONS) $(OUTPUT_EVERY_X_GEN) $(MUTATION_RATE) $(POPULATION_SIZE) $(TOURNEY_SIZE) \ No newline at end of file diff --git a/python/gen/README.md b/python/gen/README.md index 675b0b4..21e71b6 100644 --- a/python/gen/README.md +++ b/python/gen/README.md @@ -1 +1,13 @@ siddkumar sk35435 + + +edit Makefile to set + input file path, + number of generations, + how often you want to print output, + the mutation rate, + the population size, + and the tournament size. + +special thanks to http://www.theprojectspot.com/tutorial-post/applying-a-genetic-algorithm-to-the-travelling-salesman-problem/5 + diff --git a/python/gen/main.py b/python/gen/main.py index b7ac760..45f8066 100644 --- a/python/gen/main.py +++ b/python/gen/main.py @@ -203,44 +203,16 @@ def cross(self,tourA, tourB): return childtour def distance(posa, posb): - return ((posb[1] - posa[1]) ** 2 + (posb[0] - posa[0]) ** 2) ** 0.5 + return ( (posb[1] - posa[1]) ** 2 + (posb[0] - posa[0]) ** 2 ) ** 0.5 def main(): - - places = [0,1,2] - - - a = vertex(0) - b = vertex(1) - c = vertex(2) - - - a.addEdge(b,2) - a.addEdge(c,5) - b.addEdge(c,10) - - cities = [a,b,c] - - mypop = pop(4, True, cities) - best = mypop.max_fit() - - # mutation rate, popsize, tourneysize, eliteflag, cities - ga = gen(.10, 100, 5, False, cities) - - ga.evolve() - - macks = ga.pool.max_fit() - # print macks[0].tostr(),macks[1], macks[0].getdist() - - # - # - # path = sys.argv[1] - with gzip.open(path,'rb') as f: - file = f.readlines() + # with gzip.open(path,'rb') as f: + # file = f.readlines() + file = open(path, 'r') counter = 0 curr_cities = [] @@ -259,14 +231,23 @@ def main(): curr_cities.append(city) counter += 1 - + + r = int(sys.argv[2]) + p = int(sys.argv[3]) + + # mutation rate, popsize, tourneysize, eliteflag, cities - ga = gen(.10, 100, 25, True, curr_cities) + mr = float(sys.argv[4]) + ps = int(sys.argv[5]) + ts = int(sys.argv[6]) + + + ga = gen(mr, ps, ts, True, curr_cities) - for i in range(1000): + for i in range(r): ga.evolve() - if i % 250 == 1: + if i % p == 0: print "Best route at Generation", i macks = ga.pool.max_fit() print macks[0].tostr(),macks[1], macks[0].getdist()