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.wherelike 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 following 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.
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
Room Reservation Systemand include the entire program. You state that the function takes 60% of the program execution time, it would help us optimize the code if we could see the rest of the program. The title should be about what the code does, and not what your concerns are about the code. Actual questions about the code should be in the body of the post. Please read How do I ask a good question? for more details. \$\endgroup\$