def calculate_path(self, start):
        g = Dijkstra.Graph()
        nodes = {}
        i = 0
        for t in self.white_tiles:
            nodes[i] = t.id
            i += 1
        for n in self.white_tiles:
            l = -1
            vertex = {}
            for k in range(0, len(nodes)):
                if nodes[k] == n.id:
                    l = k
                    break
            for i in range(1, len(n.neighbour_ids) + 1):
                for j in range(0, len(nodes)):
                    if n.neighbour_ids[i - 1][0] == nodes[j][
                            0] and n.neighbour_ids[i - 1][1] == nodes[j][1]:
                        tmp = str(j)
                        vertex[tmp] = n.neighbour_distances[i]
                        break
            l = str(l)
            g.add_vertex(l, vertex)

        s, t = 0, 0
        for i in range(0, len(nodes)):
            if nodes[i] == start.id:
                s = str(i)
            if nodes[i] == self.target.id:
                t = str(i)

        tmp_path = g.shortest_path(s, t)
        path = []
        for v in tmp_path:
            path.append(nodes.get(int(v)))
        return path
Example #2
0
        zipcode_ID[j] = int(i['source'])
        j = j + 1
    if int(i['destination']) not in zipcode_ID.values():
        zipcode_ID[j] = int(i['destination'])
        j = j + 1
#print (zipcode_ID)
# reserve ID and map to use them again in final result:
for k in zipcode_ID:
    value = zipcode_ID[k]
    ID_zipcode[value] = k
#print (ID_zipcode)
#print ('values',zipcode_ID.values())
#--------------------------------------------------------------------

number_of_zipcodes = len(ID_zipcode)
graph = Dijkstra.Graph(number_of_zipcodes)

for i in previous_node:
    source_zipcode = int(i['source'])
    destination_zipcode = int(i['destination'])
    source_id = ID_zipcode[source_zipcode]
    destination_id = ID_zipcode[destination_zipcode]
    distance = int(i['distance'])
    graph.addEdge(source_id, destination_id, distance)
    #graph.addEdge(int(i['i']), int(i['destination']), int(i['distance']))
    #graph.addEdge(int(edg['source']), int(edg['destination']), int(edg['distance']))
    #print(edg['source'], edg['destination'], edg['distance'])

# read csv files for vehicles and request and put them in dictionary:---------------------
vehicles_file = csv.DictReader(open("vehicles_case.csv"))
vehicles = []
Example #3
0
Dijkstra.graph = Dijkstra.Graph([
    ("0", "1", d_p(targets[0], targets[1]), 0),
    ("0", "9", d_p(targets[0], targets[9]), 0),
    ("0", "10", d_p(targets[0], targets[10]), 0),
    ("1", "0", d_p(targets[1], targets[0]), 0),
    ("1", "10", d_p(targets[1], targets[10]), 2),
    ("2", "10", d_p(targets[2], targets[10]), 2),
    ("2", "3", d_p(targets[2], targets[3]), 2),
    ("3", "2", d_p(targets[3], targets[2]), 2),
    ("3", "4", d_p(targets[3], targets[4]), 2),
    ("3", "10", d_p(targets[3], targets[10]), 2),
    ("3", "11", d_p(targets[3], targets[11]), 0),
    ("4", "3", d_p(targets[4], targets[3]), 2),
    ("4", "5", d_p(targets[4], targets[5]), 2),
    ("4", "10", d_p(targets[4], targets[10]), 2),
    ("4", "11", d_p(targets[4], targets[11]), 0),
    ("4", "12", d_p(targets[4], targets[12]), 2),
    ("5", "4", d_p(targets[5], targets[4]), 2),
    ("5", "6", d_p(targets[5], targets[6]), 2),
    ("5", "11", d_p(targets[5], targets[11]), 0),
    ("5", "12", d_p(targets[5], targets[12]), 2),
    ("6", "5", d_p(targets[6], targets[5]), 2),
    ("6", "12", d_p(targets[6], targets[12]), 2),
    ("7", "8", d_p(targets[7], targets[8]), 0),
    ("7", "12", d_p(targets[7], targets[12]), 2),
    ("8", "7", d_p(targets[8], targets[7]), 0),
    ("8", "9", d_p(targets[8], targets[9]), 0),
    ("8", "12", d_p(targets[8], targets[12]), 0),
    ("9", "0", d_p(targets[9], targets[0]), 0),
    ("9", "8", d_p(targets[9], targets[8]), 0),
    ("9", "11", d_p(targets[9], targets[11]), 0),
    ("10", "0", d_p(targets[10], targets[0]), 0),
    ("10", "1", d_p(targets[10], targets[1]), 2),
    ("10", "2", d_p(targets[10], targets[2]), 2),
    ("10", "3", d_p(targets[10], targets[3]), 2),
    ("10", "4", d_p(targets[10], targets[4]), 2),
    ("11", "3", d_p(targets[11], targets[3]), 0),
    ("11", "4", d_p(targets[11], targets[4]), 0),
    ("11", "5", d_p(targets[11], targets[5]), 0),
    ("11", "9", d_p(targets[11], targets[9]), 0),
    ("12", "4", d_p(targets[12], targets[4]), 2),
    ("12", "5", d_p(targets[12], targets[5]), 2),
    ("12", "6", d_p(targets[12], targets[6]), 2),
    ("12", "7", d_p(targets[12], targets[7]), 2),
    ("12", "8", d_p(targets[12], targets[8]), 0),
])