Class ConstraintSolver::AllDifferentConstraint
In: lib/AllDifferentConstraint.rb
Parent: AbstractConstraint
Test::Unit::TestCase AllDifferentConstraintTest SolutionTest ConstraintSolverTest ConstraintListTest VariableTest DomainTest BinaryConstraintTest ProblemTest BinaryRelationTest Exception DomainWipeoutException UndoStackEmptyException AbstractConstraint BinaryConstraint AllDifferentConstraint Array ConstraintList BinaryRelation Variable Solution ConstraintSolver Problem Domain test/DomainTest.rb test/SolutionTest.rb lib/BinaryConstraint.rb lib/Variable.rb test/ConstraintListTest.rb lib/ConstraintList.rb test/ProblemTest.rb lib/Solution.rb test/BinaryConstraintTest.rb lib/ConstraintSolver.rb test/VariableTest.rb test/AllDifferentConstraintTest.rb lib/AllDifferentConstraint.rb lib/Problem.rb test/ConstraintSolverTest.rb lib/Domain.rb lib/AbstractConstraint.rb ConstraintSolver dot/m_19_0.png

Represents the all different constraint.

Methods

==   allAssigned?   each   holds?   include?   new   revise   to_s   to_s_full  

Attributes

variables  [R] 

Public Class methods

Initialises a new all different constraint. The constructor takes the list of variables that must be different as an argument.

[Source]

# File lib/AllDifferentConstraint.rb, line 13
        def initialize(variables)
            if variables.empty? or variables.size == 1
                raise ArgumentError, "List of variables must contain at least two elements!"
            end
            @variables = variables
        end

Public Instance methods

[Source]

# File lib/AllDifferentConstraint.rb, line 54
        def ==(constraint)
            return false unless constraint.kind_of?(AllDifferentConstraint)
            @variables == constraint.variables
        end

[Source]

# File lib/AllDifferentConstraint.rb, line 38
        def allAssigned?
            (@variables.collect { |var| var.assigned? }).foldLeft(Proc.new { |a,b| a & b })
        end

[Source]

# File lib/AllDifferentConstraint.rb, line 59
        def each
            @variables.each { |var|
                yield var
            }
        end

Checks whether all the variables have different values assigned to them.

[Source]

# File lib/AllDifferentConstraint.rb, line 22
        def holds?
            if allAssigned?
                @variables.each { |variable|
                    @variables.eachAfter(variable) { |otherVariable|
                        if variable.value == otherVariable.value
                            return false
                        end
                    }
                }
            else
                raise RuntimeError, "Cannot check whether " + self.to_s +
                    " holds because not all variables have been assigned values!"
            end
            return true
        end

[Source]

# File lib/AllDifferentConstraint.rb, line 42
        def include?(variable)
            @variables.include?(variable)
        end

[Source]

# File lib/AllDifferentConstraint.rb, line 65
        def revise
            revisedVariables = Array.new
            pruneList = Set.new
            @variables.find_all { |var| var.assigned? }.each { |assigned|
                pruneList << assigned.value
            }
            unassignedVariables = @variables.find_all { |var| not var.assigned? }
            unassignedVariables.each { |unassigned|
                unless (unassigned.domain.values & pruneList).empty?
                    unassigned.domain.prune(pruneList)
                    revisedVariables << unassigned
                end
            }
            if unassignedVariables.size >
                ((unassignedVariables.collect { |var| var.values }).foldLeft(Proc.new { |x,y| x | y })).size
                revisedVariables.each { |var| var.domain.undoPruning }
                raise DomainWipeoutException
            end
            return revisedVariables, 0
        end

[Source]

# File lib/AllDifferentConstraint.rb, line 46
        def to_s
            @variables.collect { |var| var.name }.join(" != ")
        end

[Source]

# File lib/AllDifferentConstraint.rb, line 50
        def to_s_full
            @variables.collect { |var| var.to_s }.join(" != ")
        end

[Validate]