Skip to content

Commit

Permalink
Added plotting
Browse files Browse the repository at this point in the history
  • Loading branch information
phiriv committed Jul 6, 2021
1 parent 12f62e1 commit 0b015f8
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions TSPGeneticImp.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def xover(self, pc=0.1):
#otherwise, generate 2 children via two-point xover
else:
par1, par2=self.parents[np.random.randint(cnt, size=2), :]
idy=np.random.choice(range(sz), size=2, rep=False)
idy=np.random.choice(range(sz), size=2)
start, end=min(idy), max(idy) #randomly generated slice of geno to cross
child=[None]*sz

Expand All @@ -95,7 +95,7 @@ def xover(self, pc=0.1):
return childoos

#eazy breezy element swap
def mutate (chromo):
def mutate (self, chromo):
x, y = np.random.choice(len(chromo), 2)
chromo[x], chromo[y]=chromo[y], chromo[x]

Expand All @@ -108,7 +108,10 @@ def variation (self, pc=0.1, pm=0.1):

for chi in chili:
if np.random.rand() < pm:
nextBog.append(self.mutate(chi))
try:
nextBog.append(self.mutate(chi))
except TypeError:
break
else:
nextBog.append(chi)

Expand Down Expand Up @@ -154,4 +157,8 @@ def GA(cities, adj_mat, n_pop=10, n_iter=1000, selective=0.2, pc=0.5, pm=0.05, p
print(pop.variation())


GA(cities, distances, True)
optimum, hist=GA(cities, distances, 10, 100, 0.2, 0.5, 0.1, 50, True)

plt.plot(range(len(hist)), hist)
plt.show()
print(optimum)

0 comments on commit 0b015f8

Please sign in to comment.