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


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 Algorithms

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.


class Item

  attr_accessor :weight, :value

  def initialize(weight, value)
    @weight = weight
    @value = value

The Item class has both a weight and a value, which will be set randomly within a range.

class Knapsack

  attr_accessor :chromosome, :total_weight, :total_value

  def initialize(chromosome)
    @chromosome = chromosome
    total_weight = 0.0
    total_value = 0.0

The Knapsack class has a chromosome, or array of zeroes and ones 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.


if ARGV.length > 0
  num_items = ARGV[0].chomp.to_i
  num_knapsacks = ARGV[1].chomp.to_i
  num_generations = ARGV[2].chomp.to_i
  verbose = ARGV[3]
  if verbose == "true"
    verbose = true
    verbose = false

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.

items = []
knapsacks = []
generation = 1

# Generate random items
num_items.times do
  ran_weight = (rand * 10).round(2)
  ran_value = (rand * 100).round(2)
  items <<, ran_value)

# Generate initial knapsacks
num_knapsacks.times do 
  ran_items = []
  num_items.times do
    if rand < 0.1
      ran_items << 1
      ran_items << 0

  knapsacks <<

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.

# Main loop
until generation > num_generations

  puts "==================================" if verbose
  puts "Begin generation: " + generation.to_s if verbose
  puts "==================================" if verbose

  sum_value = 0.0
  best_value = 0.0
  best_knapsack = 0
  max_weight = 50.0

  # Calculate value and weight
  knapsacks.each_with_index do |knapsack, index|
    total_weight = 0.0
    total_value = 0.0
    knapsack.chromosome.each_with_index do |gene, gene_index|
      if gene === 1
        total_weight += items[gene_index].weight
        total_value += items[gene_index].value

    if total_weight <= max_weight
      if total_value > best_value
        best_value = total_value
        best_knapsack = index
      total_value = 0.0

    knapsack.total_weight = total_weight
    knapsack.total_value = total_value
    sum_value += total_value

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.

  # Use Roulette wheel algorithm to proportionately create next generation
  new_generation = []
  elitist =[best_knapsack].chromosome.clone)
  puts 'Elitist: ' + best_knapsack.to_s if verbose
  p elitist if verbose
  (num_knapsacks-1).times do
    rnd = rand();
    rnd_sum = 0.0
    rnd_selected = 0

    knapsacks.each do |knapsack|
      rel_value = knapsack.total_value / sum_value
      rnd_sum += rel_value
      if rnd_sum > rnd
        rnd_selected += 1

    new_generation <<[rnd_selected].chromosome.clone)

  # Replace old generation with new
  knapsacks = []
  knapsacks = new_generation
  generation += 1

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.

  # Randomly select two knapsacks
  rnd_knap_1 = (0...num_knapsacks-1).to_a.sample
  rnd_knap_2 = rnd_knap_1
  until (rnd_knap_2 != rnd_knap_1)
    rnd_knap_2 = (0...num_knapsacks-1).to_a.sample

  # Perform crossover
  split_point = (0...num_items-1).to_a.sample
  front_1 = knapsacks[rnd_knap_1].chromosome[0, split_point]
  front_2 = knapsacks[rnd_knap_2].chromosome[0, split_point]
  back_1 = knapsacks[rnd_knap_1].chromosome[split_point, num_items-1]
  back_2 = knapsacks[rnd_knap_2].chromosome[split_point, num_items-1]
  new_chr_1 = front_1 + back_2
  new_chr_2 = front_2 + back_1
  new_1 =
  new_2 =

  knapsacks[rnd_knap_1] = new_1
  knapsacks[rnd_knap_2] = new_2

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.

  # Perform mutation
  knapsacks.each do |knapsack|
    knapsack.chromosome.each_with_index do |gene, index|
      if rand < 0.01
        puts 'Successful mutation at gene: ' + index.to_s if verbose
        gene == 0 ? gene = 1 : gene = 0
        knapsack.chromosome[index] = gene

Mutation is performed to make smaller "tweaks" in the search space. For each knapsack and each gene in its chromosome there is a 1% chance that it will flip, either going from 0 to 1 or 1 to 0.

  knapsacks << elitist

  puts 'Last knapsack:' if verbose
  p knapsacks[num_knapsacks-1] if verbose
  puts 'Best value:'
  p best_value

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.


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.