-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Create fractional_cover_problem.py #9973
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
9111d75
Create fractional_cover_problem.py
debnath003 18b625f
Update fractional_cover_problem.py
debnath003 64e32b3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 3436d20
Update fractional_cover_problem.py
debnath003 0d3bbfb
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 32b02e3
Update fractional_cover_problem.py
debnath003 31017ca
Update fractional_cover_problem.py
debnath003 184b146
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 1d38216
Update fractional_cover_problem.py
debnath003 32b219f
Update fractional_cover_problem.py
debnath003 cf108ba
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 3c80327
Update fractional_cover_problem.py
debnath003 a81fa7b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 60d5b76
Lose __eq__()
cclauss 503c3bd
Update fractional_cover_problem.py
debnath003 b000f15
Define Item property ratio
cclauss 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 |
---|---|---|
@@ -0,0 +1,96 @@ | ||
# Explanation:- https://en.wikipedia.org/wiki/Set_cover_problem | ||
|
||
from dataclasses import dataclass | ||
|
||
|
||
@dataclass | ||
class Item: | ||
weight: int | ||
value: int | ||
|
||
def __eq__(self, other: 'Item') -> bool: | ||
debnath003 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
Compare two Item objects for equality based on weight and value attributes. | ||
|
||
Args: | ||
other (Item): Another Item object to compare with. | ||
|
||
Returns: | ||
bool: True if the objects are equal, False otherwise. | ||
""" | ||
return self.weight == other.weight and self.value == other.value | ||
|
||
|
||
def fractional_cover(items: list[Item], capacity: int) -> float: | ||
""" | ||
Solve the Fractional Cover Problem. | ||
|
||
Args: | ||
items (List[Item]): A list of items, where each item is represented as an | ||
Item object with weight and value attributes. | ||
capacity (int): The maximum weight capacity of the knapsack. | ||
|
||
Returns: | ||
float: The maximum value that can be obtained by selecting fractions of items to | ||
cover the knapsack's capacity. | ||
|
||
Examples: | ||
>>> items = [Item(10, 60), Item(20, 100), Item(30, 120)] | ||
>>> fractional_cover(items, 50) | ||
240.0 | ||
|
||
>>> items = [Item(20, 100), Item(30, 120), Item(10, 60)] | ||
>>> fractional_cover(items, 25) | ||
135.0 | ||
|
||
>>> items = [Item(10, 60), Item(20, 100), Item(30, 120)] | ||
>>> fractional_cover(items, 60) | ||
280.0 | ||
|
||
>>> items = [Item(5, 30), Item(10, 60), Item(15, 90)] | ||
>>> fractional_cover(items, 30) | ||
180.0 | ||
|
||
>>> items = [] | ||
>>> fractional_cover(items, 50) | ||
0.0 | ||
|
||
>>> items = [Item(10, 60)] | ||
>>> fractional_cover(items, 5) | ||
30.0 | ||
|
||
>>> items = [Item(10, 60)] | ||
>>> fractional_cover(items, 1) | ||
6.0 | ||
|
||
>>> items = [Item(1, 1)] | ||
>>> fractional_cover(items, 0) | ||
0.0 | ||
""" | ||
# Calculate the value-to-weight ratios for each item | ||
ratios = [(item.value / item.weight, item) for item in items] | ||
|
||
# Sort the items by their value-to-weight ratio in descending order | ||
ratios.sort(key=lambda item_ratio: item_ratio[0], reverse=True) | ||
|
||
total_value = 0.0 | ||
remaining_capacity = capacity | ||
|
||
for ratio, item in ratios: | ||
if remaining_capacity == 0: | ||
break | ||
|
||
weight_taken = min(item.weight, remaining_capacity) | ||
total_value += weight_taken * ratio | ||
remaining_capacity -= weight_taken | ||
|
||
return total_value | ||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
result = doctest.testmod().failed | ||
if result == 0: | ||
print("All tests passed") | ||
else: | ||
print(f"{result} test(s) failed") |
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.
Uh oh!
There was an error while loading. Please reload this page.