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_solution_is_node(self):
node = genetic(self.problem, iterations_limit=1, mutation_chance=0, population_size=1)
self.assertIsInstance(node, SearchNode)
def test_equals(self):
n1 = SearchNode(problem=self.problem, state='i')
n2 = SearchNode(problem=self.problem, state='i')
n3 = SearchNode(problem=self.problem, state='i', action='a')
n4 = SearchNode(problem=self.problem, state='ia')
self.assertTrue(n1 == n2)
self.assertTrue(n1 == n3)
self.assertFalse(n1 == n4)
def test_path(self):
n1 = SearchNode(problem=self.problem, state='i')
n2 = SearchNode(action='a', state='ia', parent=n1)
n3 = SearchNode(action='b', state='iab', parent=n2)
path = [(None, 'i'), ('a', 'ia'), ('b', 'iab')]
self.assertEqual(n3.path(), path)
def test_path(self):
n1 = SearchNode(problem=self.problem, state='i')
n2 = SearchNode(action='a', state='ia', parent=n1)
n3 = SearchNode(action='b', state='iab', parent=n2)
path = [(None, 'i'), ('a', 'ia'), ('b', 'iab')]
self.assertEqual(n3.path(), path)
def test_equals(self):
n1 = SearchNode(problem=self.problem, state='i')
n2 = SearchNode(problem=self.problem, state='i')
n3 = SearchNode(problem=self.problem, state='i', action='a')
n4 = SearchNode(problem=self.problem, state='ia')
self.assertTrue(n1 == n2)
self.assertTrue(n1 == n3)
self.assertFalse(n1 == n4)
def test_equals(self):
n1 = SearchNode(problem=self.problem, state='i')
n2 = SearchNode(problem=self.problem, state='i')
n3 = SearchNode(problem=self.problem, state='i', action='a')
n4 = SearchNode(problem=self.problem, state='ia')
self.assertTrue(n1 == n2)
self.assertTrue(n1 == n3)
self.assertFalse(n1 == n4)
def _search(problem, fringe, graph_search=False, depth_limit=None,
node_factory=SearchNode, graph_replace_when_better=False,
viewer=None):
'''
Basic search algorithm, base of all the other search algorithms.
'''
if viewer:
viewer.event('started')
memory = {}
initial_node = node_factory(state=problem.initial_state,
problem=problem)
fringe.append(initial_node)
memory[problem.initial_state] = initial_node
while fringe:
if viewer:
viewer.event('new_iteration', list(fringe))
def __lt__(self, other):
return self.cost < other.cost
class SearchNodeValueOrdered(SearchNode):
def __init__(self, *args, **kwargs):
super(SearchNodeValueOrdered, self).__init__(*args, **kwargs)
self.value = self.problem.value(self.state)
def __lt__(self, other):
# value must work inverted, because heapq sorts 1-9
# and we need 9-1 sorting
return -self.value < -other.value
class SearchNodeHeuristicOrdered(SearchNode):
def __init__(self, *args, **kwargs):
super(SearchNodeHeuristicOrdered, self).__init__(*args, **kwargs)
self.heuristic = self.problem.heuristic(self.state)
def __lt__(self, other):
return self.heuristic < other.heuristic
class SearchNodeStarOrdered(SearchNodeHeuristicOrdered):
def __lt__(self, other):
return self.heuristic + self.cost < other.heuristic + other.cost
class CspProblem(object):
def __init__(self, variables, domains, constraints):
self.variables = variables
def __eq__(self, other):
return isinstance(other, SearchNode) and self.state == other.state
def __hash__(self):
return hash((
self.state,
self.parent,
self.action,
self.cost,
self.depth,
))
class SearchNodeCostOrdered(SearchNode):
def __lt__(self, other):
return self.cost < other.cost
class SearchNodeValueOrdered(SearchNode):
def __init__(self, *args, **kwargs):
super(SearchNodeValueOrdered, self).__init__(*args, **kwargs)
self.value = self.problem.value(self.state)
def __lt__(self, other):
# value must work inverted, because heapq sorts 1-9
# and we need 9-1 sorting
return -self.value < -other.value
class SearchNodeHeuristicOrdered(SearchNode):
def __init__(self, *args, **kwargs):
super(SearchNodeHeuristicOrdered, self).__init__(*args, **kwargs)
self.heuristic = self.problem.heuristic(self.state)
def __lt__(self, other):