Persist table distributions
This commit is contained in:
		
							parent
							
								
									7926928feb
								
							
						
					
					
						commit
						191684a11c
					
				@ -1,2 +1,3 @@
 | 
			
		||||
class TablesArrangement < ApplicationRecord
 | 
			
		||||
  has_many :seats
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
@ -1,6 +1,6 @@
 | 
			
		||||
module Tables
 | 
			
		||||
  class Distribution
 | 
			
		||||
    attr_reader :tables
 | 
			
		||||
    attr_accessor :tables
 | 
			
		||||
 | 
			
		||||
    def initialize(min_per_table:, max_per_table:)
 | 
			
		||||
      @min_per_table = min_per_table
 | 
			
		||||
@ -29,6 +29,30 @@ module Tables
 | 
			
		||||
      end.join("\n")
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def deep_dup
 | 
			
		||||
      self.class.new(min_per_table: @min_per_table, max_per_table: @max_per_table).tap do |new_distribution|
 | 
			
		||||
        new_distribution.tables = @tables.map(&:dup)
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def save!
 | 
			
		||||
      ActiveRecord::Base.transaction do
 | 
			
		||||
        arrangement = TablesArrangement.create!
 | 
			
		||||
 | 
			
		||||
        records_to_store = []
 | 
			
		||||
 | 
			
		||||
        tables.each_with_index do |table, table_number|
 | 
			
		||||
          table.each do |person|
 | 
			
		||||
            records_to_store << { guest_id: person.id, tables_arrangement_id: arrangement.id, table_number: }
 | 
			
		||||
          end
 | 
			
		||||
        end
 | 
			
		||||
 | 
			
		||||
        Seat.insert_all!(records_to_store)
 | 
			
		||||
 | 
			
		||||
        arrangement.update!(discomfort:)
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    private
 | 
			
		||||
 | 
			
		||||
    def local_discomfort(table)
 | 
			
		||||
 | 
			
		||||
@ -1,6 +1,7 @@
 | 
			
		||||
class CreateTablesArrangements < ActiveRecord::Migration[7.1]
 | 
			
		||||
  def change
 | 
			
		||||
    create_table :tables_arrangements, id: :uuid do |t|
 | 
			
		||||
      t.integer :discomfort
 | 
			
		||||
 | 
			
		||||
      t.timestamps
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										1
									
								
								db/schema.rb
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										1
									
								
								db/schema.rb
									
									
									
										generated
									
									
									
								
							@ -46,6 +46,7 @@ ActiveRecord::Schema[7.1].define(version: 2024_07_24_181853) do
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  create_table "tables_arrangements", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
 | 
			
		||||
    t.integer "discomfort"
 | 
			
		||||
    t.datetime "created_at", null: false
 | 
			
		||||
    t.datetime "updated_at", null: false
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
@ -12,5 +12,9 @@ namespace :vns do
 | 
			
		||||
    engine.target_function(&:discomfort)
 | 
			
		||||
 | 
			
		||||
    best_solution = engine.run
 | 
			
		||||
 | 
			
		||||
    binding.pry
 | 
			
		||||
 | 
			
		||||
    best_solution.save!
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user