Skip to content

Commit

Permalink
added makefile
Browse files Browse the repository at this point in the history
  • Loading branch information
siddkumar committed May 12, 2016
1 parent f0b064b commit d324bd6
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 36 deletions.
9 changes: 9 additions & 0 deletions python/gen/Makefile
Original file line number Diff line number Diff line change
@@ -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)
12 changes: 12 additions & 0 deletions python/gen/README.md
Original file line number Diff line number Diff line change
@@ -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

53 changes: 17 additions & 36 deletions python/gen/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand All @@ -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()
Expand Down

0 comments on commit d324bd6

Please sign in to comment.