Class: CSP::Problem

Inherits:
Object
  • Object
show all
Includes:
Constraints
Defined in:
lib/csp/problem.rb

Overview

TODO: implement dependent factor with weight TODO: implement lookahead, arc-consistency, ac3

Constant Summary collapse

InvalidConstraintVariable =
Class.new(StandardError)
VariableShouldNotBeEmpty =
Class.new(StandardError)
DomainsShouldNotBeEmpty =
Class.new(StandardError)
VariableAlreadySeted =
Class.new(StandardError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Constraints

#all_different, #unique

Constructor Details

#initialize(max_solutions: 1) ⇒ Problem

Returns a new instance of Problem.



17
18
19
20
21
22
# File 'lib/csp/problem.rb', line 17

def initialize(max_solutions: 1)
  @variables = []
  @domains = {}
  @constraints = {}
  @max_solutions = max_solutions
end

Instance Attribute Details

#constraintsObject (readonly)

Returns the value of attribute constraints.



9
10
11
# File 'lib/csp/problem.rb', line 9

def constraints
  @constraints
end

#domainsObject (readonly)

Returns the value of attribute domains.



9
10
11
# File 'lib/csp/problem.rb', line 9

def domains
  @domains
end

#filtering_algorithmObject (readonly)

Returns the value of attribute filtering_algorithm.



9
10
11
# File 'lib/csp/problem.rb', line 9

def filtering_algorithm
  @filtering_algorithm
end

#lookahead_algorithmObject (readonly)

Returns the value of attribute lookahead_algorithm.



9
10
11
# File 'lib/csp/problem.rb', line 9

def lookahead_algorithm
  @lookahead_algorithm
end

#max_solutionsObject (readonly)

Returns the value of attribute max_solutions.



9
10
11
# File 'lib/csp/problem.rb', line 9

def max_solutions
  @max_solutions
end

#ordering_algorithmObject (readonly)

Returns the value of attribute ordering_algorithm.



9
10
11
# File 'lib/csp/problem.rb', line 9

def ordering_algorithm
  @ordering_algorithm
end

#variablesObject (readonly)

Returns the value of attribute variables.



9
10
11
# File 'lib/csp/problem.rb', line 9

def variables
  @variables
end

Instance Method Details

#add_constraint(constraint = nil, variables: nil, &block) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/csp/problem.rb', line 50

def add_constraint(constraint = nil, variables: nil, &block)
  validate_parameters(constraint, variables, block)

  constraint = CustomConstraint.new(variables, block) if block

  constraint.variables.each do |variable|
    next constraints[variable] << constraint if constraints.include?(variable)

    raise InvalidConstraintVariable,
          "Constraint's variable doesn't exists in CSP"
  end

  self
end

#add_filtering(filtering_algorithm) ⇒ Object



70
71
72
73
# File 'lib/csp/problem.rb', line 70

def add_filtering(filtering_algorithm)
  @filtering_algorithm = filtering_algorithm
  self
end

#add_lookahead(lookahead_algorithm) ⇒ Object



75
76
77
78
# File 'lib/csp/problem.rb', line 75

def add_lookahead(lookahead_algorithm)
  @lookahead_algorithm = lookahead_algorithm
  self
end

#add_ordering(ordering_algorithm) ⇒ Object



65
66
67
68
# File 'lib/csp/problem.rb', line 65

def add_ordering(ordering_algorithm)
  @ordering_algorithm = ordering_algorithm
  self
end

#add_variable(variable, domains:) ⇒ Object



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

def add_variable(variable, domains:)
  if (variable.respond_to?(:empty?) && variable.empty?) || variable.nil?
    raise VariableShouldNotBeEmpty, 'Variable was empty in the function parameter'
  end
  raise DomainsShouldNotBeEmpty, 'Domains was empty in the function parameter' if domains.empty?
  raise VariableAlreadySeted, "Variable #{variable} has already been seted" if variables.include?(variable)

  variables << variable
  @domains[variable] = domains
  constraints[variable] = []

  self
end

#add_variables(variables, domains:) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/csp/problem.rb', line 42

def add_variables(variables, domains:)
  variables.each do |variable|
    add_variable(variable, domains: domains)
  end

  self
end

#solve(assignment = {}) ⇒ Object



24
25
26
# File 'lib/csp/problem.rb', line 24

def solve(assignment = {})
  Utils::Array.wrap(search_solution(assignment))
end