-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
160 lines (122 loc) · 5.35 KB
/
main.py
File metadata and controls
160 lines (122 loc) · 5.35 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
### Calculate optimal heat network for given opportunity zone and its characteristics
from utils.import_db import import_db
from utils.split_db import split_db
from utils.projection_route import projection_route
from utils.creer_graph import creer_graph
from utils.tree import tree
from utils.tracer_reseau import tracer_reseau
from utils.calcul_perf import calcul_perf
from utils.export_reseau import export_reseau
from utils.merge_reseaux import merge_reseaux_cluster, merge_reseaux_combine
from utils.trim_leaves import trim_leaves
from utils.diametre import get_demande_totale
import argparse
from multiprocessing import Pool
import time
import csv
### Argument Parser for global variable ###
parser = argparse.ArgumentParser()
parser.add_argument('path', help='Path to the folder with the input data')
parser.add_argument('-v', '--verbeux', help='Booleen pour afficher les informations textuels')
parser.add_argument('-t', '--trim', help='Booleen pour le trim des feuilles')
parser.add_argument('-m', '--merge', help='Booleen pour le merge des zones opportunite')
args = parser.parse_args()
if args.path != None :
path = args.path
else :
raise ValueError('Path to the data must be specified')
if args.verbeux :
verbeux = True
else : verbeux = False
if args.trim :
trim = True
else : trim = False
if args.merge :
merge = True
else : merge = False
### Definition des fonctions ###
def worker_calcul(id_zone, bats, routes) :
# Premier worker pour le calcul en parallele : de la projection des batiments à l'arbre complet sur chaque zone
if verbeux :
print(f"----- Calcul du réseau {id_zone} : {bats.shape[0]} batiments -----")
# 3.1 - Projection des batiments sur les routes
td = time.time()
bats, routes = projection_route(bats, routes)
d1 = time.time() - td
# 3.2 - Création des graph sur plusieurs clusters
td = time.time()
G_list, bats = creer_graph(bats, routes)
d2 = time.time() - td
# 3.3 - Steiner
td = time.time()
reseaux = tree(G_list, bats)
d3 = time.time() - td
# 3.4 - Regrouper les graphs des clusters
td = time.time()
reseau = merge_reseaux_cluster(reseaux, routes)
d4 = time.time() - td
# On stock les temps de calcul
with open('time.csv', 'a', newline='') as time_csv :
spamwriter = csv.writer(time_csv, delimiter=' ', quotechar='|', quoting=csv.QUOTE_MINIMAL)
spamwriter.writerow([id_zone, 'calcul', bats.shape[0], d1, d2, d3, d4, 0, 0, 0])
return (id_zone, reseau, bats, routes)
def worker_trace(id_zone, reseau, bats, routes) :
# Deuxieme worker pour le calcul en parallele : du trim au calcul des performances
if verbeux :
print(f"----- Tracé du réseau {id_zone} : {bats.shape[0]} batiments -----")
crs = routes.crs
# 5.1 - On optimise le reseau en enlevant les feuilles qui baissent la densité
td = time.time()
if trim :
reseau_trimmed = trim_leaves(reseau, bats)
else :
reseau_trimmed = reseau
d5 = time.time() - td
# 5.2 - On calcul la demande passant dans chaque canalisations
td = time.time()
reseau_demande_totale = get_demande_totale(reseau_trimmed)
d6 = time.time() - td
# 5.3 - Tracer le réseau
td = time.time()
reseau_linestring, reseau_gdf = tracer_reseau(reseau_demande_totale, routes, bats, id_zone, crs)
d7 = time.time() - td
# 5.4 - Calcul des performances
reseau_linestring_perf = calcul_perf(reseau_linestring, bats)
# On stock les temps de calcul
with open('time.csv', 'a', newline='') as time_csv :
spamwriter = csv.writer(time_csv, delimiter=' ', quotechar='|', quoting=csv.QUOTE_MINIMAL)
spamwriter.writerow([id_zone, 'trace', bats.shape[0], 0, 0, 0, 0, d5, d6, d7])
return (reseau_linestring_perf, reseau_gdf)
def main() :
t_total_debut = time.time()
# 1 - On importe nos base de données
print("On importe nos base de données")
bats_db, routes_db, crs, diametre_table = import_db(path)
# 2 - On découpe nos base de données par zone d'interets
print("On découpe nos base de données")
data_liste_calcul = split_db(bats_db, routes_db)
# On initialise la base de donnée de temps
with open('time.csv', 'w', newline='') as time_csv :
spamwriter = csv.writer(time_csv, delimiter=' ', quotechar='|', quoting=csv.QUOTE_MINIMAL)
spamwriter.writerow(['ID', 'Type', 'NbBats', 'Proj', 'Graph', 'Tree', 'Trace', 'Meta'])
# 3 - On calcul les réseaux pour chaque zone d'interets avec du multiprocessing
print("On calcul nos réseaux")
with Pool(processes = 8) as pool :
list_reseaux = pool.starmap(worker_calcul, data_liste_calcul)
# 4 - Regrouper les reseaux pour lequels on augmente la densité
if merge :
print("On combine nos zones d'intérets")
reseaux_merged = merge_reseaux_combine(list_reseaux, routes_db, min_density=1.5, max_length=500)
else :
reseaux_merged = list_reseaux
# 5 - On trace les réseaux pour chaque zone d'interets avec du multiprocessing
print("On trace nos réseaux")
with Pool(processes = 8) as pool :
reseaux_finaux = pool.starmap(worker_trace, reseaux_merged)
# 6 - Export
print("Export")
export_reseau(reseaux_finaux, crs, diametre_table)
t_total = time.time() - t_total_debut
print(f'Temps de calcul : {t_total}')
if __name__ == '__main__':
main()