Class: Sommelier

Inherits:
Object
  • Object
show all
Defined in:
lib/sommelier.rb,
lib/sommelier/version.rb,
lib/sommelier/preference.rb,
lib/sommelier/match_maker.rb,
lib/sommelier/match_catalog.rb,
lib/sommelier/match_maker/decider.rb,
lib/sommelier/match_maker/generator.rb,
lib/sommelier/descending_insertion_sort_array.rb

Overview

Pair off members of one set with members of another, optimizing for the total match score of all pairings. This is similar to how a sommelier may provide a complete set of wine pairings for each dish on a menu.

Example:

sommelier = Sommelier.new
sommelier.add_match('Asparagus', 'Pinot Noir', 0.366)
sommelier.add_match('Asparagus', 'Sauvignon Blanc', 0.453)
sommelier.add_match('Asparagus', 'Chardonnay', 0.245)
sommelier.add_match('Tofu', 'Rosé', 0.486)
sommelier.add_match('Tofu', 'Sauvignon Blanc', 0.304)
sommelier.add_match('Eggplant', 'Sauvignon Blanc', 0.299)
sommelier.add_match('Salmon', 'Sauvignon Blanc', 0.602)
puts sommelier.pairings
# {
#   "Salmon" => "Sauvignon Blanc",
#   "Tofu" => "Rosé",
#   "Asparagus" => "Pinot Noir"
# }

# Note: neither "Eggplant" nor "Chardonnay" were matched in the pairings map

Constant Summary collapse

VERSION =
'0.2.0'

Instance Method Summary collapse

Constructor Details

#initializeSommelier

Returns a new instance of Sommelier.



31
32
33
# File 'lib/sommelier.rb', line 31

def initialize
  @match_catalog = MatchCatalog.new
end

Instance Method Details

#add_match(dish, wine, score) ⇒ Object

Include a potential pairing between the dish and wine. The pairing has a score (a higher score means a better pairing) that is used the generate preferences of dish => wines and wine => dishes. These preferences are used in a variant of the Gale-Shapely algorithm.

Since the dishes are acting as the suitors (in Gale-Shapely terminology), they tend to get better preferred wines than vice versa. However, since a pairing is symmetrical, passing the wines in as the dish and the dishes in as the wine will work, but yield potentially different pairings.

Parameters:

  • dish (Object)

    the proposal maker in the Gale-Shapely algorithm.

  • wine (Object)

    the proposal acceptor/rejector in the Gale-Shapely algorithm.

  • score (Numeric)

    a numeric representation of the strength of the pairing. A higher score means a better paring.



50
51
52
# File 'lib/sommelier.rb', line 50

def add_match(dish, wine, score)
  match_catalog.add(dish, wine, score)
end

#pairingsHash<Object, Object>

A map with dishes as keys and wines as objects

Returns:

  • (Hash<Object, Object>)

    the final matching between dishes and wines.



57
58
59
# File 'lib/sommelier.rb', line 57

def pairings
  MatchMaker.new(match_catalog).pairings
end