Advertisement
lameski

Untitled

May 24th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. def depth_limited_search_IDA(problem, h = None, limit=0):
  2. "depth first search with limited depth"
  3. h = memoize(h or problem.h, 'h')
  4. def recursive_dls(node,problem,limit):
  5. (result,nlimit) = recursive_dls_pom(node,problem,limit)
  6. if(result=='cutoff'):
  7. return recursive_dls(node,problem,nlimit)
  8. return result
  9.  
  10. def recursive_dls_pom(node, problem, limit):
  11. "helper function for depth limited"
  12.  
  13.  
  14. if problem.goal_test(node.state):
  15. return (node,limit)
  16. else:
  17. if(h(node)+node.path_cost <= limit):
  18. list = node.expand(problem)
  19. #init with max integer number
  20. minlimit = sys.maxsize
  21. for successor in list: #[lista for lista in list if lista.path_cost + h(lista) <= limit]:
  22. (result,limitt) = recursive_dls_pom(successor, problem, limit)
  23. if(limitt<minlimit):
  24. minlimit=limitt
  25. #if result == 'cutoff':
  26. # cutoff_occurred = True
  27. #if result > limit:
  28. # limit = result.path_cost + h(result)
  29.  
  30. #recursive_dls(result, problem, result.path_cost + h(result))
  31. if result != None:
  32. return (result,minlimit)
  33. return ('cutoff',minlimit)
  34. else:
  35. return (None,h(node)+node.path_cost)
  36.  
  37. #if cutoff_occurred:
  38. # return 'cutoff'
  39. #else:
  40. # return None
  41.  
  42. # Body of depth_limited_search:
  43. return recursive_dls(Node(problem.initial), problem, limit)
  44.  
  45. def idastar_search(problem, h=None):
  46. h = memoize(h or problem.h, 'h')
  47. return depth_limited_search_IDA(problem, h)
  48.  
  49.  
  50. '''
  51. #idaStar prebaruvanje
  52.  
  53. #def depth_limited_search2(problem, limit=50):
  54. # "depth first search with limited depth"
  55.  
  56. # def recursive_dls(node, problem, limit):
  57. # "helper function for depth limited"
  58. # cutoff_occurred = False
  59. # if problem.goal_test(node.state):
  60. # return node
  61. # limitot treba da e pathcost + heuristika
  62. # elif node.f >= limit:
  63. # return 'cutoff'
  64. # else:
  65. # for successor in node.expand(problem):
  66. # result = recursive_dls(successor, problem, limit)
  67. # if result == 'cutoff':
  68. # cutoff_occurred = True
  69. # elif result != None:
  70. # return result
  71. # if cutoff_occurred:
  72. # return 'cutoff'
  73. # else:
  74. # return None
  75.  
  76. # Body of depth_limited_search:
  77. # return recursive_dls(Node(problem.initial), problem, limit)
  78.  
  79. #def depth_limited(problem, h = None):
  80.  
  81. # def RBFS(problem, node, flimit):
  82. # if problem.goal_test(node.state):
  83. # return node, 0 # (The second value is immaterial)
  84. # successors = node.expand(problem)
  85. # if len(successors) == 0:
  86. # return None, infinity
  87. # for s in successors:
  88. # s.f = max(s.path_cost + h(s), node.f)
  89. # while True:
  90. # # Order by lowest f value
  91. # successors.sort(key=lambda x: x.f)
  92. # best = successors[0]
  93. # if best.f > flimit:
  94. # return None, best.f
  95. # if len(successors) > 1:
  96. # alternative = successors[1].f
  97. # else:
  98. # alternative = infinity
  99. # result, best.f = RBFS(problem, best, min(flimit, alternative))
  100. # if result is not None:
  101. # return result, best.f
  102. '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement