You are on page 1of 3

import matplotlib.

pyplot as plt
import numpy
import random
import copy
def get_tour():
tour = [['Al Haouaria', 37.052704300958574,11.011884212493896],
['Sidi Daoud', 37.02223628247824, 10.911998748779297],
[' Hammam Al Ghezaz', 36.878742 ,11.117649],
['Klibia', 36.84578, 11.100483],
['Manzil Tamim', 36.778932, 10.991478],
['Qurbus', 36.812473, 10.580177]]
['Soliman', 36.688134 , 10.49366]]
['Korba, ', 36.570762, 10.864449]]
['Nabeul', 36.453764, 10.720253]]
['Hammamet', 36.400173, 10.621376]]
return(tour)
def distance(tour, i, j):
dx = tour[i][1]-tour[j][1]
dy = tour[i][2]-tour[j][2]
return(dx**2+dy**2)**0.5
def longueur_tour(tour):
d = 0
for i in xrange(0,len(tour)-1):
d += distance(tour, i, i+1)
d += distance(tour, 0, -1)
return(d)
def graph(tour):
x = [t[1] for t in tour]
y = [t[2] for t in tour]
x += [x[0]]
y += [y[0]]
plt.plot(x,y)
for ville, x, y in tour:
plt.text(x,y,ville)
plt.show()
def permutation(tour):
best = longueur_tour(tour)
fix = 0
while True:
i = random.randint(0, len(tour)-1)
j = random.randint(0, len(tour)-1)
if i==j: continue
e = tour[i]
tour[i] = tour[j]
tour[j] = e
d = longueur_tour(tour)
if d>= best:
fix += 1
e = tour[i]
tour[i] = tour[j]
tour[j] = e
else:
best = d
fix = 0
if fix > 10000: break

def retourne(tour, i, j):


while i <= j:
e = tour[i]
tour[i] = tour[j]
tour[j] = e
i += 1
j -= 1
def croisement(tour):
best = longueur_tour(tour)
fix = 0
while True:
i = random.randint(0,len(tour)-2)
j = random.randint(i+1,len(tour)-1)
retourne(tour, i, j)
d = longueur_tour(tour)
if d >= best:
fix += 1
retourne(tour, i,j)
else:
fix = 0
best = d
if fix > 10000 : break
def enchaine(tour):
best = longueur_tour(tour)
tttt = copy.deepcopy(tour)
print "debut", best
nom = 0
while True:
croisement(tour)
d = longueur_tour(tour)
print "croisement", d, best
permutation(tour)
d = longueur_tour(tour)
print "permutation", d, best
if d < best:
best = d
tttt = copy.deepcopy(tour)
nom = 0
elif nom >2:
break
else:
nom += 1
for k in range(0,3):
i = random.randint(0, len(tour)-2)
j = random.randint(i+1, len(tour)-1)
e = tour[i]
tour[i] = tour[j]
tour[j] = e
return tttt
if __name__ == "__main__":
tour = get_tour()
graph(tour)
tour = enchaine(tour)
graph(tour)
import folium

map_1 = folium.Map(location=[37.0000272, 10], zoom_start=7,tiles='Stamen Ter


rain')

folium.Marker([37.052704300958574,11.011884212493896], popup='Al Haouaria'').add


_to(map_1)
folium.Marker([37.02223628247824, 10.911998748779297, popup='Sidi Daoud').add_to
(map_1)
folium.Marker([36.878742 ,11.117649], popup='Hammam Al Ghezaz').add_to(map_1)
folium.Marker(36.84578, 11.100483], popup='Klibia').add_to(map_1)
folium.Marker([36.778932, 10.991478], popup='Manzil Tamim').add_to(map_1)
folium.Marker([36.812473, 10.580177], popup='Qurbus').add_to(map_1)
folium.Marker([36.688134 , 10.49366], popup='Soliman').add_to(map_1)
folium.Marker([36.570762, 10.864449], popup='Korba').add_to(map_1)
folium.Marker([36.453764, 10.720253], popup='Nabeul').add_to(map_1)
folium.Marker([36.400173, 10.621376], popup='Hammamet').add_to(map_1)
folium.LatLngPopup().add_to(map_1)
map_1.save('map.html')

You might also like