Skip to content

Commit

Permalink
Fishchimp/feature/optimiser v2 2 (#300)
Browse files Browse the repository at this point in the history
## Describe your changes
Refactor and Enhancement of the Optimiser Class for Shift and Volunteer
Assignment

This PR focuses on improving the Optimiser class's overall code
readability, functionality, and alignment with the new requirements for
volunteer shift management. Major changes include the replacement of
vehicle-based requirements with role-based requirements, the addition of
logging for better debugging, and the introduction of new constraints
for skill and compatibility handling in the optimisation model.

**Key Changes:**

1. **Logging Integration:**
- Added logging functionality to track errors and provide insights
during the optimisation process.
- Enhanced the debugging output for clearer visibility of the
optimiser's internal state.

2. **Role-Based Optimisation:**
- Transitioned from the asset/vehicle-based logic in the old
implementation to a role-based structure in the new version.
- Replaced `AssetRequestVolunteer` and `AssetRequestVehicle` logic with
`ShiftRequestVolunteer` to align with shift management requirements.
   
3. **MiniZinc Model Refactor:**
- Introduced a new MiniZinc model that simplifies the assignment of
volunteers to shifts and roles.
- The compatibility, mastery, and skill requirements are now handled
with flattened 1D matrices to optimise the data passing to the solver.
- Constraints added to ensure that volunteers are only assigned to roles
they are compatible with and have mastered.

4. **Constraints and Objective:**
- The new model includes constraints for skill requirements, ensuring
the required number of roles is filled.
- The objective is now focused on maximising the number of valid role
assignments while respecting constraints.

5. **Improved Database Handling:**
- Enhanced `save_result` function to store possible volunteer
assignments with a `PENDING` status in the `ShiftRequestVolunteer`
table.
- Added exception handling and rollback mechanisms for better
reliability in database transactions.

6. **Flattened Matrices:**
- Helper methods were introduced (`flatten_compatibility`,
`flatten_skill_requirements`) to flatten 2D matrices into 1D arrays for
passing into the MiniZinc model.

7. **Miscellaneous:**
- Removed unnecessary vehicle and position requirements that were
irrelevant to the current optimisation scenario.
   - Refactored the entire `solve()` method for clarity and performance.

**Testing:**

- The MiniZinc model string was tested by running it in the MiniZincIDE
with hardcoded inputs to validate the optimisation logic and
constraints.
Further testing for data persistence is required, particularly for
interactions with the database. Since I am not yet familiar with this
process, I need some guidance on setting up and using a database mockup.

## Issue ticket number and link
### Related Issue Tickets

- [FIR-4: Scheduler Create
Shifts](https://fireapp-emergiq-2024.atlassian.net/browse/FIR-4)
- [FIR-111: Optimiser outputs optimal
volunteers](https://fireapp-emergiq-2024.atlassian.net/browse/FIR-111)

---------

Co-authored-by: Muhammad Hafizh Hasyim <[email protected]>
  • Loading branch information
fishchimp and hafizhasyimm authored Oct 3, 2024
1 parent e91734a commit cb6368e
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 181 deletions.
14 changes: 14 additions & 0 deletions repository/shift_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,17 @@ def update_shift_status(self, user_id, shift_id, new_status):
session.rollback()
logging.error(f"Error updating shift request for user {user_id} and shift_id {shift_id}: {e}")
return False

def save_shift_volunteers(self, volunteers: List[ShiftRequestVolunteer]) -> None:
"""
Saves a list of ShiftRequestVolunteer objects to the database.
"""
with session_scope() as session:
try:
session.bulk_save_objects(volunteers)
session.commit()
logging.info(f"Successfully saved {len(volunteers)} shift volunteers.")
except Exception as e:
session.rollback()
logging.error(f"Error saving shift volunteers: {e}")
raise
Loading

0 comments on commit cb6368e

Please sign in to comment.