-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path04_ga_tsp_basic.py
49 lines (38 loc) · 1.85 KB
/
04_ga_tsp_basic.py
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
#!/usr/bin/env python
# Created by "Thieu" at 01:15, 21/05/2022 ----------%
# Email: [email protected] %
# Github: https://github.com/thieu1995 %
# --------------------------------------------------%
### 1. Import libraries
import numpy as np
from mealpy.swarm_based import WOA
from models.tsp_model import TravellingSalesmanProblem
from models.tsp_solution import generate_stable_solution, generate_unstable_solution
### 2. Define data and problem
np.random.seed(10)
N_CITIES = 15
CITY_POSITIONS = np.random.rand(N_CITIES, 2)
TSP = TravellingSalesmanProblem(n_cities=N_CITIES, city_positions=CITY_POSITIONS)
TSP.plot_cities(pathsave="./results/GA-1-TSP", filename="cities_map")
LB = [0, ] * TSP.n_cities
UB = [(TSP.n_cities - 0.01), ] * TSP.n_cities
problem = {
"fit_func": TSP.fitness_function,
"lb": LB,
"ub": UB,
"minmax": "min", # Trying to find the minimum distance
"log_to": "console",
"amend_position": generate_stable_solution
}
### 3. Call the model
model = WOA.BaseWOA(problem, epoch=100, pop_size=50, pc = 0.9, pm = 0.05, selection="roulette", crossover="multi_points")
### 4. Train the model
best_position, best_fitness = model.solve()
### 5. Show the results
print(f"Best solution: {best_position}, Obj = Total Distance: {best_fitness}")
print(len(model.history.list_global_best))
dict_solutions = {}
for idx, g_best in enumerate(model.history.list_global_best):
dict_solutions[idx] = [g_best[0], g_best[1][0]] # Final solution and fitness
# TSP.plot_animate(dict_solutions, filename="GA-1-TSP-results", pathsave="./results/GA-1-TSP")
TSP.plot_solutions(dict_solutions, filename="g-best-solutions-after-epochs", pathsave="./results/GA-1-TSP")