Advertisement
Guest User

DANKE TRUMP

a guest
Jan 22nd, 2017
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.63 KB | None | 0 0
  1. def mrv(csp):
  2.     counter = 0
  3.     refvariable = csp.variables[0], 0
  4.     for var in csp.variables:
  5.         if var.get_value() is None:
  6.             for peer in var.peers:
  7.                 if peer.get_value() is not None:
  8.                     counter += 1
  9.                     print peer
  10.             variable = var, counter
  11.             counter = 0
  12.             if variable[1] > refvariable[1]:
  13.                 refvariable = variable
  14.  
  15.     print refvariable
  16.     return refvariable
  17.  
  18. def backtracking(csp, ac_3=False):
  19.     """
  20.    Implement the basic backtracking algorithm to solve a CSP.
  21.  
  22.    Optional: if ac_3 == True use the AC-3 algorithm for constraint
  23.              propagation. Important: if ac_3 == false don't use
  24.              AC-3!
  25.    :param csp: A csp.ConstrainedSatisfactionProblem object
  26.                representing the CSP to solve
  27.    :return: A csp.ConstrainedSatisfactionProblem, where all Variables
  28.             are set and csp.complete() returns True. (I.e. the solved
  29.             CSP)
  30.    """
  31.     variable = csp.variables[0]
  32.     if csp.complete():
  33.         return csp
  34.     for var in csp.variables:
  35.         if var.get_value() is None:
  36.             variable = var
  37.     for value in variable.domain:
  38.         variable.set_value(value)
  39.         if csp.consistent():
  40.             result = backtracking(csp)
  41.             if csp.consistent():
  42.                 return result
  43.             variable.set_value(None)
  44.  
  45.  
  46. def minimum_remaining_values(csp, ac_3=False):
  47.     """
  48.    Implement the basic backtracking algorithm to solve a CSP with
  49.    minimum remaining values heuristic and no tie-breaker. Thus the
  50.    first of all best solution is taken.
  51.  
  52.    Optional: if ac_3 == True use the AC-3 algorithm for constraint
  53.              propagation. Important: if ac_3 == false don't use
  54.              AC-3!
  55.    :param csp: A csp.ConstrainedSatisfactionProblem object
  56.                representing the CSP to solve
  57.    :return: A tuple of 1) a csp.ConstrainedSatisfactionProblem, where
  58.             all Variables are set and csp.complete() returns True. (I.e.
  59.             the solved CSP) and 2) a list of all variables in the order
  60.             they have been assigned.
  61.    """
  62.     return min_rem_rec(csp, [])
  63.  
  64.  
  65. def min_rem_rec(csp, varlist):
  66.     if csp.complete():
  67.         return csp, varlist
  68.     variable,_ = mrv(csp)
  69.     varlist.append(variable)
  70.     print varlist
  71.     for value in variable.domain:
  72.         variable.set_value(value)
  73.         if csp.consistent():
  74.             result, varlist = min_rem_rec(csp, varlist)
  75.             if csp.consistent():
  76.                 return result, varlist
  77.             variable.set_value(None)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement