Compare commits

..

1 Commits

Author SHA1 Message Date
5dce77c29d Merge pull request 'Define a benchmark to measure the VNS performance and prevent redundant hierarchy calculations' (#298) from benchmark into main
All checks were successful
Run unit tests / rubocop (push) Has been skipped
Run unit tests / copyright_notice (push) Has been skipped
Run unit tests / check-licenses (push) Has been skipped
Run unit tests / unit_tests (push) Successful in 54s
Run unit tests / build-static-assets (push) Successful in 9m27s
Reviewed-on: #298
2025-07-18 14:26:10 +00:00
6 changed files with 26 additions and 149 deletions

View File

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

View File

@ -73,8 +73,6 @@ module Tables
def local_discomfort(table)
table.discomfort ||= DiscomfortCalculator.new(table:, hierarchy:).calculate
table.freeze
table.discomfort
end
end
end

View File

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

View File

@ -12,45 +12,25 @@ module Tables
def each
@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)|
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
original_discomfort_a = table_a.reset
original_discomfort_b = table_b.reset
new_solution.tables.delete(table_a)
new_solution.tables.delete(table_b)
table_a.delete(person_a)
table_b.delete(person_b)
table_a << person_b
table_b << person_a
modified_table_a = table_a.dup.tap(&:reset)
modified_table_b = table_b.dup.tap(&:reset)
yield(@initial_solution)
ensure
table_a.delete(person_b)
table_b.delete(person_a)
new_solution.tables << modified_table_a
new_solution.tables << modified_table_b
table_a << person_a
table_b << person_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
table_a.discomfort = original_discomfort_a
table_b.discomfort = original_discomfort_b
end
end
end

View File

@ -20,16 +20,7 @@ module VNS
@perturbations << klass
end
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
attr_writer :initial_solution
def run
raise 'No target function defined' unless @target_function
@ -39,10 +30,6 @@ module VNS
@best_solution = @initial_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|
optimize(perturbation)
end
@ -57,27 +44,9 @@ module VNS
optimized = false
perturbation_klass.new(@best_solution).each do |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}")
score = @target_function.call(alternative_solution)
next if score >= @best_score
# Rails.logger.info("Found better solution with score: #{score} (previous: #{@best_score})")
@best_solution = alternative_solution.deep_dup
@best_score = score
optimized = true

View File

@ -1,51 +0,0 @@
namespace :ractors do
desc 'Run the Ractors example'
task run: :environment do
list = (1..20)
number_of_ractors = Concurrent.processor_count
ractors = number_of_ractors.times.map do |i|
Ractor.new(name: "Ractor #{i}") do
loop do
number = Ractor.receive
puts "Processing #{number} in #{Ractor.current.name}"
Ractor.yield "eureka" if number == 17
end
end
end.cycle
list.each do |i|
ractors.next.send(i)
end
puts "all enqueued"
binding.pry
Ractor.select(*ractors)
# binding.pry
# number_of_ractors.times do |i|
# r = Ractor.new(name: "Ractor #{i}") do
# Ractor.receive.each do |i|
# puts "Processing index #{i} in #{Ractor.current.name}"
# end
# end
# r.send((i * 250)...((i + 1) * 250))
# end
# ractor = Ractor.new do
# loop do
# puts 'I will receive a message soon'
# msg = Ractor.receive
# puts 'I will return a tripple of what I receive'
# Ractor.yield(msg * 3)
# end
# end
# heavy_calculation.send(15)
# puts heavy_calculation.take
end
end