コード例 #1
0
ファイル: solver.py プロジェクト: styts/snakes
    def _populate_graph_bfs(gr,root,points,quit_on_first=False):
        # BFS algo from http://en.wikipedia.org/wiki/Breadth-first_search
        from Queue import Queue
        q = Queue()

        s = State()
        s.load_from_json(root)

        q.put(s)

        h = hashlib.md5(root).hexdigest()
        gr.graph['visited'].append(h)

        while not q.empty():
            # debug output
            o = gr.order(); p0 = points[0]
            if o >= p0:
                points.remove(p0) # so it's only called once
                print "Order >= %s, Visited: %s, Finals: %s" % (p0, len(gr.graph['visited']), len(gr.graph['finals']))

            s = q.get()
            hs = hashlib.md5(s.to_json()).hexdigest()
            gr.add_nodes_from([hs])

            if s.is_complete():
                gr.graph['finals'].append(hs)

            for ns in s.get_neighbour_states():
                hns = hashlib.md5(ns.to_json()).hexdigest()
                if not gr.has_node(hns):
                    gr.add_nodes_from([hns])
                gr.add_edge(hs,hns)
                if hns not in gr.graph['visited']:
                    gr.graph['visited'].append(hns)
                    q.put(ns)

            if len(gr.graph['finals']) and quit_on_first:
                break # found first
コード例 #2
0
ファイル: solver.py プロジェクト: styts/snakes
    def _populate_graph(gr,root,depth=0,max_depth=0,found_new=0,has_one=False,debug_info=None,quit_on_first=None):
        """Recursively called"""
        global points

        sxp_digest = hashlib.md5(root).hexdigest()
        if sxp_digest in gr.graph['visited']:
            return -3
        gr.graph['visited'].append(sxp_digest)


        o = gr.order(); p0 = points[0]
        if o >= p0:
            points.remove(p0) # so it's only called once
            print "Order >= %s, Visited: %s, Finals: %s" % (p0, len(gr.graph['visited']), len(gr.graph['finals']))

        if max_depth and depth >= max_depth:
            return 3

        # its a try-catch so that no 'video system not initalized' error comes when running in console mode
        try:
            # process message queue
            pygame.event.pump()
        except:
            pass

        found_new = 0
        s = State()
        s.load_from_json(root)
        nss = s.get_neighbour_states()
        nssx = []
        completed = []
        for ns in nss:
            j = ns.to_json()
            nssx.append(j)
            if ns.is_complete():
                completed.append(j)
            ns.release()
        del nss
        s.release()
        del s

        Solver._attach_debugs(debug_info,gr,depth=depth)

        for nsx in nssx:
            digest = hashlib.md5(nsx).hexdigest()
            if not gr.has_node(digest):
                gr.add_nodes_from([digest])
                found_new = found_new + 1
                if nsx in completed:
                    gr.node[digest]['complete'] = True
                    has_one = True
                    gr.graph['finals'].append(digest)
                # else: # once again, drawing will fail if we set this.
                #     gr.node[digest] = None # help with memory? otehrwsise a {} is stored there

            if not gr.has_edge(sxp_digest,digest):
                gr.add_edge(sxp_digest,digest) # attr_dict={} by default. memory. and otherwise drawing does not work
            if has_one and quit_on_first:
                return 42

        for nsx in nssx:
            if not found_new: # if no new neighbours were found during the previous iteration
                return 7
            if quit_on_first and len(gr.graph['finals']) and not max_depth:
                return 8
            Solver._populate_graph(gr,nsx,depth=depth+1,max_depth=max_depth,found_new=found_new,debug_info=debug_info,quit_on_first=quit_on_first) # recurse