Skip to content

Commit

Permalink
Fortran version (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmyrberg authored Jul 23, 2022
1 parent 4d160c9 commit 2bb122b
Show file tree
Hide file tree
Showing 39 changed files with 13,448 additions and 1,022 deletions.
3 changes: 3 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[flake8]
max-line-length = 79
exclude = .svn,CVS,.bzr,.hg,.git,__pycache__,.eggs,*.egg,*/_vendor/*,node_modules
6 changes: 6 additions & 0 deletions .github/release-drafter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name-template: 'Version $RESOLVED_VERSION'
tag-template: '$RESOLVED_VERSION'
template: |
## What's Changed
$CHANGES
55 changes: 55 additions & 0 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
name: Test on pull request

on:
pull_request:
branches: [master]

jobs:
lint:
name: Lint with flake8
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v3
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install flake8
- name: Lint with flake8
run: |
flake8 .
test:
name: Test
runs-on: ${{ matrix.os }}
timeout-minutes: 15
strategy:
fail-fast: true
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
python-version: ["3.10"]
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Install Fortran compiler on Linux
if: contains(matrix.os, 'ubuntu')
run: |
sudo apt-get install -y gfortran
- name: Install Fortran compiler on MacOS
if: contains(matrix.os, 'macos')
run: |
brew unlink gcc && brew link gcc
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
- name: Install Python dependencies
run: |
python -m pip install --upgrade pip
pip install -e . -vv
pip install -r requirements-dev.txt
- name: Test with pytest
run: |
pytest -vv
68 changes: 68 additions & 0 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
name: Test, draft release and upload distribution to Test PyPi

on:
push:
branches: [master]

permissions:
contents: read

jobs:

test:
name: Test
runs-on: ${{ matrix.os }}
timeout-minutes: 15
strategy:
fail-fast: true
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
python-version: ["3.10"]
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Install Fortran compiler on Linux
if: contains(matrix.os, 'ubuntu')
run: |
sudo apt-get install -y gfortran
- name: Install Fortran compiler on MacOS
if: contains(matrix.os, 'macos')
run: |
brew unlink gcc && brew link gcc
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
- name: Install Python dependencies
run: |
python -m pip install --upgrade pip
pip install -e . -vv
pip install -r requirements-dev.txt
- name: Test with pytest
run: |
pytest -vv
draft-release:
name: Draft release and upload distribution to Test PyPi
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: actions/setup-python@v3
- uses: release-drafter/release-drafter@v5
env:
GITHUB_TOKEN: ${{ secrets.RELEASE_DRAFTER_PAT }}
- name: Build and upload distribution to Test PyPi
env:
TWINE_USERNAME: __token__
TWINE_PASSWORD: ${{ secrets.TEST_PYPI_API_TOKEN }}
run: |
python -m pip install --upgrade pip
pip install build twine
python -m build --sdist --wheel --outdir dist/ .
twine upload -r testpypi dist/*
24 changes: 24 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Upload distribution to PyPi

on:
release:
types: [published]

jobs:
build-and-release:
name: Build and release
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: actions/setup-python@v3
- name: Build and upload distribution to PyPi
env:
TWINE_USERNAME: __token__
TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }}
run: |
python -m pip install --upgrade pip
pip install build twine
python -m build --sdist --wheel --outdir dist/ .
twine upload dist/*
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# Custom
docs/*/*build*
notebooks/
_version.py
.DS_Store
.vscode/
scripts/
Expand Down
23 changes: 23 additions & 0 deletions .readthedocs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# .readthedocs.yaml
# Read the Docs configuration file
# See https://docs.readthedocs.io/en/stable/config-file/v2.html for details

# Required
version: 2

# Set the version of Python and other tools you might need
build:
os: ubuntu-20.04
tools:
python: "3.9"

# Build documentation in the docs/ directory with Sphinx
sphinx:
builder: html
configuration: docs/source/conf.py
fail_on_warning: false

# Optionally declare the Python requirements required to build your docs
python:
install:
- requirements: docs/requirements.txt
62 changes: 0 additions & 62 deletions .travis.yml

This file was deleted.

20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Copyright (c) 2022 Jesse Myrberg

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3 changes: 2 additions & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
include VERSION cpp/**
recursive-include *.f
exclude .github
53 changes: 30 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,48 +1,55 @@
# mknapsack

[![Build Status](https://travis-ci.com/jmyrberg/mknapsack.svg?branch=master)](https://travis-ci.com/jmyrberg/mknapsack)
[![CICD](https://github.com/jmyrberg/mknapsack/actions/workflows/push.yml/badge.svg)](https://github.com/jmyrberg/mknapsack/actions/workflows/push.yml)
[![Documentation](https://readthedocs.org/projects/mknapsack/badge/?version=latest)](https://mknapsack.readthedocs.io/en/latest/?badge=latest)

Algorithms for solving the [Multiple 0-1 Knapsack Problem](http://www.or.deis.unibo.it/kp/Chapter6.pdf) (MKP).
Currently, only the [MTM algorithm by S. Martello and P. Toth (1981)](https://www.sciencedirect.com/science/article/pii/0166218X81900056) is implemented,
which guarantees an exact solution. This repository contains a Python interface to C++ implementation of the algorithm.
![mknapsack cover](https://github.com/jmyrberg/mknapsack/blob/master/docs/cover.png?raw=true)

Algorithms for solving knapsack problems with Python:
* [Multiple 0-1 Knapsack Problem](http://www.or.deis.unibo.it/kp/Chapter6.pdf): MTM, MTHM

This library provides a Python interface to the Fortran code from [Knapsack problems: algorithms and computer implementations](https://dl.acm.org/doi/book/10.5555/98124) by S. Martello and P. Toth, 1990.


## Installation

`pip install mknapsack`
1. Install Fortran compiler, if you don't already have
* MacOS / Linux:
`brew install gcc`
* Linux / Windows Subsystem for Linux:
`sudo apt-get install gfortran`
* Windows (experimental)
* Option 1):
`conda install -c conda-forge m2w64-toolchain_win-64`
* Option 2):
[Install MSYS2](https://www.msys2.org) and run
`pacman -S --needed base-devel mingw-w64-x86_64-toolchain`

2. `pip install -U mknapsack`


## Example usage

Given ten items with the following profits and weights:
### Multiple 0-1 Knapsack Problem

```python
from mknapsack import solve_multiple_knapsack

# Given ten items with the following profits and weights:
profits = [78, 35, 89, 36, 94, 75, 74, 79, 80, 16]
weights = [18, 9, 23, 20, 59, 61, 70, 75, 76, 30]
```

and two knapsacks with the following capacities:

```python
# ...and two knapsacks with the following capacities:
capacities = [90, 100]
```

How should we assign these items to knapsacks in order to maximize the profit?

```python
from mknapsack.algorithms import mtm

z, x, bt, glopt = mtm(profits, weights, capacities)
print('Total profit: %d' % z)
print('Solution: %s' % x)
print('Number of backtracks performed: %d' % bt)
print('Global optimum: %s' % glopt)
# Assign items into knapsacks while maximizing profit
res = solve_multiple_knapsack(profits, weights, capacities)
```

## References

* [MTM algorithm by Martello and Toth](http://people.sc.fsu.edu/~jburkardt/f77_src/knapsack/knapsack.f) (Fortran)
* [MTHM and MTHG algorithms by Jeff Hetherly](https://github.com/jhetherly/python_knapsack) (Python)
* [Knapsack problems: algorithms and computer implementations](https://dl.acm.org/doi/book/10.5555/98124) by S. Martello and P. Toth, 1990
* [Fortran77 source code](http://people.sc.fsu.edu/~jburkardt/f77_src/knapsack/knapsack.f) by S. Martello and P. Toth

---
Jesse Myrberg ([email protected])
1 change: 0 additions & 1 deletion VERSION

This file was deleted.

Binary file added cover.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 2bb122b

Please sign in to comment.