Class: ConstraintSolver::BinaryConstraint

Inherits:
AbstractConstraint show all
Defined in:
lib/BinaryConstraint.rb

Overview

Represents a binary constraint.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lhs, rhs, relation, violationCost = 1) ⇒ BinaryConstraint

Initialises a new binary constraint. The arguments are the variable at the left hand side of the relation, the variable at the right hand side of the relation and the relation itself. Optionally, a cost for violating the constraint can be specified.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/BinaryConstraint.rb', line 15

def initialize(lhs, rhs, relation, violationCost=1)
    if relation.arity != 2
	raise ArgumentError, "The relation given must take two arguments!"
    end
    @lhs = lhs
    @rhs = rhs
    @relation = relation
    @supported = { @lhs.name => {}, @rhs.name => {} }
    [ @lhs, @rhs ].each { |var|
	var.domain.each { |v| @supported[var.name][v] = Set.new }
    }
    @unsupported = { @lhs.name => {}, @rhs.name => {} }
    [ @lhs, @rhs ].each { |var|
	var.domain.each { |v| @unsupported[var.name][v] = Set.new }
    }
    @violationCost = violationCost
end

Instance Attribute Details

#lhsObject (readonly)

Returns the value of attribute lhs.



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

def lhs
  @lhs
end

#relationObject (readonly)

Returns the value of attribute relation.



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

def relation
  @relation
end

#rhsObject (readonly)

Returns the value of attribute rhs.



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

def rhs
  @rhs
end

#violationCostObject (readonly)

Returns the value of attribute violationCost.



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

def violationCost
  @violationCost
end

Instance Method Details

#==(constraint) ⇒ Object



59
60
61
62
63
# File 'lib/BinaryConstraint.rb', line 59

def ==(constraint)
    return false unless constraint.kind_of?(BinaryConstraint)
    (@lhs == constraint.lhs) and (@rhs == constraint.rhs) and
	(@relation == constraint.relation) and (@violationCost == constraint.violationCost)
end

#allAssigned?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/BinaryConstraint.rb', line 43

def allAssigned?
    @lhs.assigned? and @rhs.assigned?
end

#eachObject



65
66
67
68
69
# File 'lib/BinaryConstraint.rb', line 65

def each
    [ @lhs, @rhs ].each { |variable|
	yield variable
    }
end

#holds?Boolean

Checks whether the relation returns true when called with the values assigned to left hand side and right hand side.

Returns:

  • (Boolean)


35
36
37
38
39
40
41
# File 'lib/BinaryConstraint.rb', line 35

def holds?
    if allAssigned?
	@relation.call(@lhs.value, @rhs.value)
    else
	return true
    end
end

#include?(variable) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/BinaryConstraint.rb', line 47

def include?(variable)
    (@lhs == variable) or (@rhs == variable)
end

#reviseObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/BinaryConstraint.rb', line 71

def revise
    checks = 0
    revisedVariables = Array.new
    wipeout = false
    return revisedVariables, checks, wipeout if allAssigned?
    assigned = [ @lhs, @rhs ].find { |var| var.assigned? }
    if assigned.nil?
	pruneListLeft = Set.new
	pruneListRight = Set.new(@rhs.values)
	@lhs.domain.each { |lvalue|
	    @lhs.value = lvalue
	    pruneList, thisChecks = findSupport(@lhs, @rhs)
	    checks += thisChecks
	    if pruneList.size == @rhs.values.size
		pruneListLeft << lvalue
	    else
		pruneListRight &= pruneList
	    end
	}
	@lhs.reset
	unless pruneListLeft.empty?
	    begin
		@lhs.domain.prune(pruneListLeft)
	    rescue DomainWipeoutException
		wipeout = true
	    end
	    revisedVariables << @lhs
	end
	unless pruneListRight.empty?
	    begin
		@rhs.domain.prune(pruneListRight)
	    rescue DomainWipeoutException
		wipeout = true
	    end
	    revisedVariables << @rhs
	end
    else
	unassigned = ([ @lhs, @rhs ] - [ assigned ]).first
	pruneList, checks = findSupport(assigned, unassigned)
	unless pruneList.empty?
	    begin
		unassigned.domain.prune(pruneList)
	    rescue DomainWipeoutException
		wipeout = true
	    end
	    revisedVariables << unassigned
	end
    end
    return revisedVariables, checks, wipeout
end

#to_sObject



51
52
53
# File 'lib/BinaryConstraint.rb', line 51

def to_s
    @lhs.name + " " + @relation.to_s + " " + @rhs.name
end

#to_s_fullObject



55
56
57
# File 'lib/BinaryConstraint.rb', line 55

def to_s_full
    @lhs.to_s + " " + @relation.to_s + " " + @rhs.to_s
end