updating mopso demo
This commit is contained in:
@@ -25,7 +25,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 1,
|
||||
"id": "5cf6f9f8",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
@@ -35,7 +35,8 @@
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import copy\n",
|
||||
"from mopso import MOPSO \n",
|
||||
"from surrogate_handler import SurrogateHandler"
|
||||
"from surrogate_handler import SurrogateHandler\n",
|
||||
"import pandas as pd"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -51,13 +52,11 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 2,
|
||||
"id": "f783ba32",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"\n",
|
||||
"\n",
|
||||
"class SmartMOPSO(MOPSO):\n",
|
||||
" def __init__(self, model_type=None, **kwargs):\n",
|
||||
" super().__init__(**kwargs)\n",
|
||||
@@ -118,45 +117,99 @@
|
||||
" self.update_archive()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "83075d35",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## 3. Utility functions\n",
|
||||
"Manage CSV files reading and calculate physical constants"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"id": "a7789c8c",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def calculate_elec_prices(csv_file:str, sep:str=';'):\n",
|
||||
" elec_df = pd.read_csv(filepath_or_buffer=csv_file, sep=sep, skipinitialspace=True)\n",
|
||||
"\n",
|
||||
" # Mean of Winter and Summer of 2025 electric prices (Euros/MWh)\n",
|
||||
" elec_mean = (elec_df['Winter 2025'].mean() + elec_df['Summer 2025'].mean())/2\n",
|
||||
"\n",
|
||||
" # Standard variation of Winter and Summer of 2025 electric prices (Euros/MWh)\n",
|
||||
" elec_std = (elec_df['Winter 2025'].std() + elec_df['Summer 2025'].std())/2\n",
|
||||
"\n",
|
||||
" print(f'Electricity prices:\\n - Mean: ${elec_mean}€/Mwh\\n - Std: ${elec_std}€/Mwh')\n",
|
||||
" return elec_mean, elec_std\n",
|
||||
"\n",
|
||||
"def generate_capacities(csv_file:str, nb_vehicles:int, seed:int=42, sep:str=';'):\n",
|
||||
" cap_df = pd.read_csv(filepath_or_buffer=csv_file, sep=sep)\n",
|
||||
"\n",
|
||||
" # Getting back all kind of battery capacities with unique values\n",
|
||||
" all_capacities = cap_df['Battery Capacity kwh'].dropna().unique()\n",
|
||||
"\n",
|
||||
" # Extracting random values for generating the array of capacities\n",
|
||||
" capacities = pd.Series(all_capacities).sample(n=nb_vehicles, random_state=seed)\n",
|
||||
"\n",
|
||||
" print(f'Capacities of vehicles (kwh): ${capacities}')\n",
|
||||
" return capacities.tolist()\n",
|
||||
"\n",
|
||||
"def get_power_constants(nb_vehicles:int, nb_consumers:int=67000000):\n",
|
||||
" mean_consumption = (87028 + 46847 + 52374 + 29819)/4 # Mean of consumption in France in 2025 (estimate according to data/grid_capacity.txt)\n",
|
||||
" sim_ratio = nb_vehicles / nb_consumers # Ratio to reduce A_max of simulation to realistic restrictions\n",
|
||||
"\n",
|
||||
" a_max = sim_ratio * mean_consumption\n",
|
||||
" x_max = a_max / nb_vehicles # For init, uniform charging/discharging for every vehicle\n",
|
||||
" x_min = -x_max\n",
|
||||
" return a_max, x_max, x_min"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "a2b510ed",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## 3. Execution Function\n",
|
||||
"## 4. Execution Function\n",
|
||||
"Execute the pipeline on a scenario with simulation parameters by using the SmartMOPSO class for the classic MOPSO or for MOPSO and Surrogate. It does the calculation for the functions f1 and f3, and calculate or predict by AI the f2 function"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 4,
|
||||
"id": "a8797755",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def run_scenario(scenario_name, model_type=None):\n",
|
||||
"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):\n",
|
||||
" A_MAX, X_MAX, X_MIN = get_power_constants(nb_vehicles=nb_vehicles)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
" print(f\"\\n--- Launching Scenario: {scenario_name} ---\")\n",
|
||||
" start_time = time.time()\n",
|
||||
" \n",
|
||||
" # Simulation parameters\n",
|
||||
" params = {\n",
|
||||
" 'f_weights': [1,1,1], 'A_max': 500, 'price_mean': 0.15, 'price_std': 0.05,\n",
|
||||
" 'capacities': [50]*10, 'n': 20, 't': 50, \n",
|
||||
" 'w': 0.4, 'c1': 2.0, 'c2': 2.0,\n",
|
||||
" 'nb_vehicles': 10, 'delta_t': 60, 'nb_of_ticks': 72\n",
|
||||
" 'A_max': A_MAX, 'price_mean': price_mean, 'price_std': price_std,\n",
|
||||
" 'capacities': capacities, 'n': n, 't': t, \n",
|
||||
" 'w': w, 'c1': c1, 'c2': c2,\n",
|
||||
" 'nb_vehicles': nb_vehicles, 'delta_t': delta_t, 'nb_of_ticks': nb_of_ticks,\n",
|
||||
" 'x_min':X_MIN, 'x_max':X_MAX\n",
|
||||
" }\n",
|
||||
" \n",
|
||||
" # Instantiate extended class\n",
|
||||
" optimizer = SmartMOPSO(model_type=model_type, **params)\n",
|
||||
" \n",
|
||||
" start_time = time.time()\n",
|
||||
" \n",
|
||||
" # Run simulation\n",
|
||||
" optimizer.iterate()\n",
|
||||
" \n",
|
||||
" end_time = time.time()\n",
|
||||
" duration = end_time - start_time\n",
|
||||
" \n",
|
||||
" # Retrieve best f2\n",
|
||||
" best_f2 = min([p.f_current[1] for p in optimizer.archive]) if optimizer.archive else 0\n",
|
||||
" # Retrieve best f2 (e.g. from archive)\n",
|
||||
" best_f2 = min([p.f_best[1] for p in optimizer.archive]) if optimizer.archive else 0\n",
|
||||
" \n",
|
||||
" print(f\"Finished in {duration:.2f} seconds.\")\n",
|
||||
" print(f\"Best f2 found: {best_f2:.4f}\")\n",
|
||||
@@ -169,7 +222,7 @@
|
||||
"id": "cdbd5f06",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Scenario comparison\n",
|
||||
"## 5. Scenario comparison\n",
|
||||
"\n",
|
||||
"We use here 3 configurations to evaluate our surrogate models :\n",
|
||||
"1. **No AI (Baseline) :** Exact calculation, without any approximation.\n",
|
||||
@@ -181,27 +234,116 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 7,
|
||||
"id": "6cdd4953",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Electricity prices:\n",
|
||||
" - Mean: $84.69424€/Mwh\n",
|
||||
" - Std: $43.11248284136333€/Mwh\n",
|
||||
"Capacities of vehicles (kwh): $33 83.0\n",
|
||||
"36 20.0\n",
|
||||
"4 72.8\n",
|
||||
"13 35.8\n",
|
||||
"30 77.4\n",
|
||||
"26 17.3\n",
|
||||
"6 65.0\n",
|
||||
"27 11.4\n",
|
||||
"24 82.0\n",
|
||||
"15 87.0\n",
|
||||
"17 93.4\n",
|
||||
"8 65.5\n",
|
||||
"16 79.2\n",
|
||||
"12 58.0\n",
|
||||
"19 26.0\n",
|
||||
"9 39.2\n",
|
||||
"32 90.0\n",
|
||||
"0 95.0\n",
|
||||
"25 200.0\n",
|
||||
"5 75.0\n",
|
||||
"dtype: float64\n",
|
||||
"\n",
|
||||
"--- Launching Scenario: No AI ---\n",
|
||||
"Finished in 0.38 seconds.\n",
|
||||
"Best f2 found: 3.5594\n",
|
||||
"\n",
|
||||
"--- Launching Scenario: With MLP ---\n",
|
||||
"Finished in 0.55 seconds.\n",
|
||||
"Best f2 found: 4.4396\n",
|
||||
"\n",
|
||||
"--- Launching Scenario: With Random Forest ---\n",
|
||||
"Finished in 0.43 seconds.\n",
|
||||
"Best f2 found: 4.8796\n",
|
||||
"\n",
|
||||
"=== SUMMARY ===\n",
|
||||
"Mode | Time (s) | Best f2 \n",
|
||||
"---------------------------------------------\n",
|
||||
"No-AI | 0.38 | 3.5594 \n",
|
||||
"MLP | 0.55 | 4.4396 \n",
|
||||
"RF | 0.43 | 4.8796 \n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"if __name__ == \"__main__\":\n",
|
||||
" # CSV files\n",
|
||||
" elec_price_csv = 'data/elec_prices.csv'\n",
|
||||
" capacity_csv = 'data/vehicle_capacity.csv'\n",
|
||||
"\n",
|
||||
" # Global Simulation parameters\n",
|
||||
" T = 30 # Number of iterations (for the particles)\n",
|
||||
" W = 0.4 # Inertia (for exploration)\n",
|
||||
" C1 = 0.3 # Individual trust\n",
|
||||
" C2 = 0.2 # Social trust\n",
|
||||
" ARC_SIZE = 10 # Archive size\n",
|
||||
" nb_vehicle = 20\n",
|
||||
"\n",
|
||||
" P_MEAN, P_STD = calculate_elec_prices(elec_price_csv)\n",
|
||||
" CAPACITIES = generate_capacities(capacity_csv, nb_vehicles=nb_vehicle)\n",
|
||||
"\n",
|
||||
" NB_TICKS = 48\n",
|
||||
" DELTA = 60\n",
|
||||
"\n",
|
||||
" results = {}\n",
|
||||
" \n",
|
||||
" # 1. Without Surrogate (Baseline)\n",
|
||||
" d1, f1_score = run_scenario(\"No AI\", model_type=None)\n",
|
||||
" d1, f1_score = run_scenario(\n",
|
||||
" \"No AI\", \n",
|
||||
" capacities=CAPACITIES, \n",
|
||||
" price_mean=P_MEAN, \n",
|
||||
" price_std=P_STD, \n",
|
||||
" nb_vehicles=nb_vehicle, # Important pour la cohérence\n",
|
||||
" model_type=None\n",
|
||||
" )\n",
|
||||
" results['No-AI'] = (d1, f1_score)\n",
|
||||
" \n",
|
||||
" # 2. With MLP\n",
|
||||
" d2, f2_score = run_scenario(\"With MLP\", model_type='mlp')\n",
|
||||
" d2, f2_score = run_scenario(\n",
|
||||
" \"With MLP\", \n",
|
||||
" capacities=CAPACITIES, \n",
|
||||
" price_mean=P_MEAN, \n",
|
||||
" price_std=P_STD, \n",
|
||||
" nb_vehicles=nb_vehicle,\n",
|
||||
" model_type='mlp'\n",
|
||||
" )\n",
|
||||
" results['MLP'] = (d2, f2_score)\n",
|
||||
" \n",
|
||||
"\n",
|
||||
" # 3. With Random Forest\n",
|
||||
" d3, f3_score = run_scenario(\"With Random Forest\", model_type='rf')\n",
|
||||
" d3, f3_score = run_scenario(\n",
|
||||
" \"With Random Forest\", \n",
|
||||
" capacities=CAPACITIES, \n",
|
||||
" price_mean=P_MEAN, \n",
|
||||
" price_std=P_STD, \n",
|
||||
" nb_vehicles=nb_vehicle,\n",
|
||||
" model_type='rf'\n",
|
||||
" )\n",
|
||||
" results['RF'] = (d3, f3_score)\n",
|
||||
" \n",
|
||||
" # DISPLAY RESULTS\n",
|
||||
" # --- DISPLAY RESULTS ---\n",
|
||||
" print(\"\\n=== SUMMARY ===\")\n",
|
||||
" print(f\"{'Mode':<15} | {'Time (s)':<10} | {'Best f2':<10}\")\n",
|
||||
" print(\"-\" * 45)\n",
|
||||
@@ -212,13 +354,21 @@
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "venv",
|
||||
"display_name": "Optim_Metaheuristique (3.11.14)",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"version": "3.10.12"
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.11.14"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
||||
Reference in New Issue
Block a user