Optimization¶
optimization
¶
Bi-objective optimization methods for battery sizing and control.
This module provides two optimization approaches for the tramway onboard battery design problem:
- Monte Carlo random sampling of the decision space.
- A simplified NSGA-II-inspired genetic search.
The optimization problem minimizes two objectives:
- battery capacity, used as a proxy for cost;
- maximum voltage drop at the train terminals, used as an electrical performance indicator.
evaluate_design(time_s, position_m, capacity_kwh, power_threshold_kw, network=None, train=None, battery_efficiency=0.9)
¶
Evaluate a battery design through a full time-domain simulation.
A design is defined by a battery capacity and a line power threshold. The battery is simulated over the whole trip using the rule-based energy management strategy, then the maximum voltage drop is returned.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
time_s
|
ndarray
|
Simulation time samples, in seconds. |
required |
position_m
|
ndarray
|
Train position at each time sample, in meters. |
required |
capacity_kwh
|
float
|
Battery capacity, in kilowatt-hours. |
required |
power_threshold_kw
|
float
|
Line power threshold, in kilowatts. |
required |
network
|
ElectricalNetwork | None
|
Electrical network configuration.
If |
None
|
train
|
TrainConfig | None
|
Train configuration. If |
None
|
battery_efficiency
|
float
|
Battery charge/discharge efficiency. |
0.9
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Maximum voltage drop over the trip, in volts. |
Source code in src\tramway_optimization\optimization.py
monte_carlo_search(time_s, position_m, n_samples=1000, capacity_bounds_kwh=(0.0, 14.0), threshold_bounds_kw=(0.0, 1000.0), random_seed=42, network=None, train=None)
¶
Explore the design space by random sampling.
This method samples battery capacities and power thresholds uniformly within the provided bounds. Each sampled design is evaluated independently through a complete simulation. The Pareto front is then extracted from the objective space using battery capacity and maximum voltage drop.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
time_s
|
ndarray
|
Simulation time samples, in seconds. |
required |
position_m
|
ndarray
|
Train position at each time sample, in meters. |
required |
n_samples
|
int
|
Number of random designs to evaluate. |
1000
|
capacity_bounds_kwh
|
tuple[float, float]
|
Lower and upper bounds for battery capacity, in kilowatt-hours. |
(0.0, 14.0)
|
threshold_bounds_kw
|
tuple[float, float]
|
Lower and upper bounds for the line power threshold, in kilowatts. |
(0.0, 1000.0)
|
random_seed
|
int | None
|
Seed used by NumPy's random generator. Use |
42
|
network
|
ElectricalNetwork | None
|
Electrical network configuration.
If |
None
|
train
|
TrainConfig | None
|
Train configuration. If |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
OptimizationResult |
OptimizationResult
|
Evaluated designs, objective values, and indices of |
OptimizationResult
|
non-dominated solutions. |
Notes
Monte Carlo sampling is simple and robust, but inefficient: many evaluated designs may be far from the Pareto front.
Source code in src\tramway_optimization\optimization.py
simplified_nsga2_search(time_s, position_m, population_size=100, n_generations=10, capacity_bounds_kwh=(0.0, 14.0), threshold_bounds_kw=(0.0, 1000.0), random_seed=42, network=None, train=None)
¶
Approximate the Pareto front with a simplified genetic search.
The algorithm starts from a random population of battery designs. At each generation, all individuals are evaluated, ranked using a dominance count, and the best half of the population is selected as parents. New individuals are created by crossover until the population size is restored.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
time_s
|
ndarray
|
Simulation time samples, in seconds. |
required |
position_m
|
ndarray
|
Train position at each time sample, in meters. |
required |
population_size
|
int
|
Number of individuals maintained at each generation. |
100
|
n_generations
|
int
|
Number of generations to run. |
10
|
capacity_bounds_kwh
|
tuple[float, float]
|
Lower and upper bounds for battery capacity, in kilowatt-hours. |
(0.0, 14.0)
|
threshold_bounds_kw
|
tuple[float, float]
|
Lower and upper bounds for the line power threshold, in kilowatts. |
(0.0, 1000.0)
|
random_seed
|
int | None
|
Seed used by NumPy's random generator. Use |
42
|
network
|
ElectricalNetwork | None
|
Electrical network configuration.
If |
None
|
train
|
TrainConfig | None
|
Train configuration. If |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
OptimizationResult |
OptimizationResult
|
All evaluated designs across generations, their |
OptimizationResult
|
objective values, and the extracted Pareto front. |
Notes
This function is intentionally not a full NSGA-II implementation. It keeps the main educational ideas: population-based search, dominance-based selection, crossover, and repeated improvement over generations.
Source code in src\tramway_optimization\optimization.py
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 | |