diff --git a/runtime.py b/runtime.py index 483a128..bd67658 100644 --- a/runtime.py +++ b/runtime.py @@ -71,6 +71,7 @@ class RuntimeProductLines: self.product_lines_list = list(self.product_lines) +# 运行时工序 class RuntimeProcess: def __init__(self, runtime_product: RuntimeProduct, process: model.Process): @@ -80,6 +81,7 @@ class RuntimeProcess: self.delay = self.runtime_product.ddl - timedelta(minutes=process.pdt_time) +# 运行时资源需求 class RuntimeResourceNeed: def __int__(self, @@ -97,6 +99,7 @@ class RuntimeResourceNeed: self.end: datetime = end +# 运行时资源 class RuntimeResource: def __init__(self, resource: model.Resource): @@ -125,7 +128,37 @@ class RuntimeResource: self.schedules = sorted(self.schedules, key=lambda need: need.start) return True + def get_earliest_available_free_times(self, duration: timedelta) -> datetime: + # 没有已分配任务 + if self.schedules == 0: + return datetime.now() + + # 只有一个已分配任务 + if self.schedules == 1: + target_schedule = self.schedules[0] + if datetime.now() < target_schedule.start: + if target_schedule.start - datetime.now() > duration: + return target_schedule.start - duration + + else: + return target_schedule.end + # 正常执行 + for i in range(len(self.schedules) - 2): + pre_schedule = self.schedules[i] + back_schedule = self.schedules[i + 1] + + if datetime.now() < pre_schedule.start: + if pre_schedule.start - datetime.now() > duration: + return pre_schedule.start - duration + + elif back_schedule.start - pre_schedule.end > duration: + return pre_schedule.end + + return self.schedules[-1].end + + +# 资源池 class RuntimeResourcePool: def __init__(self, resources: List[model.Resource]): @@ -138,13 +171,45 @@ class RuntimeResourcePool: def alloc_resource(self, resource_need: RuntimeResourceNeed) -> bool: # 精确搜索 for runtime_resource in self.pool: + # 排除不同车间 + if runtime_resource.workspace != resource_need.workspace: + continue if runtime_resource.basic_attr == resource_need.resource_attr: if runtime_resource.add_schedule(resource_need) is True: return True # 放宽条件搜索 for runtime_resource in self.pool: + # 排除不同车间 + if runtime_resource.workspace != resource_need.workspace: + continue if resource_need.resource_attr in runtime_resource.resource_attrs: if runtime_resource.add_schedule(resource_need) is True: return True return False + + def get_earliest_free_time(self, resource_need: RuntimeResourceNeed) -> datetime: + + earliest_time: Optional[datetime] = None + + duration = resource_need.end - resource_need.start + + # 精确搜索 + for runtime_resource in self.pool: + if runtime_resource.basic_attr == resource_need.resource_attr: + temp_earliest_time = runtime_resource.get_earliest_available_free_times(duration) + if earliest_time is None or earliest_time > temp_earliest_time: + earliest_time = temp_earliest_time + + # 优先利用对口资源 + if earliest_time is not None: + return earliest_time + + # 放宽条件搜索 + for runtime_resource in self.pool: + if resource_need.resource_attr in runtime_resource.resource_attrs: + temp_earliest_time = runtime_resource.get_earliest_available_free_times(duration) + if earliest_time is None or earliest_time > temp_earliest_time: + earliest_time = temp_earliest_time + + return earliest_time