-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
192 lines (167 loc) · 4.99 KB
/
Copy pathmain.py
File metadata and controls
192 lines (167 loc) · 4.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import tkinter as tk
from tkinter import messagebox
import plotly.graph_objects as go
import genetic_for_tsp as tsp
# Interface configuration
BG_COLOR = "#FFFFFF"
ACCENT_BLUE = "#87CEEB"
TEXT_COLOR = "#2C3E50"
INPUT_BG = "#EEF4FB"
TEXTAREA_BG = "#F4F8FC"
BUTTON_BG = "#2F80C1"
BORDER_SOFT = "#D7E4F1"
BORDER_FOCUS = "#7EA9CF"
FONT_MAIN = ("Inter", 11)
FONT_TITLE = ("Inter", 13, "bold")
def run_tsp():
try:
n = int(entree_n.get())
nb_generations = 500
villes_init = tsp.generate_random_villes(n)
chemin, distance = tsp.tsp_algorithm(villes_init, gens=nb_generations)
label_resultat.config(
text=f"Distance : {distance:.2f} km | Générations : {nb_generations}"
)
zone_texte.delete("1.0", tk.END)
zone_texte.tag_configure(
"header", foreground=ACCENT_BLUE, font=FONT_MAIN + ("bold",)
)
zone_texte.tag_configure("ville", foreground=TEXT_COLOR)
zone_texte.insert(tk.END, "ENSEMBLE DES VILLES:\n", "header")
for v in villes_init:
zone_texte.insert(tk.END, f" • {v.nom}\n", "ville")
zone_texte.insert(tk.END, "\n" + "=" * 30 + "\n\n")
zone_texte.insert(
tk.END,
f" Chemin optimal trouvé (Départ choisi : {chemin[0].nom})\n",
"header",
)
for i, v in enumerate(chemin, start=1):
zone_texte.insert(tk.END, f" {i}. {v.nom}\n", "ville")
zone_texte.insert(
tk.END, f" {len(chemin)+1}. {chemin[0].nom} (Retour)\n", "header"
)
afficher_carte(chemin, distance)
except FileNotFoundError:
messagebox.showerror(
"Erreur", "Le fichier 'villes_france.json' est introuvable."
)
except Exception as e:
messagebox.showerror("Erreur", str(e))
def afficher_carte(chemin, distance):
trace = chemin + [chemin[0]]
mode_affichage = "lines+markers+text" if len(chemin) < 25 else "lines+markers"
fig = go.Figure(
go.Scattermapbox(
lat=[v.latitude for v in trace],
lon=[v.longitude for v in trace],
mode=mode_affichage,
text=[f"{i+1}. {v.nom}" for i, v in enumerate(chemin)]
+ ["Ville de départ"],
textposition="top right",
marker=dict(size=8, color=ACCENT_BLUE),
line=dict(width=2, color=ACCENT_BLUE),
hoverinfo="text",
)
)
fig.update_layout(
title=f"Circuit IA - {distance:.2f} km",
mapbox_style="open-street-map",
mapbox=dict(center=dict(lat=46.6, lon=2.2), zoom=5),
margin={"r": 0, "t": 50, "l": 0, "b": 0},
)
fig.show()
root = tk.Tk()
root.title("Résolution du TSP avec Algorithme Génétique")
root.geometry("900x650")
root.configure(bg=BG_COLOR)
header = tk.Frame(root, bg=ACCENT_BLUE, height=60)
header.pack(fill=tk.X)
tk.Label(
header,
text="TSP : Algorithme Génétique",
bg=ACCENT_BLUE,
fg="white",
font=FONT_TITLE,
).pack(pady=15)
main_frame = tk.Frame(root, bg=BG_COLOR)
main_frame.pack(expand=True, fill=tk.BOTH, padx=30, pady=20)
frame_ctrl = tk.Frame(main_frame, bg=BG_COLOR)
frame_ctrl.pack(side=tk.LEFT, anchor="n", padx=10)
tk.Label(frame_ctrl, text="Nombre de villes", bg=BG_COLOR, font=FONT_MAIN).pack(
pady=(0, 5)
)
entree_n = tk.Entry(
frame_ctrl,
font=FONT_MAIN,
justify="center",
width=8,
bg=INPUT_BG,
fg=TEXT_COLOR,
insertbackground=TEXT_COLOR,
relief="flat",
bd=0,
highlightthickness=1,
highlightbackground=BORDER_SOFT,
highlightcolor=BORDER_FOCUS,
)
entree_n.insert(0, "20")
entree_n.pack(pady=5)
btn_calcul = tk.Button(
frame_ctrl,
text="CALCULER",
command=run_tsp,
bg=BUTTON_BG,
fg="white",
activebackground="#256AA2",
activeforeground="white",
font=FONT_TITLE,
relief="flat",
bd=0,
highlightthickness=1,
highlightbackground=BORDER_SOFT,
highlightcolor=BORDER_FOCUS,
cursor="hand2",
padx=20,
)
btn_calcul.pack(pady=20)
label_resultat = tk.Label(
frame_ctrl, text="Résolution du TSP", bg=BG_COLOR, font=FONT_MAIN, fg=TEXT_COLOR
)
label_resultat.pack(pady=10)
frame_liste = tk.Frame(main_frame, bg=BG_COLOR)
frame_liste.pack(side=tk.RIGHT, expand=True, fill=tk.BOTH)
tk.Label(
frame_liste,
text="Liste des villes et chemin optimal trouvé :",
bg=BG_COLOR,
font=FONT_TITLE,
fg=TEXT_COLOR,
).pack(anchor="w")
scrollbar = tk.Scrollbar(
frame_liste,
relief="flat",
bd=0,
highlightthickness=1,
highlightbackground=BORDER_SOFT,
activebackground="#B5CBE0",
troughcolor="#EDF3F9",
)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
zone_texte = tk.Text(
frame_liste,
font=FONT_MAIN,
bg=TEXTAREA_BG,
fg=TEXT_COLOR,
relief="flat",
bd=0,
padx=10,
pady=10,
highlightthickness=1,
highlightbackground=BORDER_SOFT,
highlightcolor=BORDER_FOCUS,
yscrollcommand=scrollbar.set,
)
zone_texte.pack(expand=True, fill=tk.BOTH, pady=5)
scrollbar.config(command=zone_texte.yview)
root.mainloop()