Compare commits
5 Commits
main
...
vns-pertur
Author | SHA1 | Date | |
---|---|---|---|
a1f06dae5b | |||
e8a88b50e2 | |||
185f359942 | |||
543b53d938 | |||
e1a5e4f73e |
@ -12,8 +12,8 @@ class TableSimulatorJob < ApplicationJob
|
||||
ActsAsTenant.with_tenant(Wedding.find(wedding_id)) do
|
||||
engine = VNS::Engine.new
|
||||
|
||||
engine.add_perturbation(Tables::Swap)
|
||||
engine.add_perturbation(Tables::Shift)
|
||||
engine.add_optimization(Tables::Swap)
|
||||
engine.add_optimization(Tables::Shift)
|
||||
|
||||
initial_solution = Tables::Distribution.new(min_per_table: MIN_PER_TABLE, max_per_table: MAX_PER_TABLE)
|
||||
initial_solution.random_distribution(Guest.potential.shuffle)
|
||||
|
21
app/services/tables/wheel_swap.rb
Normal file
21
app/services/tables/wheel_swap.rb
Normal file
@ -0,0 +1,21 @@
|
||||
# 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
|
||||
new_solution = @initial_solution.deep_dup
|
||||
|
||||
selected_guests = new_solution.tables.map(&:pop).cycle.tap(&:next)
|
||||
new_solution.tables.each { |table| table << selected_guests.next }
|
||||
|
||||
new_solution
|
||||
end
|
||||
end
|
||||
end
|
@ -15,6 +15,11 @@ module VNS
|
||||
@target_function = function
|
||||
end
|
||||
|
||||
def add_optimization(klass)
|
||||
@optimizations ||= Set.new
|
||||
@optimizations << klass
|
||||
end
|
||||
|
||||
def add_perturbation(klass)
|
||||
@perturbations ||= Set.new
|
||||
@perturbations << klass
|
||||
@ -24,32 +29,38 @@ module VNS
|
||||
|
||||
def run
|
||||
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
|
||||
|
||||
@perturbations ||= Set.new
|
||||
|
||||
@best_solution = @initial_solution
|
||||
@best_score = @target_function.call(@best_solution)
|
||||
|
||||
self.class.sequence(@perturbations).each do |perturbation|
|
||||
optimize(perturbation)
|
||||
self.class.sequence(@optimizations).each do |optimization|
|
||||
optimize(optimization)
|
||||
Rails.logger.debug { "Finished optimization phase: #{optimization}" }
|
||||
end
|
||||
|
||||
Rails.logger.debug { "Finished all optimization phases" }
|
||||
|
||||
@best_solution
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def optimize(perturbation_klass)
|
||||
def optimize(optimization_klass)
|
||||
loop do
|
||||
optimized = false
|
||||
|
||||
perturbation_klass.new(@best_solution).each do |alternative_solution|
|
||||
optimization_klass.new(@best_solution).each do |alternative_solution|
|
||||
score = @target_function.call(alternative_solution)
|
||||
next if score >= @best_score
|
||||
|
||||
@best_solution = alternative_solution.deep_dup
|
||||
@best_score = score
|
||||
optimized = true
|
||||
Rails.logger.debug { "[#{optimization_klass}] Found better solution with score: #{score}" }
|
||||
|
||||
break
|
||||
end
|
||||
|
@ -10,4 +10,10 @@ class Set
|
||||
def to_table
|
||||
Tables::Table.new(self)
|
||||
end
|
||||
|
||||
def pop
|
||||
element = self.to_a.sample
|
||||
self.delete(element)
|
||||
element
|
||||
end
|
||||
end
|
@ -8,8 +8,8 @@ namespace :vns do
|
||||
|
||||
engine = VNS::Engine.new
|
||||
|
||||
engine.add_perturbation(Tables::Swap)
|
||||
engine.add_perturbation(Tables::Shift)
|
||||
engine.add_optimization(Tables::Swap)
|
||||
engine.add_optimization(Tables::Shift)
|
||||
|
||||
hierarchy = AffinityGroupsHierarchy.new
|
||||
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 @@
|
||||
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).not_to include(initial_solution.tables[0])
|
||||
expect(result.tables).not_to include(initial_solution.tables[1])
|
||||
expect(result.tables).not_to include(initial_solution.tables[2])
|
||||
|
||||
expect(result.tables.map(&:to_a).flatten).to contain_exactly(*(:a..:i).to_a)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
x
Reference in New Issue
Block a user