Compare commits
No commits in common. "e8a88b50e2750abfa4198d317e990a5583379183" and "4dfd428ce48e4a3b44b40eb66dd0d51865e79744" have entirely different histories.
e8a88b50e2
...
4dfd428ce4
@ -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,16 +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
|
|
||||||
@initial_solution.deep_dup
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@ -15,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
|
||||||
@ -29,38 +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_solution = @initial_solution
|
||||||
@best_score = @target_function.call(@best_solution)
|
@best_score = @target_function.call(@best_solution)
|
||||||
|
|
||||||
self.class.sequence(@optimizations).each do |optimization|
|
self.class.sequence(@perturbations).each do |perturbation|
|
||||||
optimize(optimization)
|
optimize(perturbation)
|
||||||
Rails.logger.debug { "Finished optimization phase: #{optimization}" }
|
|
||||||
end
|
end
|
||||||
|
|
||||||
Rails.logger.debug { "Finished all optimization phases" }
|
|
||||||
|
|
||||||
@best_solution
|
@best_solution
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def optimize(optimization_klass)
|
def optimize(perturbation_klass)
|
||||||
loop do
|
loop do
|
||||||
optimized = false
|
optimized = false
|
||||||
|
|
||||||
optimization_klass.new(@best_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
|
||||||
|
|
||||||
@best_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
|
||||||
|
|||||||
@ -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,21 +0,0 @@
|
|||||||
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)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
Loading…
x
Reference in New Issue
Block a user