Compare commits
3 Commits
a1f06dae5b
...
db85580c1f
| Author | SHA1 | Date | |
|---|---|---|---|
| db85580c1f | |||
| 4befb8505b | |||
| b1df5d2f53 |
@ -9,11 +9,23 @@ module Tables
|
|||||||
@initial_solution = initial_solution
|
@initial_solution = initial_solution
|
||||||
end
|
end
|
||||||
|
|
||||||
def call
|
def call(size = 1)
|
||||||
|
Rails.logger.debug { "WheelSwap with size: #{size}" }
|
||||||
new_solution = @initial_solution.deep_dup
|
new_solution = @initial_solution.deep_dup
|
||||||
|
|
||||||
selected_guests = new_solution.tables.map(&:pop).cycle.tap(&:next)
|
selected_guests = []
|
||||||
new_solution.tables.each { |table| table << selected_guests.next }
|
|
||||||
|
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
|
new_solution
|
||||||
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
|
||||||
@ -34,30 +35,50 @@ module VNS
|
|||||||
|
|
||||||
@perturbations ||= Set.new
|
@perturbations ||= Set.new
|
||||||
|
|
||||||
@best_solution = @initial_solution
|
@current_solution = @initial_solution
|
||||||
@best_score = @target_function.call(@best_solution)
|
@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
|
||||||
|
|
||||||
|
best_solution
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def run_all_optimizations
|
||||||
self.class.sequence(@optimizations).each do |optimization|
|
self.class.sequence(@optimizations).each do |optimization|
|
||||||
optimize(optimization)
|
optimize(optimization)
|
||||||
Rails.logger.debug { "Finished optimization phase: #{optimization}" }
|
Rails.logger.debug { "Finished optimization phase: #{optimization}" }
|
||||||
end
|
end
|
||||||
|
Rails.logger.debug { 'Finished all optimization phases' }
|
||||||
Rails.logger.debug { "Finished all optimization phases" }
|
|
||||||
|
|
||||||
@best_solution
|
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def optimize(optimization_klass)
|
def optimize(optimization_klass)
|
||||||
loop do
|
loop do
|
||||||
optimized = false
|
optimized = false
|
||||||
|
|
||||||
optimization_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}" }
|
Rails.logger.debug { "[#{optimization_klass}] Found better solution with score: #{score}" }
|
||||||
|
|||||||
@ -1,8 +1,10 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
require 'rails_helper'
|
require 'rails_helper'
|
||||||
|
|
||||||
module Tables
|
module Tables
|
||||||
RSpec.describe WheelSwap do
|
RSpec.describe WheelSwap do
|
||||||
context "when the solution has three tables" do
|
context 'when the solution has three tables' do
|
||||||
let(:initial_solution) do
|
let(:initial_solution) do
|
||||||
Distribution.new(min_per_table: 3, max_per_table: 3).tap do |distribution|
|
Distribution.new(min_per_table: 3, max_per_table: 3).tap do |distribution|
|
||||||
distribution.tables << Set[:a, :b, :c].to_table
|
distribution.tables << Set[:a, :b, :c].to_table
|
||||||
@ -11,17 +13,17 @@ module Tables
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it "swaps a random guest from each table with a guest from another table", :aggregate_failures do
|
it 'swaps a random guest from each table with a guest from another table', :aggregate_failures do
|
||||||
result = described_class.new(initial_solution).call
|
result = described_class.new(initial_solution).call
|
||||||
|
|
||||||
expect(result.tables.size).to eq(3)
|
expect(result.tables.size).to eq(3)
|
||||||
expect(result.tables.map(&:size)).to all(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[0])
|
||||||
expect(result.tables).not_to include(initial_solution.tables[1])
|
# expect(result.tables).not_to include(initial_solution.tables[1])
|
||||||
expect(result.tables).not_to include(initial_solution.tables[2])
|
# expect(result.tables).not_to include(initial_solution.tables[2])
|
||||||
|
|
||||||
expect(result.tables.map(&:to_a).flatten).to contain_exactly(*(:a..:i).to_a)
|
expect(result.tables.map(&:to_a).flatten).to match_array((:a..:i).to_a)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user