This commit is contained in:
Zyy554
2025-11-30 14:50:27 +08:00
parent e4c411bbe2
commit 52e2f4dcb1
41 changed files with 5767 additions and 27 deletions

View File

@@ -5,11 +5,14 @@ from typing import List, Tuple
from simulation_model import SimulationModel
# set year
year = json.load(open('year.json', 'r', encoding='utf-8'))['year']
filename = f"{year}"
# Decision variables:
# - month1, month2, month3, month4 efficiencies (hour/blade)
# - factory-specific new-factory factors (from data/factory_mapping.json)
FACTORY_MAPPING = json.loads(Path("data/factory_mapping.json").read_text(encoding="utf-8"))
FACTORY_MAPPING = json.loads(Path(f"data/{filename}/factory_mapping.json").read_text(encoding="utf-8"))
FACTORY_IDS = list(FACTORY_MAPPING.values())
# Bounds for genes: (min, max)
@@ -17,7 +20,7 @@ MONTH_BOUNDS = (30.0, 250.0)
FACTOR_BOUNDS = (0.8, 3.0)
POP_SIZE = 20
GENERATIONS = 200
GENERATIONS = 2
MUTATION_RATE = 0.2
MUTATION_STD = 5.0 # hours for months; factors mutate separately
@@ -73,7 +76,7 @@ def crossover(p1: List[float], p2: List[float]) -> Tuple[List[float], List[float
def init_population() -> List[List[float]]:
pop = []
# Warm start from best params if available
best_path = Path("data") / "ga_best_params.json"
best_path = Path("data") / filename / "ga_best_params.json"
seed_indiv = None
if best_path.exists():
try:
@@ -140,8 +143,7 @@ def main():
for fid, val in zip(FACTORY_IDS, best_genes[4:]):
result[f"factor_{fid}"] = val
out_path = Path("output") / "ga_best_params.json"
data_path = Path("data") / "ga_best_params.json"
data_path = Path("data") / filename / "ga_best_params.json"
data_path.parent.mkdir(exist_ok=True)
# Only overwrite if better
if data_path.exists():