Created
January 11, 2024 22:12
-
-
Save Furkan-Gulsen/3b03088b8099428f6e504f1775abe4c9 to your computer and use it in GitHub Desktop.
Get Task Index at Cycle (dependencies)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_task_index_at_cycle(jobs, dependencies, cycle): | |
"""Finds the index of the task running at the given cycle, considering dependencies. | |
Args: | |
jobs: A list of task durations. | |
dependencies: A list of task dependencies, where -1 indicates no dependency. | |
cycle: The cycle time to check. | |
Returns: | |
The index of the task running at the given cycle, or -1 if no task is running. | |
""" | |
completed_tasks = set() | |
current_time = 0 | |
while current_time < cycle: | |
available_tasks = [ | |
task_index | |
for task_index, task_duration in enumerate(jobs) | |
if task_index not in completed_tasks | |
and (dependencies[task_index] == -1 or dependencies[task_index] in completed_tasks) | |
] | |
if not available_tasks: | |
break | |
next_task_index = min(available_tasks, key=lambda i: (jobs[i], i)) | |
current_time += jobs[next_task_index] | |
completed_tasks.add(next_task_index) | |
if current_time >= cycle: | |
return next_task_index | |
return -1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment