Rename perturbation -> optimization to reflect the nature of swap and shift operations
This commit is contained in:
parent
4dfd428ce4
commit
e1a5e4f73e
@ -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)
|
||||||
|
@ -15,23 +15,23 @@ module VNS
|
|||||||
@target_function = function
|
@target_function = function
|
||||||
end
|
end
|
||||||
|
|
||||||
def add_perturbation(klass)
|
def add_optimization(klass)
|
||||||
@perturbations ||= Set.new
|
@optimizations ||= Set.new
|
||||||
@perturbations << klass
|
@optimizations << klass
|
||||||
end
|
end
|
||||||
|
|
||||||
attr_writer :initial_solution
|
attr_writer :initial_solution
|
||||||
|
|
||||||
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
|
@best_solution = @initial_solution
|
||||||
@best_score = @target_function.call(@best_solution)
|
@best_score = @target_function.call(@best_solution)
|
||||||
|
|
||||||
self.class.sequence(@perturbations).each do |perturbation|
|
self.class.sequence(@optimizations).each do |optimization|
|
||||||
optimize(perturbation)
|
optimize(optimization)
|
||||||
end
|
end
|
||||||
|
|
||||||
@best_solution
|
@best_solution
|
||||||
@ -39,11 +39,11 @@ module VNS
|
|||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def optimize(perturbation_klass)
|
def optimize(optimization_klass)
|
||||||
loop do
|
loop do
|
||||||
optimized = false
|
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)
|
score = @target_function.call(alternative_solution)
|
||||||
next if score >= @best_score
|
next if score >= @best_score
|
||||||
|
|
||||||
|
@ -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:)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user