Coder Social home page Coder Social logo

knapsacksolver's Introduction

KnapsackSolver

A solver for some knapsack problems:

  • 0-1 knapsack problem
  • Subset sum problem
  • Multiple-choice subset sum problem

knapsack

image source

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:

Implemented algorithms

Knapsack

  • Greedy

    • O(n) -a greedy
  • Upper bounds

    • Dantzig upper bound -a dantzig
  • Dynamic Programming

    • Bellman
      • Recursive -a dynamic-programming-bellman-rec
      • Array (only optimal value) -a dynamic-programming-bellman-array
      • Array + parallel (only optimal value) -a dynamic-programming-bellman-array-parallel
      • Array (all) -a dynamic-programming-bellman-array-all
      • Array (one) -a dynamic-programming-bellman-array-one
      • Array (partial solution) -a dynamic-programming-bellman-array-part
      • Array (recursive scheme) -a dynamic-programming-bellman-array-rec
      • List (only optimal value) -a dynamic-programming-bellman-list --sort 0
    • 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

Compile:

cmake -S . -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build --config Release --parallel
cmake --install build --config Release --prefix install

Download data:

python3 scripts/download_data.py

Solve:

./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
====================================
           KnapsackSolver           
====================================

Problem
-------
Knapsack problem

Instance
--------
Number of items:      10000
Capacity:             24537085856
Highest item profit:  10972919
Highest item weight:  9999761
Total item profit:    49624323576
Total item weight:    49564913430
Weight ratio:         2.02

Algorithm
---------
Dynamic programming - primal-dual - partial

Parameters
----------
Time limit:                          inf
Messages
    Verbosity level:                 1
    Standard output:                 1
    File path:                       
    # streams:                       0
Logger
    Has logger:                      0
    Standard error:                  0
    File path:                       
Greedy:                              1
Pairing:                             0
Partial solution size:               64

    Time (s)  Sol.           Value           Bound             Gap Gap (%)                         Comment
    --------  ----           -----           -----             --- -------                         -------
       0.000     1               0     49624323576     4.96243e+10  100.00                                
       0.000     1     27018043776     49624323576     2.26063e+10   45.55                          greedy
       0.000     1     27018043776     27018127904           84128    0.00             dantzig upper bound
       0.000     1     27018043776     27018127851           84075    0.00                    it 1 (bound)
       0.000     1     27018043776     27018127670           83894    0.00                    it 3 (bound)
       0.000     1     27018043776     27018127668           83892    0.00                    it 5 (bound)
       0.001     1     27018043776     27018127663           83887    0.00                    it 7 (bound)
       0.001     0     27018069931     27018127663           57732    0.00                    it 8 (value)
       0.001     0     27018069931     27018127651           57720    0.00                    it 9 (bound)
       0.001     0     27018115040     27018127651           12611    0.00                   it 10 (value)
       0.001     0     27018115040     27018127648           12608    0.00                   it 11 (bound)
       0.001     0     27018115040     27018127635           12595    0.00                   it 13 (bound)
       0.002     0     27018115040     27018127634           12594    0.00                   it 15 (bound)
       0.002     0     27018115176     27018127634           12458    0.00                   it 16 (value)
       0.002     0     27018117035     27018127634           10599    0.00                   it 17 (value)
       0.002     0     27018117035     27018127633           10598    0.00                   it 17 (bound)
       0.003     0     27018119811     27018127633            7822    0.00                   it 18 (value)
       0.003     0     27018119811     27018127631            7820    0.00                   it 19 (bound)
       0.003     0     27018119811     27018127629            7818    0.00                   it 21 (bound)
       0.003     0     27018121352     27018127629            6277    0.00                   it 22 (value)
       0.004     0     27018121352     27018127623            6271    0.00                   it 25 (bound)
       0.004     0     27018121352     27018127620            6268    0.00                   it 27 (bound)
       0.004     0     27018121352     27018127615            6263    0.00                   it 29 (bound)
       0.004     0     27018121352     27018127611            6259    0.00                   it 33 (bound)
       0.004     0     27018121498     27018127611            6113    0.00                   it 34 (value)
       0.005     0     27018121498     27018127595            6097    0.00                   it 35 (bound)
       0.005     0     27018121498     27018127594            6096    0.00                   it 37 (bound)
       0.005     0     27018121498     27018127588            6090    0.00                   it 39 (bound)
       0.005     0     27018121498     27018127576            6078    0.00                   it 43 (bound)
       0.005     0     27018121498     27018127572            6074    0.00                   it 45 (bound)
       0.006     0     27018121498     27018127570            6072    0.00                   it 47 (bound)
       0.006     0     27018121498     27018127565            6067    0.00                   it 49 (bound)
       0.006     0     27018121498     27018127563            6065    0.00                   it 51 (bound)
       0.006     0     27018121498     27018127556            6058    0.00                   it 53 (bound)
       0.006     0     27018121498     27018127553            6055    0.00                   it 55 (bound)
       0.006     0     27018121498     27018127552            6054    0.00                   it 57 (bound)
       0.006     0     27018121928     27018127552            5624    0.00                   it 58 (value)
       0.006     0     27018121928     27018127548            5620    0.00                   it 61 (bound)
       0.006     0     27018121928     27018127539            5611    0.00                   it 63 (bound)
       0.006     0     27018121928     27018127536            5608    0.00                   it 64 (bound)
       0.006     0     27018121928     27018127533            5605    0.00                   it 65 (bound)
       0.006     0     27018121928     27018127527            5599    0.00                   it 66 (bound)
       0.006     0     27018121928     27018127512            5584    0.00                   it 67 (bound)
       0.006     0     27018121928     27018127511            5583    0.00                   it 68 (bound)
       0.006     0     27018121928     27018127499            5571    0.00                   it 69 (bound)
       0.006     0     27018121928     27018127489            5561    0.00                   it 70 (bound)
       0.006     0     27018121928     27018127442            5514    0.00                   it 71 (bound)
       0.006     0     27018121928     27018127379            5451    0.00                   it 72 (bound)
       0.006     0     27018121928     27018127109            5181    0.00                   it 73 (bound)
       0.006     0     27018121928     27018127103            5175    0.00                   it 74 (bound)
       0.007     0     27018122233     27018127103            4870    0.00                   it 75 (value)
       0.007     0     27018122468     27018127103            4635    0.00                   it 75 (value)
       0.007     0     27018122468     27018126889            4421    0.00                   it 75 (bound)
       0.007     0     27018122468     27018126854            4386    0.00                   it 76 (bound)
       0.007     0     27018122468     27018126307            3839    0.00                   it 77 (bound)
       0.007     0     27018122468     27018125684            3216    0.00                   it 78 (bound)
       0.007     0     27018122468     27018122468               0    0.00                   it 79 (bound)
       0.007     1     27018122468     27018122468               0    0.00        algorithm end (solution)

Final statistics
----------------
Value:                        27018122468
Has solution:                 1
Bound:                        27018122468
Absolute optimality gap:      0
Relative optimality gap (%):  0
Time (s):                     0.00667005
Number of recursive calls:    2

Solution
--------
Number of items:  4959 / 10000 (49.59%)
Weight:           24537085626 / 24537085856 (100%)
Profit:           27018122468
Feasible:         1
./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
cd build/test
ctest --parallel

knapsacksolver's People

Contributors

fontanf avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

knapsacksolver's Issues

Issues with compiling the project

Salut Florian, ça va?

We have issues compiling this project (Windows 11, Visual Studio 2019).
We encounter bind2nd and mem_fun no longer being supported in newer C++ versions.

`PS C:\Users\Pasca\Desktop\knapsacksolver> bazel build --verbose_failures //python:knapsacksolver.so
WARNING: while reading option defaults file 'c:\users\pasca\desktop\knapsacksolver.bazelrc':
invalid command name 'echo'.
WARNING: while reading option defaults file 'c:\users\pasca\desktop\knapsacksolver.bazelrc':
invalid command name 'echo'.
WARNING: while reading option defaults file 'c:\users\pasca\desktop\knapsacksolver.bazelrc':
invalid command name 'echo'.
INFO: Analyzed target //python:knapsacksolver.so (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
ERROR: C:/users/pasca/desktop/knapsacksolver/knapsacksolver/algorithms/BUILD:124:11: Compiling knapsacksolver/algorithms/bellman.cpp failed: (Exit 2): cl.exe failed: error executing command
cd /d C:/users/pasca/bazel_pasca/pew4ifnh/execroot/main
SET INCLUDE=C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\ATLMFC\include;C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include;C:\Program Files (x86)\Windows Kits\10\include\10.0.19041.0\ucrt;C:\Program Files (x86)\Windows Kits\10\include\10.0.19041.0\shared;C:\Program Files (x86)\Windows Kits\10\include\10.0.19041.0\um;C:\Program Files (x86)\Windows Kits\10\include\10.0.19041.0\winrt;C:\Program Files (x86)\Windows Kits\10\include\10.0.19041.0\cppwinrt
SET PATH=C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\Extensions\Microsoft\IntelliCode\CLI;C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\bin\HostX64\x64;C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\VC\VCPackages;C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\CommonExtensions\Microsoft\TestWindow;C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer;C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\bin\Roslyn;C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Team Tools\Performance Tools\x64;C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Team Tools\Performance Tools;C:\Program Files (x86)\Microsoft Visual Studio\Shared\Common\VSPerfCollectionTools\vs2019\x64;C:\Program Files (x86)\Microsoft Visual Studio\Shared\Common\VSPerfCollectionTools\vs2019;C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\CommonExtensions\Microsoft\FSharp\Tools;C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\Tools\devinit;C:\Program Files (x86)\Windows Kits\10\bin\10.0.19041.0\x64;C:\Program Files (x86)\Windows Kits\10\bin\x64;C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin;C:\Windows\Microsoft.NET\Framework64\v4.0.30319;C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE;C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\Tools;;C:\WINDOWS\system32;C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin;C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\CommonExtensions\Microsoft\CMake\Ninja;C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\VC\Linux\bin\ConnectionManagerExe
SET PWD=/proc/self/cwd
SET RUNFILES_MANIFEST_ONLY=1
SET TEMP=C:\Users\Pasca\AppData\Local\Temp
SET TMP=C:\Users\Pasca\AppData\Local\Temp
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\bin\HostX64\x64\cl.exe /nologo /DCOMPILER_MSVC /DNOMINMAX /D_WIN32_WINNT=0x0601 /D_CRT_SECURE_NO_DEPRECATE /D_CRT_SECURE_NO_WARNINGS /bigobj /Zm500 /EHsc /wd4351 /wd4291 /wd4250 /wd4996 /I. /Ibazel-out/x64_windows-opt/bin /Iexternal/optimizationtools /Ibazel-out/x64_windows-opt/bin/external/optimizationtools /Iexternal/json /Ibazel-out/x64_windows-opt/bin/external/json /Ibazel-out/x64_windows-opt/bin/external/json/virtual_includes/json /showIncludes /MD /O2 /Oy- /DNDEBUG /wd4117 -D__DATE
="redacted" -D__TIMESTAMP__="redacted" -D__TIME__="redacted" /Gy /Gw /std:c++latest /Fobazel-out/x64_windows-opt/bin/knapsacksolver/algorithms/_objs/bellman/bellman.obj /c knapsacksolver/algorithms/bellman.cpp

Configuration: 6933d94e9fd1de3542dd7b00af1b432484d3fa768156416469aa665c9cda71a2

Execution platform: @local_config_platform//:host

external/optimizationtools\optimizationtools/utils/info.hpp(181): error C2039: 'bind2nd': is not a member of 'std'
bazel-out/x64_windows-opt/bin/external/json/virtual_includes/json\nlohmann/json.hpp(22631): note: see declaration of 'std'
external/optimizationtools\optimizationtools/utils/info.hpp(181): error C2039: 'mem_fun': is not a member of 'std'
bazel-out/x64_windows-opt/bin/external/json/virtual_includes/json\nlohmann/json.hpp(22631): note: see declaration of 'std'
external/optimizationtools\optimizationtools/utils/info.hpp(181): error C3861: 'mem_fun': identifier not found
external/optimizationtools\optimizationtools/utils/info.hpp(181): error C3861: 'bind2nd': identifier not found
.\knapsacksolver/part_solution_1.hpp(62): warning C4319: '~': zero extending 'unsigned long' to 'const knapsacksolver::PartSol1' of greater size
ERROR: C:/users/pasca/desktop/knapsacksolver/knapsacksolver/BUILD:3:11: Compiling knapsacksolver/solution.cpp failed: (Exit 2): cl.exe failed: error executing command
cd /d C:/users/pasca/bazel_pasca/pew4ifnh/execroot/main
SET INCLUDE=C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\ATLMFC\include;C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include;C:\Program Files (x86)\Windows Kits\10\include\10.0.19041.0\ucrt;C:\Program Files (x86)\Windows Kits\10\include\10.0.19041.0\shared;C:\Program Files (x86)\Windows Kits\10\include\10.0.19041.0\um;C:\Program Files (x86)\Windows Kits\10\include\10.0.19041.0\winrt;C:\Program Files (x86)\Windows Kits\10\include\10.0.19041.0\cppwinrt
SET PATH=C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\Extensions\Microsoft\IntelliCode\CLI;C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\bin\HostX64\x64;C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\VC\VCPackages;C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\CommonExtensions\Microsoft\TestWindow;C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer;C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\bin\Roslyn;C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Team Tools\Performance Tools\x64;C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Team Tools\Performance Tools;C:\Program Files (x86)\Microsoft Visual Studio\Shared\Common\VSPerfCollectionTools\vs2019\x64;C:\Program Files (x86)\Microsoft Visual Studio\Shared\Common\VSPerfCollectionTools\vs2019;C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\CommonExtensions\Microsoft\FSharp\Tools;C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\Tools\devinit;C:\Program Files (x86)\Windows Kits\10\bin\10.0.19041.0\x64;C:\Program Files (x86)\Windows Kits\10\bin\x64;C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin;C:\Windows\Microsoft.NET\Framework64\v4.0.30319;C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE;C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\Tools;;C:\WINDOWS\system32;C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin;C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\CommonExtensions\Microsoft\CMake\Ninja;C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\VC\Linux\bin\ConnectionManagerExe
SET PWD=/proc/self/cwd
SET RUNFILES_MANIFEST_ONLY=1
SET TEMP=C:\Users\Pasca\AppData\Local\Temp
SET TMP=C:\Users\Pasca\AppData\Local\Temp
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\bin\HostX64\x64\cl.exe /nologo /DCOMPILER_MSVC /DNOMINMAX /D_WIN32_WINNT=0x0601 /D_CRT_SECURE_NO_DEPRECATE /D_CRT_SECURE_NO_WARNINGS /bigobj /Zm500 /EHsc /wd4351 /wd4291 /wd4250 /wd4996 /I. /Ibazel-out/x64_windows-opt/bin /Iexternal/optimizationtools /Ibazel-out/x64_windows-opt/bin/external/optimizationtools /Iexternal/json /Ibazel-out/x64_windows-opt/bin/external/json /Ibazel-out/x64_windows-opt/bin/external/json/virtual_includes/json /showIncludes /MD /O2 /Oy- /DNDEBUG /wd4117 -D__DATE
="redacted" -D__TIMESTAMP
="redacted" -D__TIME__="redacted" /Gy /Gw /std:c++latest /Fobazel-out/x64_windows-opt/bin/knapsacksolver/_objs/knapsacksolver/solution.obj /c knapsacksolver/solution.cpp

Configuration: 6933d94e9fd1de3542dd7b00af1b432484d3fa768156416469aa665c9cda71a2

Execution platform: @local_config_platform//:host

external/optimizationtools\optimizationtools/utils/info.hpp(181): error C2039: 'bind2nd': is not a member of 'std'
bazel-out/x64_windows-opt/bin/external/json/_virtual_includes/json\nlohmann/json.hpp(22631): note: see declaration of 'std'
external/optimizationtools\optimizationtools/utils/info.hpp(181): error C2039: 'mem_fun': is not a member of 'std'
bazel-out/x64_windows-opt/bin/external/json/_virtual_includes/json\nlohmann/json.hpp(22631): note: see declaration of 'std'
external/optimizationtools\optimizationtools/utils/info.hpp(181): error C3861: 'mem_fun': identifier not found
external/optimizationtools\optimizationtools/utils/info.hpp(181): error C3861: 'bind2nd': identifier not found
Target //python:knapsacksolver.so failed to build
INFO: Elapsed time: 2.966s, Critical Path: 2.64s
INFO: 9 processes: 9 internal.
FAILED: Build did NOT complete successfully```

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.