Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
LongxingTan committed Aug 8, 2023
1 parent 71d6620 commit a397670
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ repos:
hooks:
- id: black
- repo: https://github.com/nbQA-dev/nbQA
rev: 1.3.1
rev: 1.2.3
hooks:
- id: nbqa-black
- id: nbqa-isort
Expand Down
77 changes: 66 additions & 11 deletions lekin/lekin_struct/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ def add_time_slots_to_resource(resource, start_times, processing_times):
add_time_slots_to_resource(resource3, resource3_start_times, resource3_processing_times)
"""

import math

import pandas as pd

from lekin.lekin_struct.timeslot import TimeSlot
Expand Down Expand Up @@ -88,25 +91,77 @@ def assign_task(self, time_slot, operation):
def get_task_at_time_slot(self, time_slot):
return self.tasks.get(time_slot)

def get_available_time_slots_within_time(self, start_time, limit_time, forward):
def get_available_time_slots_within_time(self, start=None, end=None, periods=None, freq="1H", forward=True):
available_hours = []
current_hour = start_time

pd.date_range(start_time, limit_time)
print(end, periods)

# check_periods = pd.date_range(start=start, end=end, periods=periods, freq=freq)
# if not forward:
# check_periods = check_periods[::-1]
# occupied_periods = [i.hours for i in self.assigned_time_slot]
# for period in check_periods:
# if period not in occupied_periods:
# available_hours.append(period)
# else: # considering the continuous assignment
# break

# all_available_end_time = [slot.end_time for slot in self.available_timeslots]
# just_in_time_end_time_slot = max([i for i in all_available_end_time if i <= end])
if forward:
selected_time_slot = [i for i in self.available_timeslots if i.start_time >= start]
else:
selected_time_slot = [i for i in self.available_timeslots if i.end_time <= end][::-1]

# print(just_in_time_end_time_slot)
# print([i.start_time for i in self.available_timeslots])
# print([i.end_time for i in self.available_timeslots])
# print(self.available_timeslots[0].hours)

left_periods = periods

for time_slot in selected_time_slot:
if left_periods <= 0:
break

while len(self.available_timeslots) > 0:
if current_hour not in self.assigned_time_slot:
available_hours.append(current_hour)
# num_hours -= 1
if start and time_slot.start_time < start:
continue

if end and time_slot.start_time >= end:
break

# if forward:
# current_time = max(start, time_slot.start_time)
# else:
# current_time = min(end, time_slot.end_time)

if time_slot in self.assigned_time_slot:
break

if time_slot.duration_of_hours < left_periods:
available_hours += time_slot.hours
left_periods -= time_slot.duration_of_hours

if forward:
current_hour += 1
else:
current_hour -= 1
available_hours += time_slot.hours[-math.ceil(left_periods) :]
break
#
# print(left_periods, available_hours)

# for current_time in range(max(start, time_slot.start_time),
# min(end, time_slot.end_time - period) + 1):
# if all(self.is_available(current_time + offset, current_time +offset+1) for offset in range(periods)):
# available_hours.append(current_time)

return available_hours

def get_unoccupied_time_slots(self):
def is_available(self, start_time, end_time):
for assigned_time_slot in self.assigned_time_slots:
if not (end_time <= assigned_time_slot.start_time or start_time >= assigned_time_slot.end_time):
return False
return True

def get_unoccupied_time_slots_within_time(self):
unoccupied_slots = []
prev_end_time = None
for time_slot in self.time_slots:
Expand Down
6 changes: 3 additions & 3 deletions lekin/lekin_struct/timeslot.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ def is_occupied(self):

@property
def hours(self):
return pd.date_range(start=self.start_time, end=self.end_time, freq="1H")
return pd.date_range(start=self.start_time, end=self.end_time, freq="1H").tolist()[:-1]

@property
def num_of_hours(self):
return len(pd.date_range(start=self.start_time, end=self.end_time, freq="1H"))
def duration_of_hours(self):
return len(pd.date_range(start=self.start_time, end=self.end_time, freq="1H")) - 1


# class TimeSlot:
Expand Down

0 comments on commit a397670

Please sign in to comment.