updating main.py

This commit is contained in:
2026-01-18 17:29:46 +01:00
parent ac5cbbc690
commit ca254e97ac

182
main.py
View File

@@ -1,12 +1,45 @@
import time import time
import pandas as pd
import numpy as np import numpy as np
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import copy import copy
from mopso import MOPSO from mopso import MOPSO
from surrogate_handler import SurrogateHandler from surrogate_handler import SurrogateHandler
import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D # Nécessaire pour la 3D
def plot_pareto_3d(archive, model_type:str):
fig = plt.figure(figsize=(12, 8))
ax = fig.add_subplot(111, projection='3d')
# Extraction des scores depuis l'archive
# f_best[0] = Coût, f_best[1] = Insatisfaction, f_best[2] = Stress Réseau
f1 = [p.f_best[0] for p in archive]
f2 = [p.f_best[1] for p in archive]
f3 = [p.f_best[2] for p in archive]
# Création du nuage de points
img = ax.scatter(f1, f2, f3, c=f3, cmap='viridis', s=60, edgecolors='black')
ax.set_xlabel('Coût (€)')
ax.set_ylabel('Insatisfaction (SoC manquant)')
ax.set_zlabel('Pic Réseau (kW)')
ax.set_title(f'Front de Pareto des Solutions Non-Dominées ({model_type})')
# Barre de couleur
cbar = fig.colorbar(img, ax=ax, pad=0.1)
cbar.set_label('Intensité du Pic Réseau (kW)')
# Sauvegarde et affichage
filename = f"{model_type}_pareto_3d.png"
plt.savefig(filename)
print(f"Graphique sauvegardé sous : {filename}")
plt.show()
# --- EXTENDED CLASS (Inheritance) ---
class SmartMOPSO(MOPSO): class SmartMOPSO(MOPSO):
def __init__(self, model_type=None, **kwargs): def __init__(self, model_type=None, **kwargs):
super().__init__(**kwargs) super().__init__(**kwargs)
@@ -43,18 +76,18 @@ class SmartMOPSO(MOPSO):
self.particles[i].keep_boudaries(self.A_max) self.particles[i].keep_boudaries(self.A_max)
if use_ai: if use_ai:
# 1. Fast exact calculation (f1, f3) # Fast exact calculation (f1, f3)
f1 = self.particles[i].f1(self.prices) f1 = self.particles[i].f1(self.prices)
f3 = self.particles[i].f3() f3 = self.particles[i].f3()
# 2. Slow prediction (f2) via AI # Slow prediction (f2) by using Surrogate
f2_pred = self.surrogate_handler.predict(self.particles[i].x) f2_pred = self.surrogate_handler.predict(self.particles[i].x)
# 3. Inject scores without running the expensive 'updating_socs' # Inject scores without running the expensive 'updating_socs'
self.particles[i].f_current = [f1, f2_pred, f3] self.particles[i].f_current = [f1, f2_pred, f3]
else: else:
# Standard Calculation (Slow & Exact) # Standard Calculation (Slow and Exact)
self.particles[i].updating_socs(self.socs, self.capacities) self.particles[i].updating_socs(self.socs, self.capacities)
self.particles[i].evaluate(self.prices, self.socs, self.socs_req, self.times) self.particles[i].evaluate(self.prices, self.socs, self.socs_req, self.times)
@@ -66,8 +99,9 @@ class SmartMOPSO(MOPSO):
self.update_archive() self.update_archive()
def calculate_elec_prices(csv_file:str, sep:str=';'): def calculate_elec_prices(csv_file:str, sep:str=';'):
elec_df = pd.read_csv(filepath_or_buffer=csv_file, sep=sep) elec_df = pd.read_csv(filepath_or_buffer=csv_file, sep=sep, skipinitialspace=True)
# Mean of Winter and Summer of 2025 electric prices (Euros/MWh) # Mean of Winter and Summer of 2025 electric prices (Euros/MWh)
elec_mean = (elec_df['Winter 2025'].mean() + elec_df['Summer 2025'].mean())/2 elec_mean = (elec_df['Winter 2025'].mean() + elec_df['Summer 2025'].mean())/2
@@ -75,6 +109,10 @@ def calculate_elec_prices(csv_file:str, sep:str=';'):
# Standard variation of Winter and Summer of 2025 electric prices (Euros/MWh) # Standard variation of Winter and Summer of 2025 electric prices (Euros/MWh)
elec_std = (elec_df['Winter 2025'].std() + elec_df['Summer 2025'].std())/2 elec_std = (elec_df['Winter 2025'].std() + elec_df['Summer 2025'].std())/2
elec_mean = elec_mean / 1000
elec_std = elec_std / 1000
print(f'Electricity prices:\n - Mean: ${elec_mean}€/Mwh\n - Std: ${elec_std}€/Mwh') print(f'Electricity prices:\n - Mean: ${elec_mean}€/Mwh\n - Std: ${elec_std}€/Mwh')
return elec_mean, elec_std return elec_mean, elec_std
@@ -88,7 +126,7 @@ def generate_capacities(csv_file:str, nb_vehicles:int, seed:int=42, sep:str=';')
capacities = pd.Series(all_capacities).sample(n=nb_vehicles, random_state=seed) capacities = pd.Series(all_capacities).sample(n=nb_vehicles, random_state=seed)
print(f'Capacities of vehicles (kwh): ${capacities}') print(f'Capacities of vehicles (kwh): ${capacities}')
return capacities return capacities.tolist()
def get_power_constants(nb_vehicles:int, nb_consumers:int=67000000): def get_power_constants(nb_vehicles:int, nb_consumers:int=67000000):
mean_consumption = (87028 + 46847 + 52374 + 29819)/4 # Mean of consumption in France in 2025 (estimate according to data/grid_capacity.txt) mean_consumption = (87028 + 46847 + 52374 + 29819)/4 # Mean of consumption in France in 2025 (estimate according to data/grid_capacity.txt)
@@ -99,7 +137,8 @@ def get_power_constants(nb_vehicles:int, nb_consumers:int=67000000):
x_min = -x_max x_min = -x_max
return a_max, x_max, x_min return a_max, x_max, x_min
# --- EXECUTION FUNCTION ---
def run_scenario(scenario_name, capacities:list, price_mean:float, price_std:float, model_type=None, n:int=20, t:int=30, w:float=0.4, c1:float=0.3, c2:float=0.2, archive_size:int=10, nb_vehicles:int=10, delta_t:int=60, nb_of_ticks:int=48): def run_scenario(scenario_name, capacities:list, price_mean:float, price_std:float, model_type=None, n:int=20, t:int=30, w:float=0.4, c1:float=0.3, c2:float=0.2, archive_size:int=10, nb_vehicles:int=10, delta_t:int=60, nb_of_ticks:int=48):
A_MAX, X_MAX, X_MIN = get_power_constants(nb_vehicles=nb_vehicles) A_MAX, X_MAX, X_MIN = get_power_constants(nb_vehicles=nb_vehicles)
@@ -131,10 +170,69 @@ def run_scenario(scenario_name, capacities:list, price_mean:float, price_std:flo
print(f"Finished in {duration:.2f} seconds.") print(f"Finished in {duration:.2f} seconds.")
print(f"Best f2 found: {best_f2:.4f}") print(f"Best f2 found: {best_f2:.4f}")
return duration, best_f2 return duration, best_f2, optimizer.archive
import matplotlib.pyplot as plt
import numpy as np
def plot_time_benchmark(nb_particles_list, results_dict):
t_mopso = [item[0] for item in results_dict['MOPSO']]
t_mlp = [item[0] for item in results_dict['MLP']]
t_rf = [item[0] for item in results_dict['RF']]
plt.figure(figsize=(10, 6))
plt.plot(nb_particles_list, t_mopso, 'o-', label='Sans IA (MOPSO)', color='#1f77b4', linewidth=2)
plt.plot(nb_particles_list, t_mlp, 's--', label='Avec MLP', color='#ff7f0e', linewidth=2)
plt.plot(nb_particles_list, t_rf, '^-.', label='Avec Random Forest', color='#2ca02c', linewidth=2)
plt.title("Temps d'exécution selon le nombre de particules", fontsize=14, fontweight='bold')
plt.xlabel("Nombre de Particules", fontsize=12)
plt.ylabel("Temps (s)", fontsize=12)
plt.grid(True, linestyle=':', alpha=0.7)
plt.legend(fontsize=11)
plt.tight_layout()
plt.show()
import matplotlib.pyplot as plt
def plot_f2_benchmark(nb_particles_list, results_dict):
s_mopso = [item[1] for item in results_dict['MOPSO']]
s_mlp = [item[1] for item in results_dict['MLP']]
s_rf = [item[1] for item in results_dict['RF']]
plt.figure(figsize=(10, 6))
plt.plot(nb_particles_list, s_mopso, 'o-', label='Sans IA (MOPSO)', color='#1f77b4', linewidth=2)
plt.plot(nb_particles_list, s_mlp, 's--', label='Avec MLP', color='#ff7f0e', linewidth=2)
plt.plot(nb_particles_list, s_rf, '^-.', label='Avec Random Forest', color='#2ca02c', linewidth=2)
plt.title("Meilleur Score F2 (Convergence) selon le nombre de particules", fontsize=14, fontweight='bold')
plt.xlabel("Nombre de Particules (log scale)", fontsize=12)
plt.ylabel("Meilleur F2 Score", fontsize=12)
plt.grid(True, linestyle=':', alpha=0.7)
plt.legend(fontsize=11)
plt.xscale('log')
plt.tight_layout()
plt.show()
def main():
# --- MAIN ---
if __name__ == "__main__":
# CSV files # CSV files
elec_price_csv = 'data/elec_prices.csv' elec_price_csv = 'data/elec_prices.csv'
capacity_csv = 'data/vehicle_capacity.csv' capacity_csv = 'data/vehicle_capacity.csv'
@@ -145,30 +243,70 @@ if __name__ == "__main__":
C1 = 0.3 # Individual trust C1 = 0.3 # Individual trust
C2 = 0.2 # Social trust C2 = 0.2 # Social trust
ARC_SIZE = 10 # Archive size ARC_SIZE = 10 # Archive size
nb_vehicle = 20
P_MEAN, P_STD = calculate_elec_prices(elec_price_csv) P_MEAN, P_STD = calculate_elec_prices(elec_price_csv)
CAPACITIES = generate_capacities(capacity_csv, N) CAPACITIES = generate_capacities(capacity_csv, nb_vehicles=nb_vehicle)
NB_TICKS = 48 NB_TICKS = 48
DELTA = 60 DELTA = 60
results = {} results = {
'MOPSO':[],
'MLP': [],
'RF': []
}
nb_particles = [20,50,500,1000,10000]
for k in range(len(nb_particles)):
# 1. Without Surrogate (Baseline) # 1. Without Surrogate (Baseline)
d1, f1_score = run_scenario("No AI", model_type=None) d1, f1_score, _ = run_scenario(
results['No-AI'] = (d1, f1_score) "Only MOPSO",
capacities=CAPACITIES,
price_mean=P_MEAN,
price_std=P_STD,
nb_vehicles=nb_vehicle, # Important pour la cohérence
model_type=None,
n=nb_particles[k]
)
results['MOPSO'].append((d1, f1_score))
# 2. With MLP # 2. With MLP
d2, f2_score = run_scenario("With MLP", model_type='mlp') d2, f2_score, _ = run_scenario(
results['MLP'] = (d2, f2_score) "With MLP",
capacities=CAPACITIES,
price_mean=P_MEAN,
price_std=P_STD,
nb_vehicles=nb_vehicle,
model_type='mlp',
n=nb_particles[k]
)
results['MLP'].append((d2, f2_score))
# 3. With Random Forest # 3. With Random Forest
d3, f3_score = run_scenario("With Random Forest", model_type='rf') d3, f3_score, _ = run_scenario(
results['RF'] = (d3, f3_score) "With Random Forest",
capacities=CAPACITIES,
price_mean=P_MEAN,
price_std=P_STD,
nb_vehicles=nb_vehicle,
model_type='rf',
n=nb_particles[k]
)
results['RF'].append((d3, f3_score))
# --- DISPLAY RESULTS --- # --- DISPLAY RESULTS ---
print("\n=== SUMMARY ===") print("\n=== SUMMARY ===")
print(f"{'Mode':<15} | {'Time (s)':<10} | {'Best f2':<10}") print(f"{'Mode':<15} | {'Time (s)':<10} | {'Best f2':<10}")
print("-" * 45) print("-" * 45)
for k, v in results.items(): for k, v in results.items():
print(f"{k:<15} | {v[0]:<10.2f} | {v[1]:<10.4f}") for i in range(len(nb_particles)):
print(f"{k:<15}_{nb_particles[i]:<15} | {v[i][0]:<10.2f} | {v[i][1]:<10.4f}")
plot_time_benchmark(nb_particles, results)
plot_f2_benchmark(nb_particles, results)
main()