-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
94 lines (74 loc) · 3.15 KB
/
main.py
File metadata and controls
94 lines (74 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
"""
main.py - Command-line entry point for running the Warehouse Simulation (Backend Execution Mode)
This script performs the same core simulation as `visualisation.py`, but instead of launching a
web-based interface, it runs entirely from the command line. This mode is ideal for developers
who want more control over execution, easier debugging, or custom integrations.
Features:
---------
- Initializes and steps through a warehouse simulation using a JSON config file.
- Logs progress and simulation steps to console.
- Collects and saves time-series data for further analysis.
- Automatically generates performance plots to `output/`.
Configuration:
--------------
Simulation parameters are read from a `.env` file located in the project root.
Expected .env Variables:
------------------------
CONFIG_PATH : str
Path to the warehouse configuration JSON file.
STEPS : int
Number of simulation steps to execute.
Usage:
------
$ python main.py
Main Function:
--------------
- main(): Runs the simulation loop, handles logging, data collection, and plot generation.
"""
import os
import logging
from dotenv import load_dotenv
from backend.warehouse_model.utils import warehouse_logs
from frontend.plots_from_main import plots
from backend.warehouse_model.base_simulation import Warehouse
from backend.metrics.metrics_analysis import key_metric_analysis
def main():
"""
Initializes and runs the warehouse simulation using parameters from a .env file.
Environment Variables:
CONFIG_PATH (str): Path to the configuration JSON file.
STEPS (int): Number of simulation steps to run.
"""
# Extract warehouse simulation setups from .env file
load_dotenv()
config_path = os.getenv("CONFIG_PATH", "input/config.json")
steps = int(os.getenv("STEPS", 500))
# Set up loggings
warehouse_logs(log_file_path=os.getenv("MAIN_LOG_FILEPATH", "output/run_from_main/warehouse_simulation.log"))
logging.info("🔄 Starting Warehouse Simulation...")
# Run the warehouse simulation
try:
warehouse = Warehouse(config_path)
logging.info("✅ Warehouse initialized successfully.")
logging.info("")
for step in range(steps):
logging.info(f"📍 Step {step + 1}/{steps}")
warehouse.step()
logging.info("🏁 Simulation completed successfully.")
except Exception as e:
logging.error("❌ Simulation failed", exc_info=True)
return
# Retrieve the warehouse data collected from the simulation
data = warehouse.datacollector.get_model_vars_dataframe()
# Check if data is empty
if data.empty:
print("No data collected. Please ensure DataCollector is properly configured.")
return
# Analyse key metrics from the warehouse data collected
key_metric_analysis(warehouse, steps, data,
output_json_name=os.getenv("OUTPUT_JSON_FILENAME", "metrics_output.json"),
output_dir=os.getenv("MAIN_OUTPUT_DIR", "output/run_from_main/"))
# Create and save plots to the dedicated directory
plots(data, os.getenv("MAIN_OUTPUT_DIR", "output/run_from_main/"))
if __name__ == "__main__":
main()