Define a benchmark to measure the VNS performance and prevent redundant hierarchy calculations
Some checks failed
Run unit tests / copyright_notice (pull_request) Successful in 38s
Run unit tests / rubocop (pull_request) Failing after 58s
Run unit tests / check-licenses (pull_request) Successful in 1m0s
Run unit tests / unit_tests (pull_request) Successful in 1m14s
Run unit tests / build-static-assets (pull_request) Successful in 14m33s
Some checks failed
Run unit tests / copyright_notice (pull_request) Successful in 38s
Run unit tests / rubocop (pull_request) Failing after 58s
Run unit tests / check-licenses (pull_request) Successful in 1m0s
Run unit tests / unit_tests (pull_request) Successful in 1m14s
Run unit tests / build-static-assets (pull_request) Successful in 14m33s
This commit is contained in:
parent
1953ba9d7f
commit
da51a073cc
@ -14,17 +14,18 @@ module Tables
|
||||
|
||||
attr_accessor :tables, :min_per_table, :max_per_table, :hierarchy
|
||||
|
||||
def initialize(min_per_table:, max_per_table:)
|
||||
def initialize(min_per_table:, max_per_table:, hierarchy: AffinityGroupsHierarchy.new)
|
||||
@min_per_table = min_per_table
|
||||
@max_per_table = max_per_table
|
||||
@hierarchy = AffinityGroupsHierarchy.new
|
||||
@hierarchy = hierarchy
|
||||
@tables = []
|
||||
end
|
||||
|
||||
def random_distribution(people)
|
||||
def random_distribution(people, random: Random.new)
|
||||
min_tables = (people.count * 1.0 / @max_per_table).ceil
|
||||
max_tables = (people.count * 1.0 / @min_per_table).ceil
|
||||
@tables = people.in_groups(rand(min_tables..max_tables), false)
|
||||
table_size = random.rand(min_tables..max_tables)
|
||||
@tables = people.in_groups(table_size, false)
|
||||
.map { |group| Table.new(group) }
|
||||
.each { |table| table.min_per_table = @min_per_table }
|
||||
.each { |table| table.max_per_table = @max_per_table }
|
||||
@ -41,7 +42,7 @@ module Tables
|
||||
end
|
||||
|
||||
def deep_dup
|
||||
self.class.new(min_per_table: @min_per_table, max_per_table: @max_per_table).tap do |new_distribution|
|
||||
self.class.new(min_per_table: @min_per_table, max_per_table: @max_per_table, hierarchy: @hierarchy).tap do |new_distribution|
|
||||
new_distribution.tables = @tables.map(&:dup)
|
||||
end
|
||||
end
|
||||
|
27
lib/tasks/vns.rake
Normal file
27
lib/tasks/vns.rake
Normal file
@ -0,0 +1,27 @@
|
||||
namespace :vns do
|
||||
desc "Benchmarks the efficiency of the VNS implementation"
|
||||
task benchmark: :environment do
|
||||
ActsAsTenant.with_tenant(Wedding.first) do
|
||||
Rails.logger.info "There are #{Guest.potential.count} potential guests"
|
||||
|
||||
engine = VNS::Engine.new
|
||||
|
||||
engine.add_perturbation(Tables::Swap)
|
||||
engine.add_perturbation(Tables::Shift)
|
||||
|
||||
hierarchy = AffinityGroupsHierarchy.new
|
||||
initial_solution = Tables::Distribution.new(min_per_table: 8, max_per_table: 10, hierarchy:)
|
||||
|
||||
random = Random.new(561163)
|
||||
initial_solution.random_distribution(Guest.potential.shuffle(random:), random:)
|
||||
|
||||
engine.initial_solution = initial_solution
|
||||
|
||||
engine.target_function(&:discomfort)
|
||||
|
||||
solution = Rails.benchmark("VNS Benchmarking") {engine.run}
|
||||
|
||||
Rails.logger.info "Best solution found with discomfort: #{solution.discomfort}"
|
||||
end
|
||||
end
|
||||
end
|
Loading…
x
Reference in New Issue
Block a user