Update main.py
Browse files
main.py
CHANGED
|
@@ -13,21 +13,12 @@ import numpy_financial as npf
|
|
| 13 |
|
| 14 |
optuna.logging.set_verbosity(optuna.logging.WARNING)
|
| 15 |
|
| 16 |
-
FAST_MODE = True
|
| 17 |
PROJECT_YEARS = 15
|
| 18 |
BASE_CAPACITY_KTA = 300
|
| 19 |
INFLATION_RATE = 0.015
|
| 20 |
TAX_RATE = 0.10
|
| 21 |
DEPRECIATION_YEARS = 15
|
| 22 |
|
| 23 |
-
|
| 24 |
-
n_calls = 45 if FAST_MODE else 50
|
| 25 |
-
n_trials = 90 if FAST_MODE else 100
|
| 26 |
-
population_size = 40 if FAST_MODE else 50
|
| 27 |
-
generations = 80 if FAST_MODE else 100
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
TECHNOLOGY_DATA = {
|
| 32 |
"JNC": {"capex_base_M": 180.0, "opex_base_cents_kg": 150.0},
|
| 33 |
"Hoechst_AG": {"capex_base_M": 230.0, "opex_base_cents_kg": 155.0},
|
|
@@ -59,7 +50,7 @@ PRODUCT_PRICES_USD_PER_TON = {
|
|
| 59 |
|
| 60 |
OPTIMIZATION_SPACE = {
|
| 61 |
'capacity_kta': (500, 600),
|
| 62 |
-
'technology': ["Engro_Pakistan", "Shin_Etsu_2004"],
|
| 63 |
'sourcing_strategy': ['Integrated_Production'],
|
| 64 |
'export_market_mix': (0.6, 0.8),
|
| 65 |
'sell_byproducts': [True]
|
|
@@ -129,14 +120,12 @@ def run_optimizations_without_ml():
|
|
| 129 |
results.append(run_optuna_direct())
|
| 130 |
return results
|
| 131 |
|
| 132 |
-
|
| 133 |
-
|
| 134 |
def run_genetic_algorithm():
|
| 135 |
print("Running Genetic Algorithm (GA)...")
|
| 136 |
-
population = [{k: random.choice(v) if isinstance(v, list) else random.uniform(*v) for k,v in OPTIMIZATION_SPACE.items()} for _ in range(
|
| 137 |
best_overall_individual = None
|
| 138 |
best_overall_fitness = -float('inf')
|
| 139 |
-
for _ in range(
|
| 140 |
fitnesses = [calculate_project_kpis(**ind)['irr'] for ind in population]
|
| 141 |
if max(fitnesses) > best_overall_fitness:
|
| 142 |
best_overall_fitness = max(fitnesses)
|
|
@@ -165,7 +154,7 @@ def run_bayesian_optimization():
|
|
| 165 |
@use_named_args(skopt_space)
|
| 166 |
def objective(**params):
|
| 167 |
return -calculate_project_kpis(**params)['irr']
|
| 168 |
-
res = gp_minimize(objective, skopt_space, n_calls=
|
| 169 |
best_params = {space.name: val for space, val in zip(skopt_space, res.x)}
|
| 170 |
kpis = calculate_project_kpis(**best_params)
|
| 171 |
return {"Method": "Bayesian Opt", **kpis, "Params": best_params}
|
|
@@ -182,7 +171,7 @@ def run_optuna_direct():
|
|
| 182 |
}
|
| 183 |
return calculate_project_kpis(**params)['irr']
|
| 184 |
study = optuna.create_study(direction="maximize")
|
| 185 |
-
study.optimize(objective, n_trials=
|
| 186 |
kpis = calculate_project_kpis(**study.best_params)
|
| 187 |
return {"Method": "Optuna (TPE - Direct)", **kpis, "Params": study.best_params}
|
| 188 |
|
|
|
|
| 13 |
|
| 14 |
optuna.logging.set_verbosity(optuna.logging.WARNING)
|
| 15 |
|
|
|
|
| 16 |
PROJECT_YEARS = 15
|
| 17 |
BASE_CAPACITY_KTA = 300
|
| 18 |
INFLATION_RATE = 0.015
|
| 19 |
TAX_RATE = 0.10
|
| 20 |
DEPRECIATION_YEARS = 15
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
TECHNOLOGY_DATA = {
|
| 23 |
"JNC": {"capex_base_M": 180.0, "opex_base_cents_kg": 150.0},
|
| 24 |
"Hoechst_AG": {"capex_base_M": 230.0, "opex_base_cents_kg": 155.0},
|
|
|
|
| 50 |
|
| 51 |
OPTIMIZATION_SPACE = {
|
| 52 |
'capacity_kta': (500, 600),
|
| 53 |
+
'technology': ["Engro_Pakistan"], #, "Shin_Etsu_2004"],
|
| 54 |
'sourcing_strategy': ['Integrated_Production'],
|
| 55 |
'export_market_mix': (0.6, 0.8),
|
| 56 |
'sell_byproducts': [True]
|
|
|
|
| 120 |
results.append(run_optuna_direct())
|
| 121 |
return results
|
| 122 |
|
|
|
|
|
|
|
| 123 |
def run_genetic_algorithm():
|
| 124 |
print("Running Genetic Algorithm (GA)...")
|
| 125 |
+
population = [{k: random.choice(v) if isinstance(v, list) else random.uniform(*v) for k,v in OPTIMIZATION_SPACE.items()} for _ in range(50)]
|
| 126 |
best_overall_individual = None
|
| 127 |
best_overall_fitness = -float('inf')
|
| 128 |
+
for _ in range(100):
|
| 129 |
fitnesses = [calculate_project_kpis(**ind)['irr'] for ind in population]
|
| 130 |
if max(fitnesses) > best_overall_fitness:
|
| 131 |
best_overall_fitness = max(fitnesses)
|
|
|
|
| 154 |
@use_named_args(skopt_space)
|
| 155 |
def objective(**params):
|
| 156 |
return -calculate_project_kpis(**params)['irr']
|
| 157 |
+
res = gp_minimize(objective, skopt_space, n_calls=100, random_state=42, n_initial_points=20)
|
| 158 |
best_params = {space.name: val for space, val in zip(skopt_space, res.x)}
|
| 159 |
kpis = calculate_project_kpis(**best_params)
|
| 160 |
return {"Method": "Bayesian Opt", **kpis, "Params": best_params}
|
|
|
|
| 171 |
}
|
| 172 |
return calculate_project_kpis(**params)['irr']
|
| 173 |
study = optuna.create_study(direction="maximize")
|
| 174 |
+
study.optimize(objective, n_trials=200, n_jobs=-1)
|
| 175 |
kpis = calculate_project_kpis(**study.best_params)
|
| 176 |
return {"Method": "Optuna (TPE - Direct)", **kpis, "Params": study.best_params}
|
| 177 |
|