gw/production_line.py

65 lines
2.4 KiB
Python

from mesa.agent import Agent
class ProductionLineAgent(Agent):
"""A production line agent with basic production scheduling."""
def __init__(self, model, line_id, region, factory, is_new_factory, schedule, ramp_ranges):
super().__init__(model)
self.line_id = line_id
self.region = region
self.factory = factory
self.is_new_factory = is_new_factory
# List of dicts: {"product": str, "start_month": int, "end_month": int}
self.schedule = schedule
# ramp_ranges: {1: value, 2: value, 3: value, 4: value}
self.ramp_ranges = ramp_ranges
self.blade_stock = {} # 临时叶片库存 {product: blades not yet assembled}
self.unit_stock = {} # 叶片机组库存 {product: assembled units}
def _sample_cycle_hours(self, product, month_index):
idx = min(max(month_index, 1), 4)
base = self.ramp_ranges[idx]
factor = self.model.get_factory_factor(self.factory)
return base * factor
def step(self):
month = self.model.current_month
available_hours = self.model.get_available_hours(month)
active = [
entry for entry in self.schedule if entry["start_month"] <= month <= entry["end_month"]
]
if not active:
return
# If multiple entries overlap, take the first one in the list.
task = active[0]
product = task["product"]
if not self.is_new_factory:
cycle_hours = self._sample_cycle_hours(product, 4)
else:
if task["start_month"] == 1:
month_index = 4
else:
month_index = month - task["start_month"] + 1
cycle_hours = self._sample_cycle_hours(product, month_index)
produced_blades = available_hours / cycle_hours
# Update blade and unit inventories
current_blades = self.blade_stock.get(product, 0) + produced_blades
units_to_add = int(current_blades // 3)
remaining_blades = current_blades - units_to_add * 3
if units_to_add > 0:
self.unit_stock[product] = self.unit_stock.get(product, 0) + units_to_add
self.blade_stock[product] = remaining_blades
self.model.record_production(
line_id=self.line_id,
factory=self.factory,
region=self.region,
month=month,
product=product,
units=units_to_add,
)