How to use config method of Knapsack Package

Best Knapsack_ruby code snippet using Knapsack.config

branch_and_bound.rb

Source:branch_and_bound.rb Github

copy

Full Screen

...6 #7 # @param instance [Instance] 0/1 knapsack problem instance8 def initialize(instance)9 @instance = instance10 @config = Array.new(instance.things.size)11 end12 # Solve the instance of 0/1 knapsack problem.13 #14 # @return [Hash] resulting price and thing configuration (0 = thing is not in the knapsack, 1 = thing is there)15 def run16 solve(0)17 { price: @best_price, config: @best_config }18 end19 protected20 # Solve the problem starting at specified thing.21 #22 # @param index [Integer] index of thing which will be decided (put in or out from the knapsack) the next23 def solve(index)24 @config[index] = 025 solve(index + 1) unless stop(index)26 @config[index] = 127 solve(index + 1) unless stop(index)28 end29 # Determine if solving of current branch should continue.30 #31 # @param index [Integer] index of the last decided thing so far32 # @return [true, false] weather to continue with solving current branch.33 def stop(index)34 # Update of the best price so far35 weight = config_weight(0, index)36 price = config_price(0, index)37 update_best_price(price, weight, index)38 # No more things to put into the knapsack39 return true if index >= (@instance.things.size - 1)40 # The knapsack is overloaded, do not continue this branch41 return true if weight > @instance.weight_capacity42 if instance_variable_defined?('@best_price') &&43 ((price + get_price_of_remaining_things(index + 1)) <= @best_price)44 # Adding all the ramining things does not produce better price45 return true46 end47 false48 end49 # Update the best price achieved so far.50 #51 # @param price [Integer] price of the current configuration52 # @param weight [Integer] weight of the current configuration53 # @param index [Integer] index of the next thing presence of which will be decided54 def update_best_price(price, weight, index)55 if !instance_variable_defined?('@best_price') ||56 ((weight <= @instance.weight_capacity) && (price > @best_price))57 @best_price = price58 valid_len = index + 159 remaining = @config.size - index - 160 # All undecided things will not be put into the knapsack61 @best_config = @config.slice(0, valid_len).fill(0, valid_len, remaining)62 @best_config_index = index63 end64 end65 # Gets weight of set of things. The set is subset of the things ordered by66 # their index.67 #68 # @param start_index [Integer] index of the first thing included in the set69 # @param end_index [Integer] index of the last thing included in the set70 # @return [Integer] weight of the things71 def config_weight(start_index, end_index)72 weight = 073 @config[start_index..end_index].each_with_index do |presence, index|74 weight += presence * @instance.things[index].weight75 end76 weight77 end78 # Gets price of set of things. The set is subset of the things ordered by79 # their index.80 #81 # @param start_index [Integer] index of the first thing included in the set82 # @param end_index [Integer] index of the last thing included in the set83 # @return [Integer] price of the things84 def config_price(start_index, end_index)85 price = 086 @config[start_index..end_index].each_with_index do |presence, index|87 price += presence * @instance.things[index].price88 end89 price90 end91 # Gets sum of prices of things for which their presence in the knapsack92 # was not decided yet.93 #94 # @param from_index [Integer] index of the first undecided thing95 # @return [Integer] price of the remaining things96 def get_price_of_remaining_things(from_index)97 price = 098 to_index = @instance.things.size - 199 @instance.things[from_index..to_index].each { |t| price += t.price }100 price...

Full Screen

Full Screen

heuristic_price_weight.rb

Source:heuristic_price_weight.rb Github

copy

Full Screen

...8 #9 # @param instance [Instance] 0/1 knapsack problem instance10 def initialize(instance)11 @instance = instance12 @config = Array.new(instance.things.size) { 0 }13 @sorted_things = instance.things.sort do |a, b|14 (b.price.to_f / b.weight) <=> (a.price.to_f / a.weight)15 end16 end17 # Solve the instance of 0/1 knapsack problem.18 #19 # @return [Hash] resulting price and thing configuration (0 = thing is not in the knapsack, 1 = thing is there)20 def run21 solve22 { price: @best_price, config: @best_config }23 end24 protected25 # Solve the instance of 0/1 knapsack problem.26 def solve27 @sorted_things.each do |thing|28 break if (config_weight + thing.weight) > @instance.weight_capacity29 @config[thing.index] = 130 end31 @best_price = config_price32 @best_config = @config.dup33 end34 # Gets total weight of things present in the knapsack.35 #36 # @return [Integer] total weight37 def config_weight38 @config.each_with_index.reduce(0) do |weight, (presence, index)|39 weight + presence * @instance.things[index].weight40 end41 end42 # Gets total price of things present in the knapsack.43 #44 # @return [Integer] total price45 def config_price46 @config.each_with_index.reduce(0) do |price, (presence, index)|47 price + presence * @instance.things[index].price48 end49 end50 end51end...

Full Screen

Full Screen

timing_adapter.rb

Source:timing_adapter.rb Github

copy

Full Screen

1require 'knapsack'2# Generates more accurate reports for knapsack by timing :all blocks3class TimingAdapter < Knapsack::Adapters::RspecAdapter4 def bind_time_tracker5 ::RSpec.configure do |config|6 config.before(:all) do7 Knapsack.tracker.test_path = self.class.metadata[:example_group][:file_path]8 Knapsack.tracker.start_timer9 end10 config.after(:all) do11 Knapsack.tracker.stop_timer12 end13 config.after(:suite) do14 Knapsack.logger.info(Knapsack::Presenter.global_time)15 end16 end17 end18end19TimingAdapter.bind...

Full Screen

Full Screen

config

Using AI Code Generation

copy

Full Screen

1 { weight: 1, value: 2 },2 { weight: 3, value: 3 },3 { weight: 2, value: 4 },4 { weight: 4, value: 5 }

Full Screen

Full Screen

config

Using AI Code Generation

copy

Full Screen

1knapsack.config(100, 10)2 def config(capacity, weight)3knapsack.config(100, 10)

Full Screen

Full Screen

config

Using AI Code Generation

copy

Full Screen

1 { weight: 1, value: 2 },2 { weight: 3, value: 3 },3 { weight: 2, value: 4 },4 { weight: 4, value: 5 }

Full Screen

Full Screen

config

Using AI Code Generation

copy

Full Screen

1knapsack.config(100, 10)2 def config(capacity, weight)3knapsack.config(100, 10)

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful