Class: StableMatching::Roommate::PhaseIIIRunner

Inherits:
PhaseRunner
  • Object
show all
Defined in:
lib/stable-matching/roommate/phase_iii_runner.rb

Instance Method Summary collapse

Constructor Details

#initialize(preference_table, opts = {}) ⇒ PhaseIIIRunner

Returns a new instance of PhaseIIIRunner.



100
101
102
103
# File 'lib/stable-matching/roommate/phase_iii_runner.rb', line 100

def initialize(preference_table, opts = {})
  @preference_table = preference_table
  @logger = opts.fetch(:logger)
end

Instance Method Details

#runObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/stable-matching/roommate/phase_iii_runner.rb', line 105

def run
  # The output of previous phase may have resulted in a complete
  # table, in which case this step doesn't need to be run
  return @preference_table if @preference_table.complete?

  while table_is_stable? && !@preference_table.complete?
    x = [@preference_table.members_with_multiple_preferences.first]
    y = [nil]

    until any_repeats?(x)
      # require 'pry'; binding.pry if x.last.second_preference.nil?
      y << x.last.second_preference
      x << y.last.last_preference
    end

    detect_and_reject_cycles(x, y)
  end
end