Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_remove(self):
q = BoundedPriorityQueue(2)
a = DummyNode(1)
b = DummyNode(2)
q.append(a)
q.append(b)
q.remove(a)
self.assertEqual(len(q), 1)
self.assertIs(q[0], b)
def test_sorted_priority(self):
q = BoundedPriorityQueue()
q.append(DummyNode(3))
q.append(DummyNode(1))
q.append(DummyNode(2))
self.assertTrue(sorted_equals_pop(q))
def test_limit_works_on_append(self):
q = BoundedPriorityQueue(2)
q.append(DummyNode(1))
q.append(DummyNode(1))
q.append(DummyNode(1))
self.assertEqual(len(q), 2)
def test_limit_works_on_extend(self):
q = BoundedPriorityQueue(2)
q.extend([DummyNode(1), DummyNode(1), DummyNode(1)])
self.assertEqual(len(q), 2)
def test_pop_works_with_order(self):
q = BoundedPriorityQueue()
q.append(DummyNode(3))
q.append(DummyNode(1))
q.append(DummyNode(2))
self.assertEqual(q.pop().value, 1)
def _local_search(problem, fringe_expander, iterations_limit=0, fringe_size=1,
random_initial_states=False, stop_when_no_better=True,
viewer=None):
'''
Basic algorithm for all local search algorithms.
'''
if viewer:
viewer.event('started')
fringe = BoundedPriorityQueue(fringe_size)
if random_initial_states:
for _ in xrange(fringe_size):
s = problem.generate_random_state()
fringe.append(SearchNodeValueOrdered(state=s, problem=problem))
else:
fringe.append(SearchNodeValueOrdered(state=problem.initial_state,
problem=problem))
finish_reason = ''
iteration = 0
run = True
best = None
while run:
if viewer:
viewer.event('new_iteration', list(fringe))
def uniform_cost(problem, graph_search=False, viewer=None):
'''
Uniform cost search.
If graph_search=True, will avoid exploring repeated states.
Requires: SearchProblem.actions, SearchProblem.result,
SearchProblem.is_goal, and SearchProblem.cost.
'''
return _search(problem,
BoundedPriorityQueue(),
graph_search=graph_search,
node_factory=SearchNodeCostOrdered,
graph_replace_when_better=True,
viewer=viewer)