Exemplo n.º 1
0
def spider(beginurl,pages,downFolder):

    priQueue = PriorityQueue()              #保存待下载url的链接
    downlist = PriorityQueue()              #已下载url集合,防止重复下载
    priQueue.push((1,beginurl))
    i = 0
    while not priQueue.empty() and i <pages:
        k,url = priQueue.pop()
        if downloadUrl(i+1,url,priQueue,downlist,downFolder,beginurl):
            i+=1

    print '\nDownload',i,'pages.'
Exemplo n.º 2
0
def a_star(G, start, dest):
    """Der A-Stern-Algorithmus versucht, den optimalen Weg zwischen start und dest zu ermitteln, indem er den Weg verfolgt, der *wahrscheinlich* zum Ziel führt.
    """
    # initialize fields
    openlist = PriorityQueue()
    closedset = set()
    dist = dict()
    pred = dict()
    
    # add root-node
    openlist.push(start, 0)
    dist[start] = 0
    pred[start] = None
    
    count = 0
    while len(openlist) >= 0: # while openlist is not empty
        u = openlist.pop() # get node from openlist
        if u == dest: 
            return dist, pred, count # destination reached
        
        closedset.add(u) 

        for v in nx.neighbors(G, u): # for each neighbour of u
            if v in closedset: # v already found and checked
                continue
            # v has not been "found" yet
            count += 1

            length = G[u][v][0]['length']  # get length of edge[u][v]
            tentative_g = dist[u] + length # get length from start to v

            if v in dist and tentative_g >= dist[v]: # if has a distance and if this distance is smaller than the possible new distance - continue
                continue
            # else - v is not yet in dist[] or the possible new distance is smaller than the saved distance
            pred[v] = u # (new) predecessor of v is u
            dist[v] = tentative_g # (new) dist from start to v is tentative_g

            h = haversine(G.nodes[v]['y'], G.nodes[v]['x'], G.nodes[dest]['y'], G.nodes[dest]['x'])*1000 # get predicted distance
            f = tentative_g + h # add predicted distance to existing distance

            if openlist.is_in(v): # if v has already been discovered (= exists in the openlist) update its predicted distance
                openlist.update(v, f)
            else: # else add it with its predicted distance
                openlist.push(v, f)

    return dist, pred, count
Exemplo n.º 3
0
def dijkstra(G, p):
    """Der Dijkstra-Algorithmus bestimmt den Abstand aller Punkte von einem Startpunkt aus.
    Rückgabe ist ein dictionary, das zu jedem Punkt den Abstand und den Vorgänger enthält.
    
    Übernommen von Prof. Gawron und lediglich Benennung angepasst.
    """

    dist = dict()
    pred = dict()
    
    pq = PriorityQueue()
    
    # Initialisierung: Der Startpunkt hat Abstand 0 und keinen Vorgänger
    pq.push(p, 0)
    dist[p] = 0
    pred[p] = None
    
    count = 1
    # Wähle den Punkt mit dem kleinsten Abstand und aktualisiere von ihm aus die Abstände
    while len(pq) > 0:
        u = pq.pop()
        for v in nx.neighbors(G, u):
            count += 1 
            length = G[u][v][0]['length']
            
            if (v not in dist) or (dist[u] + length < dist[v]):
                count += 1 
                # Kürzeren Weg nach r gefunden
                dist[v] = dist[u] + length
                pred[v] = u
                pq.push(v, dist[v])
    
    return dist, pred, count
Exemplo n.º 4
0
def travel(adj_mat, src=0):
    optimal_tour = []
    n = len(adj_mat)
    if not n:
        raise ValueError("Invalid adj Matrix")
    u = Node()
    PQ = PriorityQueue()
    optimal_length = 0
    v = Node(level=0, path=[0])
    min_length = float('inf')  # infinity
    v.bound = bound(adj_mat, v)
    PQ.put(v)
    while not PQ.empty():
        v = PQ.get()
        if v.bound < min_length:
            u.level = v.level + 1
            for i in filter(lambda x: x not in v.path, range(1, n)):
                u.path = v.path[:]
                u.path.append(i)
                if u.level == n - 2:
                    l = set(range(1, n)) - set(u.path)
                    u.path.append(list(l)[0])
                    # putting the first vertex at last
                    u.path.append(0)

                    _len = length(adj_mat, u)
                    if _len < min_length:
                        min_length = _len
                        optimal_length = _len
                        optimal_tour = u.path[:]

                else:
                    u.bound = bound(adj_mat, u)
                    if u.bound < min_length:
                        PQ.put(u)
                # make a new node at each iteration! python it is!!
                u = Node(level=u.level)

    # shifting to proper source(start of path)
    optimal_tour_src = optimal_tour
    if src is not 1:
        optimal_tour_src = optimal_tour[:-1]
        y = optimal_tour_src.index(src)
        optimal_tour_src = optimal_tour_src[y:] + optimal_tour_src[:y]
        optimal_tour_src.append(optimal_tour_src[0])

    return optimal_tour_src, optimal_length
def greenTravel(adj_mat, src):
    optimal_tour = []
    n = len(adj_mat)

    u = Node()
    PQ = PriorityQueue()
    optimal_length = 0
    v = Node(level=0, path=[0])
    min_length = float('inf')
    v.bound = bound(adj_mat, v)

    PQ.put(v)
    while not PQ.empty():
        v = PQ.get()
        if v.bound < min_length:
            u.level = v.level + 1
            for i in filter(lambda x: x not in v.path, range(1, n)):
                u.path = v.path[:]
                u.path.append(i)
                if u.level == n - 2:
                    l = set(range(1, n)) - set(u.path)
                    u.path.append(list(l)[0])

                    u.path.append(0)

                    _len = length(adj_mat, u)
                    if _len < min_length:
                        min_length = _len
                        optimal_length = _len
                        optimal_tour = u.path[:]

                else:
                    u.bound = bound(adj_mat, u)
                    if u.bound < min_length:
                        PQ.put(u)
                u = Node(level=u.level)

    optimal_tour_src = optimal_tour
    if src is not 1:
        optimal_tour_src = optimal_tour[:-1]
        y = optimal_tour_src.index(src)
        optimal_tour_src = optimal_tour_src[y:] + optimal_tour_src[:y]
        optimal_tour_src.append(optimal_tour_src[0])

    return optimal_tour_src, optimal_length
Exemplo n.º 6
0
def travel(adj_mat, src=0):
    optimal_tour = []
    n = len(adj_mat)
    if not n:
        raise ValueError("Invalid adj Matrix")
    u = Node()
    PQ = PriorityQueue()
    optimal_length = 0
    v = Node(level=0, path=[0])
    min_length = float('inf')  # infinity
    v.bound = bound(adj_mat, v)
    PQ.put(v)
    while not PQ.empty():
        v = PQ.get()
        if v.bound < min_length:
            u.level = v.level + 1
            for i in filter(lambda x: x not in v.path, range(1, n)):
                u.path = v.path[:]
                u.path.append(i)
                if u.level == n - 2:
                    l = set(range(1, n)) - set(u.path)
                    u.path.append(list(l)[0])
                    # putting the first vertex at last
                    u.path.append(0)

                    _len = length(adj_mat, u)
                    if _len < min_length:
                        min_length = _len
                        optimal_length = _len
                        optimal_tour = u.path[:]

                else:
                    u.bound = bound(adj_mat, u)
                    if u.bound < min_length:
                        PQ.put(u)
                # make a new node at each iteration! python it is!!
                u = Node(level=u.level)

    # shifting to proper source(start of path)
    optimal_tour_src = optimal_tour
    if src is not 1:
        optimal_tour_src = optimal_tour[:-1]
        y = optimal_tour_src.index(src)
        optimal_tour_src = optimal_tour_src[y:] + optimal_tour_src[:y]
        optimal_tour_src.append(optimal_tour_src[0])

    return optimal_tour_src, optimal_length
Exemplo n.º 7
0
def travel(adj_mat, src=0):

  tailleMTsp = np.shape(adj_mat)[0]
  if tailleMTsp == 0:
    #print ("Impossible: Pas de colis choisis")
    optimal_tour_src = 0
    optimal_length = 0

  elif tailleMTsp == 2:
    optimal_tour_src = [0, 1, 0]
    optimal_length = adj_mat[0][1]


  else:
    optimal_tour = []
    n = len(adj_mat)
    if not n:
      raise ValueError("Invalid adj Matrix")
    u = Node1()
    PQ = PriorityQueue()
    optimal_length = 0
    v = Node1(level=0, path=[0])
    min_length = float('inf')  # infinity
    v.bound = bound(adj_mat, v)
    PQ.put(v)
    while not PQ.empty():
      v = PQ.get()
      if v.bound < min_length:
        u.level = v.level + 1
        for i in filter(lambda x: x not in v.path, range(1, n)):
          u.path = v.path[:]
          u.path.append(i)
          if u.level == n - 2:
            l = set(range(1, n)) - set(u.path)
            u.path.append(list(l)[0])
            # putting the first vertex at last
            u.path.append(0)

            _len = length(adj_mat, u)
            if _len < min_length:
              min_length = _len
              optimal_length = _len
              optimal_tour = u.path[:]

          else:
            u.bound = bound(adj_mat, u)
            if u.bound < min_length:
              PQ.put(u)
          # make a new node at each iteration! python it is!!
          u = Node1(level=u.level)

    # shifting to proper source(start of path)
    optimal_tour_src = optimal_tour
    if src is not 1:
      optimal_tour_src = optimal_tour[:-1]
      y = optimal_tour_src.index(src)
      optimal_tour_src = optimal_tour_src[y:] + optimal_tour_src[:y]
      optimal_tour_src.append(optimal_tour_src[0])
      # optimal_length
    # on met le résultat dans le format json afin de pouvoir l'enregistré

  return optimal_tour_src, optimal_length