Class ConstraintSolver::Solution
In: lib/Solution.rb
Parent: Object
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

This class represents a solution to a constraint satisfaction problem.

Methods

<=>   ==   each   new   to_s  

Attributes

merit  [R] 
variables  [R] 

Public Class methods

Initialises a new solution with a list of variables. The variables must all have values assigned to them. Optionally, a map to map domain values of variables to their merit can be spcified.

[Source]

# File lib/Solution.rb, line 11
        def initialize(variables, meritMap={})
            if not variables.kind_of?(Array)
                variables = Array.new([ variables ])
            end
            if ((variables.collect { |var| var.assigned? }).foldLeft(Proc.new { |a,b| a & b }))
                @variables = Array.new
                variables.each { |variable|
                    @variables << variable.dup
                }
            else
                raise ArgumentError, "All variables must have values assigned to them!"
            end
            @merit = 0
            @variables.each { |var|
                @merit += meritMap[var.value] ? var.merit * meritMap[var.value] : var.merit
            }
        end

Public Instance methods

[Source]

# File lib/Solution.rb, line 35
        def <=>(solution)
            @value <=> solution.value
        end

[Source]

# File lib/Solution.rb, line 39
        def ==(solution)
            return false unless solution.kind_of?(Solution)
            @variables == solution.variables and @merit == solution.merit
        end

[Source]

# File lib/Solution.rb, line 29
        def each
            @variables.each { |variable|
                yield variable
            }
        end

[Source]

# File lib/Solution.rb, line 44
        def to_s
            s = (@variables.collect { |variable|
                variable.to_s
            }).join(", ")
            s += ", merit " + merit.to_s unless merit.nil?
            return s
        end

[Validate]