{"id":21368277,"url":"https://github.com/chrisvilches/knapsack-2d-allocation","last_synced_at":"2025-03-16T08:12:45.790Z","repository":{"id":94023021,"uuid":"406633292","full_name":"ChrisVilches/Knapsack-2D-Allocation","owner":"ChrisVilches","description":"Genetic algorithm that solves the 2D rectangle knapsack problem. Try to fit as many rectangular items as possible inside a container, while maximizing the total value the items provide.","archived":false,"fork":false,"pushed_at":"2021-09-16T09:11:04.000Z","size":375,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-22T20:29:46.379Z","etag":null,"topics":["algorithm","computational-geometry","genetic-algorithm","genetic-algorithms","geometry","knapsack-problem","rust","rust-lang"],"latest_commit_sha":null,"homepage":"","language":"Rust","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/ChrisVilches.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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":"2021-09-15T06:04:29.000Z","updated_at":"2021-09-16T09:11:07.000Z","dependencies_parsed_at":"2023-03-13T17:08:12.109Z","dependency_job_id":null,"html_url":"https://github.com/ChrisVilches/Knapsack-2D-Allocation","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ChrisVilches%2FKnapsack-2D-Allocation","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ChrisVilches%2FKnapsack-2D-Allocation/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ChrisVilches%2FKnapsack-2D-Allocation/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ChrisVilches%2FKnapsack-2D-Allocation/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ChrisVilches","download_url":"https://codeload.github.com/ChrisVilches/Knapsack-2D-Allocation/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243841217,"owners_count":20356446,"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","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":["algorithm","computational-geometry","genetic-algorithm","genetic-algorithms","geometry","knapsack-problem","rust","rust-lang"],"created_at":"2024-11-22T07:23:37.249Z","updated_at":"2025-03-16T08:12:45.768Z","avatar_url":"https://github.com/ChrisVilches.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Knapsack 2D Allocation\n\nGenetic algorithm that solves the 2D rectangle knapsack problem. Try to fit as many rectangular items as possible inside a container, while maximizing the total value the items provide.\n\nExample (visual output):\n\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"./example_big.jpg\" width=\"350\" title=\"Visual output example\"\u003e\u003c/img\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"./resident_evil.jpg\" width=\"400\" title=\"Resident Evil inventory\"\u003e\u003c/img\u003e\n\u003c/p\u003e\n\nPicture above is taken from Resident Evil 4. The player must organize the inventory to fit important items (unlike most games, it doesn't have an *infinite pocket*). This algorithm follows the same concept.\n\n## Specifications\n\n* Every rectangular item has a `benefit` value assigned to them.\n* Tries to maximize the summed benefit of all items. It doesn't try to minimize wasted room. In other words, fit the most important items, avoiding low-value items that use up space.\n* The algorithm consists of a mix of the genetic algorithm and ad-hoc heuristics.\n* For now, the rectangles are not attempted to be inserted rotated.\n* Made with Rust (rustc 1.55.0 and cargo 1.55.0).\n\n## How to run\n\n### Randomized example (easiest)\n\nGenerate a random scenario:\n\n```bash\ncargo run --release -- random --container-square-side 20 --item-count 160 --item-max-square-side 11 --max-benefit 10\n```\n\nIn this example, the optimal value goes from 149 to 207 in ~1500 generations. An image is also generated to visualize the result.\n\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"./readme_output_example.png\" width=\"256\" title=\"Random example output\"\u003e\u003c/img\u003e\n\u003c/p\u003e\n\nOutput log:\n\n```\nGen #0 | Best score: 149 | Gen avg: 114.17 | Current optimal: 149 | Optimal ID: 05566633821962aa9d8f5c4a3eaa5b3d | Wasted room: 4\nGen #1 | Best score: 153 | Gen avg: 112.93 | Current optimal: 153 | Optimal ID: 0f1325681a46b1abfffce280463813aa | Wasted room: 2\n...\nGen #63 | Best score: 177 | Gen avg: 89.69 | Current optimal: 183 | Optimal ID: 01d2be0431939ced28fdc937479c5515 | Wasted room: 4\nGen #64 | Best score: 188 | Gen avg: 93.27 | Current optimal: 188 | Optimal ID: 67dd012fdf627c4cd66ff96332e80db5 | Wasted room: 19\n...\nGen #244 | Best score: 201 | Gen avg: 97.31 | Current optimal: 201 | Optimal ID: 8a7e5cabda5c1b33bc0db4d0efa3085b | Wasted room: 7\n...\nGen #1058 | Best score: 192 | Gen avg: 86.28 | Current optimal: 209 | Optimal ID: 9ae42a1c03f59d2feff45938973c5d36 | Wasted room: 3\n...\nGen #1509 | Best score: 207 | Gen avg: 101.49 | Current optimal: 214 | Optimal ID: 9a2e12dd39ef38787ebb47aba54e13b5 | Wasted room: 11\n\n# Press CTRL+C to stop\nStopping...\n\nGen #1510 | Best score: 207 | Gen avg: 111.95 | Current optimal: 214 | Optimal ID: 9a2e12dd39ef38787ebb47aba54e13b5 | Wasted room: 11\nMax score assuming infinite container: 693\nTotal generations: 1511\nCurrent optimal: 214\nOptimal ID: 9a2e12dd39ef38787ebb47aba54e13b5\nWasted room: 11\nGenerations where local optimums were found: [0, 1, 8, 15, 29, 64, 76, 85, 137, 146, 175, 602, 630, 980, 1343, 1480]\nBest solution found: [79, 37, 116, 153, 57, 25, 122, 41, 21, 8, 60, 27, 31, 128, 49, 154, 102, 119, 132, 112, 98, 0, 39, 89, 83, 149, 23, 156, 12, 16, 86, 29, 92, 14, 123, 142, 10, 3, 93, 100, 124, 150, 71, 67, 28, 72, 125, 52, 121, 64, 82, 109, 17, 81, 104, 76, 51, 129, 151, 34, 107, 131, 113, 61, 42, 117, 84, 87, 133, 11, 43, 130, 114, 70, 33, 137, 50, 115, 105, 157, 47, 90, 95, 152, 126, 99, 20, 103, 94, 55, 85, 88, 145, 18, 6, 75, 38, 97, 45, 65, 1, 69, 80, 135, 9, 148, 66, 56, 106, 74, 108, 158, 2, 54, 4, 78, 136, 48, 44, 15, 144, 143, 62, 155, 101, 141, 68, 73, 118, 30, 140, 120, 7, 26, 19, 40, 36, 32, 96, 134, 63, 53, 139, 77, 111, 35, 24, 138, 59, 91, 159, 13, 127, 46, 58, 146, 5, 22, 147, 110]\n```\n\n### Read input from file\n\nCreate a file named `input_file.txt` with a content like this:\n\n```\n12 12\n15\n3 5 7\n7 3 6\n6 2 4\n7 1 5\n10 2 3\n6 7 4\n2 6 3\n1 2 3\n8 2 7\n5 9 2\n10 2 1\n5 9 8\n8 6 6\n4 9 9\n2 9 3\n```\n\nThe first integer is `container width`.\n\nThe second integer is `container height`.\n\nThe third integer is `number of items`.\n\nThe next `number of items` lines contain three integers each, `item width`, `item height` and its `benefit` value.\n\n```bash\ncargo run --release -- file --file-input input_file.txt\n```\n\n## Issues \u0026 To-Do\n\n* Fix many `TODO:` comments in the source code (mostly refactoring).\n* Selection, crossover, etc. may be improved in the future to provide better solutions.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchrisvilches%2Fknapsack-2d-allocation","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchrisvilches%2Fknapsack-2d-allocation","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchrisvilches%2Fknapsack-2d-allocation/lists"}