I’m using a function to determine if resources can be used again or not.
This is the numpy array i’m using.
'Resource_id', 'Start_date', 'end_date', 'start_time', 'end_time', 'overload'
(548, '2019-05-16', '2019-05-16', '08:45:00', '17:40:00',2),
(546, '2019-05-16', '2019-05-16', '08:45:00', '17:40:00',2),
(546, '2019-05-16', '2019-05-16', '08:45:00', '17:40:00',2),
(543, '2019-05-16', '2019-05-16', '08:45:00', '17:40:00',1),
-
First step is to find all resource available on a date, for example (2019-05-16 from 8:30 to 17:30). To achieve this i used np.where like the example below:
av_resource_np = resource_availability_np(np.where(
(resource_availability_np(:,1) <= '2019-05-16')
& (resource_availability_np(:,2) >= '2019-05-16')
& (resource_availability_np(:,3) <= '17:30:00')
& (resource_availability_np(:,4) >= '08:30:00')))
-
Here i try to find unique resource ids and the sum of their overload factor using np.unique()
unique_id, count_nb = np.unique(av_resource_np(:,(0,5)), axis=0, return_counts=True)
availability_mat = np.column_stack((unique_id, count_nb ))
Which yields the followig results:
'Resource_id' 'overload' 'Count'
548 2 1
546 2 2
543 1 1
-
A simple filtering is done to select which resource hat can’t be used in this date. If a resource is used in the same date more or equal (>=)
to itsoverload
, then we can’t use it again.
rejected_resources = availability_mat (np.where(availability_mat (:, 2) >= availability_mat (:, 1)))
Result here should be both resource 543
and 546
which can’t be used again.
So this is main idea behind my function, the problem here is that it takes more than 60% of the whole program runtime and i would appreciate any advice about how to make it more efficient/faster. Thank you.
Full code:
def get_available_rooms_on_date_x(date, start_time, end_time, resource_availability_np):
av_resource_np = resource_availability_np(np.where(
(resource_availability_np(:,1) <= date)
& (resource_availability_np(:,2) >= date)
& (resource_availability_np(:,3) <= end_time)
& (resource_availability_np(:,4) >= start_time)))
unique_id, count_nb = np.unique(av_resource_np(:,(0,5)), axis=0, return_counts=True)
availability_mat = np.column_stack((unique_id, count_nb ))
rejected_resources = availability_mat (np.where(availability_mat (:, 2) >= availability_mat (:, 1)))
return rejected_resources