# frozen_string_literal: true namespace :vns do desc 'Benchmarks the efficiency of the VNS implementation' task benchmark: :environment do ActsAsTenant.with_tenant(Wedding.first) do Rails.logger.info "There are #{Guest.potential.count} potential guests" engine = VNS::Engine.new engine.add_optimization(Tables::Swap) engine.add_optimization(Tables::Shift) hierarchy = AffinityGroupsHierarchy.new initial_solution = Tables::Distribution.new(min_per_table: 8, max_per_table: 10, hierarchy:) random = Random.new(561_163) initial_solution.random_distribution(Guest.potential.shuffle(random:), random:) engine.initial_solution = initial_solution engine.target_function(&:discomfort) engine.notify_progress do |current_progress| Rails.logger.info "Progress: #{(current_progress * 100.0).round(2)}%" end engine.on_better_solution do |better_solution| Rails.logger.info "New best solution found with discomfort: #{better_solution.discomfort}" end solution = Rails.benchmark('VNS Benchmarking') { engine.run } Rails.logger.info "Best solution found with discomfort: #{solution.discomfort}" end end end