{"id":16383172,"url":"https://github.com/eliancordoba/knapsack-problem","last_synced_at":"2026-06-18T06:31:39.756Z","repository":{"id":97309774,"uuid":"451251552","full_name":"ElianCordoba/knapsack-problem","owner":"ElianCordoba","description":"Tackle the Knapsack problem with genetic algorithms","archived":false,"fork":false,"pushed_at":"2022-01-27T19:07:35.000Z","size":15,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-21T19:44:18.990Z","etag":null,"topics":["genetic-algorithm","genetic-evolution","np"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/ElianCordoba.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-23T22:53:41.000Z","updated_at":"2022-01-27T19:41:07.000Z","dependencies_parsed_at":null,"dependency_job_id":"7b7e7deb-a9b9-4007-831c-815908a71e90","html_url":"https://github.com/ElianCordoba/knapsack-problem","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/ElianCordoba/knapsack-problem","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ElianCordoba%2Fknapsack-problem","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ElianCordoba%2Fknapsack-problem/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ElianCordoba%2Fknapsack-problem/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ElianCordoba%2Fknapsack-problem/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ElianCordoba","download_url":"https://codeload.github.com/ElianCordoba/knapsack-problem/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ElianCordoba%2Fknapsack-problem/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34479552,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-18T02:00:06.871Z","response_time":128,"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":["genetic-algorithm","genetic-evolution","np"],"created_at":"2024-10-11T04:07:37.087Z","updated_at":"2026-06-18T06:31:39.750Z","avatar_url":"https://github.com/ElianCordoba.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Knapsack problem\n\nThis is tool models and tackles the [Knapsack problem](https://en.wikipedia.org/wiki/Knapsack_problem), an [NP complexity](https://en.wikipedia.org/wiki/NP_(complexity)) problem. It's well documented so you can easily read the code and understand how it works under the hood.\n\n## Credits\nI got inspired by this great [video](https://www.youtube.com/watch?v=uQj5UNhCPuo\u0026t=0s) by Kia Codes. This repository started as a rewrite of the code used in that video series (found [here](https://github.com/kiecodes/genetic-algorithms)), moving it from Python to Typescript. After the initial port was done I decided to add my touch to adapt it to my own coding style.\n\n# Usage\n\n```bash\n  # Simplest configuration, with all default options.\n  # Uses:\n  # - Single gene mutation with 50% chance of mutating the gene\n  # - Single point crossover function\n  npm run example-1\n\n  # Run the tests in watch mode\n  npm run test\n```\n\n## Knapsack problem overview\n\nYou have a bag with a maximum weight of things it can carry. You also have a list of items with a given value and weight, your task is to come up with a list of items whose weight combined does not exceed the maximum weight and has the maximum value.\n\n## Functionality overview\n\n\u003e tl;dr of how this genetic algorithm works\n\n1. Create a `population`, an array of `genomes`, which itself it's just a bitmap, an array of 1 or 0, used to identify if the given item it's included or not\n\n```javascript\nconst population = [\n  [ 0, 0, 1 ], // Genome 1 = Item index 2 included\n  [ 1, 0, 1 ]  // Genome 2 = Items indexes 0 \u0026 2 included\n  [ 1, 1, 0 ]  // Genome 3 = Items indexes 0 \u0026 1 included\n];\n```\n\n2. Begin iterating until you hit the `generation_limit` or, if provided, until you hit the `max_fitness`. You need one of these criteria otherwise the simulation would run forever\n\n3. Sort the population by `fitness`, it's a measurement of how well the `genome` performs at the given task, in this case, the more total value you can carry without exceeding the weight limit the more `fitness` you will have.\n\n4. Take the fittest N `genomes` and put them into the next generation.\n\n5. Take two `genomes` in a weighted random choice*\n\n6. Apply a [crossover](https://en.wikipedia.org/wiki/Crossover_(genetic_algorithm)#Examples) (See One-point crossover\n for the simples example) function to those `genomes`\n\n7. Apply a mutation (see the `gene_mutation` function as an example) to both the `genomes` and push them to the next generation\n\n8. Repeat this process (jumping to step 5) until you have the desired number of `genomes` in the `next_population`\n\n9. Once the stop criteria, defined in step 2, is reached, sort the population one last time by fitness and return the fittest `genome`, that's your solution\n\n\n\n\\* A weighed choice means that you randomly choose elements in a list assigning them different probabilities of being selected, for example:\n\n```\n  Element 1 = weight 70 = 70%\n  Element 2 = weight 20 = 20%\n  Element 3 = weigth 10 = 10%\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feliancordoba%2Fknapsack-problem","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Feliancordoba%2Fknapsack-problem","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feliancordoba%2Fknapsack-problem/lists"}