Module constraint :: Class Problem
[show private | hide private]
[frames | no frames]

Type Problem

object --+
         |
        Problem


Class used to define a problem and retrieve solutions
Method Summary
  __init__(self, solver)
  addConstraint(self, constraint, variables)
Add a constraint to the problem
  addVariable(self, variable, domain)
Add a variable to the problem
  addVariables(self, variables, domain)
Add one or more variables to the problem
dictionary mapping variables to values getSolution(self)
Find and return a solution to the problem
  getSolutionIter(self)
Return an iterator to the solutions of the problem
list of dictionaries mapping variables to values getSolutions(self)
Find and return all solutions to the problem
instance of a Solver subclass getSolver(self)
Obtain the problem solver currently in use
  reset(self)
Reset the current problem definition
  setSolver(self, solver)
Change the problem solver currently in use
    Inherited from object
  __delattr__(...)
x.__delattr__('name') <==> del x.name
  __getattribute__(...)
x.__getattribute__('name') <==> x.name
  __hash__(x)
x.__hash__() <==> hash(x)
  __new__(T, S, ...)
T.__new__(S, ...) -> a new object with type S, a subtype of T
  __reduce__(...)
helper for pickle
  __reduce_ex__(...)
helper for pickle
  __repr__(x)
x.__repr__() <==> repr(x)
  __setattr__(...)
x.__setattr__('name', value) <==> x.name = value
  __str__(x)
x.__str__() <==> str(x)

Method Details

__init__(self, solver=None)
(Constructor)

Parameters:
solver - Problem solver used to find solutions (default is BacktrackingSolver)
           (type=instance of a Solver subclass)
Overrides:
__builtin__.object.__init__

addConstraint(self, constraint, variables=None)

Add a constraint to the problem

Example:
>>> problem = Problem()
>>> problem.addVariables(["a", "b"], [1, 2, 3])
>>> problem.addConstraint(lambda a, b: b == a+1, ["a", "b"])
>>> solutions = problem.getSolutions()
>>>
Parameters:
constraint - Constraint to be included in the problem
           (type=instance a Constraint subclass or a function to be wrapped by FunctionConstraint)
variables - Variables affected by the constraint (default to all variables). Depending on the constraint type the order may be important.
           (type=set or sequence of variables)

addVariable(self, variable, domain)

Add a variable to the problem

Example:
>>> problem = Problem()
>>> problem.addVariable("a", [1, 2])
>>> problem.getSolution() in ({'a': 1}, {'a': 2})
True
Parameters:
variable - Object representing a problem variable
           (type=hashable object)
domain - Set of items defining the possible values that the given variable may assume
           (type=list, tuple, or instance of Domain)

addVariables(self, variables, domain)

Add one or more variables to the problem

Example:
>>> problem = Problem()
>>> problem.addVariables(["a", "b"], [1, 2, 3])
>>> solutions = problem.getSolutions()
>>> len(solutions)
9

>>> {'a': 3, 'b': 1} in solutions
True
Parameters:
variables - Any object containing a sequence of objects represeting problem variables
           (type=sequence of hashable objects)
domain - Set of items defining the possible values that the given variables may assume
           (type=list, tuple, or instance of Domain)

getSolution(self)

Find and return a solution to the problem

Example:
>>> problem = Problem()
>>> problem.getSolution() is None
True

>>> problem.addVariables(["a"], [42])
>>> problem.getSolution()
{'a': 42}
Returns:
Solution for the problem
           (type=dictionary mapping variables to values)

getSolutionIter(self)

Return an iterator to the solutions of the problem

Example:
>>> problem = Problem()
>>> list(problem.getSolutionIter()) == []
True

>>> problem.addVariables(["a"], [42])
>>> iter = problem.getSolutionIter()
>>> iter.next()
{'a': 42}

>>> iter.next()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
StopIteration

getSolutions(self)

Find and return all solutions to the problem

Example:
>>> problem = Problem()
>>> problem.getSolutions() == []
True

>>> problem.addVariables(["a"], [42])
>>> problem.getSolutions()
[{'a': 42}]
Returns:
All solutions for the problem
           (type=list of dictionaries mapping variables to values)

getSolver(self)

Obtain the problem solver currently in use

Example:
>>> solver = BacktrackingSolver()
>>> problem = Problem(solver)
>>> problem.getSolver() is solver
True
Returns:
Solver currently in use
           (type=instance of a Solver subclass)

reset(self)

Reset the current problem definition

Example:
>>> problem = Problem()
>>> problem.addVariable("a", [1, 2])
>>> problem.reset()
>>> problem.getSolution()
>>>

setSolver(self, solver)

Change the problem solver currently in use

Example:
>>> solver = BacktrackingSolver()
>>> problem = Problem(solver)
>>> problem.getSolver() is solver
True
Parameters:
solver - New problem solver
           (type=instance of a Solver subclass)

Generated by Epydoc 2.1 on Thu Jul 7 02:05:09 2005 http://epydoc.sf.net