Class: ConstraintSolver::OneOfEqualsConstraint

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

Overview

Represents a one-of-equals constraint.

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#valueObject (readonly)

Returns the value of attribute value.



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

def value
  @value
end

#variablesObject (readonly)

Returns the value of attribute variables.



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

def variables
  @variables
end

#violationCostObject (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

Returns:

  • (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

#eachObject



56
57
58
59
60
# File 'lib/OneOfEqualsConstraint.rb', line 56

def each
    @variables.each { |var|
	yield var
    }
end

#holds?Boolean

Returns:

  • (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

Returns:

  • (Boolean)


38
39
40
# File 'lib/OneOfEqualsConstraint.rb', line 38

def include?(variable)
    @variables.include?(variable)
end

#reviseObject



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_sObject



42
43
44
# File 'lib/OneOfEqualsConstraint.rb', line 42

def to_s
    @variables.collect { |var| var.name }.join(" != ")
end

#to_s_fullObject



46
47
48
# File 'lib/OneOfEqualsConstraint.rb', line 46

def to_s_full
    @variables.collect { |var| var.to_s }.join(" != ")
end