Add a perturbation step to the VNS engine #305
@ -12,8 +12,8 @@ class TableSimulatorJob < ApplicationJob
|
|||||||
ActsAsTenant.with_tenant(Wedding.find(wedding_id)) do
|
ActsAsTenant.with_tenant(Wedding.find(wedding_id)) do
|
||||||
engine = VNS::Engine.new
|
engine = VNS::Engine.new
|
||||||
|
|
||||||
engine.add_perturbation(Tables::Swap)
|
engine.add_optimization(Tables::Swap)
|
||||||
engine.add_perturbation(Tables::Shift)
|
engine.add_optimization(Tables::Shift)
|
||||||
|
|
||||||
initial_solution = Tables::Distribution.new(min_per_table: MIN_PER_TABLE, max_per_table: MAX_PER_TABLE)
|
initial_solution = Tables::Distribution.new(min_per_table: MIN_PER_TABLE, max_per_table: MAX_PER_TABLE)
|
||||||
initial_solution.random_distribution(Guest.potential.shuffle)
|
initial_solution.random_distribution(Guest.potential.shuffle)
|
||||||
|
33
app/services/tables/wheel_swap.rb
Normal file
33
app/services/tables/wheel_swap.rb
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
# Copyright (C) 2024-2025 LibreWeddingPlanner contributors
|
||||||
|
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
module Tables
|
||||||
|
class WheelSwap
|
||||||
|
private attr_reader :initial_solution
|
||||||
|
def initialize(initial_solution)
|
||||||
|
@initial_solution = initial_solution
|
||||||
|
end
|
||||||
|
|
||||||
|
def call(size = 1)
|
||||||
|
Rails.logger.debug { "WheelSwap with size: #{size}" }
|
||||||
|
new_solution = @initial_solution.deep_dup
|
||||||
|
|
||||||
|
selected_guests = []
|
||||||
|
|
||||||
|
size.times do
|
||||||
|
selected_guests += new_solution.tables.map(&:pop)
|
||||||
|
end
|
||||||
|
|
||||||
|
selected_guests.shuffle!
|
||||||
|
|
||||||
|
tables = new_solution.tables.cycle
|
||||||
|
|
||||||
|
tables.next << selected_guests.pop while selected_guests.any?
|
||||||
|
|
||||||
|
new_solution.tables.each(&:reset)
|
||||||
|
|
||||||
|
new_solution
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
module VNS
|
module VNS
|
||||||
class Engine
|
class Engine
|
||||||
|
PERTURBATION_SIZES = [1, 1, 1, 2, 2, 3].freeze
|
||||||
class << self
|
class << self
|
||||||
def sequence(elements)
|
def sequence(elements)
|
||||||
elements = elements.to_a
|
elements = elements.to_a
|
||||||
@ -15,6 +16,11 @@ module VNS
|
|||||||
@target_function = function
|
@target_function = function
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def add_optimization(klass)
|
||||||
|
@optimizations ||= Set.new
|
||||||
|
@optimizations << klass
|
||||||
|
end
|
||||||
|
|
||||||
def add_perturbation(klass)
|
def add_perturbation(klass)
|
||||||
@perturbations ||= Set.new
|
@perturbations ||= Set.new
|
||||||
@perturbations << klass
|
@perturbations << klass
|
||||||
@ -24,32 +30,58 @@ module VNS
|
|||||||
|
|
||||||
def run
|
def run
|
||||||
raise 'No target function defined' unless @target_function
|
raise 'No target function defined' unless @target_function
|
||||||
raise 'No perturbations defined' unless @perturbations
|
raise 'No optimizations defined' unless @optimizations
|
||||||
raise 'No initial solution defined' unless @initial_solution
|
raise 'No initial solution defined' unless @initial_solution
|
||||||
|
|
||||||
@best_solution = @initial_solution
|
@perturbations ||= Set.new
|
||||||
@best_score = @target_function.call(@best_solution)
|
|
||||||
|
|
||||||
self.class.sequence(@perturbations).each do |perturbation|
|
@current_solution = @initial_solution
|
||||||
optimize(perturbation)
|
@best_score = @target_function.call(@current_solution)
|
||||||
|
|
||||||
|
run_all_optimizations
|
||||||
|
|
||||||
|
best_solution = @current_solution
|
||||||
|
|
||||||
|
50.times do
|
||||||
|
@current_solution = Tables::WheelSwap.new(best_solution).call(PERTURBATION_SIZES.sample)
|
||||||
|
@best_score = @target_function.call(@current_solution)
|
||||||
|
Rails.logger.debug { "After perturbation: #{@best_score}" }
|
||||||
|
|
||||||
|
run_all_optimizations
|
||||||
|
|
||||||
|
next unless best_solution.discomfort > @current_solution.discomfort
|
||||||
|
|
||||||
|
best_solution = @current_solution
|
||||||
|
Rails.logger.debug do
|
||||||
|
"Found better solution after perturbation optimization: #{@current_solution.discomfort}"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@best_solution
|
best_solution
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def optimize(perturbation_klass)
|
def run_all_optimizations
|
||||||
|
self.class.sequence(@optimizations).each do |optimization|
|
||||||
|
optimize(optimization)
|
||||||
|
Rails.logger.debug { "Finished optimization phase: #{optimization}" }
|
||||||
|
end
|
||||||
|
Rails.logger.debug { 'Finished all optimization phases' }
|
||||||
|
end
|
||||||
|
|
||||||
|
def optimize(optimization_klass)
|
||||||
loop do
|
loop do
|
||||||
optimized = false
|
optimized = false
|
||||||
|
|
||||||
perturbation_klass.new(@best_solution).each do |alternative_solution|
|
optimization_klass.new(@current_solution).each do |alternative_solution|
|
||||||
score = @target_function.call(alternative_solution)
|
score = @target_function.call(alternative_solution)
|
||||||
next if score >= @best_score
|
next if score >= @best_score
|
||||||
|
|
||||||
@best_solution = alternative_solution.deep_dup
|
@current_solution = alternative_solution.deep_dup
|
||||||
@best_score = score
|
@best_score = score
|
||||||
optimized = true
|
optimized = true
|
||||||
|
Rails.logger.debug { "[#{optimization_klass}] Found better solution with score: #{score}" }
|
||||||
|
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
|
@ -10,4 +10,10 @@ class Set
|
|||||||
def to_table
|
def to_table
|
||||||
Tables::Table.new(self)
|
Tables::Table.new(self)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def pop
|
||||||
|
element = self.to_a.sample
|
||||||
|
self.delete(element)
|
||||||
|
element
|
||||||
|
end
|
||||||
end
|
end
|
@ -8,8 +8,8 @@ namespace :vns do
|
|||||||
|
|
||||||
engine = VNS::Engine.new
|
engine = VNS::Engine.new
|
||||||
|
|
||||||
engine.add_perturbation(Tables::Swap)
|
engine.add_optimization(Tables::Swap)
|
||||||
engine.add_perturbation(Tables::Shift)
|
engine.add_optimization(Tables::Shift)
|
||||||
|
|
||||||
hierarchy = AffinityGroupsHierarchy.new
|
hierarchy = AffinityGroupsHierarchy.new
|
||||||
initial_solution = Tables::Distribution.new(min_per_table: 8, max_per_table: 10, hierarchy:)
|
initial_solution = Tables::Distribution.new(min_per_table: 8, max_per_table: 10, hierarchy:)
|
||||||
|
28
spec/services/tables/wheel_swap_spec.rb
Normal file
28
spec/services/tables/wheel_swap_spec.rb
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
# Copyright (C) 2024-2025 LibreWeddingPlanner contributors
|
||||||
|
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
module Tables
|
||||||
|
RSpec.describe WheelSwap do
|
||||||
|
context 'when the solution has three tables' do
|
||||||
|
let(:initial_solution) do
|
||||||
|
Distribution.new(min_per_table: 3, max_per_table: 3).tap do |distribution|
|
||||||
|
distribution.tables << Set[:a, :b, :c].to_table
|
||||||
|
distribution.tables << Set[:d, :e, :f].to_table
|
||||||
|
distribution.tables << Set[:g, :h, :i].to_table
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'swaps a random guest from each table with a guest from another table', :aggregate_failures do
|
||||||
|
result = described_class.new(initial_solution).call
|
||||||
|
|
||||||
|
expect(result.tables.size).to eq(3)
|
||||||
|
expect(result.tables.map(&:size)).to all(eq(3))
|
||||||
|
|
||||||
|
expect(result.tables.map(&:to_a).flatten).to match_array((:a..:i).to_a)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
x
Reference in New Issue
Block a user