Пример #1
0
	def running(self):
		results = ''
		num = 1230
		block_road = 6200
		everyTime = 32
		for i in range(10):
			for car in self.data_car[num//10*i : num//10*(i+1)]:

				cross_idStart = car.startId
				cross_idEnd = car.endId
				min_path = shortest_path(self.data_cross, self.path, self.id2index[cross_idStart], self.id2index[cross_idEnd])
				road_data = list()
				for j in range(len(min_path) - 1):
					self.crossIds = min_path[j] + '_' + min_path[j + 1]
					if self.crossIds in self.use_roadNum.keys():
						self.use_roadNum[self.crossIds] += 1
					else:
						self.use_roadNum[self.crossIds] = 0
					if self.use_roadNum[self.crossIds] > block_road:
						self.path, self.id2index = floyd(self.data_road, self.data_cross, self.crossIds)
						self.use_roadNum[self.crossIds] = 0
					road_data.append(self.crossId2roadId[self.crossIds])
				road_ids = ''
				for k in range(len(road_data) - 1):
					road_ids += road_data[k].roadId + ','
				road_ids += road_data[-1].roadId
				result = str('(' + car.carId + ',' + str(car.planTime + i) + ',' + road_ids + ')')
				results += str(result) + '\n'

		long = (len(self.data_car) - num) // everyTime
		for i in range(long):
			if i + 1 == long:
				movingCars = self.data_car[everyTime * i + num:]
			else:
				movingCars = self.data_car[everyTime * i + num: everyTime * (i + 1) + num]
			for car in movingCars:
				cross_idStart = car.startId
				cross_idEnd = car.endId
				min_path = shortest_path(self.data_cross, self.path, self.id2index[cross_idStart], self.id2index[cross_idEnd])
				road_data = list()
				for j in range(len(min_path) - 1):
					self.crossIds = min_path[j] + '_' + min_path[j + 1]
					if self.crossIds in self.use_roadNum.keys():
						self.use_roadNum[self.crossIds] += 1
					else:
						self.use_roadNum[self.crossIds] = 0
					if self.use_roadNum[self.crossIds] > block_road:
						self.path, self.id2index = floyd(self.data_road, self.data_cross, self.crossIds)
						self.use_roadNum[self.crossIds] = 0
					road_data.append(self.crossId2roadId[self.crossIds])
				road_ids = ''
				for k in range(len(road_data) - 1):
					road_ids += road_data[k].roadId + ','
				road_ids += road_data[-1].roadId
				result = str('(' + car.carId + ',' + str(car.planTime + 11 + i) + ',' + road_ids + ')')
				results += str(result) + '\n'
		return results
Пример #2
0
def color_shortest(g):
    assert len(g.nodes) > 1
    path = shortest_path(g,
                         list(g.nodes().keys())[0],
                         list(g.nodes().keys())[-1])
    g.add_path(path, color="blue")
    g.add_nodes_from(path, color="blue")
Пример #3
0
def route(x, y, z, w):
    g = nx.Graph()
    start_sqaure, start_node = closest_node(x, y)
    end_square, end_node = closest_node(z, w)

    route_graph = nx.compose(start_sqaure, end_square)
    process_edges(route_graph)
    _, path = shortest_path(route_graph, start_node, end_node)
    g.add_path(path, color="blue")
    return g
Пример #4
0
def color_shortest_test():
    s1 = random.randrange(0, 400)
    s2 = random.randrange(0, 400)

    g1 = download(s1)
    g2 = download(s2)
    g = download_and_merge(g1, s2)

    start = random.choice([n for n in g1.nodes()])
    end = random.choice([n for n in g2.nodes()])

    g = download_and_merge(download(0), 399)

    g, path = shortest_path(g, 0, 200 * 200 - 1)
    g.add_path(path, color="blue")
    g.add_nodes_from(path, color="blue")
    draw_graph(g)
Пример #5
0
    new_content = []
    for line in content:
        if '>Rosalind' in line:
            new_content.append(line.strip('>'))
            new_content.append('')
        else:
            new_content[-1] += line

    dict = {}
    for i in range(len(new_content) - 1):
        if i % 2 == 0:
            dict[new_content[i]] = new_content[i + 1]

    return dict


graph = AdjacencySetGraph(8, directed=False)

fasta = read_fasta('data/overlap_graph.txt')
for tup in graph_generate(fasta, 3):
    if tup[0] != tup[1]:
        graph.add_edge(Node(tup[0]), Node(tup[1]))

graph.add_edge(Node("1"), Node("2"))
graph.add_edge(Node("1"), Node("3"))
graph.add_edge(Node("3"), Node("4"))
graph.display()

shortest_path(graph, Node("Rosalind_5028"), Node("Rosalind_3843"))
Пример #6
0
from road_map import *
from shortest_path import *
from pbft import *
from get_plot import *

G = road_map("graph.txt")
G.gen_graph()
print("User has requested for a shortest path from source to destination")
path = shortest_path(G, 7, 2)
path.get_path()

GPLOT = getPlot(G.X, G.Y, G.W)
SPLOT = getPlot(G.X, G.Y, G.W, path.edge)
pb = pbft(G, path, GPLOT, SPLOT, 7, 2, 3)

pb.controller()
Пример #7
0
    def running(self):
        results = ''
        num = 0
        block_road = 14500
        everyTime = 21
        preset_num = 0

        long = (len(self.data_car) - num) // everyTime
        for i in range(long):
            if i + 1 == long:
                movingCars = self.data_car[everyTime * i + num:]
            else:
                movingCars = self.data_car[everyTime * i + num:everyTime *
                                           (i + 1) + num]
            for car in movingCars:
                if car.maxSpeed < 8 and self.flag == 0:
                    self.flag = 1
                cross_idStart = car.startId
                cross_idEnd = car.endId
                if car.preset == 1:
                    preset_num += 1
                    if preset_num >= self.presetNum // 10:
                        min_road = self.data_preset[car.carId]
                        min_path = list()
                        last = ''
                        for road in min_road:
                            s2e = self.road_dic[road]
                            data = s2e.split('_')
                            # last = ''
                            if data[0] == car.startId:
                                min_path.append(data[0])
                                min_path.append(data[1])
                                last = data[1]
                            elif data[1] == car.startId:
                                min_path.append(data[1])
                                min_path.append(data[0])
                                last = data[0]
                            elif last == data[0]:
                                min_path.append(data[1])
                                last = data[1]
                            else:
                                min_path.append(data[0])
                                last = data[0]
                    else:
                        min_path = shortest_path(self.data_cross, self.path,
                                                 self.id2index[cross_idStart],
                                                 self.id2index[cross_idEnd])
                        self.flag2 = 1
                else:
                    min_path = shortest_path(self.data_cross, self.path,
                                             self.id2index[cross_idStart],
                                             self.id2index[cross_idEnd])
                road_data = list()
                for j in range(len(min_path) - 1):
                    self.crossIds = min_path[j] + '_' + min_path[j + 1]
                    if self.crossIds in self.use_roadNum.keys():
                        self.use_roadNum[self.crossIds] += 1
                    else:
                        self.use_roadNum[self.crossIds] = 0
                    if self.use_roadNum[self.crossIds] > block_road:
                        self.path, self.id2index = floyd(
                            self.data_road, self.data_cross, self.crossIds,
                            self.flag)
                        self.use_roadNum[self.crossIds] = 0
                    road_data.append(self.crossId2roadId[self.crossIds])
                road_ids = ''
                for k in range(len(road_data) - 1):
                    road_ids += road_data[k].roadId + ','
                road_ids += road_data[-1].roadId
                if car.preset == 1 and self.flag2 == 1:
                    result = str('(' + car.carId + ',' + str(car.planTime) +
                                 ',' + road_ids + ')')
                    self.flag2 = 0
                elif car.preset == 1:
                    continue
                else:
                    result = str('(' + car.carId + ',' +
                                 str(car.planTime + i) + ',' + road_ids + ')')
                results += str(result) + '\n'
        return results
Пример #8
0
def main():
    # Task1: User Input
    # tuple_user = (450000, 83000)
    tuple_user = (sys.argv[1], sys.argv[2])
    # Error handling
    try:
        x = float(tuple_user[0])
        y = float(tuple_user[1])
    except ValueError:
        print("The coordinate should be number, please input again: ")
        while True:
            try:
                x = float(input("x coordinate: "))
                y = float(input("y coordinate: "))
                break
            except ValueError:
                print("The coordinate should be number, please input again: ")
    pt_user = Point(x, y)

    # Test whether the user is within the box
    min_x = 430000
    max_x = 465000
    min_y = 80000
    max_y = 95000
    if pt_user.x > max_x or pt_user.x < min_x or pt_user.y > max_y or pt_user.y < min_y:
        print("The user is outside this box. ")
        sys.exit()

    # Task2: Highest Point Identification
    # Read the elevation
    ele_fp = 'F:/PycharmProjects/Material/elevation/SZ.asc'
    elevation = rasterio.open(ele_fp)
    # Create an object of the highest point
    point_highest = highest_pt(pt_user, elevation)
    pt_highest, max_ele, buffer_ele = point_highest.get_highest_pt()

    print("The coordinate of the highest point is: " + str(pt_highest.x) +
          ", " + str(pt_highest.y))
    print("The elevation of the highest point is: " + str(max_ele))

    # Task 3: Nearest Integrated Transport Network
    node_user_id = itn(pt_user.x, pt_user.y)
    print(node_user_id)
    node_highest_id = itn(pt_highest.x, pt_highest.y)
    print(node_highest_id)

    # Task 4: Shortest Path
    # Read the network
    iow_itn_json = "F:/PycharmProjects/Material/itn/solent_itn.json"
    with open(iow_itn_json, "r") as f:
        iow_itn = json.load(f)
    # Read the elevation info
    dataset = elevation
    # View the route, load the background map
    mersea_background = "F:/PycharmProjects/Material/background/raster-50k_2724246.tif"
    background = rasterio.open(str(mersea_background))
    # The start point and end point
    start = node_user_id
    end = node_highest_id
    # Create an object of the shortest path
    path_shortest = shortest_path(start, end, iow_itn, dataset)
    g_map = path_shortest.g_map()
    path = path_shortest.shortest_path(g_map)

    # Task 5: Map Plotting

    plotter = Plotter(pt_user, pt_highest, background, elevation, g_map, path,
                      iow_itn)
    plotter.visual_path()
Пример #9
0
    def running(self):
        results = ''
        num = 0
        block_road = 4500
        everyTime = 14
        # for i in range(10):
        # 	for car in self.data_car[num//10*i : num//10*(i+1)]:
        # 		# if car.preset == 1:
        # 		# 	results += self.data_preset[car.carId]
        # 		# 	continue
        # 		cross_idStart = car.startId
        # 		cross_idEnd = car.endId
        # 		if car.preset == 1:
        # 			# if(car.carId == '31330'):
        # 			# 	print('oo')
        # 			min_road = self.data_preset[car.carId]
        # 			min_path = list()
        # 			last = ''
        # 			for road in min_road:
        # 				s2e = self.road_dic[road]
        # 				data = s2e.split('_')
        # 				# last = ''
        # 				if data[0] == car.startId:
        # 					min_path.append(data[0])
        # 					min_path.append(data[1])
        # 					last = data[1]
        # 				elif data[1] == car.startId:
        # 					min_path.append(data[1])
        # 					min_path.append(data[0])
        # 					last = data[0]
        # 				elif last == data[0]:
        # 					min_path.append(data[1])
        # 					last = data[1]
        # 				else:
        # 					min_path.append(data[0])
        # 					last = data[0]
        # 		else:
        # 			min_path = shortest_path(self.data_cross, self.path, self.id2index[cross_idStart], self.id2index[cross_idEnd])
        # 		road_data = list()
        # 		# print(car.carId)
        # 		for j in range(len(min_path) - 1):
        # 			self.crossIds = min_path[j] + '_' + min_path[j + 1]
        # 			if self.crossIds in self.use_roadNum.keys():
        # 				self.use_roadNum[self.crossIds] += 1
        # 			else:
        # 				self.use_roadNum[self.crossIds] = 0
        # 			if self.use_roadNum[self.crossIds] > block_road:
        # 				self.path, self.id2index = floyd(self.data_road, self.data_cross, self.crossIds)
        # 				self.use_roadNum[self.crossIds] = 0
        # 			road_data.append(self.crossId2roadId[self.crossIds])
        # 		road_ids = ''
        # 		for k in range(len(road_data) - 1):
        # 			road_ids += road_data[k].roadId + ','
        # 		road_ids += road_data[-1].roadId
        # 		if car.preset == 1:
        # 			result = str('(' + car.carId + ',' + str(car.planTime) + ',' + road_ids + ')')
        # 		else:
        # 			result = str('(' + car.carId + ',' + str(car.planTime + i) + ',' + road_ids + ')')
        # 		results += str(result) + '\n'

        long = (len(self.data_car) - num) // everyTime
        for i in range(long):
            if i + 1 == long:
                movingCars = self.data_car[everyTime * i + num:]
            else:
                movingCars = self.data_car[everyTime * i + num:everyTime *
                                           (i + 1) + num]
            for car in movingCars:
                cross_idStart = car.startId
                cross_idEnd = car.endId
                if car.preset == 1:
                    min_road = self.data_preset[car.carId]
                    min_path = list()
                    last = ''
                    for road in min_road:
                        s2e = self.road_dic[road]
                        data = s2e.split('_')
                        # last = ''
                        if data[0] == car.startId:
                            min_path.append(data[0])
                            min_path.append(data[1])
                            last = data[1]
                        elif data[1] == car.startId:
                            min_path.append(data[1])
                            min_path.append(data[0])
                            last = data[0]
                        elif last == data[0]:
                            min_path.append(data[1])
                            last = data[1]
                        else:
                            min_path.append(data[0])
                            last = data[0]
                else:
                    min_path = shortest_path(self.data_cross, self.path,
                                             self.id2index[cross_idStart],
                                             self.id2index[cross_idEnd])
                road_data = list()
                for j in range(len(min_path) - 1):
                    self.crossIds = min_path[j] + '_' + min_path[j + 1]
                    if self.crossIds in self.use_roadNum.keys():
                        self.use_roadNum[self.crossIds] += 1
                    else:
                        self.use_roadNum[self.crossIds] = 0
                    if self.use_roadNum[self.crossIds] > block_road:
                        self.path, self.id2index = floyd(
                            self.data_road, self.data_cross, self.crossIds)
                        self.use_roadNum[self.crossIds] = 0
                    road_data.append(self.crossId2roadId[self.crossIds])
                road_ids = ''
                for k in range(len(road_data) - 1):
                    road_ids += road_data[k].roadId + ','
                road_ids += road_data[-1].roadId
                if car.preset == 1:
                    result = str('(' + car.carId + ',' + str(car.planTime) +
                                 ',' + road_ids + ')')
                else:
                    result = str('(' + car.carId + ',' +
                                 str(car.planTime + i) + ',' + road_ids + ')')
                results += str(result) + '\n'
        return results