-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path03_tlo_dcb.py
136 lines (117 loc) · 6.4 KB
/
03_tlo_dcb.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
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
#!/usr/bin/env python
# Created by "Thieu" at 09:48, 07/03/2022 ----------%
# Email: nguyenthieu2102@gmail.com %
# Github: https://github.com/thieu1995 %
# --------------------------------------------------%
#### Let's try to use Teaching Learning based Optimization (TLO) this time
### 1. Import libraries
### 2. Define data
### 3. Design fitness function (objective function)
### 4. Design problem dictionary
### 5. Call the model
### 6. Train the model
### 7. Show the results
## Example: drilling a circuit board
## Same same idea as TSP
## https://developers.google.com/optimization/routing/tsp#circuit_board
import numpy as np
from mealpy.human_based import TLO
from models.tsp_model import TravellingSalesmanProblem
from models.tsp_solution import generate_stable_solution
# np.random.seed(10)
DATA_POS = [
(288, 149), (288, 129), (270, 133), (256, 141), (256, 157), (246, 157),
(236, 169), (228, 169), (228, 161), (220, 169), (212, 169), (204, 169),
(196, 169), (188, 169), (196, 161), (188, 145), (172, 145), (164, 145),
(156, 145), (148, 145), (140, 145), (148, 169), (164, 169), (172, 169),
(156, 169), (140, 169), (132, 169), (124, 169), (116, 161), (104, 153),
(104, 161), (104, 169), (90, 165), (80, 157), (64, 157), (64, 165),
(56, 169), (56, 161), (56, 153), (56, 145), (56, 137), (56, 129),
(56, 121), (40, 121), (40, 129), (40, 137), (40, 145), (40, 153),
(40, 161), (40, 169), (32, 169), (32, 161), (32, 153), (32, 145),
(32, 137), (32, 129), (32, 121), (32, 113), (40, 113), (56, 113),
(56, 105), (48, 99), (40, 99), (32, 97), (32, 89), (24, 89),
(16, 97), (16, 109), (8, 109), (8, 97), (8, 89), (8, 81),
(8, 73), (8, 65), (8, 57), (16, 57), (8, 49), (8, 41),
(24, 45), (32, 41), (32, 49), (32, 57), (32, 65), (32, 73),
(32, 81), (40, 83), (40, 73), (40, 63), (40, 51), (44, 43),
(44, 35), (44, 27), (32, 25), (24, 25), (16, 25), (16, 17),
(24, 17), (32, 17), (44, 11), (56, 9), (56, 17), (56, 25),
(56, 33), (56, 41), (64, 41), (72, 41), (72, 49), (56, 49),
(48, 51), (56, 57), (56, 65), (48, 63), (48, 73), (56, 73),
(56, 81), (48, 83), (56, 89), (56, 97), (104, 97), (104, 105),
(104, 113), (104, 121), (104, 129), (104, 137), (104, 145), (116, 145),
(124, 145), (132, 145), (132, 137), (140, 137), (148, 137), (156, 137),
(164, 137), (172, 125), (172, 117), (172, 109), (172, 101), (172, 93),
(172, 85), (180, 85), (180, 77), (180, 69), (180, 61), (180, 53),
(172, 53), (172, 61), (172, 69), (172, 77), (164, 81), (148, 85),
(124, 85), (124, 93), (124, 109), (124, 125), (124, 117), (124, 101),
(104, 89), (104, 81), (104, 73), (104, 65), (104, 49), (104, 41),
(104, 33), (104, 25), (104, 17), (92, 9), (80, 9), (72, 9),
(64, 21), (72, 25), (80, 25), (80, 25), (80, 41), (88, 49),
(104, 57), (124, 69), (124, 77), (132, 81), (140, 65), (132, 61),
(124, 61), (124, 53), (124, 45), (124, 37), (124, 29), (132, 21),
(124, 21), (120, 9), (128, 9), (136, 9), (148, 9), (162, 9),
(156, 25), (172, 21), (180, 21), (180, 29), (172, 29), (172, 37),
(172, 45), (180, 45), (180, 37), (188, 41), (196, 49), (204, 57),
(212, 65), (220, 73), (228, 69), (228, 77), (236, 77), (236, 69),
(236, 61), (228, 61), (228, 53), (236, 53), (236, 45), (228, 45),
(228, 37), (236, 37), (236, 29), (228, 29), (228, 21), (236, 21),
(252, 21), (260, 29), (260, 37), (260, 45), (260, 53), (260, 61),
(260, 69), (260, 77), (276, 77), (276, 69), (276, 61), (276, 53),
(284, 53), (284, 61), (284, 69), (284, 77), (284, 85), (284, 93),
(284, 101), (288, 109), (280, 109), (276, 101), (276, 93), (276, 85),
(268, 97), (260, 109), (252, 101), (260, 93), (260, 85), (236, 85),
(228, 85), (228, 93), (236, 93), (236, 101), (228, 101), (228, 109),
(228, 117), (228, 125), (220, 125), (212, 117), (204, 109), (196, 101),
(188, 93), (180, 93), (180, 101), (180, 109), (180, 117), (180, 125),
(196, 145), (204, 145), (212, 145), (220, 145), (228, 145), (236, 145),
(246, 141), (252, 125), (260, 129), (280, 133)
]
DATA_POS = np.array(DATA_POS)
N_CITIES = DATA_POS.shape[0]
TSP = TravellingSalesmanProblem(n_cities=N_CITIES, city_positions=DATA_POS)
TSP.plot_cities(pathsave="./results/TLO-DCB", filename="holes_map", size=30, show_id=False)
LB = [1, ] * (N_CITIES - 1)
UB = [(N_CITIES - 0.01), ] * (N_CITIES - 1)
## Depot always start at 0 --> problem size = n_dims = N_CITIES - 1
def generate_position(lb, ub): # We remove index 0, because depot at 0 position
lower = int(lb[0])
upper = int(round(ub[0]))
return np.random.permutation(list(range(lower, upper)))
def correct_full_solution(solution_optimization):
return np.insert(solution_optimization, 0, 0) # Now we need to insert city index 0 to the generated solution
def fitness_function(position):
pos = correct_full_solution(position)
print(f"hell no: {len(pos)}")
dist = 0
for idx_city in range(0, N_CITIES):
idx_next = idx_city + 1
if idx_city == N_CITIES - 1:
idx_next = 0
dist += np.linalg.norm(DATA_POS[pos[idx_city]] - DATA_POS[pos[idx_next]])
return dist
## Example
solution_optimization = np.random.uniform(LB, UB)
solution_amended = generate_stable_solution(solution_optimization, LB, UB)
# solution_full = correct_full_solution(solution_amended)
# print(solution_full)
distance = fitness_function(solution_amended) # correct_full_solution is already called in fitness_function
print(f"Example distance: {distance}")
problem = {
"fit_func": TSP.fitness_function,
"lb": LB,
"ub": UB,
"minmax": "min",
"amend_position": generate_stable_solution,
"generate_position": generate_position
}
## Run the algorithm
model = TLO.BaseTLO(problem, epoch=10, pop_size=50)
best_position, best_fitness = model.solve()
print(f"Best position: {best_position}, Best fit: {best_fitness}")
dict_solutions = {}
for idx, g_best in enumerate(model.history.list_global_best):
dict_solutions[idx] = [correct_full_solution(g_best[0]), g_best[1][0]] # Final solution and fitness
TSP.plot_animate(dict_solutions, filename="TLO-DCB-results", pathsave="./results/TLO-DCB", size=20, show_id=False)
TSP.plot_solutions(dict_solutions, filename="g-best-solutions-after-epochs", pathsave="./results/TLO-DCB", size=20, show_id=False)