Advertisement
Guest User

150p4

a guest
May 26th, 2015
422
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.42 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. __author__ = 'Please write your names, separated by commas.'
  3. __email__ = 'Please write your email addresses, separated by commas.'
  4.  
  5. from collections import deque
  6.  
  7.  
  8. def ac3(csp, arcs=None):
  9.     """Executes the AC3 or the MAC (p.218 of the textbook) algorithms.
  10.  
  11.    If the parameter 'arcs' is None, then this method executes AC3 - that is, it will check the arc consistency
  12.    for all arcs in the CSP.  Otherwise, this method starts with only the arcs present in the 'arcs' parameter
  13.    in the queue.
  14.  
  15.    Note that the current domain of each variable can be retrieved by 'variable.domains'.
  16.  
  17.    This method returns True if the arc consistency check succeeds, and False otherwise."""
  18.  
  19.     queue_arcs = deque(arcs if arcs is not None else csp.constraints.arcs())
  20.  
  21.     while not queue_arcs:
  22.         (xi, xj) = queue_arcs.pop()
  23.         print xi
  24.         print xj
  25.         if revise(csp, xi, xj):
  26.             if len(xi.domain) == 0:
  27.                 return False
  28.             for xk in csp.constraints[xi]:
  29.                 if xk != xj:
  30.                     queue_arcs.append(xk, xi)
  31.     return True
  32.    
  33.  
  34. def revise(csp, xi, xj):
  35.     # You may additionally want to implement the 'revise' method.
  36.     revised = False
  37.     for x in xi.domain:
  38.         for y in xj.domain:
  39.             if not xi.is_satisfied(x, y):
  40.                 xi.domain.remove(x)
  41.                 revised = True
  42.     return revised
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement