Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ jobs:
python-version: ["3.8"]

env:
KNAPSACK_DATA: ${{ github.workspace }}/data/knapsack
SUBSET_SUM_DATA: ${{ github.workspace }}/data/subset_sum
MULITPLE_CHOICE_SUBSET_SUM_DATA: ${{ github.workspace }}/data/multiple_choice_subset_sum
KNAPSACK_DATA: ${{ github.workspace }}/data

steps:
- name: Checkout code
Expand All @@ -28,16 +26,15 @@ jobs:
python-version: ${{ matrix.python-version }}
- name: Download data
run: |
python3 -m pip install gdown
python3 -m pip install -r requirements.txt
python3 -u scripts/download_data.py
- name: Build
run: |
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build --config Release --parallel
cmake --install build --config Release --prefix install
- name: Run unit tests
working-directory: build/test
run: ctest --parallel
run: ctest --test-dir build/test --parallel
- name: Run tests
run: python3 -u scripts/run_tests.py test_results
- name: Checkout main branch
Expand Down
105 changes: 6 additions & 99 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,33 +1,18 @@
# KnapsackSolver

A solver for some knapsack problems:
* 0-1 knapsack problem
* Subset sum problem
* Multiple-choice subset sum problem
A solver for the 0-1 knapsack problem

![knapsack](knapsack.png?raw=true "knapsack")

[image source](https://commons.wikimedia.org/wiki/File:Knapsack.svg)

These problems often appear as subproblems of more complex problems. For example:
* To generate cuts in branch-and-cut algorithms
* To solve pricing problems in column generation algorithms
* To lift bin and item dimensions in packing problems

And therefore, having efficient algorithms with reliable implementations to solve them is very useful.

The goal of this repository is to provide such efficient and reliable implementations.

Here are some usage examples of this library:
* [Lifting the length of a bin in a 3D packing problem](https://github.com/fontanf/packingsolver/blob/2cddb90686fb4a0e92f4ee1b4335ceaa2048d2f4/src/boxstacks/branching_scheme.cpp#L272)
* [Solving a 0-1 knapsack subproblem inside an algorithm for a geometrical variable-sized bin packing problem](https://github.com/fontanf/packingsolver/blob/2cddb90686fb4a0e92f4ee1b4335ceaa2048d2f4/src/algorithms/dichotomic_search.hpp#L149)
* Solving the pricing problem inside a column generation algorithm for the [cutting stock problem](https://github.com/fontanf/columngenerationsolver/blob/096802d9e20d2826aed5b44e3b68ac9df6b20da2/include/columngenerationsolver/examples/cutting_stock.hpp#L123), the [multiple knapsack problem](https://github.com/fontanf/columngenerationsolver/blob/096802d9e20d2826aed5b44e3b68ac9df6b20da2/include/columngenerationsolver/examples/multiple_knapsack.hpp#L154), or the [genralized assignment problem](https://github.com/fontanf/generalizedassignmentsolver/blob/68be0ab77efd897fd583f1031dd6cbc946b33f5a/src/algorithms/column_generation.cpp#L177)
* [Solving a 0-1 knapsack subproblem inside an algorithm for the packing while travelling problem](https://github.com/fontanf/travellingthiefsolver/blob/ce1b6805e8aee8ee3300fbbc97dbc9153eecff01/src/packing_while_travelling/algorithms/sequential_value_correction.cpp#L19)
<!--Here are some usage examples of this library:-->
<!--* [Solving a 0-1 knapsack subproblem inside an algorithm for a geometrical variable-sized bin packing problem](https://github.com/fontanf/packingsolver/blob/2cddb90686fb4a0e92f4ee1b4335ceaa2048d2f4/src/algorithms/dichotomic_search.hpp#L149)-->
<!--* Solving the pricing problem inside a column generation algorithm for the [cutting stock problem](https://github.com/fontanf/columngenerationsolver/blob/096802d9e20d2826aed5b44e3b68ac9df6b20da2/include/columngenerationsolver/examples/cutting_stock.hpp#L123), the [multiple knapsack problem](https://github.com/fontanf/columngenerationsolver/blob/096802d9e20d2826aed5b44e3b68ac9df6b20da2/include/columngenerationsolver/examples/multiple_knapsack.hpp#L154), or the [genralized assignment problem](https://github.com/fontanf/generalizedassignmentsolver/blob/68be0ab77efd897fd583f1031dd6cbc946b33f5a/src/algorithms/column_generation.cpp#L177)-->
<!--* [Solving a 0-1 knapsack subproblem inside an algorithm for the packing while travelling problem](https://github.com/fontanf/travellingthiefsolver/blob/ce1b6805e8aee8ee3300fbbc97dbc9153eecff01/src/packing_while_travelling/algorithms/sequential_value_correction.cpp#L19)-->

## Implemented algorithms

### Knapsack

* Greedy
* `O(n)` `-a greedy`

Expand All @@ -47,25 +32,6 @@ Here are some usage examples of this library:
* Primal-dual (minknap)
* List (partial solution) `-a "dynamic-programming-primal-dual --partial-solution-size 64 --pairing 0"`

### Subset sum

* Dynamic programming
* Bellman
* Array `-a dynamic-programming-bellman-array`
* List (only optimal value) `-a dynamic-programming-bellman-list`
* Word RAM (only optimal value) `-a dynamic-programming-bellman-word-ram`
* Word RAM with recursive scheme `-a dynamic-programming-bellman-word-ram-rec`
* Balancing
* Array (only optimal value) `-a dynamic-programming-balancing-array`

### Multiple-choice subset sum

* Dynamic programming
* Bellman
* Array `-a dynamic-programming-bellman-array`
* Word RAM (only optimal value) `-a dynamic-programming-bellman-word-ram`
* Word RAM with recursive scheme `-a dynamic-programming-bellman-word-ram-rec`

## Usage

### Command line
Expand All @@ -85,17 +51,13 @@ python3 scripts/download_data.py
Solve:

```shell
./install/bin/knapsacksolver_knapsack --verbosity-level 1 --algorithm dynamic-programming-primal-dual --input data/knapsack/largecoeff/knapPI_2_10000_10000000/knapPI_2_10000_10000000_50.csv --format pisinger
./install/bin/knapsacksolver --verbosity-level 1 --algorithm dynamic-programming-primal-dual --input data/knapsack/largecoeff/knapPI_2_10000_10000000/knapPI_2_10000_10000000_50.csv --format pisinger
```
```
====================================
KnapsackSolver
====================================

Problem
-------
Knapsack problem

Instance
--------
Number of items: 10000
Expand Down Expand Up @@ -205,61 +167,6 @@ Profit: 27018122468
Feasible: 1
```

```shell
./install/bin/knapsacksolver_subset_sum --verbosity-level 1 --input data/subset_sum/pthree/pthree_1000_1 --algorithm dynamic-programming-bellman-word-ram-rec
```
```
====================================
KnapsackSolver
====================================

Problem
-------
Subset sum problem

Instance
--------
Number of items: 1000
Capacity: 250000

Algorithm
---------
Dynamic programming - Bellman - word RAM - recursive scheme

Parameters
----------
Time limit: inf
Messages
Verbosity level: 1
Standard output: 1
File path:
# streams: 0
Logger
Has logger: 0
Standard error: 0
File path:

Time (s) Sol. Value Bound Gap Gap (%) Comment
-------- ---- ----- ----- --- ------- -------
0.000 1 0 250000 250000 100.00
0.033 1 250000 250000 0 0.00 algorithm end (solution)

Final statistics
----------------
Value: 250000
Has solution: 1
Bound: 250000
Absolute optimality gap: 0
Relative optimality gap (%): 0
Time (s): 0.0330949

Solution
--------
Number of items: 484 / 1000 (48.4%)
Weight: 250000 / 250000 (100%)
Feasible: 1
```

Run tests:
```
export KNAPSACK_DATA=$(pwd)/data/knapsack
Expand Down
14 changes: 7 additions & 7 deletions extern/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ endif()

# Fetch googletest.
if(KNAPSACKSOLVER_BUILD_TEST)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
set(INSTALL_GTEST OFF)
FetchContent_MakeAvailable(googletest)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
set(INSTALL_GTEST OFF)
FetchContent_MakeAvailable(googletest)
endif()

# Fetch fontanf/optimizationtools.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
#pragma once

#include "knapsacksolver/knapsack/solution.hpp"
#include "knapsacksolver/solution.hpp"

namespace knapsacksolver
{
namespace knapsack
{

class AlgorithmFormatter
{
Expand Down Expand Up @@ -63,4 +61,3 @@ class AlgorithmFormatter
};

}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
#pragma once

#include "knapsacksolver/knapsack/solution.hpp"
#include "knapsacksolver/solution.hpp"

namespace knapsacksolver
{
namespace knapsack
{

Output dynamic_programming_bellman_rec(
const Instance& instance,
Expand Down Expand Up @@ -157,4 +155,3 @@ Output dynamic_programming_bellman_list(
const DynamicProgrammingBellmanListParameters& parameters = {});

}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
#pragma once

#include "knapsacksolver/knapsack/solution.hpp"
#include "knapsacksolver/solution.hpp"

namespace knapsacksolver
{
namespace knapsack
{

struct DynamicProgrammingPrimalDualParameters: Parameters
{
Expand Down Expand Up @@ -73,4 +71,3 @@ const DynamicProgrammingPrimalDualOutput dynamic_programming_primal_dual(
const DynamicProgrammingPrimalDualParameters& parameters = {});

}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
#pragma once

#include "knapsacksolver/knapsack/solution.hpp"
#include "knapsacksolver/knapsack/sort.hpp"
#include "knapsacksolver/solution.hpp"
#include "knapsacksolver/sort.hpp"

namespace knapsacksolver
{
namespace knapsack
{

Output trivial(
const Instance& instance,
Expand Down Expand Up @@ -49,4 +47,3 @@ Output greedy(
const GreedyParameters& parameters = {});

}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
#pragma once

#include "knapsacksolver/knapsack/solution.hpp"
#include "knapsacksolver/solution.hpp"

namespace knapsacksolver
{
namespace knapsack
{

using SolveCallback = std::function<Output(const Instance&)>;

Expand All @@ -19,4 +17,3 @@ Output surrogate_relaxation(
const SurrogateRelaxationParameters& parameters = {});

}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
#pragma once

#include "knapsacksolver/knapsack/solution.hpp"
#include "knapsacksolver/knapsack/sort.hpp"
#include "knapsacksolver/solution.hpp"
#include "knapsacksolver/sort.hpp"

namespace knapsacksolver
{
namespace knapsack
{

struct UpperBoundDantzigParameters: Parameters
{
Expand Down Expand Up @@ -44,4 +42,3 @@ Output upper_bound_dantzig(
const UpperBoundDantzigParameters& parameters = {});

}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
#pragma once

#include "knapsacksolver/knapsack/instance.hpp"
#include "knapsacksolver/instance.hpp"

#include <random>

namespace knapsacksolver
{
namespace knapsack
{

Instance generate_u(
ItemPos number_of_items,
Expand All @@ -17,4 +15,3 @@ Instance generate_u(
std::mt19937_64& generator);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

namespace knapsacksolver
{
namespace knapsack
{

using Weight = int64_t;
using Profit = int64_t;
Expand Down Expand Up @@ -131,4 +129,3 @@ class Instance
};

}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
#pragma once

#include "knapsacksolver/knapsack/instance.hpp"
#include "knapsacksolver/instance.hpp"

namespace knapsacksolver
{
namespace knapsack
{

class InstanceBuilder
{
Expand Down Expand Up @@ -107,4 +105,3 @@ class InstanceFromFloatProfitsBuilder
};

}
}
Loading
Loading