Compare commits

..

4 Commits

Author SHA1 Message Date
25bd53600e Use frozen versions of tables
Some checks failed
Run unit tests / rubocop (pull_request) Failing after 34s
Run unit tests / copyright_notice (pull_request) Successful in 56s
Run unit tests / check-licenses (pull_request) Successful in 1m12s
Run unit tests / unit_tests (pull_request) Failing after 1m27s
Run unit tests / build-static-assets (pull_request) Has been skipped
2025-07-18 18:12:53 +02:00
bf15184360 Merge branch 'benchmark' into ractors 2025-07-18 16:10:25 +02:00
03e09c74a0 Rubocop fixes
All checks were successful
Run unit tests / copyright_notice (pull_request) Successful in 54s
Run unit tests / check-licenses (pull_request) Successful in 1m14s
Run unit tests / rubocop (pull_request) Successful in 1m24s
Run unit tests / unit_tests (pull_request) Successful in 4m10s
Run unit tests / build-static-assets (pull_request) Successful in 12m27s
2025-07-18 16:09:28 +02:00
da51a073cc 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
2025-07-18 16:05:02 +02:00
6 changed files with 134 additions and 31 deletions

View File

@ -11,6 +11,7 @@ module Tables
end end
def calculate def calculate
# Rails.logger.info "Calculating discomfort of table with id #{table.object_id}"
breakdown.values.sum breakdown.values.sum
end end

View File

@ -14,17 +14,18 @@ module Tables
attr_accessor :tables, :min_per_table, :max_per_table, :hierarchy 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 @min_per_table = min_per_table
@max_per_table = max_per_table @max_per_table = max_per_table
@hierarchy = AffinityGroupsHierarchy.new @hierarchy = hierarchy
@tables = [] @tables = []
end end
def random_distribution(people) def random_distribution(people, random: Random.new)
min_tables = (people.count * 1.0 / @max_per_table).ceil min_tables = (people.count * 1.0 / @max_per_table).ceil
max_tables = (people.count * 1.0 / @min_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) } .map { |group| Table.new(group) }
.each { |table| table.min_per_table = @min_per_table } .each { |table| table.min_per_table = @min_per_table }
.each { |table| table.max_per_table = @max_per_table } .each { |table| table.max_per_table = @max_per_table }
@ -41,7 +42,8 @@ module Tables
end end
def deep_dup 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) new_distribution.tables = @tables.map(&:dup)
end end
end end
@ -71,6 +73,8 @@ module Tables
def local_discomfort(table) def local_discomfort(table)
table.discomfort ||= DiscomfortCalculator.new(table:, hierarchy:).calculate table.discomfort ||= DiscomfortCalculator.new(table:, hierarchy:).calculate
table.freeze
table.discomfort
end end
end end
end end

View File

@ -12,19 +12,37 @@ module Tables
def each def each
@initial_solution.tables.permutation(2) do |table_a, table_b| @initial_solution.tables.permutation(2) do |table_a, table_b|
table_a.dup.each do |person| table_a.dup.each do |person|
original_discomfort_a = table_a.reset
original_discomfort_b = table_b.reset new_solution = Distribution.new(
min_per_table: @initial_solution.min_per_table,
max_per_table: @initial_solution.max_per_table,
hierarchy: @initial_solution.hierarchy
)
table_a.delete(person) new_solution.tables = @initial_solution.tables.dup
table_b << person
yield(@initial_solution) new_solution.tables.delete(table_a)
ensure new_solution.tables.delete(table_b)
table_b.delete(person)
table_a << person
table_a.discomfort = original_discomfort_a modified_table_a = table_a.dup.tap(&:reset)
table_b.discomfort = original_discomfort_b modified_table_b = table_b.dup.tap(&:reset)
new_solution.tables << modified_table_a
new_solution.tables << modified_table_b
# original_discomfort_a = table_a.reset
# original_discomfort_b = table_b.reset
modified_table_a.delete(person)
modified_table_b << person
yield(new_solution)
# ensure
# table_b.delete(person)
# table_a << person
# table_a.discomfort = original_discomfort_a
# table_b.discomfort = original_discomfort_b
end end
end end
end end

View File

@ -12,25 +12,45 @@ module Tables
def each def each
@initial_solution.tables.combination(2) do |table_a, table_b| @initial_solution.tables.combination(2) do |table_a, table_b|
table_a.to_a.product(table_b.to_a).each do |(person_a, person_b)| table_a.to_a.product(table_b.to_a).each do |(person_a, person_b)|
original_discomfort_a = table_a.reset
original_discomfort_b = table_b.reset new_solution = Distribution.new(
min_per_table: @initial_solution.min_per_table,
max_per_table: @initial_solution.max_per_table,
hierarchy: @initial_solution.hierarchy
)
new_solution.tables = @initial_solution.tables.dup
table_a.delete(person_a) new_solution.tables.delete(table_a)
table_b.delete(person_b) new_solution.tables.delete(table_b)
table_a << person_b
table_b << person_a
yield(@initial_solution) modified_table_a = table_a.dup.tap(&:reset)
ensure modified_table_b = table_b.dup.tap(&:reset)
table_a.delete(person_b)
table_b.delete(person_a)
table_a << person_a new_solution.tables << modified_table_a
table_b << person_b new_solution.tables << modified_table_b
table_a.discomfort = original_discomfort_a
table_b.discomfort = original_discomfort_b # original_discomfort_a = table_a.reset
# original_discomfort_b = table_b.reset
modified_table_a.delete(person_a)
modified_table_b.delete(person_b)
modified_table_a << person_b
modified_table_b << person_a
yield(new_solution)
# ensure
# table_a.delete(person_b)
# table_b.delete(person_a)
# table_a << person_a
# table_b << person_b
# table_a.discomfort = original_discomfort_a
# table_b.discomfort = original_discomfort_b
end end
end end
end end

View File

@ -20,7 +20,16 @@ module VNS
@perturbations << klass @perturbations << klass
end end
attr_writer :initial_solution def initialize_ractors(count: Concurrent.processor_count)
@ractors = count.times.map do |i|
Ractor.new(name: "VNS Ractor #{i}") do
# @target_function.call(Ractor.receive)
Ractor.receive.discomfort # Hard-coded for now, should be dynamic
end
end.cycle
end
attr_writer :initial_solution, :ractors
def run def run
raise 'No target function defined' unless @target_function raise 'No target function defined' unless @target_function
@ -30,6 +39,10 @@ module VNS
@best_solution = @initial_solution @best_solution = @initial_solution
@best_score = @target_function.call(@best_solution) @best_score = @target_function.call(@best_solution)
@best_solution.freeze
@best_solution.tables.freeze
# @best_solution.tables.each(&:freeze)
self.class.sequence(@perturbations).each do |perturbation| self.class.sequence(@perturbations).each do |perturbation|
optimize(perturbation) optimize(perturbation)
end end
@ -44,9 +57,27 @@ module VNS
optimized = false optimized = false
perturbation_klass.new(@best_solution).each do |alternative_solution| perturbation_klass.new(@best_solution).each do |alternative_solution|
score = @target_function.call(alternative_solution) # original_score =
# ractor = @ractors.next
# Rails.logger.info("Processed by ractor #{ractor.name}")
# ractor.send(alternative_solution)
# new_score = ractor.take
# binding.pry if new_score != original_score
# score = new_score
score = @target_function.call(alternative_solution)
# Rails.logger.info("Evaluating alternative solution with score: #{score}")
next if score >= @best_score next if score >= @best_score
# Rails.logger.info("Found better solution with score: #{score} (previous: #{@best_score})")
@best_solution = alternative_solution.deep_dup @best_solution = alternative_solution.deep_dup
@best_score = score @best_score = score
optimized = true optimized = true

29
lib/tasks/vns.rake Normal file
View File

@ -0,0 +1,29 @@
# frozen_string_literal: true
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(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 }
Rails.logger.info "Best solution found with discomfort: #{solution.discomfort}"
end
end
end