From da51a073cc3bed67696b729a174a15b8c60b7bcf Mon Sep 17 00:00:00 2001 From: bustikiller Date: Fri, 18 Jul 2025 16:05:02 +0200 Subject: [PATCH 1/2] Define a benchmark to measure the VNS performance and prevent redundant hierarchy calculations --- app/services/tables/distribution.rb | 11 ++++++----- lib/tasks/vns.rake | 27 +++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 5 deletions(-) create mode 100644 lib/tasks/vns.rake diff --git a/app/services/tables/distribution.rb b/app/services/tables/distribution.rb index cc51308..a361a26 100644 --- a/app/services/tables/distribution.rb +++ b/app/services/tables/distribution.rb @@ -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 diff --git a/lib/tasks/vns.rake b/lib/tasks/vns.rake new file mode 100644 index 0000000..4178ee0 --- /dev/null +++ b/lib/tasks/vns.rake @@ -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 -- 2.47.1 From 03e09c74a01307bd362da15fe28c7f5bcbb0b341 Mon Sep 17 00:00:00 2001 From: bustikiller Date: Fri, 18 Jul 2025 16:09:28 +0200 Subject: [PATCH 2/2] Rubocop fixes --- app/services/tables/distribution.rb | 3 ++- lib/tasks/vns.rake | 8 +++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/app/services/tables/distribution.rb b/app/services/tables/distribution.rb index a361a26..4751cf8 100644 --- a/app/services/tables/distribution.rb +++ b/app/services/tables/distribution.rb @@ -42,7 +42,8 @@ module Tables end def deep_dup - self.class.new(min_per_table: @min_per_table, max_per_table: @max_per_table, hierarchy: @hierarchy).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 diff --git a/lib/tasks/vns.rake b/lib/tasks/vns.rake index 4178ee0..b8c7b3a 100644 --- a/lib/tasks/vns.rake +++ b/lib/tasks/vns.rake @@ -1,5 +1,7 @@ +# frozen_string_literal: true + namespace :vns do - desc "Benchmarks the efficiency of the VNS implementation" + 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" @@ -12,14 +14,14 @@ namespace :vns do hierarchy = AffinityGroupsHierarchy.new initial_solution = Tables::Distribution.new(min_per_table: 8, max_per_table: 10, hierarchy:) - random = Random.new(561163) + random = Random.new(561_163) 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} + solution = Rails.benchmark('VNS Benchmarking') { engine.run } Rails.logger.info "Best solution found with discomfort: #{solution.discomfort}" end -- 2.47.1