Multiple Knapsack Problem Solver

Package solves multiple knapsack optimisation problem. Given a set of items, each with volume and value, it will allocate them to knapsacks of a given size in a way that value of top N knapsacks is as large as possible.

Build Status

This package solves multiple knapsack problem by assigning items optimally to knapsacks using Mixed Integer Linear Programming (MILP) solver of choice.

Definition of the mknapsack problem

We start with a list of items that we want to order with each assigned a:

  • sku - this is an id of the product / item that we want to order.
  • profit - expected profit from sales of this item
  • volume - this can be m3 of the box for example
  • moq - mininum order quanity (MOQ)
  • sold - flag that defines if this item must be added as highest priority prior to othe items

Those items should be optimally packed into multiple containers of the a given size (cap). Items should be aded to containers in the way that each container is more profitable than the following one.

Supported solvers

Package implements interface to several solvers which can be set via mknapsack.solver option.

Currently you can choose from those options:

lpsolve is default option.


Solve problem with CBC COIN-OR solver:

options(mknapsack.solver = "cbc")
items <- data.table(
  volume = pmin(rlnorm(100, log(2), log(3)), 15),
  profit = rgamma(100, shape = 1, scale = 100) - 25
items[, knapsack := 
    profit = profit, 
    volume = volume, 
    cap = 65
#Aggregate solution to knapsacks
knapsacks <- items[order(knapsack), 
                    .(volume = sum(volume), profit = sum(profit)), 
                    by = knapsack]
#    knapsack volume   profit
# 1:        1 64.89659 5000.27608
# 2:        2 64.40358 1540.40302
# 3:        3 64.97235  340.92516
# 4:        4 53.33824   88.02793
# 5:       NA 91.13399 -272.54349


Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.


0.1.0 by Bulat Yapparov, 3 years ago

Report a bug at

Browse source code at

Authors: Bulat Yapparov [aut, cre] , [cph]

Documentation:   PDF Manual  

Task views: Optimization and Mathematical Programming

GPL-2 license

Imports assertthat, data.table, lpSolve

Suggests testthat, mockery, Rglpk, ROI, ROI.plugin.glpk

See at CRAN