Class: SportsManager::TournamentProblemBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/sports_manager/tournament_problem_builder.rb

Overview

TODO: better name this Public: Builds a CSP for Tournament Scheduling.

Usage example: builder = ProblemBuilder.new(tournament)

builder

.add_variables(variables)
.add_domains(domains)
.add_ordering(NoOrder)
.add_filtering(NoFilter)

builder

.add_constraint(AllDifferentConstraint)
.add_constraint(NoOverlapping)

csp = builder.build

Constant Summary collapse

MINIMUM_SOLUTIONS =
1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tournament) ⇒ TournamentProblemBuilder

Returns a new instance of TournamentProblemBuilder.



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/sports_manager/tournament_problem_builder.rb', line 28

def initialize(tournament)
  @variables = []
  @domains = {}
  @constraints = []

  @filtering_algorithm = nil
  @ordering_algorithm = nil
  @lookahead_algorithm = nil

  @max_solutions = MINIMUM_SOLUTIONS

  @tournament = tournament
end

Instance Attribute Details

#constraintsObject (readonly)

Returns the value of attribute constraints.



22
23
24
# File 'lib/sports_manager/tournament_problem_builder.rb', line 22

def constraints
  @constraints
end

#domainsObject (readonly)

Returns the value of attribute domains.



22
23
24
# File 'lib/sports_manager/tournament_problem_builder.rb', line 22

def domains
  @domains
end

#filtering_algorithmObject (readonly)

Returns the value of attribute filtering_algorithm.



22
23
24
# File 'lib/sports_manager/tournament_problem_builder.rb', line 22

def filtering_algorithm
  @filtering_algorithm
end

#lookahead_algorithmObject (readonly)

Returns the value of attribute lookahead_algorithm.



22
23
24
# File 'lib/sports_manager/tournament_problem_builder.rb', line 22

def lookahead_algorithm
  @lookahead_algorithm
end

#max_solutionsObject (readonly)

Returns the value of attribute max_solutions.



22
23
24
# File 'lib/sports_manager/tournament_problem_builder.rb', line 22

def max_solutions
  @max_solutions
end

#ordering_algorithmObject (readonly)

Returns the value of attribute ordering_algorithm.



22
23
24
# File 'lib/sports_manager/tournament_problem_builder.rb', line 22

def ordering_algorithm
  @ordering_algorithm
end

#tournamentObject (readonly)

Returns the value of attribute tournament.



22
23
24
# File 'lib/sports_manager/tournament_problem_builder.rb', line 22

def tournament
  @tournament
end

#variablesObject (readonly)

Returns the value of attribute variables.



22
23
24
# File 'lib/sports_manager/tournament_problem_builder.rb', line 22

def variables
  @variables
end

Instance Method Details

#add_constraint(constraint_class) ⇒ Object

TODO: change to pass symbol instead of class?



73
74
75
76
77
# File 'lib/sports_manager/tournament_problem_builder.rb', line 73

def add_constraint(constraint_class)
  @constraints |= Utils::Array.wrap(constraint_class)

  self
end

#add_domains(domains) ⇒ Object



48
49
50
51
52
# File 'lib/sports_manager/tournament_problem_builder.rb', line 48

def add_domains(domains)
  @domains = domains

  self
end

#add_filtering(filtering_algorithm_class) ⇒ Object



60
61
62
63
64
# File 'lib/sports_manager/tournament_problem_builder.rb', line 60

def add_filtering(filtering_algorithm_class)
  @filtering_algorithm = filtering_algorithm_class

  self
end

#add_lookahead(lookahead_algorithm_class) ⇒ Object



66
67
68
69
70
# File 'lib/sports_manager/tournament_problem_builder.rb', line 66

def add_lookahead(lookahead_algorithm_class)
  @lookahead_algorithm = lookahead_algorithm_class

  self
end

#add_max_solutions(max_solutions) ⇒ Object



79
80
81
82
83
# File 'lib/sports_manager/tournament_problem_builder.rb', line 79

def add_max_solutions(max_solutions)
  @max_solutions = [max_solutions, MINIMUM_SOLUTIONS].find(&:positive?)

  self
end

#add_ordering(ordering_algorithm_class) ⇒ Object



54
55
56
57
58
# File 'lib/sports_manager/tournament_problem_builder.rb', line 54

def add_ordering(ordering_algorithm_class)
  @ordering_algorithm = ordering_algorithm_class

  self
end

#add_variables(variables) ⇒ Object



42
43
44
45
46
# File 'lib/sports_manager/tournament_problem_builder.rb', line 42

def add_variables(variables)
  @variables = variables

  self
end

#buildObject



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/sports_manager/tournament_problem_builder.rb', line 85

def build
  csp.tap do |problem|
    variables.each do |variable|
      problem.add_variable(variable, domains: domains[variable])
    end

    problem.add_ordering(ordering(problem))
    problem.add_filtering(filtering(problem))
    problem.add_lookahead(lookahead(problem))

    build_constraint(problem)
  end
end