Class: CSP::Problem
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
-
#constraints ⇒ Object
readonly
Returns the value of attribute constraints.
-
#domains ⇒ Object
readonly
Returns the value of attribute domains.
-
#filtering_algorithm ⇒ Object
readonly
Returns the value of attribute filtering_algorithm.
-
#lookahead_algorithm ⇒ Object
readonly
Returns the value of attribute lookahead_algorithm.
-
#max_solutions ⇒ Object
readonly
Returns the value of attribute max_solutions.
-
#ordering_algorithm ⇒ Object
readonly
Returns the value of attribute ordering_algorithm.
-
#variables ⇒ Object
readonly
Returns the value of attribute variables.
Instance Method Summary collapse
- #add_constraint(constraint = nil, variables: nil, &block) ⇒ Object
- #add_filtering(filtering_algorithm) ⇒ Object
- #add_lookahead(lookahead_algorithm) ⇒ Object
- #add_ordering(ordering_algorithm) ⇒ Object
- #add_variable(variable, domains:) ⇒ Object
- #add_variables(variables, domains:) ⇒ Object
-
#initialize(max_solutions: 1) ⇒ Problem
constructor
A new instance of Problem.
- #solve(assignment = {}) ⇒ Object
Methods included from Constraints
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
#constraints ⇒ Object (readonly)
Returns the value of attribute constraints.
9 10 11 |
# File 'lib/csp/problem.rb', line 9 def constraints @constraints end |
#domains ⇒ Object (readonly)
Returns the value of attribute domains.
9 10 11 |
# File 'lib/csp/problem.rb', line 9 def domains @domains end |
#filtering_algorithm ⇒ Object (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_algorithm ⇒ Object (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_solutions ⇒ Object (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_algorithm ⇒ Object (readonly)
Returns the value of attribute ordering_algorithm.
9 10 11 |
# File 'lib/csp/problem.rb', line 9 def ordering_algorithm @ordering_algorithm end |
#variables ⇒ Object (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 |