{"id":19006412,"url":"https://github.com/anty-filidor/knapsack-problem","last_synced_at":"2025-07-29T05:34:56.883Z","repository":{"id":106652076,"uuid":"452860878","full_name":"anty-filidor/knapsack-problem","owner":"anty-filidor","description":"Comparison of linear programming and heuristic as a case study of knapsack problem","archived":false,"fork":false,"pushed_at":"2022-01-30T21:31:37.000Z","size":167,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-21T14:24:28.640Z","etag":null,"topics":["experiment","heuristics","linear-programming","python"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/anty-filidor.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-01-27T22:08:18.000Z","updated_at":"2023-01-20T20:39:21.000Z","dependencies_parsed_at":null,"dependency_job_id":"aaf0ed67-8ebf-405f-912b-1544de919484","html_url":"https://github.com/anty-filidor/knapsack-problem","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/anty-filidor/knapsack-problem","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anty-filidor%2Fknapsack-problem","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anty-filidor%2Fknapsack-problem/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anty-filidor%2Fknapsack-problem/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anty-filidor%2Fknapsack-problem/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/anty-filidor","download_url":"https://codeload.github.com/anty-filidor/knapsack-problem/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anty-filidor%2Fknapsack-problem/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267633679,"owners_count":24118778,"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-07-29T02:00:12.549Z","response_time":2574,"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":["experiment","heuristics","linear-programming","python"],"created_at":"2024-11-08T18:32:15.580Z","updated_at":"2025-07-29T05:34:56.856Z","avatar_url":"https://github.com/anty-filidor.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Knapsack problem - linear programming vs. naive heuristic\n\n## Problem\n\nWe need to pack a knapsack which has certain capacity (_C_ - e.g. 20 kg). There\nare several (_n_) items that can be loaded into it. Each item has its weight\n(_w_ - e.g. 2 kg) and value (_p_ - e.g. 30 €). The task is to choose a subset\nof items that (1) can be stored inside knapsack, so that it is not overloaded\nand (2) their total value is maximal.\n\nWe can write the problem with following equations:\n\n\u003cimg src=\"./img/equations.png\" alt=\"Model equation\" width=\"300\"/\u003e\n\nThis problem can be solved with multiple approaches. We selected two methods\nand, after they had been implemented, we compared their efficiency.\n\n## Methods\n\n### Linear programming\n\nFirst method was implemented using linear programming approach. We just\nrewrote equations into the syntax of the selected library\n([pulp](https://coin-or.github.io/pulp/index.html)). We used\n[CBC](https://projects.coin-or.org/Cbc) solver to tackle equations. As a result\nwe obtained function _knapsack_gd_ in the script\n[linprog_method.py](./linprog_method.py).\n\nIt's good to mention that this approach guaranteed to obtain optimal solution\nfor the problem.\n\n### Naive heuristic\n\nSecond method was a greedy heuristic. We borrowed some ideas from\n[article](https://www.pythonpool.com/knapsack-problem-python/) available\nonline and improved it. The operation principle was to (1) compute 'usability'\nfor each item (as its _p/w_), (2) sort all items descending by that feature and\n(3) pack item by item to the knapsack following obtained sequence until it is\nfull. This method has been implemented as function _knapsack_gd_ in the script\n[greedy_method.py](./greedy_method.py).\n\nOf course this method, as all heuristics, didn't guarantee to get optimal\nsolution.\n\n## Setting up experiment\n\nTo answer the question \"which of two methods is better\" we prepared an\nexperiment to compare (1) time efficiency and (2) correctness of algorithms as\na function of data complexity.\n\n### Data generator\n\nAs first step, a utility that generates data with varying 'complexity' was\nimplemented. Every individual of created dataset had to meet following \nconditions:\n\n- sum of weights of items must be smaller than _1.3C_\n- each item can have weight from range _[1, 10]_\n- each item can have value from range _[1, 10]_\n\nThe parameter that determined a 'complexity' of the data was _C_, such that\n_C_ of *j*th generated tuple is _50_ units higher than _C_ of *j-1*th generated\ntuple.\n\nFunction that generates data can be found in\n[data_generator.py](./data_generator.py) script.\n\n### An 'engine'\n\nIn order to compute results, we prepared a dedicated set of functions\n([timer.py](./timer.py)). Their responsibility was to measure time of execution\nof each evaluated method for varying input data, check if result obtained for\ngreedy approach is optimal, and visualise experiment on the chat.\n\n## Results and discussion\n\nWe ran several experiments for different ranges of _C_. As a result, an\ninteresting phenomena has been observed for small and relatively big values of\n_C_ (see figures below):\n\n\u003cimg src=\"./img/exp_1_100.png\" alt=\"Results for small C\" width=\"500\"/\u003e\n\n\u003cimg src=\"./img/exp_100_10000.png\" alt=\"Results for big C\" width=\"500\"/\u003e\n\nAnalysis of the charts can tell us that greedy implementation is much faster\nthan its linear counterpart. Here, it's good to mention, that function\n_knapsack_gd_ wasn't prepared to meet high-efficiency condition.\n\nWe can also notice, that _knapsack_gd_ is almost accurate as linear programming\nimplementation for small _C_. However, for higher data complexity it gets less \nprecise, so that it cannot find optimal solution!\n\n## Conclusions\n\nExperiment that has been conducted using this codebase was intended to show\ndifferences in solving the knapsack problem with linear programming framework\nand heuristic. Without surprises, we were able to see, that those two,\ncompetitive methods have their own advantages and drawbacks. There is a\ntrade-off between time efficiency and precision of obtained result. Therefore,\nit's very important to determine the exact nature of the problem before\nimplementation. When it comes to accuracy, linear programming wins, but when\nspeed is the main goal, and we don't care if the result will be optimal -\ngreedy method should be chosen.\n\n## How to run the code\n\n- create environment (`conda create --name knapsack python=3.9`)\n- activate environment (`conda activate knapsack`)\n- install required libraries (`pip install -r requirements.txt`)\n- run `main.py` (`python main.py`)\n\n## References\n\n\\[1\\] https://coin-or.github.io/pulp/index.html  \n\\[2\\] https://www.pythonpool.com/knapsack-problem-python/  \n\\[3\\] https://realpython.com/linear-programming-python/#linear-programming-python-implementation  \n\\[4\\] https://projects.coin-or.org/Cbc\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanty-filidor%2Fknapsack-problem","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fanty-filidor%2Fknapsack-problem","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanty-filidor%2Fknapsack-problem/lists"}