-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCluster.jl
229 lines (200 loc) · 6.46 KB
/
Cluster.jl
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
include("TSPTW.jl")
include("distance.jl")
mutable struct Cluster
points::Vector{Int}
gare
depot
len
end
function Base.show(io::IO, cluster::Cluster)
str = "Points : \n"
for i in 1:length(cluster.points)
str *= " $(cluster.points[i])\n"
end
str *= "Depot : $(cluster.depot.start_point)"
print(io, str)
end
struct Solution
clusters::Vector{Cluster}
length_max
map
all_people
end
function Base.show(io::IO, solution::Solution)
str = "Cluster : \n"
for i in 1:length(solution.clusters)
str *= " id : $(i)\n"
str *= " $(solution.clusters[i])\n"
end
print(io, str)
end
function add_point!(point, cluster::Cluster, nb_point)
push!(cluster.points, point)
cluster.len += nb_point
end
function remove_point!(id_point, cluster::Cluster)
deleteat!(cluster.points, id_point)
end
function remove_point_cluster!(cluster::Cluster, point)
"""Fonction that remove a point from the cluster"""
remove!(cluster.points, point)
cluster.len -= 1
end
function add_cluster!(cluster, solution)
push!(solution.clusters, cluster)
end
function remove_cluster!(id_cluster, solution)
deleteat!(solution.clusters, id_cluster)
end
function closest_pers(point, map, people)
return people[argmin([map[point, i.start_point] for i in people])]
end
function closest(point, Solution, metric, list=false)
# if list==false, return the closest cluster to point
# if list==true, return a list of the order of clusters for the point
if list
return sortperm([metric(point, Solution.map, i) for i in Solution.clusters]), sort([metric(point, Solution.map, i) for i in Solution.clusters])
else
return argmin([metric(point, Solution.map, i) for i in Solution.clusters])
end
end
function closest_bus(id_point, buses, id_bus, metric, gare, depots, map)
clusters = []
for b in buses
# creons le cluster correspondant au trajet du bus
# pour cela, il faut trouver quel point correspond au dépôt
depot_bus = Person(start_point=0, start_time=0., end_time=0.)
id_depot = b.stops[1]
find_depot = false
for d in depots
if d.start_point == id_depot
depot_bus = d
find_depot = true
end
end
if !find_depot
println("PROBLEME : on n'a pas trouvé le dépôt.")
println(id_depot)
end
# ensuite, il faut une liste des points à visiter sans le dépôt ni la gare
points = []
find_depot = false
for p in b.people
if (p.start_point != id_depot) && (p.start_point != gare.start_point)
push!(points, p.start_point)
elseif find_depot
push!(points, p.start_point)
elseif (p.start_point != gare.start_point)
find_depot = true
end
end
push!(clusters, Cluster(points, gare, depot_bus, length(b.people)))
end
arg_dist = sortperm([metric(id_point, map, i) for i in clusters])
if arg_dist[1] == id_bus
return arg_dist[2]
else
return arg_dist[1]
end
end
function best_cluster(point, sol, size, metric, check = false)
id_clusters, dist = closest(point, sol, metric, true)
for c in id_clusters
if sol.clusters[c].len + size < sol.length_max
if check
cluster = deepcopy(sol.clusters[c])
add_point!(point, cluster, size)
test = check_cluster(cluster, sol.map, sol.all_people, sol.length_max)
if test
return c, dist[c]
end
else
return c, dist[c]
end
end
end
throw(ErrorException("Tous les clusters sont pleins ou on ne peut insérer le point nul part"))
end
function get_new_time(cluster, point)
new_cluster = Cluster(cluster.points, cluster.gare, cluster.depot, cluster.len)
add_point!(point, new_cluster, 1)
try
bus = creation_bus(new_cluster, 1, sol.map, sol.all_people)
return get_total_time(bus)
catch
return 1e10
end
end
function even_better_cluster(point, all_people, sol)
size = length(nbre_people(point, all_people))
min = 1e9
ind = 0
for c in 1:length(sol.clusters)
if size + sol.clusters[c].len <= sol.length_max
time = get_new_time(sol.clusters[c], point)
if time < min
ind = c
end
end
end
if ind == 0
throw(ErrorException("Can't be added any where"))
end
return ind
end
function closest_mean(point, Solution, list=false)
# if list==true, return the closest cluster to point
# if list==false, return a list of the order of clusters for the point
if list
return sortperm([dist_mean(point, Solution.map, i) for i in Solution.clusters])
else
return argmin([dist_mean(point, Solution.map, i) for i in Solution.clusters])
end
end
function nbre_people(point, people)
return [people[i] for i in 1:length(people) if people[i].start_point == point]
end
function creation_bus(cluster, id, map, all_people)
people = find_people(cluster, all_people)
people = new_people_cluster(people, cluster.gare, cluster.depot)
stops, time = order_point(resolution_tsptw(length(people), people, map, 10000), people)
return Bus(id=id, people=people, stops=stops, time=time)
end
function compute_solution(solution)
return [creation_bus(solution.clusters[i], i, solution.map, solution.all_people) for i in 1:length(solution.clusters)]
end
function check_cluster(cluster, map, all_people, length_max)
try
creation_bus(cluster, 1, map, all_people)
return true
catch
return false
end
end
function add_point!(point, sol::Solution)
cluster = sol.clusters[closest(point, sol)]
nb_point = length(nbre_people(point, sol.all_people))
if cluster.len <= sol.length_max - nb_point
add_point!(point, cluster, nb_point)
else
println("Impossible")
end
end
function update!(cluster, people)
for i in cluster.points
cluster.len += length(nbre_people(i, people))
end
end
function get_points(clusters)
points = []
for c in clusters
append!(points, c.points)
end
return fastuniq(points)
end
function already_in(person, clusters)
return person.start_point in get_points(clusters)
end
function points_left(all_people)
return fastuniq([p.start_point for p in all_people])
end