How to use the simpleai.search.models.SearchNode function in simpleai

To help you get started, we’ve selected a few simpleai examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github simpleai-team / simpleai / tests / search / test_local.py View on Github external
def test_solution_is_node(self):
        node = genetic(self.problem, iterations_limit=1, mutation_chance=0, population_size=1)
        self.assertIsInstance(node, SearchNode)
github simpleai-team / simpleai / tests / search / test_models.py View on Github external
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)
github simpleai-team / simpleai / tests / search / test_models.py View on Github external
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)
github simpleai-team / simpleai / tests / search / test_models.py View on Github external
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)
github simpleai-team / simpleai / tests / search / test_models.py View on Github external
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)
github simpleai-team / simpleai / tests / search / test_models.py View on Github external
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)
github simpleai-team / simpleai / simpleai / search / traditional.py View on Github external
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))
github simpleai-team / simpleai / simpleai / search / models.py View on Github external
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
github simpleai-team / simpleai / simpleai / search / models.py View on Github external
def __eq__(self, other):
        return isinstance(other, SearchNode) and self.state == other.state
github simpleai-team / simpleai / simpleai / search / models.py View on Github external
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):