Skip to content

Commit 2bfab7d

Browse files
committed
Updated benchmark method
1 parent c3ad125 commit 2bfab7d

1 file changed

Lines changed: 113 additions & 18 deletions

File tree

main.py

Lines changed: 113 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import datetime
22
import multiprocessing
3+
import sys
4+
35
import networkx as nx
46
from pyvis.network import Network
57
from exact_mif import get_decycling_number_mif
@@ -48,12 +50,43 @@ def parse_adj_matrices(file):
4850
return graphs
4951

5052

53+
def log_error_matrix(filename, graph_idx, method_name, error_message, graph):
54+
"""
55+
Enregistre la matrice d'adjacence et l'erreur dans errors.txt
56+
"""
57+
try:
58+
with open("errors.txt", "a", encoding="utf-8") as f_err:
59+
f_err.write("=" * 40 + "\n")
60+
f_err.write(f"DATE : {time.ctime()}\n")
61+
f_err.write(f"FILE : {filename}\n")
62+
f_err.write(f"GRAPH IDX : {graph_idx + 1}\n")
63+
f_err.write(f"METHOD : {method_name}\n")
64+
f_err.write(f"ERROR : {error_message}\n")
65+
f_err.write("-" * 40 + "\n")
66+
f_err.write("ADJACENCY MATRIX :\n")
67+
68+
try:
69+
# Conversion du graphe NetworkX en matrice NumPy
70+
adj_matrix = nx.to_numpy_array(graph)
71+
for row in adj_matrix:
72+
# Formatage simple des nombres (général)
73+
row_str = " ".join(f"{x:g}" for x in row)
74+
f_err.write(f"{row_str}\n")
75+
except Exception as e_mat:
76+
f_err.write(f"Error converting graph to matrix: {e_mat}\n")
77+
f_err.write(f"Raw graph object: {graph}\n")
78+
79+
f_err.write("=" * 40 + "\n\n")
80+
except Exception as io_e:
81+
print(f"[WARNING] Could not write to errors.txt: {io_e}")
82+
83+
5184
def run_method_in_process(method_func, graph_data, result_queue):
5285
try:
5386
start_time = time.perf_counter()
54-
method_func(graph_data)
87+
method_result = method_func(graph_data)
5588
end_time = time.perf_counter()
56-
result_queue.put(end_time - start_time)
89+
result_queue.put((method_result, end_time - start_time))
5790
except Exception as e:
5891
result_queue.put(e)
5992

@@ -127,6 +160,7 @@ def benchmark_graph_methods(
127160

128161
for i, graph in enumerate(graphs_in_file):
129162
print(f" Test... Graph {i + 1}/{total_graphs}", end="\r")
163+
rslt = None
130164

131165
for method in methods_list:
132166
method_name = method.__name__
@@ -152,17 +186,78 @@ def benchmark_graph_methods(
152186
break
153187
elif p.exitcode != 0:
154188
error_occurred = True
189+
log_error_matrix(
190+
filename,
191+
i,
192+
method_name,
193+
f"Process crashed (Exit code {p.exitcode})",
194+
graph,
195+
)
155196
break
156197
else:
157198
try:
158199
result = result_queue.get_nowait()
159200
if isinstance(result, Exception):
160201
error_occurred = True
202+
log_error_matrix(
203+
filename, i, method_name, str(result), graph
204+
)
161205
break
162-
else:
163-
run_times.append(result)
206+
207+
method_result, exec_time = result
208+
if rslt is None:
209+
rslt = method_result
210+
elif method_result != rslt:
211+
print(f"\n\n{'!' * 80}")
212+
print(
213+
f"[CRITICAL ERROR] Result Mismatch on Graph {i + 1} in {filename}"
214+
)
215+
print(
216+
f"Method '{method_name}' returned a different result than previous methods."
217+
)
218+
print(f"Reference Result: {rslt}")
219+
print(f"Current Result : {method_result}")
220+
print(f"{'!' * 80}\n")
221+
# Écriture dans CRITICAL.txt
222+
try:
223+
with open(
224+
"CRITICAL.txt", "w", encoding="utf-8"
225+
) as f_crit:
226+
try:
227+
adj_matrix = nx.to_numpy_array(
228+
graph
229+
)
230+
for row in adj_matrix:
231+
row_str = " ".join(
232+
f"{x:g}" for x in row
233+
)
234+
f_crit.write(f"{row_str}\n")
235+
except Exception as e_mat:
236+
f_crit.write(
237+
f"Error converting graph to matrix: {e_mat}\n"
238+
)
239+
f_crit.write(
240+
f"Raw graph object: {graph}\n"
241+
)
242+
print(
243+
"Data successfully written to 'CRITICAL.txt'."
244+
)
245+
except Exception as io_e:
246+
print(
247+
f"Failed to write CRITICAL.txt: {io_e}"
248+
)
249+
sys.exit(1)
250+
251+
run_times.append(result)
164252
except QueueEmpty:
165253
error_occurred = True
254+
log_error_matrix(
255+
filename,
256+
i,
257+
method_name,
258+
"Queue Empty (No result returned)",
259+
graph,
260+
)
166261
break
167262

168263
if timeout_occurred:
@@ -271,25 +366,25 @@ def benchmark_graph_methods(
271366
get_decycling_number_mif_v3,
272367
]
273368

274-
# benchmark_graph_methods(
275-
# directory_path="Benchmark graphs/diameter",
276-
# methods_list=METHODS,
277-
# timeout_seconds=TIMEOUT_MAX,
278-
# output_filename="benchmark_results_diameter_3x.txt",
279-
# )
280-
#
281-
# benchmark_graph_methods(
282-
# directory_path="Benchmark graphs/chromatic number",
283-
# methods_list=METHODS,
284-
# timeout_seconds=TIMEOUT_MAX,
285-
# output_filename="benchmark_results_chrom_number_3x.txt",
286-
# )
369+
benchmark_graph_methods(
370+
directory_path="Benchmark graphs/diameter",
371+
methods_list=METHODS,
372+
timeout_seconds=TIMEOUT_MAX,
373+
output_filename="better_benchmark_results_diameter.txt",
374+
)
375+
376+
benchmark_graph_methods(
377+
directory_path="Benchmark graphs/chromatic number",
378+
methods_list=METHODS,
379+
timeout_seconds=TIMEOUT_MAX,
380+
output_filename="better_benchmark_results_chrom_number.txt",
381+
)
287382

288383
benchmark_graph_methods(
289384
directory_path="Benchmark graphs/density",
290385
methods_list=METHODS,
291386
timeout_seconds=TIMEOUT_MAX,
292-
output_filename="benchmark_results_density_3x.txt",
387+
output_filename="better_benchmark_results_density.txt",
293388
)
294389

295390
# graphs = parse_adj_matrices("Benchmark graphs/equal_to_1.mat")

0 commit comments

Comments
 (0)