generated from CodeYourFuture/Module-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 42
Manchester | 25-SDC-Nov | Rahwa Haile | Sprint 5 |implement laptop allocation #292
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
RahwaZeslusHaile
wants to merge
4
commits into
CodeYourFuture:main
Choose a base branch
from
RahwaZeslusHaile:implement-laptop-allocation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2567a98
Ignore local virtual environment
RahwaZeslusHaile 620f093
Add laptop allocation with sadness minimization and detailed output
RahwaZeslusHaile 2b38492
Refactor laptop allocation: optimize algorithm for scalability, fix H…
RahwaZeslusHaile 6cdc16c
Fix formatting and clean up laptop allocation code
RahwaZeslusHaile File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,4 @@ | ||
| node_modules | ||
| .venv | ||
| __pycache__ | ||
| *.pyc |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| from dataclasses import dataclass | ||
| from enum import Enum | ||
| from typing import List, Dict | ||
| import numpy as np | ||
| from scipy.optimize import linear_sum_assignment | ||
|
|
||
| unpreferred_OS_penalty = 100 | ||
|
|
||
| class OperatingSystem(Enum): | ||
| MACOS = "macOS" | ||
| ARCH = "Arch Linux" | ||
| UBUNTU = "Ubuntu" | ||
|
|
||
| @dataclass(frozen=True) | ||
| class Person: | ||
| name: str | ||
| age: int | ||
| preferred_operating_system: tuple | ||
|
|
||
| @dataclass(frozen=True) | ||
| class Laptop: | ||
| id: int | ||
| manufacturer: str | ||
| model: str | ||
| screen_size_in_inches: float | ||
| operating_system: OperatingSystem | ||
|
|
||
| def allocate_laptops(people: List[Person], laptops: List[Laptop]) -> Dict[Person, Laptop]: | ||
| if len(people) != len(laptops): | ||
| raise ValueError("Number of people must match number of laptops") | ||
|
|
||
| n = len(people) | ||
| cost_matrix = np.zeros((n, n), dtype=int) | ||
|
|
||
| for i, person in enumerate(people): | ||
| for j, laptop in enumerate(laptops): | ||
| if laptop.operating_system in person.preferred_operating_system: | ||
| cost_matrix[i, j] = person.preferred_operating_system.index(laptop.operating_system) | ||
| else: | ||
| cost_matrix[i, j] = unpreferred_OS_penalty | ||
|
|
||
| person_indices, laptop_indices = linear_sum_assignment(cost_matrix) | ||
|
|
||
| return { | ||
| people[i]: laptops[j] for i, j in zip(person_indices, laptop_indices) | ||
| } | ||
|
|
||
| laptops = [ | ||
| Laptop(1, "Dell", "XPS 13", 13, OperatingSystem.ARCH), | ||
| Laptop(2, "HP", "Spectre 15", 15, OperatingSystem.UBUNTU), | ||
| Laptop(3, "Lenovo", "ThinkPad 14", 14, OperatingSystem.UBUNTU), | ||
| Laptop(4, "Apple", "MacBook Air", 13, OperatingSystem.MACOS), | ||
| Laptop(5, "Apple", "MacBook Pro", 16, OperatingSystem.MACOS), | ||
| Laptop(6, "Dell", "Latitude", 15, OperatingSystem.ARCH), | ||
| Laptop(7, "HP", "EliteBook", 13, OperatingSystem.MACOS), | ||
| Laptop(8, "Lenovo", "Yoga", 14, OperatingSystem.UBUNTU) | ||
| ] | ||
|
|
||
| people = [ | ||
| Person("Alice", 29, (OperatingSystem.UBUNTU, OperatingSystem.MACOS)), | ||
| Person("Bob", 34, (OperatingSystem.ARCH, OperatingSystem.UBUNTU)), | ||
| Person("Charlie", 40, (OperatingSystem.MACOS, OperatingSystem.ARCH)), | ||
| Person("Diana", 25, (OperatingSystem.MACOS,)), | ||
| Person("Ethan", 31, (OperatingSystem.UBUNTU, OperatingSystem.ARCH)), | ||
| Person("Fiona", 27, (OperatingSystem.MACOS, OperatingSystem.UBUNTU)), | ||
| Person("George", 22, (OperatingSystem.ARCH, OperatingSystem.MACOS)), | ||
| Person("Zara", 33, (OperatingSystem.ARCH, OperatingSystem.MACOS)) | ||
| ] | ||
|
|
||
| assignment = allocate_laptops(people, laptops) | ||
|
|
||
| print("Laptop Assignments:\n") | ||
|
|
||
| for person, laptop in assignment.items(): | ||
| sadness = ( | ||
| person.preferred_operating_system.index(laptop.operating_system) | ||
| if laptop.operating_system in person.preferred_operating_system | ||
| else unpreferred_OS_penalty | ||
| ) | ||
| print(f"{person.name} → {laptop.manufacturer} {laptop.model} " | ||
| f"({laptop.operating_system.value}) | Score: {sadness}") | ||
|
|
||
| total_sadness = sum( | ||
| person.preferred_operating_system.index(laptop.operating_system) | ||
| if laptop.operating_system in person.preferred_operating_system else unpreferred_OS_penalty | ||
| for person, laptop in assignment.items() | ||
| ) | ||
|
|
||
| print("\nTotal sadness:", total_sadness) |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| numpy | ||
| scipy |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| import unittest | ||
| from laptop_allocation import allocate_laptops, Person, Laptop, OperatingSystem, unpreferred_OS_penalty | ||
|
|
||
| laptops = [ | ||
| Laptop(1, "Dell", "XPS 13", 13, OperatingSystem.ARCH), | ||
| Laptop(2, "HP", "Spectre 15", 15, OperatingSystem.UBUNTU), | ||
| Laptop(3, "Lenovo", "ThinkPad 14", 14, OperatingSystem.UBUNTU), | ||
| Laptop(4, "Apple", "MacBook Air", 13, OperatingSystem.MACOS), | ||
| Laptop(5, "Apple", "MacBook Pro", 16, OperatingSystem.MACOS), | ||
| Laptop(6, "Dell", "Latitude", 15, OperatingSystem.ARCH), | ||
| Laptop(7, "HP", "EliteBook", 13, OperatingSystem.MACOS), | ||
| Laptop(8, "Lenovo", "Yoga", 14, OperatingSystem.UBUNTU) | ||
| ] | ||
|
|
||
| people = [ | ||
| Person("Alice", 29, (OperatingSystem.UBUNTU, OperatingSystem.MACOS)), | ||
| Person("Bob", 34, (OperatingSystem.ARCH, OperatingSystem.UBUNTU)), | ||
| Person("Charlie", 40, (OperatingSystem.MACOS, OperatingSystem.ARCH)), | ||
| Person("Diana", 25, (OperatingSystem.MACOS,)), | ||
| Person("Ethan", 31, (OperatingSystem.UBUNTU, OperatingSystem.ARCH)), | ||
| Person("Fiona", 27, (OperatingSystem.MACOS, OperatingSystem.UBUNTU)), | ||
| Person("George", 22, (OperatingSystem.ARCH, OperatingSystem.MACOS)), | ||
| Person("Zara", 33, (OperatingSystem.ARCH, OperatingSystem.MACOS)) | ||
| ] | ||
|
|
||
| class TestLaptopAllocation(unittest.TestCase): | ||
|
|
||
| def setUp(self): | ||
| self.assignment = allocate_laptops(people, laptops) | ||
|
|
||
| def test_everyone_has_laptop(self): | ||
| assigned_people = set(self.assignment.keys()) | ||
| self.assertEqual(assigned_people, set(people), "Some people are missing a laptop") | ||
|
|
||
| def test_no_duplicate_laptops(self): | ||
| assigned_laptop_ids = [laptop.id for laptop in self.assignment.values()] | ||
| self.assertEqual(len(assigned_laptop_ids), len(set(assigned_laptop_ids)), "Duplicate laptops assigned") | ||
|
|
||
| def test_sadness_scores(self): | ||
| for person, laptop in self.assignment.items(): | ||
| if laptop.operating_system in person.preferred_operating_system: | ||
| score = person.preferred_operating_system.index(laptop.operating_system) | ||
| self.assertGreaterEqual(score, 0, f"Negative score for {person.name}") | ||
| else: | ||
| self.assertEqual(unpreferred_OS_penalty, 100, f"Unexpected unpreferred penalty for {person.name}") | ||
|
|
||
| def test_total_sadness(self): | ||
| total_sadness = sum( | ||
| person.preferred_operating_system.index(laptop.operating_system) | ||
| if laptop.operating_system in person.preferred_operating_system else unpreferred_OS_penalty | ||
| for person, laptop in self.assignment.items() | ||
| ) | ||
| calculated_sadness = sum( | ||
| person.preferred_operating_system.index(laptop.operating_system) | ||
| if laptop.operating_system in person.preferred_operating_system else unpreferred_OS_penalty | ||
| for person, laptop in self.assignment.items() | ||
| ) | ||
| self.assertEqual(total_sadness, calculated_sadness, "Total sadness mismatch") | ||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Two nice tests!