Best Knapsack_ruby code snippet using Knapsack.initialize
knapsack3.rb
Source:knapsack3.rb
...33 new(chromosome)34 end35 attr_accessor :score, :fitness36 attr_reader :chromosome37 def initialize(chromosome = nil, chromosome_size = nil)38 if chromosome39 @chromosome = chromosome40 else41 @chromosome = []42 chromosome_size.times { @chromosome << (rand(0..1) == 1) }43 end44 end45 private_class_method :new46 def chromosome_as_list47 list = []48 @chromosome.each_with_index do |gene, index|49 list << Knapsack::ITEMS[index].name if gene50 end51 list.join(', ')52 end53 def >(other)54 return true if other.nil?55 score > other.score56 end57end58class Population < Array59 def initialize(chromosome_size, population_size)60 population_size.times { self << Individual.random(chromosome_size) }61 end62 def best63 self.sort_by{|individual| individual.score}.last64 end65end66class Evaluator67 def initialize(capacity, population)68 @capacity = capacity69 @population = population70 end71 def evaluate72 @population.each {|individual| score(individual) }73 fitness74 end75 private76 def score(individual)77 value = 078 weight = 079 individual.chromosome.each_with_index do |item, index|80 if item81 value += Knapsack::ITEMS[index].value82 weight += Knapsack::ITEMS[index].weight83 end84 end85 if weight > @capacity86 value -= 2 * (weight - @capacity)87 value = 0 if value < 088 end89 individual.score = value90 end91 def fitness92 total = @population.inject(0) {|sum, individual| sum + individual.score }93 size = @population.size94 @population.each do |individual|95 individual.fitness = individual.score.to_f / total * size96 end97 end98end99class GeneticAlgorithm100 def initialize(generations, population, capacity, mutation_rate)101 @generations = generations102 @population = population103 @capacity = capacity104 @mutation_rate = mutation_rate105 @crossover = Crossover.new(Knapsack::ITEMS.size, mutation_rate)106 end107 def run108 best_ever = nil109 @generations.times do |generation|110 Evaluator.new(@capacity, @population).evaluate111 best = @population.best112 best_ever = best if best > best_ever113 display(generation, best_ever)114 next_generation115 end116 display_best_ever(best_ever)117 end118 private119 def display(generation, individual)120 puts "Gen: #{generation} Best score: #{individual.score}"121 end122 def display_best_ever(individual)123 puts "----------------------"124 puts "Best ever"125 puts "----------------------"126 puts "score: #{individual.score}"127 puts "chromosome: #{individual.chromosome_as_list}"128 end129 def next_generation130 @population.sort_by! {|i| i.score}131 elite = @population.pop(4)132 pool = MatingPool.new(@population)133 population_size = @population.size134 @population.clear135 population_size.times do136 @population << @crossover.two_point(pool.random, pool.random)137 end138 @population.concat elite139 end140end141class MatingPool142 def initialize(population)143 @mating_pool = []144 population.each do |individual|145 integer_part = individual.fitness.to_i146 real_part = individual.fitness - integer_part147 integer_part.times { @mating_pool << individual.dup }148 @mating_pool << individual.dup if rand < real_part149 end150 @size = @mating_pool.size151 end152 def random153 @mating_pool[rand(@size)]154 end155end156class Crossover157 def initialize(chromosome_size, mutation_rate)158 @size = chromosome_size159 @rate = mutation_rate160 @mutator = Mutator.new(@size, @rate)161 end162 def two_point(parent1, parent2)163 child = assemble(parent1, parent2, two_cut_points)164 child = @mutator.mutate(child)165 Individual.from_chromosome(child)166 end167 private168 def two_cut_points169 point1 = cut_point170 point2 = cut_point171 point1, point2 = point2, point1 if point1 > point2172 [point1, point2]173 end174 def cut_point175 rand(@size)176 end177 def assemble(parent1, parent2, points)178 point1, point2 = points179 parent1.chromosome[0...point1] + parent2.chromosome[point1..point2] +180 parent1.chromosome[point2+1..-1]181 end182end183class Mutator184 def initialize(chromosome_size, mutation_rate)185 @size = chromosome_size186 @rate = mutation_rate187 end188 def mutate(chromosome)189 @size.times do |index|190 if rand < @rate191 chromosome[index] = ! chromosome[index]192 end193 end194 chromosome195 end196end197knapsack_capacity = 400198generations = 100...
knapsack2.rb
Source:knapsack2.rb
...33 new(chromosome)34 end35 attr_accessor :score, :fitness36 attr_reader :chromosome37 def initialize(chromosome = nil, chromosome_size = nil)38 if chromosome39 @chromosome = chromosome40 else41 @chromosome = []42 chromosome_size.times { @chromosome << (rand(0..1) == 1) }43 end44 end45 private_class_method :new46 def chromosome_as_list47 list = []48 @chromosome.each_with_index do |gene, index|49 list << Knapsack::ITEMS[index].name if gene50 end51 list.join(', ')52 end53 def >(other)54 return true if other.nil?55 score > other.score56 end57end58class Population < Array59 def initialize(chromosome_size, population_size)60 population_size.times { self << Individual.random(chromosome_size) }61 end62 def best63 self.sort_by{|individual| individual.score}.last64 end65end66class Evaluator67 def initialize(capacity, population)68 @capacity = capacity69 @population = population70 end71 def evaluate72 @population.each {|individual| score(individual) }73 fitness74 end75 private76 def score(individual)77 value = 078 weight = 079 individual.chromosome.each_with_index do |item, index|80 if item81 value += Knapsack::ITEMS[index].value82 weight += Knapsack::ITEMS[index].weight83 end84 end85 if weight > @capacity86 individual.score = 087 else88 individual.score = value89 end90 end91 def fitness92 total = @population.inject(0) {|sum, individual| sum + individual.score }93 size = @population.size94 @population.each do |individual|95 individual.fitness = individual.score.to_f / total * size96 end97 end98end99class GeneticAlgorithm100 def initialize(generations, population, capacity, mutation_rate)101 @generations = generations102 @population = population103 @capacity = capacity104 @mutation_rate = mutation_rate105 @crossover = Crossover.new(Knapsack::ITEMS.size, mutation_rate)106 end107 def run108 best_ever = nil109 @generations.times do |generation|110 Evaluator.new(@capacity, @population).evaluate111 best = @population.best112 best_ever = best if best > best_ever113 display(generation, best)114 next_generation115 end116 display_best_ever(best_ever)117 end118 private119 def display(generation, individual)120 puts "Gen: #{generation} Best score: #{individual.score}"121 end122 def display_best_ever(individual)123 puts "----------------------"124 puts "Best ever"125 puts "----------------------"126 puts "score: #{individual.score}"127 puts "chromosome: #{individual.chromosome_as_list}"128 end129 def next_generation130 @population.sort_by! {|i| i.score}131 elite = @population.pop(4)132 pool = MatingPool.new(@population)133 population_size = @population.size134 @population.clear135 population_size.times do136 @population << @crossover.two_point(pool.random, pool.random)137 end138 @population.concat elite139 end140end141class MatingPool142 def initialize(population)143 @mating_pool = []144 population.each do |individual|145 integer_part = individual.fitness.to_i146 real_part = individual.fitness - integer_part147 integer_part.times { @mating_pool << individual.dup }148 @mating_pool << individual.dup if rand < real_part149 end150 @size = @mating_pool.size151 end152 def random153 @mating_pool[rand(@size)]154 end155end156class Crossover157 def initialize(chromosome_size, mutation_rate)158 @size = chromosome_size159 @rate = mutation_rate160 @mutator = Mutator.new(@size, @rate)161 end162 def two_point(parent1, parent2)163 child = assemble(parent1, parent2, two_cut_points)164 child = @mutator.mutate(child)165 Individual.from_chromosome(child)166 end167 private168 def two_cut_points169 point1 = cut_point170 point2 = cut_point171 point1, point2 = point2, point1 if point1 > point2172 [point1, point2]173 end174 def cut_point175 rand(@size)176 end177 def assemble(parent1, parent2, points)178 point1, point2 = points179 parent1.chromosome[0...point1] + parent2.chromosome[point1..point2] +180 parent1.chromosome[point2+1..-1]181 end182end183class Mutator184 def initialize(chromosome_size, mutation_rate)185 @size = chromosome_size186 @rate = mutation_rate187 end188 def mutate(chromosome)189 @size.times do |index|190 chromosome[index] = ! chromosome[index] if rand < @rate191 end192 chromosome193 end194end195knapsack_capacity = 400196generations = 100197population = Population.new(Knapsack::ITEMS.size, 1000)198mutation = 0.01...
knapsack.rb
Source:knapsack.rb
...33 new(chromosome)34 end35 attr_accessor :score, :fitness36 attr_reader :chromosome37 def initialize(chromosome = nil, chromosome_size = nil)38 if chromosome39 @chromosome = chromosome40 else41 @chromosome = []42 chromosome_size.times { @chromosome << (rand(0..1) == 1) }43 end44 end45 private_class_method :new46 def chromosome_as_list47 list = []48 @chromosome.each_with_index do |gene, index|49 list << Knapsack::ITEMS[index].name if gene50 end51 list.join(', ')52 end53 def >(other)54 return true if other.nil?55 score > other.score56 end57end58class Population < Array59 def initialize(chromosome_size, population_size)60 population_size.times { self << Individual.random(chromosome_size) }61 end62 def best63 self.sort_by{|individual| individual.score}.last64 end65end66class Evaluator67 def initialize(capacity, population)68 @capacity = capacity69 @population = population70 end71 def evaluate72 @population.each {|individual| score(individual) }73 fitness74 end75 private76 def score(individual)77 value = 078 weight = 079 individual.chromosome.each_with_index do |item, index|80 if item81 value += Knapsack::ITEMS[index].value82 weight += Knapsack::ITEMS[index].weight83 end84 end85 if weight > @capacity86 individual.score = 087 else88 individual.score = value89 end90 end91 def fitness92 total = @population.inject(0) {|sum, individual| sum + individual.score }93 size = @population.size94 @population.each do |individual|95 individual.fitness = individual.score.to_f / total * size96 end97 end98end99class GeneticAlgorithm100 def initialize(generations, population, capacity, mutation_rate)101 @generations = generations102 @population = population103 @capacity = capacity104 @mutation_rate = mutation_rate105 @crossover = Crossover.new(Knapsack::ITEMS.size, mutation_rate)106 end107 def run108 best_ever = nil109 @generations.times do |generation|110 Evaluator.new(@capacity, @population).evaluate111 best = @population.best112 best_ever = best if best > best_ever113 display(generation, best)114 next_generation115 end116 display_best_ever(best_ever)117 end118 private119 def display(generation, individual)120 puts "Gen: #{generation} Best score: #{individual.score}"121 end122 def display_best_ever(individual)123 puts "----------------------"124 puts "Best ever"125 puts "----------------------"126 puts "score: #{individual.score}"127 puts "chromosome: #{individual.chromosome_as_list}"128 end129 def next_generation130 pool = MatingPool.new(@population)131 population_size = @population.size132 @population.clear133 population_size.times do134 @population << @crossover.two_point(pool.random, pool.random)135 end136 end137end138class MatingPool139 def initialize(population)140 @mating_pool = []141 population.each do |individual|142 integer_part = individual.fitness.to_i143 real_part = individual.fitness - integer_part144 integer_part.times { @mating_pool << individual.dup }145 @mating_pool << individual.dup if rand < real_part146 end147 @size = @mating_pool.size148 end149 def random150 @mating_pool[rand(@size)]151 end152end153class Crossover154 def initialize(chromosome_size, mutation_rate)155 @size = chromosome_size156 @rate = mutation_rate157 @mutator = Mutator.new(@size, @rate)158 end159 def two_point(parent1, parent2)160 child = assemble(parent1, parent2, two_cut_points)161 child = @mutator.mutate(child)162 Individual.from_chromosome(child)163 end164 private165 def two_cut_points166 point1 = cut_point167 point2 = cut_point168 point1, point2 = point2, point1 if point1 > point2169 [point1, point2]170 end171 def cut_point172 rand(@size)173 end174 def assemble(parent1, parent2, points)175 point1, point2 = points176 parent1.chromosome[0...point1] + parent2.chromosome[point1..point2] +177 parent1.chromosome[point2+1..-1]178 end179end180class Mutator181 def initialize(chromosome_size, mutation_rate)182 @size = chromosome_size183 @rate = mutation_rate184 end185 def mutate(chromosome)186 @size.times do |index|187 chromosome[index] = ! chromosome[index] if rand < @rate188 end189 chromosome190 end191end192knapsack_capacity = 400193generations = 100194population = Population.new(Knapsack::ITEMS.size, 1000)195mutation = 0.01...
initialize
Using AI Code Generation
1knapsack = Knapsack.new(5, [1,2,3,4,5], [5,4,3,2,1])2knapsack = Knapsack.new(5, [1,2,3,4,5], [5,4,3,2,1])3knapsack = Knapsack.new(5, [1,2,3,4,5], [5,4,3,2,1])4knapsack = Knapsack.new(5, [1,2,3,4,5], [5,4,3,2,1])5knapsack = Knapsack.new(5, [1,2,3,4,5], [5,4,3,2,1])6knapsack = Knapsack.new(5, [1,2,3,4,5], [5,4,3,2,1])7knapsack = Knapsack.new(5, [1,2,3,4,5], [5,4,3,2,1])8knapsack = Knapsack.new(5, [1,2,3,4,5], [5,4,3,2,1])9knapsack = Knapsack.new(5, [1,2,3,
initialize
Using AI Code Generation
1k = Knapsack.new(50, 6)2k.load_items("items.txt")3k = Knapsack.new(50, 6)4k.load_items("items.txt")5k = Knapsack.new(50, 6)6k.load_items("items.txt")7k = Knapsack.new(50, 6)8k.load_items("items.txt")9k = Knapsack.new(50, 6)10k.load_items("items.txt")11k = Knapsack.new(50, 6)12k.load_items("items.txt")13k = Knapsack.new(50, 6)14k.load_items("items.txt")15k = Knapsack.new(50, 6)16k.load_items("items.txt")17k = Knapsack.new(50, 6)18k.load_items("items.txt")19k = Knapsack.new(50, 6)20k.load_items("items.txt")21k = Knapsack.new(50, 6)22k.load_items("items.txt")
initialize
Using AI Code Generation
1knapsack = Knapsack.new(100, 100, 100)2knapsack = Knapsack.new(100, 100, 100)3knapsack = Knapsack.new(100, 100, 100)4knapsack = Knapsack.new(100, 100, 100)5knapsack = Knapsack.new(100, 100, 100)
initialize
Using AI Code Generation
1 def initialize(capacity)2 def initialize(name)3 def initialize(name)4 def initialize(name)5 def initialize(name)
initialize
Using AI Code Generation
1k = Knapsack.new(50, 6)2k.load_items("items.txt")3k = Knapsack.new(50, 6)4k.load_items("items.txt")5k = Knapsack.new(50, 6)6k.load_items("items.txt")7k = Knapsack.new(50, 6)8k.load_items("items.txt")9k = Knapsack.new(50, 6)10k.load_items("items.txt")11k = Knapsack.new(50, 6)12k.load_items("items.txt")13k = Knapsack.new(50, 6)14k.load_items("items.txt")15k = Knapsack.new(50, 6)16k.load_items("items.txt")17k = Knapsack.new(50, 6)18k.load_items("items.txt")19k = Knapsack.new(50, 6)20k.load_items("items.txt")21k = Knapsack.new(50, 6)22k.load_items("items.txt")
initialize
Using AI Code Generation
1knapsack = Knapsack.new(100, 100, 100)2knapsack = Knapsack.new(100, 100, 100)3knapsack = Knapsack.new(100, 100, 100)4knapsack = Knapsack.new(100, 100, 100)5knapsack = Knapsack.new(100, 100, 100)
initialize
Using AI Code Generation
1 def initialize(capacity)2 def initialize(name)3 def initialize(name)4 def initialize(name)5 def initialize(name)
initialize
Using AI Code Generation
1k = Knapsack.new(50, 6)2k.load_items("items.txt")3k = Knapsack.new(50, 6)4k.load_items("items.txt")5k = Knapsack.new(50, 6)6k.load_items("items.txt")7k = Knapsack.new(50, 6)8k.load_items("items.txt")9k = Knapsack.new(50, 6)10k.load_items("items.txt")11k = Knapsack.new(50, 6)12k.load_items("items.txt")13k = Knapsack.new(50, 6)14k.load_items("items.txt")15k = Knapsack.new(50, 6)16k.load_items("items.txt")17k = Knapsack.new(50, 6)18k.load_items("items.txt")19k = Knapsack.new(50, 6)20k.load_items("items.txt")21k = Knapsack.new(50, 6)22k.load_items("items.txt")
initialize
Using AI Code Generation
1knapsack = Knapsack.new(100, 100, 100)2knapsack = Knapsack.new(100, 100, 100)3knapsack = Knapsack.new(100, 100, 100)4knapsack = Knapsack.new(100, 100, 100)5knapsack = Knapsack.new(100, 100, 100)
initialize
Using AI Code Generation
1 def initialize(capacity)2 def initialize(name)3 def initialize(name)4 def initialize(name)5 def initialize(name)
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!