Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/lionralfs/extended_knapsack
0-1 knapsack with an additional constraint of maximum number of items used
https://github.com/lionralfs/extended_knapsack
Last synced: 10 days ago
JSON representation
0-1 knapsack with an additional constraint of maximum number of items used
- Host: GitHub
- URL: https://github.com/lionralfs/extended_knapsack
- Owner: lionralfs
- License: bsd-2-clause
- Created: 2022-01-04T10:26:24.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2022-01-06T10:23:48.000Z (about 3 years ago)
- Last Synced: 2024-03-14T13:43:30.298Z (11 months ago)
- Language: Python
- Size: 21.5 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# extended_knapsack
[![PyPI Latest Release](https://img.shields.io/pypi/v/extended_knapsack.svg)](https://pypi.org/project/extended-knapsack/)
0-1 knapsack with an additional constraint of maximum number of items used.
## Usage
`pip3 install extended_knapsack`
```python
from extended_knapsack.knapsack import solveitems = [
{'weight': 4, 'value': 5, 'my-custom-field': 1},
{'weight': 3, 'value': 4, 'my-custom-field': 2},
{'weight': 2, 'value': 3, 'my-custom-field': 3},
{'weight': 1, 'value': 2, 'my-custom-field': 4},
]result_value, result_items = knapsack(items, 6, 2)
print(result_value)
# 8
print(result_items)
# [
# {'weight': 4, 'value': 5, 'my-custom-field': 1},
# {'weight': 2, 'value': 3, 'my-custom-field': 3}
# ]
```