{"id":28935685,"url":"https://github.com/rafaelmartinelli/knapsacks.jl","last_synced_at":"2025-08-15T15:08:36.248Z","repository":{"id":38411824,"uuid":"364674819","full_name":"rafaelmartinelli/Knapsacks.jl","owner":"rafaelmartinelli","description":"Julia package to solve Knapsack problems","archived":false,"fork":false,"pushed_at":"2023-10-23T20:12:48.000Z","size":250,"stargazers_count":22,"open_issues_count":3,"forks_count":5,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-07-22T20:11:18.276Z","etag":null,"topics":["julia","knapsack","optimization"],"latest_commit_sha":null,"homepage":"","language":"Julia","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rafaelmartinelli.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":"citation.bib","codeowners":null,"security":null,"support":null}},"created_at":"2021-05-05T18:44:02.000Z","updated_at":"2025-06-06T19:56:23.000Z","dependencies_parsed_at":"2022-09-09T12:41:43.140Z","dependency_job_id":null,"html_url":"https://github.com/rafaelmartinelli/Knapsacks.jl","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"purl":"pkg:github/rafaelmartinelli/Knapsacks.jl","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rafaelmartinelli%2FKnapsacks.jl","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rafaelmartinelli%2FKnapsacks.jl/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rafaelmartinelli%2FKnapsacks.jl/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rafaelmartinelli%2FKnapsacks.jl/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rafaelmartinelli","download_url":"https://codeload.github.com/rafaelmartinelli/Knapsacks.jl/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rafaelmartinelli%2FKnapsacks.jl/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":270586128,"owners_count":24611314,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-15T02:00:12.559Z","response_time":110,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["julia","knapsack","optimization"],"created_at":"2025-06-22T20:01:06.766Z","updated_at":"2025-08-15T15:08:36.240Z","avatar_url":"https://github.com/rafaelmartinelli.png","language":"Julia","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Knapsacks.jl\n\n[![Stable](https://img.shields.io/badge/docs-stable-blue.svg)](https://rafaelmartinelli.github.io/Knapsacks.jl/stable)\n[![Dev](https://img.shields.io/badge/docs-dev-blue.svg)](https://rafaelmartinelli.github.io/Knapsacks.jl/dev)\n[![Build Status](https://github.com/rafaelmartinelli/Knapsacks.jl/workflows/CI/badge.svg)](https://github.com/rafaelmartinelli/Knapsacks.jl/actions)\n[![Coverage](https://codecov.io/gh/rafaelmartinelli/Knapsacks.jl/branch/main/graph/badge.svg)](https://codecov.io/gh/rafaelmartinelli/Knapsacks.jl)\n[![Project Status: Active – The project has reached a stable, usable state and is being actively developed.](https://www.repostatus.org/badges/latest/active.svg)](https://www.repostatus.org/#active)\n\nThis package solves Knapsack Problems (KPs) using different algorithms.\n\n## Usage\n\nFirst, the package defines the `Knapsack` type:\n\n```julia\nstruct Knapsack\n    capacity::Int64            # Knapsack capacity\n    weights ::Vector{Int64}    # Items' weights\n    profits ::Vector{Int64}    # Items' profits\nend\n```\n\nThen, there are four available solvers, called from a single function which takes a `Knapsack` instance, and returns the optimal/best value and an `Array` with the selected items:\n\n```julia\nfunction solveKnapsack(data::KnapsackData, algorithm::Symbol = :ExpandingCore; optimizer = nothing)\n```\n\nWhere `algorithm` must be one of the following:\n\n- `DynamicProgramming`: Solves KP using a naïve dynamic programming.\n- `BinaryModel`: Solves KP using a binary programming model.\n- `ExpandingCore`: Solves KP using Pisinger's expanding core algorithm.\n- `Heuristic`: Solves KP using a simple heuristic.\n\nAlgorithm `BinaryModel` uses [JuMP](https://jump.dev/), and the user must pass the optimizer.\n\nFor example, given a `Knapsack` instance `data`:\n\n```julia\noptimal, selected = solveKnapsack(data, :DynamicProgramming)\noptimal, selected = solveKnapsack(data, :BinaryModel; optimizer = GLPK.Optimizer)\noptimal, selected = solveKnapsack(data, :ExpandingCore)\nvalue, selected = solveKnapsack(data, :Heuristic)\n```\n\n## Instance generator\n\nThe package is able to generate random instances of `Knapsack` with the following function (based on [this code](http://hjemmesider.diku.dk/~pisinger/generator.c)):\n\n```julia\nfunction generateKnapsack(num_items::Int64, range::Int64 = 1000; type::Symbol = :Uncorrelated, seed::Int64 = 42, num_tests::Int64 = 1000)::Knapsack\n```\n\nWhere:\n\n- `num_items`: Number of items.\n- `range`: Maximum weight value.\n- `type`: Profit type (`:Uncorrelated`, `:WeakCorrelated`, `:StrongCorrelated`, `:SubsetSum`).\n- `seed`: Random seed value.\n- `num_tests`: Check source code or original code.\n\n## Installation\n\nThis package is a registered Julia Package, and can be installed through the Julia package manager.  \nOpen Julia's interactive session (REPL) and type:\n\n```julia\n] add Knapsacks\n```\n\n## Benchmark\n\nBenchmark results (time in seconds) for different maximum values for weights and profits, number of items and algorithms. Average times for 10 runs and using `@timed` (`BinaryModel` using `GLPK.jl`).\n\n```text\n--------------------------------------------------------------------------------------------------\n MaxV\\Items         10        100        500       1000       2000       4000  Algorithm\n--------------------------------------------------------------------------------------------------\n             0.0000022  0.0000111  0.0000565  0.0001892  0.0007063  0.0026810  DynamicProgramming\n         10  0.0001429  0.0003092  0.0009412  0.0019578  0.0039707  0.0122269  BinaryModel\n             0.0000072  0.0000293  0.0001384  0.0003038  0.0006792  0.0013258  ExpandingCore\n             0.0000016  0.0000052  0.0000235  0.0000478  0.0001008  0.0002182  Heuristic\n--------------------------------------------------------------------------------------------------\n             0.0000062  0.0000499  0.0003760  0.0011797  0.0110915  0.0434132  DynamicProgramming\n        100  0.0001357  0.0004809  0.0017649  0.0040757  0.0093222  0.0269660  BinaryModel\n             0.0000095  0.0000600  0.0002152  0.0003791  0.0007064  0.0010730  ExpandingCore\n             0.0000013  0.0000050  0.0000192  0.0000409  0.0000928  0.0001957  Heuristic\n--------------------------------------------------------------------------------------------------\n             0.0000167  0.0001582  0.0013383  0.0115258  0.0674425  0.3561994  DynamicProgramming\n        500  0.0001290  0.0006400  0.0017707  0.0056317  0.0174576  0.0483382  BinaryModel\n             0.0000090  0.0000473  0.0002074  0.0003911  0.0006959  0.0014079  ExpandingCore\n             0.0000013  0.0000044  0.0000191  0.0000417  0.0000866  0.0001854  Heuristic\n--------------------------------------------------------------------------------------------------\n             0.0000306  0.0003130  0.0063493  0.0296504  0.1574919  0.7645551  DynamicProgramming\n       1000  0.0001279  0.0003963  0.0021209  0.0089878  0.0247364  0.0634847  BinaryModel\n             0.0000084  0.0000498  0.0002309  0.0004473  0.0010606  0.0015858  ExpandingCore\n             0.0000014  0.0000043  0.0000209  0.0000423  0.0000873  0.0001845  Heuristic\n--------------------------------------------------------------------------------------------------\n             0.0000616  0.0007209  0.0174228  0.0695316  0.3422440  1.6595295  DynamicProgramming\n       2000  0.0001297  0.0004131  0.0024877  0.0062686  0.0211603  0.0714104  BinaryModel\n             0.0000090  0.0000538  0.0002315  0.0004709  0.0008501  0.0018993  ExpandingCore\n             0.0000014  0.0000045  0.0000225  0.0000422  0.0000866  0.0001845  Heuristic\n--------------------------------------------------------------------------------------------------\n```\n\nIntel(R) Core(TM) i7-8700K CPU @ 3.70GHz, 64GB RAM, using Julia 1.7.2 on Ubuntu 20.04 LTS.\n\n## How to cite this package\n\nYou can use the [bibtex file](https://github.com/rafaelmartinelli/Knapsacks.jl/blob/main/citation.bib) available in the project.\n\nDon't forget to star our package!\n\n## Related links\n\n- [David Pisinger's optimization codes](http://hjemmesider.diku.dk/~pisinger/codes.html)\n\n## Other packages\n\n- [BPPLib.jl](https://github.com/rafaelmartinelli/BPPLib.jl): Bin Packing Problem and Cutting Stock Problem Lib\n- [GAPLib.jl](https://github.com/rafaelmartinelli/GAPLib.jl): Generalized Assignment Problem Lib\n- [FacilityLocationProblems.jl](https://github.com/rafaelmartinelli/FacilityLocationProblems.jl): Capacitated Facility Location Problem Lib\n- [CARPData.jl](https://github.com/rafaelmartinelli/CARPData.jl): Capacitated Arc Routing Problem Lib\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frafaelmartinelli%2Fknapsacks.jl","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frafaelmartinelli%2Fknapsacks.jl","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frafaelmartinelli%2Fknapsacks.jl/lists"}