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

## Background

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.

## Solution

``````class Item

attr_accessor :weight, :value

def initialize(weight, value)
@weight = weight
@value = value
end
end``````
item.rb

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
end
end``````
knapsack.rb

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.

``````require_relative('item.rb')
require_relative('knapsack.rb')

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
else
verbose = false
end
end``````
genetic.rb

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 << Item.new(ran_weight, ran_value)
end

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

knapsacks << Knapsack.new(ran_items)
end``````
genetic.rb

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
end
end

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

knapsack.total_weight = total_weight
knapsack.total_value = total_value
sum_value += total_value
end``````
genetic.rb

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 = Knapsack.new(knapsacks[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
break
else
rnd_selected += 1
end
end

new_generation << Knapsack.new(knapsacks[rnd_selected].chromosome.clone)
end

# Replace old generation with new
knapsacks = []
knapsacks = new_generation
generation += 1``````
genetic.rb

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
end

# 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 = Knapsack.new(new_chr_1)
new_2 = Knapsack.new(new_chr_2)

knapsacks[rnd_knap_1] = new_1
knapsacks[rnd_knap_2] = new_2``````
genetic.rb

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
end
end
end``````
genetic.rb

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
end``````
genetic.rb

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.