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

@@ -16,6 +16,9 @@ from demand_agent import DemandAgent
matplotlib.rcParams["font.family"] = ["Microsoft YaHei", "SimHei", "sans-serif"]
matplotlib.rcParams["axes.unicode_minus"] = False
# set year
year = json.load(open('year.json', 'r', encoding='utf-8'))['year']
filename = f"{year}"
class SimulationModel(Model):
"""
@@ -184,11 +187,11 @@ class SimulationModel(Model):
return self._output_timestamp
def _load_month_hours(self):
with open("data/month_hours.json", "r", encoding="utf-8") as f:
with open(f"data/{filename}/month_hours.json", "r", encoding="utf-8") as f:
self.month_days = {int(k): v for k, v in json.load(f).items()}
def _load_model_params(self):
with open("data/model_params.json", "r", encoding="utf-8") as f:
with open(f"data/{filename}/model_params.json", "r", encoding="utf-8") as f:
return json.load(f)
def _load_month_holiday_days(self, cfg: dict) -> dict:
@@ -207,7 +210,7 @@ class SimulationModel(Model):
return str(name).replace("-", "").replace(".", "")
def _load_factory_mapping(self) -> dict:
with open("data/factory_mapping.json", "r", encoding="utf-8") as f:
with open(f"data/{filename}/factory_mapping.json", "r", encoding="utf-8") as f:
return json.load(f)
def _parse_product_set(self, val) -> tuple:
@@ -255,7 +258,7 @@ class SimulationModel(Model):
last_error = None
for enc in encodings:
try:
df = pd.read_csv("data/ProductionLine.csv", encoding=enc)
df = pd.read_csv(f"data/{filename}/ProductionLine.csv", encoding=enc)
break
except UnicodeDecodeError as exc:
last_error = exc
@@ -300,7 +303,7 @@ class SimulationModel(Model):
last_error = None
for enc in encodings:
try:
df = pd.read_csv("data/demand.csv", encoding=enc)
df = pd.read_csv(f"data/{filename}/demand.csv", encoding=enc)
break
except UnicodeDecodeError as exc:
last_error = exc
@@ -348,13 +351,13 @@ class SimulationModel(Model):
def _load_transport_data(self):
# Distance matrix
dist_df = pd.read_csv("data/distance_matrix.csv", encoding="utf-8")
dist_df = pd.read_csv(f"data/{filename}/distance_matrix.csv", encoding="utf-8")
self.distance_lookup = {
(row["factory"], row["demand_city"]): float(row["distance_km"])
for _, row in dist_df.iterrows()
}
# Transportation prices
price_df = pd.read_csv("data/transportation_price.csv", encoding="gbk")
price_df = pd.read_csv(f"data/{filename}/transportation_price.csv", encoding="gbk")
price_df.columns = [c.strip() for c in price_df.columns]
price_df["产品型号"] = price_df["产品型号"].str.strip()
if self.product_set:
@@ -506,7 +509,7 @@ class SimulationModel(Model):
+ ["总计"]
)
timestamp = self._get_output_timestamp()
output_dir = "output"
output_dir = f"output/{filename}"
os.makedirs(output_dir, exist_ok=True)
out_path = os.path.join(output_dir, f"production_report_{timestamp}.csv")
pivot.to_csv(out_path, index=False, encoding="utf-8-sig")
@@ -515,7 +518,7 @@ class SimulationModel(Model):
if not self.monthly_allocation_summary:
return
timestamp = self._get_output_timestamp()
output_dir = "output"
output_dir = f"output/{filename}"
os.makedirs(output_dir, exist_ok=True)
out_path = os.path.join(output_dir, f"allocation_summary_{timestamp}.xlsx")
with pd.ExcelWriter(out_path, engine="openpyxl") as writer:
@@ -551,7 +554,7 @@ class SimulationModel(Model):
return
timestamp = self._get_output_timestamp()
output_dir = "output"
output_dir = f"output/{filename}"
os.makedirs(output_dir, exist_ok=True)
def plot_series(columns, label_map, ylabel, title, filename, colors=None):
@@ -687,9 +690,9 @@ class SimulationModel(Model):
# Benchmark comparison
try:
benchmark = pd.read_csv("data/benchmark.csv", encoding="utf-8-sig")
benchmark = pd.read_csv(f"data/{filename}/benchmark.csv", encoding="utf-8-sig")
except UnicodeDecodeError:
benchmark = pd.read_csv("data/benchmark.csv", encoding="gbk")
benchmark = pd.read_csv(f"data/{filename}/benchmark.csv", encoding="gbk")
benchmark_sorted = benchmark.sort_values(by=benchmark.columns[0])
total_col = "总计"
month_cols = [f"{m}" for m in range(1, 13)]
@@ -713,7 +716,7 @@ class SimulationModel(Model):
return
timestamp = self._get_output_timestamp()
output_dir = "output"
output_dir = f"output/{filename}"
os.makedirs(output_dir, exist_ok=True)
factory_report_path = os.path.join(output_dir, f"factory_report_{timestamp}.csv")
factory_pivot.to_csv(factory_report_path, index=False, encoding="utf-8-sig")