Compare commits
1 Commits
2ac4747a9d
...
3b839cb2d7
Author | SHA1 | Date | |
---|---|---|---|
![]() |
3b839cb2d7 |
10
Gemfile.lock
10
Gemfile.lock
@ -384,13 +384,13 @@ GEM
|
|||||||
securerandom (0.4.1)
|
securerandom (0.4.1)
|
||||||
shoulda-matchers (6.5.0)
|
shoulda-matchers (6.5.0)
|
||||||
activesupport (>= 5.2.0)
|
activesupport (>= 5.2.0)
|
||||||
solid_queue (1.2.1)
|
solid_queue (1.2.0)
|
||||||
activejob (>= 7.1)
|
activejob (>= 7.1)
|
||||||
activerecord (>= 7.1)
|
activerecord (>= 7.1)
|
||||||
concurrent-ruby (>= 1.3.1)
|
concurrent-ruby (>= 1.3.1)
|
||||||
fugit (~> 1.11.0)
|
fugit (~> 1.11.0)
|
||||||
railties (>= 7.1)
|
railties (>= 7.1)
|
||||||
thor (>= 1.3.1)
|
thor (~> 1.3.1)
|
||||||
sprockets (4.2.1)
|
sprockets (4.2.1)
|
||||||
concurrent-ruby (~> 1.0)
|
concurrent-ruby (~> 1.0)
|
||||||
rack (>= 2.2.4, < 4)
|
rack (>= 2.2.4, < 4)
|
||||||
@ -401,7 +401,7 @@ GEM
|
|||||||
stimulus-rails (1.3.4)
|
stimulus-rails (1.3.4)
|
||||||
railties (>= 6.0.0)
|
railties (>= 6.0.0)
|
||||||
stringio (3.1.7)
|
stringio (3.1.7)
|
||||||
thor (1.4.0)
|
thor (1.3.2)
|
||||||
tilt (2.4.0)
|
tilt (2.4.0)
|
||||||
timeout (0.4.3)
|
timeout (0.4.3)
|
||||||
tomlrb (2.0.3)
|
tomlrb (2.0.3)
|
||||||
@ -628,12 +628,12 @@ CHECKSUMS
|
|||||||
rubyzip (2.3.2) sha256=3f57e3935dc2255c414484fbf8d673b4909d8a6a57007ed754dde39342d2373f
|
rubyzip (2.3.2) sha256=3f57e3935dc2255c414484fbf8d673b4909d8a6a57007ed754dde39342d2373f
|
||||||
securerandom (0.4.1) sha256=cc5193d414a4341b6e225f0cb4446aceca8e50d5e1888743fac16987638ea0b1
|
securerandom (0.4.1) sha256=cc5193d414a4341b6e225f0cb4446aceca8e50d5e1888743fac16987638ea0b1
|
||||||
shoulda-matchers (6.5.0) sha256=ef6b572b2bed1ac4aba6ab2c5ff345a24b6d055a93a3d1c3bfc86d9d499e3f44
|
shoulda-matchers (6.5.0) sha256=ef6b572b2bed1ac4aba6ab2c5ff345a24b6d055a93a3d1c3bfc86d9d499e3f44
|
||||||
solid_queue (1.2.1) sha256=7976b3690a08080ef63d1b11281f0b77398f7697dbeda0e2c5532682639d4b15
|
solid_queue (1.2.0) sha256=482ac5305cbe91ebf845627caec493fda8545bf22b18205df01afb80999e28de
|
||||||
sprockets (4.2.1) sha256=951b13dd2f2fcae840a7184722689a803e0ff9d2702d902bd844b196da773f97
|
sprockets (4.2.1) sha256=951b13dd2f2fcae840a7184722689a803e0ff9d2702d902bd844b196da773f97
|
||||||
sprockets-rails (3.5.2) sha256=a9e88e6ce9f8c912d349aa5401509165ec42326baf9e942a85de4b76dbc4119e
|
sprockets-rails (3.5.2) sha256=a9e88e6ce9f8c912d349aa5401509165ec42326baf9e942a85de4b76dbc4119e
|
||||||
stimulus-rails (1.3.4) sha256=765676ffa1f33af64ce026d26b48e8ffb2e0b94e0f50e9119e11d6107d67cb06
|
stimulus-rails (1.3.4) sha256=765676ffa1f33af64ce026d26b48e8ffb2e0b94e0f50e9119e11d6107d67cb06
|
||||||
stringio (3.1.7) sha256=5b78b7cb242a315fb4fca61a8255d62ec438f58da2b90be66048546ade4507fa
|
stringio (3.1.7) sha256=5b78b7cb242a315fb4fca61a8255d62ec438f58da2b90be66048546ade4507fa
|
||||||
thor (1.4.0) sha256=8763e822ccb0f1d7bee88cde131b19a65606657b847cc7b7b4b82e772bcd8a3d
|
thor (1.3.2) sha256=eef0293b9e24158ccad7ab383ae83534b7ad4ed99c09f96f1a6b036550abbeda
|
||||||
tilt (2.4.0) sha256=df74f29a451daed26591a85e8e0cebb198892cb75b6573394303acda273fba4d
|
tilt (2.4.0) sha256=df74f29a451daed26591a85e8e0cebb198892cb75b6573394303acda273fba4d
|
||||||
timeout (0.4.3) sha256=9509f079b2b55fe4236d79633bd75e34c1c1e7e3fb4b56cb5fda61f80a0fe30e
|
timeout (0.4.3) sha256=9509f079b2b55fe4236d79633bd75e34c1c1e7e3fb4b56cb5fda61f80a0fe30e
|
||||||
tomlrb (2.0.3) sha256=c2736acf24919f793334023a4ff396c0647d93fce702a73c9d348deaa815d4f7
|
tomlrb (2.0.3) sha256=c2736acf24919f793334023a4ff396c0647d93fce702a73c9d348deaa815d4f7
|
||||||
|
@ -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_optimization(Tables::Swap)
|
engine.add_perturbation(Tables::Swap)
|
||||||
engine.add_optimization(Tables::Shift)
|
engine.add_perturbation(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)
|
||||||
|
@ -1,33 +0,0 @@
|
|||||||
# 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,7 +4,6 @@
|
|||||||
|
|
||||||
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
|
||||||
@ -16,11 +15,6 @@ 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
|
||||||
@ -30,58 +24,32 @@ module VNS
|
|||||||
|
|
||||||
def run
|
def run
|
||||||
raise 'No target function defined' unless @target_function
|
raise 'No target function defined' unless @target_function
|
||||||
raise 'No optimizations defined' unless @optimizations
|
raise 'No perturbations defined' unless @perturbations
|
||||||
raise 'No initial solution defined' unless @initial_solution
|
raise 'No initial solution defined' unless @initial_solution
|
||||||
|
|
||||||
@perturbations ||= Set.new
|
@best_solution = @initial_solution
|
||||||
|
@best_score = @target_function.call(@best_solution)
|
||||||
|
|
||||||
@current_solution = @initial_solution
|
self.class.sequence(@perturbations).each do |perturbation|
|
||||||
@best_score = @target_function.call(@current_solution)
|
optimize(perturbation)
|
||||||
|
|
||||||
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 run_all_optimizations
|
def optimize(perturbation_klass)
|
||||||
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
|
||||||
|
|
||||||
optimization_klass.new(@current_solution).each do |alternative_solution|
|
perturbation_klass.new(@best_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
|
||||||
|
|
||||||
@current_solution = alternative_solution.deep_dup
|
@best_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,10 +10,4 @@ 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_optimization(Tables::Swap)
|
engine.add_perturbation(Tables::Swap)
|
||||||
engine.add_optimization(Tables::Shift)
|
engine.add_perturbation(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:)
|
||||||
|
@ -1,28 +0,0 @@
|
|||||||
# 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