Solving the 0-1 Knapsack Problem with a Genetic Algorithm in Ruby

The Knapsack Problem

The Knapsack Problem is an NP combinatorial optimization problem in which items that have both value and weight are placed into a "knapsack" with a weight limit. The goal is to maximize the value of the items while keeping the total weight of the items below the weight limit threshold. A maximized solution can be approximated using a genetic algorithm.

Genetic Algorithm

Genetic algorithms are biologically inspired, using natural selection, reproduction, mutation, and other elements of evolution to obtain solutions. They are often used to solve optimization problems and model certain systems.

knapsack.rb

The Knapsack class has a chromosome, or array of 0s and 1s representing whether a specific item is included in the knapsack, along with a total weight and a total value which will be calculated and stored based on its chromosome.

Loads the Item and Knapsack classes and accepts user input for the number of items, the number of knapsacks in the population, the maximum number of generations, and whether the script should run in verbose mode or silently. Note that there isn't really any validation here, so the script assumes correct user input.

The group of items is created with a pseudorandom weights between 0.0 and 10.0 and pseudorandom values between 0.0 and 100.0. then the initial generation of knapsacks is created each with a pseudorandom chromosome where each item has a 10% chance of being turned on.

As the main loop begins, we calculate the total weight and total value of each knapsack in order to determine its fitness. If a knapsack is over the weight limit, its value becomes 0.0 which effectively will remove it from the population. Note that the best overall knapsack is stored, and the sum_value of all knapsacks is calculated as well.

Here a Roulette Wheel style algorithm is used to create a new generation. Essentially a random number is chosen between 0.0 and 1.0, then each knapsack is looped through and their relative value is summed. The knapsack whose relative value is the one that puts the sum over the random value becomes a parent in the next generation. This has the effect of each knapsack occupying its own "slice" of a Roulette wheel, with its size proportionate to its share of value in the population.

Now it is time to expand the search space, so we randomly choose two knapsacks from the new generation and perform crossover. A randomly determined point in the chromosome separates each chromosome into a head and a tail. The heads and tails are swapped between the two chromosomes, which represents a large jump in the search space.

Finally, the elitist knapsack is added back into the next population (after the crossover and mutations so that it is not effected) and the main loop ends with some useful output.

Conclusion

The solution provided by the genetic algorithm is not guaranteed to be an absolute maximum, but if it runs long enough to eventually cover the majority of the solution space then it will certainly provide a very good solution. There are lots of things that you can tweak in this algorithm as well, such as the percentage chance of mutation or the method used for crossover. You can remove the elitism if desired, or change it to include the best five. If you make any significant changes and want to share them, don't hesitate to contact me!

The full code is publicly available on GitHub, and may be more up to date than the code here although I will try to keep this post updated.