Update main.py
Browse files
main.py
CHANGED
|
@@ -50,13 +50,15 @@ PRODUCT_PRICES_USD_PER_TON = {
|
|
| 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]
|
| 57 |
}
|
| 58 |
|
|
|
|
| 59 |
def forecast_prices(base_price, years=PROJECT_YEARS, cagr=0.04):
|
|
|
|
| 60 |
historical = [base_price * (1 + cagr + random.uniform(-0.03, 0.03)) ** i for i in range(-5, 0)]
|
| 61 |
model = ARIMA(historical, order=(1, 1, 0))
|
| 62 |
fit = model.fit()
|
|
@@ -108,7 +110,7 @@ def calculate_project_kpis(**params):
|
|
| 108 |
cumulative_cash_flow = np.cumsum(cash_flows)
|
| 109 |
payback_period_years = np.where(cumulative_cash_flow > 0)[0]
|
| 110 |
payback_period = payback_period_years[0] + 1 if len(payback_period_years) > 0 else float('inf')
|
| 111 |
-
annual_profit = np.mean([cf for cf in cash_flows[1:] if cf > 0])
|
| 112 |
|
| 113 |
return {"irr": irr, "annual_profit": annual_profit, "total_capex": total_capex, "payback_period": payback_period}
|
| 114 |
|
|
@@ -122,6 +124,7 @@ def run_optimizations_without_ml():
|
|
| 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(40)]
|
| 126 |
best_overall_individual = None
|
| 127 |
best_overall_fitness = -float('inf')
|
|
|
|
| 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]
|
| 57 |
}
|
| 58 |
|
| 59 |
+
|
| 60 |
def forecast_prices(base_price, years=PROJECT_YEARS, cagr=0.04):
|
| 61 |
+
random.seed(42)
|
| 62 |
historical = [base_price * (1 + cagr + random.uniform(-0.03, 0.03)) ** i for i in range(-5, 0)]
|
| 63 |
model = ARIMA(historical, order=(1, 1, 0))
|
| 64 |
fit = model.fit()
|
|
|
|
| 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 # FIX: جلوگیری از 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) # FIX: reproducibility
|
| 128 |
population = [{k: random.choice(v) if isinstance(v, list) else random.uniform(*v) for k,v in OPTIMIZATION_SPACE.items()} for _ in range(40)]
|
| 129 |
best_overall_individual = None
|
| 130 |
best_overall_fitness = -float('inf')
|