Skip to content

Commit 11c448d

Browse files
committed
Clean
1 parent f50088f commit 11c448d

1 file changed

Lines changed: 6 additions & 47 deletions

File tree

run_full_benchmark.py

Lines changed: 6 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -687,21 +687,20 @@ def benchmark_approx_comparison(
687687
timeout_seconds,
688688
output_filename="benchmark_comparison_results.txt",
689689
):
690+
690691
benchmark_start_time = time.perf_counter()
691692

692693
if not os.path.isdir(directory_path):
693694
print(f"Error : Folder '{directory_path}' does not exist.")
694695
return
695696

696697
files_to_process = [f for f in os.listdir(directory_path) if f.endswith(".npz")]
697-
698698
if not files_to_process:
699699
print(f"No file to process has been found in folder '{directory_path}'.")
700700
return
701701

702702
try:
703703
with open(output_filename, "w", encoding="utf-8") as f_out:
704-
705704
f_out.write("=" * 100 + "\n")
706705
f_out.write(f"APPROXIMATION COMPARISON & RANKING\n")
707706
f_out.write(f"Date: {time.ctime()}\n")
@@ -714,35 +713,25 @@ def benchmark_approx_comparison(
714713
for filename in files_to_process:
715714
file_header_line = f"Treating: {filename}"
716715
file_separator = "=" * 100
717-
718-
print(f"\n{file_separator}")
719-
print(file_header_line)
720-
print(f"{file_separator}")
721-
722716
f_out.write(f"{file_separator}\n")
723717
f_out.write(f"{file_header_line}\n")
724718
f_out.write(f"{file_separator}\n\n")
725-
726719
filepath = os.path.join(directory_path, filename)
727720

728721
try:
729722
graphs_in_file = parse_npz_file(filepath)
730723
total_graphs = len(graphs_in_file)
731724
if total_graphs == 0:
732725
continue
733-
msg = f" File loaded: {total_graphs} graphs."
734-
print(msg)
735-
f_out.write(msg + "\n")
726+
f_out.write(f" File loaded: {total_graphs} graphs.\n")
736727
except Exception as e:
737728
print(f"Error loading graphs from '{filename}': {e}")
738729
continue
739730

740-
# Structure de données pour les stats
741-
# wins = nombre de fois où l'algo a trouvé la meilleure solution (ou égalité)
742731
stats = {
743732
m.__name__: {
744733
"times": [],
745-
"results": [], # On stocke les k trouvés pour info
734+
"results": [],
746735
"wins": 0,
747736
"timeouts": 0,
748737
"errors": 0,
@@ -751,10 +740,6 @@ def benchmark_approx_comparison(
751740
}
752741

753742
for i, graph in enumerate(graphs_in_file):
754-
print(f" Competing... Graph {i + 1}/{total_graphs}", end="\r")
755-
756-
# Stocke les résultats de CE graphe pour comparaison immédiate
757-
# method_name -> best_k_for_this_graph
758743
current_graph_results = {}
759744

760745
for method in methods_list:
@@ -767,7 +752,6 @@ def benchmark_approx_comparison(
767752
timeout_occurred = False
768753
error_occurred = False
769754

770-
# 3 Runs par algo, on garde le meilleur résultat (min k)
771755
for _ in range(3):
772756
q = multiprocessing.Queue()
773757
p = multiprocessing.Process(
@@ -789,18 +773,13 @@ def benchmark_approx_comparison(
789773
if isinstance(res, Exception):
790774
error_occurred = True
791775
else:
792-
# res est un tuple (k, time)
793776
k_val, t_val = res
794777

795-
# Logique: On veut minimiser k.
796-
# Si k est égal, on ne change rien (ou on pourrait prendre le meilleur temps)
797778
if k_val < best_run_k:
798779
best_run_k = k_val
799780
best_run_time = t_val
800781
run_valid = True
801782
elif k_val == best_run_k:
802-
# Optionnel: Si k égal, on garde le run le plus rapide ?
803-
# Pour l'instant on garde le premier trouvé ou on moyenne
804783
pass
805784

806785
except QueueEmpty:
@@ -816,28 +795,22 @@ def benchmark_approx_comparison(
816795
else:
817796
stats[method_name]["errors"] += 1
818797

819-
# --- COMPARAISON DU GRAPHE ---
820-
# Quel est le min k trouvé parmi tous les algos qui ont réussi ?
821798
if current_graph_results:
822799
min_k_global = min(current_graph_results.values())
823-
824-
# On donne un "point" à tous ceux qui ont trouvé ce min
825800
for m_name, k_val in current_graph_results.items():
826801
if k_val == min_k_global:
827802
stats[m_name]["wins"] += 1
828803

829-
print(f"\n Finished competition for {filename}.\n")
830804
f_out.write("\n Results table (Sorted by Best Solution Rate):\n\n")
831805

832-
# Préparation du tableau
833806
col_methode = max(len(m.__name__) for m in methods_list) + 2
834807
col_methode = max(col_methode, 15)
835808
col_time = 12
836809
col_pct = 14
837810

838811
table_header = (
839812
f"| {'Method':<{col_methode}} "
840-
f"| {'Best Sol %':>{col_pct}} " # Pourcentage de "Wins"
813+
f"| {'Best Sol %':>{col_pct}} "
841814
f"| {'Mean Time (s)':>{col_pct}} "
842815
f"| {'Median Time (s)':>{col_pct}} "
843816
f"| {'Success (%)':>{col_time}} |"
@@ -850,22 +823,18 @@ def benchmark_approx_comparison(
850823
f"|{'-' * (col_time + 1)}|"
851824
)
852825

853-
# Calcul des données finales pour le tableau
854826
rows = []
855827
for method_name, data in stats.items():
856828
num_success = len(data["times"])
857829
success_pct = (num_success / total_graphs) * 100
858-
859-
# Le "Best Solution %" est basé sur le nombre total de graphes
860-
# C'est le taux de "Victoire"
861830
win_pct = (data["wins"] / total_graphs) * 100
862-
863831
times_arr = np.array(data["times"])
832+
864833
if num_success > 0:
865834
s_mean = np.mean(times_arr)
866835
s_median = np.median(times_arr)
867836
else:
868-
s_mean = float("inf") # Pour le tri, les échecs vont à la fin
837+
s_mean = float("inf")
869838
s_median = float("inf")
870839

871840
row_obj = {
@@ -877,13 +846,7 @@ def benchmark_approx_comparison(
877846
}
878847
rows.append(row_obj)
879848

880-
# --- TRI DES RÉSULTATS ---
881-
# Critère 1: Taux de victoire (Descendant)
882-
# Critère 2: Temps moyen (Ascendant) - en cas d'égalité de score
883849
rows.sort(key=lambda x: (-x["win_pct"], x["mean_time"]))
884-
885-
print(table_header)
886-
print(table_separator)
887850
f_out.write(table_header + "\n")
888851
f_out.write(table_separator + "\n")
889852

@@ -898,30 +861,26 @@ def benchmark_approx_comparison(
898861
if row["median_time"] != float("inf")
899862
else "---"
900863
)
901-
902864
line = (
903865
f"| {row['name']:<{col_methode}} "
904866
f"| {row['win_pct']:>{col_pct - 3}.2f} % "
905867
f"| {mean_str:>{col_pct}} "
906868
f"| {med_str:>{col_pct}} "
907869
f"| {row['success_pct']:>{col_time - 3}.2f} % |"
908870
)
909-
print(line)
910871
f_out.write(line + "\n")
911872

912873
f_out.write("\n")
913874

914875
benchmark_end_time = time.perf_counter()
915876
total_seconds = benchmark_end_time - benchmark_start_time
916877
formatted_time = str(datetime.timedelta(seconds=total_seconds))
917-
918878
end_msg = (
919879
f"\n{file_separator}\n"
920880
f"Comparison Benchmark over. Results in '{output_filename}'.\n"
921881
f"Total execution time: {formatted_time}\n"
922882
f"{file_separator}"
923883
)
924-
print(end_msg)
925884
f_out.write(end_msg + "\n")
926885

927886
except IOError as e:

0 commit comments

Comments
 (0)