Compare commits

..

15 Commits

Author SHA1 Message Date
Renovate Bot
2ac4747a9d Update dependency importmap-rails to v2.2.0
Some checks failed
Run unit tests / check-licenses (pull_request) Successful in 1m57s
Run unit tests / rubocop (pull_request) Successful in 4m2s
Run unit tests / unit_tests (pull_request) Successful in 6m57s
Run unit tests / copyright_notice (pull_request) Successful in 3m35s
Run unit tests / build-static-assets (pull_request) Failing after 14m54s
2025-07-25 02:05:01 +00:00
8662652e1a Merge pull request 'Add a perturbation step to the VNS engine' (#305) from vns-perturbations into main
All checks were successful
Run unit tests / rubocop (push) Has been skipped
Run unit tests / check-licenses (push) Has been skipped
Run unit tests / copyright_notice (push) Has been skipped
Run unit tests / unit_tests (push) Successful in 1m13s
Run unit tests / build-static-assets (push) Successful in 10m9s
Reviewed-on: #305
2025-07-24 12:21:01 +00:00
3260b0b422 Add copyright notice
All checks were successful
Run unit tests / rubocop (pull_request) Successful in 2m24s
Run unit tests / check-licenses (pull_request) Successful in 2m32s
Run unit tests / copyright_notice (pull_request) Successful in 2m56s
Run unit tests / unit_tests (pull_request) Successful in 5m22s
Run unit tests / build-static-assets (pull_request) Successful in 29m2s
2025-07-24 11:46:28 +00:00
81f1e79b6d Merge branch 'main' into vns-perturbations
All checks were successful
Run unit tests / rubocop (pull_request) Successful in 1m4s
Run unit tests / check-licenses (pull_request) Successful in 1m28s
Run unit tests / copyright_notice (pull_request) Successful in 3m7s
Run unit tests / unit_tests (pull_request) Successful in 5m28s
Run unit tests / build-static-assets (pull_request) Successful in 30m6s
2025-07-24 11:45:19 +00:00
d18bddb31a Remove commented-out code
All checks were successful
Run unit tests / rubocop (pull_request) Successful in 41s
Run unit tests / check-licenses (pull_request) Successful in 1m5s
Run unit tests / copyright_notice (pull_request) Successful in 2m0s
Run unit tests / unit_tests (pull_request) Successful in 7m15s
Run unit tests / build-static-assets (pull_request) Successful in 28m52s
2025-07-24 13:42:47 +02:00
3f158d7906 Merge pull request 'Update dependency solid_queue to v1.2.1' (#304) from renovate/solid_queue-1.x-lockfile 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 36s
Run unit tests / build-static-assets (push) Successful in 35m9s
Reviewed-on: #304
2025-07-24 11:41:15 +00:00
db85580c1f Introduce a wheel swap perturbation as part of the VNS engine process 2025-07-24 13:41:01 +02:00
Renovate Bot
76ed4229ea Update dependency solid_queue to v1.2.1
All checks were successful
Run unit tests / rubocop (pull_request) Successful in 3m39s
Run unit tests / check-licenses (pull_request) Successful in 3m7s
Run unit tests / copyright_notice (pull_request) Successful in 2m55s
Run unit tests / unit_tests (pull_request) Successful in 5m5s
Run unit tests / build-static-assets (pull_request) Successful in 9m2s
2025-07-24 02:04:54 +00:00
4befb8505b Run perturbation on top of the best solution so far 2025-07-23 11:02:32 +02:00
b1df5d2f53 Shuffle guests in WheelSwap before assigning them to new tables 2025-07-23 11:01:38 +02:00
a1f06dae5b Define a WheelSwap class that randomly swaps one guest from each table 2025-07-23 10:22:41 +02:00
e8a88b50e2 Initialize Tables::WheelSwap class 2025-07-22 16:26:28 +02:00
185f359942 Include additional debugging messages 2025-07-22 15:56:25 +02:00
543b53d938 Initialize empty set of perturbations and add debug messages 2025-07-22 15:53:06 +02:00
e1a5e4f73e Rename perturbation -> optimization to reflect the nature of swap and shift operations 2025-07-22 15:39:30 +02:00
7 changed files with 117 additions and 18 deletions

View File

@ -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.0) solid_queue (1.2.1)
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.3.2) thor (1.4.0)
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.0) sha256=482ac5305cbe91ebf845627caec493fda8545bf22b18205df01afb80999e28de solid_queue (1.2.1) sha256=7976b3690a08080ef63d1b11281f0b77398f7697dbeda0e2c5532682639d4b15
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.3.2) sha256=eef0293b9e24158ccad7ab383ae83534b7ad4ed99c09f96f1a6b036550abbeda thor (1.4.0) sha256=8763e822ccb0f1d7bee88cde131b19a65606657b847cc7b7b4b82e772bcd8a3d
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

View File

@ -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)

View 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

View File

@ -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

View File

@ -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

View File

@ -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:)

View 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