Update main.py
Browse files
main.py
CHANGED
|
@@ -110,7 +110,7 @@ def calculate_project_kpis(**params):
|
|
| 110 |
cumulative_cash_flow = np.cumsum(cash_flows)
|
| 111 |
payback_period_years = np.where(cumulative_cash_flow > 0)[0]
|
| 112 |
payback_period = payback_period_years[0] + 1 if len(payback_period_years) > 0 else float('inf')
|
| 113 |
-
annual_profit = np.mean([cf for cf in cash_flows[1:] if cf > 0]) if any(cf > 0 for cf in cash_flows[1:]) else np.nan
|
| 114 |
|
| 115 |
return {"irr": irr, "annual_profit": annual_profit, "total_capex": total_capex, "payback_period": payback_period}
|
| 116 |
|
|
@@ -124,11 +124,11 @@ def run_optimizations_without_ml():
|
|
| 124 |
|
| 125 |
def run_genetic_algorithm():
|
| 126 |
print("Running Genetic Algorithm (GA)...")
|
| 127 |
-
random.seed(42)
|
| 128 |
-
population = [{k: random.choice(v) if isinstance(v, list) else random.uniform(*v) for k,v in OPTIMIZATION_SPACE.items()} for _ in range(
|
| 129 |
best_overall_individual = None
|
| 130 |
best_overall_fitness = -float('inf')
|
| 131 |
-
for _ in range(
|
| 132 |
fitnesses = [calculate_project_kpis(**ind)['irr'] for ind in population]
|
| 133 |
if max(fitnesses) > best_overall_fitness:
|
| 134 |
best_overall_fitness = max(fitnesses)
|
|
@@ -157,7 +157,7 @@ def run_bayesian_optimization():
|
|
| 157 |
@use_named_args(skopt_space)
|
| 158 |
def objective(**params):
|
| 159 |
return -calculate_project_kpis(**params)['irr']
|
| 160 |
-
res = gp_minimize(objective, skopt_space, n_calls=
|
| 161 |
best_params = {space.name: val for space, val in zip(skopt_space, res.x)}
|
| 162 |
kpis = calculate_project_kpis(**best_params)
|
| 163 |
return {"Method": "Bayesian Opt", **kpis, "Params": best_params}
|
|
@@ -174,7 +174,7 @@ def run_optuna_direct():
|
|
| 174 |
}
|
| 175 |
return calculate_project_kpis(**params)['irr']
|
| 176 |
study = optuna.create_study(direction="maximize")
|
| 177 |
-
study.optimize(objective, n_trials=
|
| 178 |
kpis = calculate_project_kpis(**study.best_params)
|
| 179 |
return {"Method": "Optuna (TPE - Direct)", **kpis, "Params": study.best_params}
|
| 180 |
|
|
|
|
| 110 |
cumulative_cash_flow = np.cumsum(cash_flows)
|
| 111 |
payback_period_years = np.where(cumulative_cash_flow > 0)[0]
|
| 112 |
payback_period = payback_period_years[0] + 1 if len(payback_period_years) > 0 else float('inf')
|
| 113 |
+
annual_profit = np.mean([cf for cf in cash_flows[1:] if cf > 0]) if any(cf > 0 for cf in cash_flows[1:]) else np.nan
|
| 114 |
|
| 115 |
return {"irr": irr, "annual_profit": annual_profit, "total_capex": total_capex, "payback_period": payback_period}
|
| 116 |
|
|
|
|
| 124 |
|
| 125 |
def run_genetic_algorithm():
|
| 126 |
print("Running Genetic Algorithm (GA)...")
|
| 127 |
+
random.seed(42)
|
| 128 |
+
population = [{k: random.choice(v) if isinstance(v, list) else random.uniform(*v) for k,v in OPTIMIZATION_SPACE.items()} for _ in range(25)]
|
| 129 |
best_overall_individual = None
|
| 130 |
best_overall_fitness = -float('inf')
|
| 131 |
+
for _ in range(50):
|
| 132 |
fitnesses = [calculate_project_kpis(**ind)['irr'] for ind in population]
|
| 133 |
if max(fitnesses) > best_overall_fitness:
|
| 134 |
best_overall_fitness = max(fitnesses)
|
|
|
|
| 157 |
@use_named_args(skopt_space)
|
| 158 |
def objective(**params):
|
| 159 |
return -calculate_project_kpis(**params)['irr']
|
| 160 |
+
res = gp_minimize(objective, skopt_space, n_calls=50, random_state=42, n_initial_points=20)
|
| 161 |
best_params = {space.name: val for space, val in zip(skopt_space, res.x)}
|
| 162 |
kpis = calculate_project_kpis(**best_params)
|
| 163 |
return {"Method": "Bayesian Opt", **kpis, "Params": best_params}
|
|
|
|
| 174 |
}
|
| 175 |
return calculate_project_kpis(**params)['irr']
|
| 176 |
study = optuna.create_study(direction="maximize")
|
| 177 |
+
study.optimize(objective, n_trials=100, n_jobs=-1)
|
| 178 |
kpis = calculate_project_kpis(**study.best_params)
|
| 179 |
return {"Method": "Optuna (TPE - Direct)", **kpis, "Params": study.best_params}
|
| 180 |
|