Class: ConstraintSolver::OneOfEqualsConstraint
- Inherits:
-
AbstractConstraint
- Object
- AbstractConstraint
- ConstraintSolver::OneOfEqualsConstraint
- Defined in:
- lib/OneOfEqualsConstraint.rb
Overview
Represents a one-of-equals constraint.
Instance Attribute Summary collapse
-
#value ⇒ Object
readonly
Returns the value of attribute value.
-
#variables ⇒ Object
readonly
Returns the value of attribute variables.
-
#violationCost ⇒ Object
readonly
Returns the value of attribute violationCost.
Instance Method Summary collapse
- #==(constraint) ⇒ Object
- #allAssigned? ⇒ Boolean
- #each ⇒ Object
- #holds? ⇒ Boolean
- #include?(variable) ⇒ Boolean
-
#initialize(variables, value, violationCost = 1) ⇒ OneOfEqualsConstraint
constructor
Initialises a new one-of-equals constraint.
- #revise ⇒ Object
- #to_s ⇒ Object
- #to_s_full ⇒ Object
Constructor Details
#initialize(variables, value, violationCost = 1) ⇒ OneOfEqualsConstraint
Initialises a new one-of-equals constraint. The arguments are the list of variables involved in the constraint and the value at least one of the variables should be equal to. Optionally, a cost for violating the constraint can be specified.
14 15 16 17 18 19 20 21 |
# File 'lib/OneOfEqualsConstraint.rb', line 14 def initialize(variables, value, violationCost=1) if variables.empty? raise ArgumentError, "List of variables must not be empty!" end @variables = variables @value = value @violationCost = violationCost end |
Instance Attribute Details
#value ⇒ Object (readonly)
Returns the value of attribute value.
9 10 11 |
# File 'lib/OneOfEqualsConstraint.rb', line 9 def value @value end |
#variables ⇒ Object (readonly)
Returns the value of attribute variables.
9 10 11 |
# File 'lib/OneOfEqualsConstraint.rb', line 9 def variables @variables end |
#violationCost ⇒ Object (readonly)
Returns the value of attribute violationCost.
9 10 11 |
# File 'lib/OneOfEqualsConstraint.rb', line 9 def violationCost @violationCost end |
Instance Method Details
#==(constraint) ⇒ Object
50 51 52 53 54 |
# File 'lib/OneOfEqualsConstraint.rb', line 50 def ==(constraint) return false unless constraint.kind_of?(OneOfEqualsConstraint) (@variables == constraint.variables) and (@value == constraint.value) and (@violationCost == constraint.violationCost) end |
#allAssigned? ⇒ Boolean
31 32 33 34 35 36 |
# File 'lib/OneOfEqualsConstraint.rb', line 31 def allAssigned? @variables.each { |var| return false if not var.assigned? } return true end |
#each ⇒ Object
56 57 58 59 60 |
# File 'lib/OneOfEqualsConstraint.rb', line 56 def each @variables.each { |var| yield var } end |
#holds? ⇒ Boolean
23 24 25 26 27 28 29 |
# File 'lib/OneOfEqualsConstraint.rb', line 23 def holds? if not allAssigned? or not @variables.find { |var| var.value == @value }.nil? return true else return false end end |
#include?(variable) ⇒ Boolean
38 39 40 |
# File 'lib/OneOfEqualsConstraint.rb', line 38 def include?(variable) @variables.include?(variable) end |
#revise ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/OneOfEqualsConstraint.rb', line 62 def revise checks = 0 revisedVariables = [] wipeout = false if @variables.find_all { |var| var.assigned? and not var.value == @value }.size == @variables.size - 1 unassigned = @variables.find { |var| not var.assigned? } pruneList = unassigned.domain.values - [ @value ] if not pruneList.empty? begin unassigned.domain.prune(pruneList) rescue DomainWipeoutException wipeout = true end revisedVariables << unassigned end end return revisedVariables, checks, wipeout end |
#to_s ⇒ Object
42 43 44 |
# File 'lib/OneOfEqualsConstraint.rb', line 42 def to_s @variables.collect { |var| var.name }.join(" != ") end |
#to_s_full ⇒ Object
46 47 48 |
# File 'lib/OneOfEqualsConstraint.rb', line 46 def to_s_full @variables.collect { |var| var.to_s }.join(" != ") end |