Ejemplo n.º 1
0
 def printClosedList(self):
     for i in self.closeList:
         print(i,
               Node.getNodeById(i).name,
               Node.getNodeById(i).f,
               Node.getNodeById(i).g,
               Node.getNodeById(i).h)
Ejemplo n.º 2
0
 def backtrack(self):
     for i in self._dest_node.path:
         print('->', Node.getNodeById(i).name)
     for i in range(len(self._dest_node.path) - 1):
         pid = self._dest_node.path[i]
         cid = self._dest_node.path[i + 1]
         parent = Node.getNodeById(pid)
         child = Node.getNodeById(cid)
         self.distance += float(parent.child[cid]['dist'])
         self.time += float(parent.child[cid]['duration'])
         print('From {0} to {1} is {2} kms and takes {3} mins'.format(
             parent.name, child.name, parent.child[cid]['dist'],
             parent.child[cid]['duration']))
     print('total time', self.time, 'mins')
     print('total distance', self.distance, 'kms')
Ejemplo n.º 3
0
def fetchDetails(nodes, dist_duration):
    global APIs, child_count
    for _, node in enumerate(nodes):
        print("{0:.2%}, {1} {2}".format(_ / len(nodes), node.name,
                                        len(node.child)))
        chi = node.getChildIds()
        for c in chi:
            for attempt in range(len(APIs)):
                try:
                    if node.child[c]['dist'] == 0:
                        C = Node.getNodeById(c)
                        d.setCoord(node.lat, node.lon, C.lat, C.lon)
                        dist, duration = d.fetch()
                        dist_duration.append([node.id, c, dist, duration])
                        print('->', C.id, C.name, node.child[c]['edist'], dist,
                              duration)
                except:
                    print('Error in {}, Switching Api Keys'.format(c))
                    time.sleep(2)
                    d.setAPI(APIs[0])
                    usedApi = APIs.pop(0)
                    APIs.append(usedApi)
                    continue
                break
    return dist_duration
Ejemplo n.º 4
0
def updateGraph(dist_duration):
    global nodes
    for pair in dist_duration:
        parent_id, child_id, dist, duration = pair
        parent_node = Node.getNodeById(parent_id)
        dist = float(re.sub('[^0-9.]', '', dist).strip())
        duration = float(re.sub('[^0-9.]', '', duration).strip())
        parent_node.update(child_id, dist, duration)
Ejemplo n.º 5
0
def printGraph():
    global nodes
    for node in nodes:
        print(node.id, node.name, len(node.child))
        chi = node.getChildIds()
        for c in chi:
            C = Node.getNodeById(c)
            print('->', C.id, C.name, node.child[c]['edist'],
                  node.child[c]['dist'], node.child[c]['duration'])
Ejemplo n.º 6
0
 def drawMap(self):
     lat = []
     lon = []
     loc = []
     for i in self._dest_node.path:
         lat.append(Node.getNodeById(i).lat)
         lon.append(Node.getNodeById(i).lon)
         loc.append(Node.getNodeById(i).name)
     gmap = gmplot.GoogleMapPlotter((lat[0] + lat[-1]) / 2,
                                    (lon[0] + lon[-1]) / 2, 12)
     gmap.plot(lat, lon, 'cornflowerblue', marker=False, edge_width=7)
     # gmap.scatter(lat, lon, '#4444aa', size=180, marker=False)
     for i in range(len(lat)):
         gmap.marker(lat[i], lon[i], '#FF0000', c=None, title=loc[i])
     # gmap.scatter(lat, lon, '#FF0000', size=60, marker=True, c=None, s=None, title=loc)
     gmap.draw('map.html')
     data = ''
     with open('map.html', 'r') as file:
         data = file.read()
     data = data.replace('\\', '/')
     with open('map.html', 'w') as file:
         file.write(data)
Ejemplo n.º 7
0
    def findRoute(self):
        while (len(self.openList.keys()) != 0):
            templist = sorted(self.openList.items(),
                              key=operator.itemgetter(1))
            pid = templist[0][0]
            self.openList.pop(pid)
            parentNode = Node.getNodeById(pid)
            children = parentNode.getChildIds()
            for cid in children:
                c_node = Node.getNodeById(cid)
                if cid == self._dest_id:
                    print('found')
                    del c_node.path[:]
                    for i in parentNode.path:
                        c_node.path.append(i)
                    c_node.path.append(cid)
                    self.openList.clear()
                    self.closeList[self._src_id] = self._src_node.f
                    break
                h_dist = self.heuristic(cid, self._dest_id)
                g_dist = parentNode.g + Node.actualDist(pid, cid)
                if cid in self.openList.keys():
                    if (g_dist < c_node.g):
                        c_node.hgf(h_dist, g_dist)
                        self.openList[cid] = c_node.f
                elif cid in self.closeList.keys():
                    if (g_dist < c_node.g):
                        _ = self.closeList.pop(cid)
                        c_node.hgf(h_dist, g_dist)
                        self.openList[cid] = Node.getNodeById(cid).f
                else:
                    c_node.hgf(h_dist, g_dist)

                    for i in parentNode.path:
                        c_node.path.append(i)
                    c_node.path.append(cid)
                    self.openList[cid] = Node.getNodeById(cid).f
            self.closeList[pid] = Node.getNodeById(pid).f
Ejemplo n.º 8
0
 def heuristic(self, n1_id, n2_id):
     n1 = Node.getNodeById(n1_id)
     n2 = Node.getNodeById(n2_id)
     return self.eucl.eucl(n1.lat, n1.lon, n2.lat, n2.lon)