Persist VNS calculation progress whenever an improvement has been made
Some checks failed
Run unit tests / check-licenses (pull_request) Failing after 1m44s
Run unit tests / rubocop (pull_request) Failing after 1m46s
Run unit tests / copyright_notice (pull_request) Successful in 2m8s
Run unit tests / unit_tests (pull_request) Failing after 3m30s
Run unit tests / build-static-assets (pull_request) Has been skipped

This commit is contained in:
Manuel Bustillo 2025-09-08 22:44:54 +02:00
parent 0d1b64256d
commit 12174b6f20
10 changed files with 56 additions and 13 deletions

View File

@ -9,10 +9,10 @@ class TablesArrangementsController < ApplicationController
render json: TablesArrangement render json: TablesArrangement
.order(valid: :desc) .order(valid: :desc)
.order(discomfort: :asc) .order(discomfort: :asc)
.select(:id, :name, :discomfort, :status) .select(:id, :name, :discomfort, :status, :progress)
.select("digest = '#{current_digest}'::uuid as valid") .select("digest = '#{current_digest}'::uuid OR discomfort IS NULL as valid")
.limit(20) .limit(20)
.as_json(only: %i[id name discomfort valid status]) .as_json(only: %i[id name discomfort valid status progress])
end end
def show def show
@ -25,7 +25,10 @@ class TablesArrangementsController < ApplicationController
end end
def create def create
TableSimulatorJob.perform_later(current_tenant.id) ActiveRecord::Base.transaction do
tables_arrangement = TablesArrangement.create!(status: :not_started)
TableSimulatorJob.perform_later(current_tenant.id, tables_arrangement.id)
end
render json: {}, status: :created render json: {}, status: :created
end end

View File

@ -8,16 +8,35 @@ class TableSimulatorJob < ApplicationJob
MIN_PER_TABLE = 8 MIN_PER_TABLE = 8
MAX_PER_TABLE = 10 MAX_PER_TABLE = 10
def perform(wedding_id) def perform(wedding_id, tables_arrangement_id)
Rails.logger.info "Starting table simulation #{tables_arrangement_id} for wedding #{wedding_id}"
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_optimization(Tables::Swap)
engine.add_optimization(Tables::Shift) engine.add_optimization(Tables::Shift)
initial_solution = Tables::Distribution.new(min_per_table: MIN_PER_TABLE, max_per_table: MAX_PER_TABLE) tables_arrangement = TablesArrangement.find(tables_arrangement_id)
initial_solution = Tables::Distribution.new(
min_per_table: MIN_PER_TABLE,
max_per_table: MAX_PER_TABLE,
tables_arrangement_id:
)
initial_solution.random_distribution(Guest.potential.shuffle) initial_solution.random_distribution(Guest.potential.shuffle)
initial_solution.save!
engine.notify_progress do |current_progress|
tables_arrangement.update_columns(status: :in_progress, progress: current_progress)
end
engine.on_better_solution do |better_solution|
better_solution.save!
tables_arrangement.update_columns(discomfort: better_solution.discomfort) # TODO: remove?
end
engine.initial_solution = initial_solution engine.initial_solution = initial_solution
engine.target_function(&:discomfort) engine.target_function(&:discomfort)
@ -25,6 +44,8 @@ class TableSimulatorJob < ApplicationJob
best_solution = engine.run best_solution = engine.run
best_solution.save! best_solution.save!
tables_arrangement.update_columns(status: :completed)
end end
end end
end end

View File

@ -10,6 +10,7 @@
# digest :uuid not null # digest :uuid not null
# discomfort :integer # discomfort :integer
# name :string not null # name :string not null
# progress :float default(0.0), not null
# status :string default("complete"), not null # status :string default("complete"), not null
# created_at :datetime not null # created_at :datetime not null
# updated_at :datetime not null # updated_at :datetime not null

View File

@ -14,7 +14,7 @@ module Tables
attr_accessor :tables, :min_per_table, :max_per_table, :hierarchy, :tables_arrangement_id attr_accessor :tables, :min_per_table, :max_per_table, :hierarchy, :tables_arrangement_id
def initialize(min_per_table:, max_per_table:, hierarchy: AffinityGroupsHierarchy.new, tables_arrangement_id: nil) def initialize(min_per_table:, max_per_table:, tables_arrangement_id:, hierarchy: AffinityGroupsHierarchy.new)
@min_per_table = min_per_table @min_per_table = min_per_table
@max_per_table = max_per_table @max_per_table = max_per_table
@hierarchy = hierarchy @hierarchy = hierarchy
@ -43,15 +43,19 @@ module Tables
end end
def deep_dup def deep_dup
self.class.new(min_per_table: @min_per_table, max_per_table: @max_per_table, self.class.new(
hierarchy: @hierarchy).tap do |new_distribution| min_per_table: @min_per_table,
max_per_table: @max_per_table,
hierarchy: @hierarchy,
tables_arrangement_id: @tables_arrangement_id
).tap do |new_distribution|
new_distribution.tables = @tables.map(&:dup) new_distribution.tables = @tables.map(&:dup)
end end
end end
def save! def save!
ActiveRecord::Base.transaction do ActiveRecord::Base.transaction do
arrangement = TablesArrangement.find_or_create_by!(id: tables_arrangement_id) arrangement = TablesArrangement.find(tables_arrangement_id)
self.tables_arrangement_id = arrangement.id self.tables_arrangement_id = arrangement.id

View File

@ -53,7 +53,7 @@ module VNS
best_solution = @current_solution best_solution = @current_solution
ITERATIONS.times do |iteration| (1..ITERATIONS).each do |iteration|
@current_solution = Tables::WheelSwap.new(best_solution).call(PERTURBATION_SIZES.sample) @current_solution = Tables::WheelSwap.new(best_solution).call(PERTURBATION_SIZES.sample)
@best_score = @target_function.call(@current_solution) @best_score = @target_function.call(@current_solution)
Rails.logger.debug { "After perturbation: #{@best_score}" } Rails.logger.debug { "After perturbation: #{@best_score}" }

View File

@ -3,4 +3,5 @@
require_relative "../config/environment" require_relative "../config/environment"
require "solid_queue/cli" require "solid_queue/cli"
SolidQueue.logger = ActiveSupport::Logger.new($stdout)
SolidQueue::Cli.start(ARGV) SolidQueue::Cli.start(ARGV)

View File

@ -0,0 +1,5 @@
class AddProgressToTablesArrangements < ActiveRecord::Migration[8.0]
def change
add_column :tables_arrangements, :progress, :float, default: 0, null: false
end
end

3
db/schema.rb generated
View File

@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[8.0].define(version: 2025_08_01_102437) do ActiveRecord::Schema[8.0].define(version: 2025_09_08_145119) do
# These are extensions that must be enabled in order to support this database # These are extensions that must be enabled in order to support this database
enable_extension "pg_catalog.plpgsql" enable_extension "pg_catalog.plpgsql"
@ -217,6 +217,7 @@ ActiveRecord::Schema[8.0].define(version: 2025_08_01_102437) do
t.uuid "wedding_id", null: false t.uuid "wedding_id", null: false
t.uuid "digest", default: -> { "gen_random_uuid()" }, null: false t.uuid "digest", default: -> { "gen_random_uuid()" }, null: false
t.string "status", default: "complete", null: false t.string "status", default: "complete", null: false
t.float "progress", default: 0.0, null: false
t.index ["wedding_id"], name: "index_tables_arrangements_on_wedding_id" t.index ["wedding_id"], name: "index_tables_arrangements_on_wedding_id"
end end

View File

@ -86,7 +86,9 @@ ActsAsTenant.with_tenant(wedding) do
# TODO: Clean up invitations with no guests # TODO: Clean up invitations with no guests
ActiveJob.perform_all_later(3.times.map { TableSimulatorJob.new(wedding.id) }) 3.times { TablesArrangement.create! }
.map { |arrangement| TableSimulatorJob.new(wedding.id, arrangement.id) }
.then { |jobs| ActiveJob.perform_all_later }
"red".dup.paint.palette.triad(as: :hex).zip(Group.roots).each { |(color, group)| group.update!(color: color.paint.desaturate(40)) } "red".dup.paint.palette.triad(as: :hex).zip(Group.roots).each { |(color, group)| group.update!(color: color.paint.desaturate(40)) }

View File

@ -12,6 +12,11 @@ server {
proxy_set_header Host $http_host; proxy_set_header Host $http_host;
} }
location /jobs/ {
proxy_pass http://backend:3000/jobs/;
proxy_set_header Host $http_host;
}
location /captcha/v2/media/ { location /captcha/v2/media/ {
proxy_pass http://libre-captcha:8888/v2/media/; proxy_pass http://libre-captcha:8888/v2/media/;
proxy_set_header Host $http_host; proxy_set_header Host $http_host;