def generic_postman(postman_type): """ Runs the CPP or RPP algorithm, prints solution, saves visualizations to filesystem Args: postman_type (str): "rural" or "chinese" """ if postman_type == 'rural': postman_algo = rpp elif postman_type == 'chinese': postman_algo = cpp # get args args = get_args() # setup logging logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) logger.info('Solving the {} postman problem..'.format(postman_type)) circuit, graph = postman_algo(edgelist_filename=args.edgelist, start_node=args.start_node, edge_weight=args.edge_weight) logger.info('Solution:') for edge in circuit: logger.info(edge) logger.info('Solution summary stats:') for k, v in calculate_postman_solution_stats(circuit).items(): logger.info(str(k) + ' : ' + str(v)) if args.viz: logger.info('Creating single image of {} postman solution...'.format(postman_type)) message_static = plot_circuit_graphviz(circuit=circuit, graph=graph, filename=args.viz_filename, format=args.viz_format, engine=args.viz_engine) logger.info(message_static) if args.animation: animation_images_dir = args.animation_images_dir if args.animation_images_dir else os.path.join( os.path.dirname(args.animation_filename), 'img') logger.info('Creating individual files for animation...') message_images = make_circuit_images(circuit=circuit, graph=graph, outfile_dir=animation_images_dir, format=args.animation_format, engine=args.animation_engine) logger.info(message_images) logger.info('Creating animation...') message_animation = make_circuit_video(infile_dir_images=animation_images_dir, outfile_movie=args.animation_filename, fps=args.fps, format=args.animation_format) logger.info(message_animation)
def test_stats_on_star_graph_with_optional_edges(GRAPH_2_CIRCUIT_RPP): stats = calculate_postman_solution_stats(GRAPH_2_CIRCUIT_RPP) assert isinstance(stats, dict) assert stats['distance_walked'] == 116 assert stats['distance_doublebacked'] == 6 assert stats['distance_walked_once'] == 110 assert stats['distance_walked_required'] == 110 assert stats['distance_walked_optional'] == 6 assert stats['edges_walked'] == 6 assert stats['edges_doublebacked'] == 2 assert stats['edges_walked_once'] == 4 assert stats['edges_walked_required'] == 4 assert stats['edges_walked_optional'] == 2
def test_stats_on_simple_graph_required_edges_only(GRAPH_1_CIRCUIT_CPP): stats = calculate_postman_solution_stats(GRAPH_1_CIRCUIT_CPP) assert isinstance(stats, dict) assert stats['distance_walked'] == 45 assert stats['distance_doublebacked'] == 5 assert stats['distance_walked_once'] == 40 assert stats['distance_walked_required'] == 45 assert stats['distance_walked_optional'] == 0 assert stats['edges_walked'] == 7 assert stats['edges_doublebacked'] == 2 assert stats['edges_walked_once'] == 5 assert stats['edges_walked_required'] == 7 assert stats['edges_walked_optional'] == 0
def postman(osm_data): Settings = IngestSettings( max_distance=Distance(km=50), max_segments=300, max_concurrent=40, quality_settings=DefaultQualitySettings, location_filter=None, ) loader = OSMIngestor(Settings) loader.load_osm(osm_data, extra_links=[(885729040, 827103027)]) # s = datetime.now() # data = write_gpickle(loader.global_graph, 'test.pickle') #nx_yaml.write_yaml(loader.global_graph, 'test.yaml') # e = datetime.now() # print(e-s) # s = datetime.now() # graph = read_gpickle('test.pickle') # e = datetime.now() # print(e-s) # import pdb; pdb.set_trace() for i, network in enumerate(loader.trail_networks()): print(network.name, network.total_length().mi) print(network.trail_names()) gmap = gmplot.GoogleMapPlotter(42.385, -71.083, 13) edge_map = {} for segment in network.trail_segments(): segment.draw(gmap) edge_map[segment.id] = segment.nodes clean_name = (network.name or f'no-name-{i}').replace(' ', '').replace("\'", '') gmap.draw(f"{clean_name}-{i}.html") with open('edges.csv', 'w') as csv_file: writer = csv.DictWriter(csv_file, fieldnames=['start', 'end', 'id', 'distance']) writer.writeheader() for segment in network.trail_segments(): writer.writerow(dict(start=segment.nodes[0].id, end=segment.nodes[-1].id, id=segment.id, distance=segment.length_m())) try: s = datetime.now() circuit, graph = cpp('edges.csv') e = datetime.now() print(f'Time: {e-s}') for k, v in calculate_postman_solution_stats(circuit).items(): print(k, v) with open(f"{clean_name}-{i}.gpx", "w") as f: f.write(circuit_to_gpx(circuit, edge_map).to_xml()) except Exception as ex: print(ex)
def solvePostmanProblem(name, write=True, write_minimal=True): filename = "graphs_in_csv/" + name + ".csv" # find CPP solution circuit, graph = cpp(edgelist_filename=filename) if write: print(circuit) if write_minimal: print("\tNumber of edges walked: " + str(len(circuit))) """ # print solution route for e in circuit: print(e) """ if write: # print solution summary stats for k, v in calculate_postman_solution_stats(circuit).items(): print(k, v) createCsvFromCircuit(circuit, name)
def main(): # Connect to Sqlite3 & create table sqlite3_conn = dbfun.create_subway_sqlite3(clear_db=True) dbfun.add_stations_table_sqlite3(sqlite3_conn) dbfun.add_edges_table_sqlite3(sqlite3_conn) edgelist = './Data/Paths-Decision-Points.csv' el = ppg.read_edgelist(edgelist, keep_optional=False) g = ppg.create_networkx_graph_from_edgelist(el) odd_nodes = ppg.get_odd_nodes(g) # This for loop gets all the euler paths for every combination of start and end nodes, # saves the routes/statistics as a dictionary, and inserts it into the database for odd_node_pair in itertools.combinations(odd_nodes, 2): circuit_name = odd_node_pair[0] + ' - ' + odd_node_pair[1] path_stats = {'path': circuit_name} logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) logger.info(f'Solved CPP for {circuit_name}') # For some reason, the no_return_cpp is returning the path backwards so the end_node is passed as the start circuit, graph = no_return_cpp(edgelist, odd_node_pair[1], odd_node_pair[0]) # Formats the route and adds it to the dictionary along with the other stats route = '-'.join([edge[0] for edge in circuit]) route = route + '-' + odd_node_pair[1] path_stats.update(calculate_postman_solution_stats(circuit)) path_stats['route'] = route # Inserts into Sqlite3 dbfun.insert_into_sqlite3(sqlite3_conn, path_stats) # Add rankings dbfun.add_route_ranks(sqlite3_conn)
def create_circuit(self, network_id: str, circuit_id: str): network = TrailNetwork.objects.get(id=network_id) circuit = Circuit.objects.get(id=circuit_id) circuit.status = InProgress circuit.error = "" circuit.save() try: graph = read_gpickle(BytesIO(network.graph.tobytes())) with NamedTemporaryFile(suffix='.csv', mode='w') as f: writer = csv.DictWriter( f, fieldnames=['start', 'end', 'id', 'distance']) writer.writeheader() edge_map = {} for segment in segments_for_graph(graph): writer.writerow( dict(start=segment.nodes[0].derived_id, end=segment.nodes[-1].derived_id, id=segment.id, distance=segment.length_m())) edge_map[segment.id] = segment.nodes f.flush() circuit_nodes, _ = solver.cpp(f.name) stats = calculate_postman_solution_stats(circuit_nodes) walked_twice = Distance(m=stats['distance_doublebacked']) walked_total = Distance(m=stats['distance_walked_required']) line_string = circuit_to_line_string(circuit_nodes, edge_map) circuit.route = line_string circuit.total_length = walked_total circuit.status = Complete circuit.error = "" except Exception as ex: circuit.status = Error circuit.error = ex raise finally: circuit.save()
# -*- coding: utf-8 -*- """ Created on Thu Jun 21 19:17:20 2018 @author: weing """ import postman_problems from postman_problems.solver import cpp from postman_problems.solver import rpp from postman_problems.stats import calculate_postman_solution_stats # find RPP solution circuit, graph = rpp(edgelist_filename='full-edgelist-river-required.csv', start_node='1') # print solution route for e in circuit: print(e) # print solution summary stats for k, v in calculate_postman_solution_stats(circuit).items(): print(k, v)
def main(): """Solve the RPP and save visualizations of the solution""" # PARAMS / DATA --------------------------------------------------------------------- # inputs EDGELIST = pkg_resources.resource_filename( 'postman_problems', 'examples/sleeping_giant/edgelist_sleeping_giant.csv') NODELIST = pkg_resources.resource_filename( 'postman_problems', 'examples/sleeping_giant/nodelist_sleeping_giant.csv') START_NODE = "b_end_east" # outputs GRAPH_ATTR = {'dpi': '65'} EDGE_ATTR = {'fontsize': '20'} NODE_ATTR = { 'shape': 'point', 'color': 'black', 'width': '0.1', 'fixedsize': 'true' } PNG_PATH = pkg_resources.resource_filename( 'postman_problems', 'examples/sleeping_giant/output/png/') RPP_SVG_FILENAME = pkg_resources.resource_filename( 'postman_problems', 'examples/sleeping_giant/output/rpp_graph') RPP_GIF_FILENAME = pkg_resources.resource_filename( 'postman_problems', 'examples/sleeping_giant/output/rpp_graph.gif') # setup logging logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) # SOLVE RPP ------------------------------------------------------------------------- logger.info('Solve RPP') circuit, graph = rpp(EDGELIST, START_NODE) logger.info('Print the RPP solution:') for e in circuit: logger.info(e) logger.info('Solution summary stats:') for k, v in calculate_postman_solution_stats(circuit).items(): logger.info(str(k) + ' : ' + str(v)) # VIZ ------------------------------------------------------------------------------- try: from postman_problems.viz import (add_pos_node_attribute, add_node_attributes, plot_circuit_graphviz, make_circuit_images, make_circuit_video) logger.info('Add node attributes to graph') nodelist_df = pd.read_csv(NODELIST) graph = add_node_attributes(graph, nodelist_df) # add attributes graph = add_pos_node_attribute( graph, origin='topleft') # add X,Y positions in format for graphviz logger.info('Add style edge attribute to make optional edges dotted') for e in graph.edges(data=True, keys=True): graph[e[0]][e[1]][e[2]]['style'] = 'solid' if graph[e[0]][e[1]][ e[2]]['required'] else 'dashed' logger.info('Creating single SVG of RPP solution') plot_circuit_graphviz(circuit=circuit, graph=graph, filename=RPP_SVG_FILENAME, format='svg', engine='neato', graph_attr=GRAPH_ATTR, edge_attr=EDGE_ATTR, node_attr=NODE_ATTR) logger.info('Creating PNG files for GIF') images_message = make_circuit_images(circuit=circuit, graph=graph, outfile_dir=PNG_PATH, format='png', engine='neato', graph_attr=GRAPH_ATTR, edge_attr=EDGE_ATTR, node_attr=NODE_ATTR) logger.info(images_message) logger.info('Creating GIF') video_message = make_circuit_video(infile_dir_images=PNG_PATH, outfile_movie=RPP_GIF_FILENAME, fps=3) logger.info(video_message) logger.info("and that's a wrap, checkout the output!") except FileNotFoundError(OSError) as e: print(e) print( "Sorry, looks like you don't have all the needed visualization dependencies." )
def main(): """Solve the RPP and save visualizations of the solution""" # PARAMS / DATA --------------------------------------------------------------------- # inputs START_NODE = 'a' N_NODES = 13 # filepaths CPP_REQUIRED_SVG_FILENAME = pkg_resources.resource_filename( 'postman_problems', 'examples/star/output/cpp_graph_req') CPP_OPTIONAL_SVG_FILENAME = pkg_resources.resource_filename( 'postman_problems', 'examples/star/output/cpp_graph_opt') RPP_SVG_FILENAME = pkg_resources.resource_filename( 'postman_problems', 'examples/star/output/rpp_graph') RPP_BASE_SVG_FILENAME = pkg_resources.resource_filename( 'postman_problems', 'examples/star/output/base_rpp_graph') PNG_PATH = pkg_resources.resource_filename('postman_problems', 'examples/star/output/png/') RPP_GIF_FILENAME = pkg_resources.resource_filename( 'postman_problems', 'examples/star/output/rpp_graph.gif') # setup logging logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) # CREATE GRAPH ---------------------------------------------------------------------- logger.info('Solve RPP') graph_base = create_star_graph(N_NODES) edgelist = nx.to_pandas_edgelist(graph_base, source='_node1', target='_node2') # SOLVE CPP ------------------------------------------------------------------------- # with required edges only edgelist_file = create_mock_csv_from_dataframe(edgelist) circuit_cpp_req, graph_cpp_req = cpp(edgelist_file, start_node=START_NODE) logger.info('Print the CPP solution (required edges only):') for e in circuit_cpp_req: logger.info(e) # with required and optional edges as required edgelist_all_req = edgelist.copy() edgelist_all_req.drop(['required'], axis=1, inplace=True) edgelist_file = create_mock_csv_from_dataframe(edgelist_all_req) circuit_cpp_opt, graph_cpp_opt = cpp(edgelist_file, start_node=START_NODE) logger.info('Print the CPP solution (optional and required edges):') for e in circuit_cpp_opt: logger.info(e) # SOLVE RPP ------------------------------------------------------------------------- edgelist_file = create_mock_csv_from_dataframe( edgelist) # need to regenerate circuit_rpp, graph_rpp = rpp(edgelist_file, start_node=START_NODE) logger.info('Print the RPP solution:') for e in circuit_rpp: logger.info(e) logger.info('Solution summary stats:') for k, v in calculate_postman_solution_stats(circuit_rpp).items(): logger.info(str(k) + ' : ' + str(v)) # VIZ ------------------------------------------------------------------------------- try: from postman_problems.viz import plot_circuit_graphviz, plot_graphviz, make_circuit_images, make_circuit_video logger.info('Creating single SVG of base graph') plot_graphviz(graph=graph_base, filename=RPP_BASE_SVG_FILENAME, edge_label_attr='distance', format='svg', engine='circo', graph_attr={ 'label': 'Base Graph: Distances', 'labelloc': 't' }) logger.info( 'Creating single SVG of CPP solution (required edges only)') plot_circuit_graphviz( circuit=circuit_cpp_req, graph=graph_cpp_req, filename=CPP_REQUIRED_SVG_FILENAME, format='svg', engine='circo', graph_attr={ 'label': 'Base Graph: Chinese Postman Solution (required edges only)', 'labelloc': 't' }) logger.info( 'Creating single SVG of CPP solution (required & optional edges)') plot_circuit_graphviz( circuit=circuit_cpp_opt, graph=graph_cpp_opt, filename=CPP_OPTIONAL_SVG_FILENAME, format='svg', engine='circo', graph_attr={ 'label': 'Base Graph: Chinese Postman Solution (required & optional edges)', 'labelloc': 't' }) logger.info('Creating single SVG of RPP solution') plot_circuit_graphviz(circuit=circuit_rpp, graph=graph_rpp, filename=RPP_SVG_FILENAME, format='svg', engine='circo', graph_attr={ 'label': 'Base Graph: Rural Postman Solution', 'labelloc': 't' }) logger.info('Creating PNG files for GIF') make_circuit_images(circuit=circuit_rpp, graph=graph_rpp, outfile_dir=PNG_PATH, format='png', engine='circo') logger.info('Creating GIF') make_circuit_video(infile_dir_images=PNG_PATH, outfile_movie=RPP_GIF_FILENAME, fps=1) except FileNotFoundError(OSError) as e: print(e) print( "Sorry, looks like you don't have all the needed visualization dependencies." )
file_identifier = input() turn_weight_coefficient = 10 input_file_directory = 'C:\\Users\jconnor\OneDrive - Bay Area Air Quality Management District\Documents\Projects\Maps\Route Testing\\' print_directory = 'F:\python\ox\\route_results\\' print_file = file_identifier + '_directed' InnerFileName = file_identifier + ' Inner Polygon.csv' OuterFileName = file_identifier + ' Outer Polygon.csv' START_NODE, req_comp_g, complete_g, elfn, GranularConnector_EdgeList = InnerAndOuterToEdgeListFile(directory=input_file_directory, InnerFileName=InnerFileName, OuterFileName=OuterFileName, turn_weight_coefficient=turn_weight_coefficient) circuit_rpp = rpp(elfn, complete_g, start_node = START_NODE, turn_weight_coefficient=turn_weight_coefficient) circuit_rpp = ml.circuit_path_string_to_int(circuit_rpp) req_and_opt_graph = ml.create_req_and_opt_graph(req_comp_g, complete_g, circuit_rpp, GranularConnector_EdgeList) grppviz = ml.create_number_of_passes_graph(circuit_rpp, complete_g) route_statistics = calculate_postman_solution_stats(circuit_rpp, edge_weight_name='length') rppdf = ml.circuit_parser(circuit_rpp, complete_g, print_file, print_directory) ml.gpx_writer(rppdf, print_file, print_directory) ml.plot_req_and_opt_graph(req_and_opt_graph) ml.plot_number_of_passes_graph(grppviz)
def main(): """Solve the CPP and save visualizations of the solution""" # PARAMS / DATA --------------------------------------------------------------------- # inputs EDGELIST = pkg_resources.resource_filename( 'postman_problems', 'examples/sleeping_giant/edgelist_sleeping_giant.csv') NODELIST = pkg_resources.resource_filename( 'postman_problems', 'examples/sleeping_giant/nodelist_sleeping_giant.csv') START_NODE = "b_end_east" # outputs GRAPH_ATTR = {'dpi': '65'} EDGE_ATTR = {'fontsize': '20'} NODE_ATTR = { 'shape': 'point', 'color': 'black', 'width': '0.1', 'fixedsize': 'true' } PNG_PATH = pkg_resources.resource_filename( 'postman_problems', 'examples/sleeping_giant/output/png/') CPP_SVG_FILENAME = pkg_resources.resource_filename( 'postman_problems', 'examples/sleeping_giant/output/cpp_graph') CPP_GIF_FILENAME = pkg_resources.resource_filename( 'postman_problems', 'examples/sleeping_giant/output/cpp_graph.gif') # setup logging logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) # SOLVE CPP ------------------------------------------------------------------------- logger.info('Solve CPP') circuit, graph = cpp(EDGELIST, START_NODE) logger.info('Print the CPP solution:') for e in circuit: logger.info(e) logger.info('Solution summary stats:') for k, v in calculate_postman_solution_stats(circuit).items(): logger.info(str(k) + ' : ' + str(v)) # VIZ ------------------------------------------------------------------------------- try: from postman_problems.viz import (add_pos_node_attribute, add_node_attributes, plot_circuit_graphviz, make_circuit_images, make_circuit_video) logger.info('Add node attributes to graph') nodelist_df = pd.read_csv(NODELIST) graph = add_node_attributes(graph, nodelist_df) # add attributes graph = add_pos_node_attribute( graph, origin='topleft') # add X,Y positions in format for graphviz logger.info('Creating single SVG of CPP solution') plot_circuit_graphviz(circuit=circuit, graph=graph, filename=CPP_SVG_FILENAME, format='svg', engine='neato', graph_attr=GRAPH_ATTR, edge_attr=EDGE_ATTR, node_attr=NODE_ATTR) except FileNotFoundError(OSError) as e: print(e) print( "Sorry, looks like you don't have all the needed visualization dependencies." )
def main(): """Solve the CPP and save visualizations of the solution""" # PARAMS / DATA --------------------------------------------------------------------- # inputs EDGELIST = pkg_resources.resource_filename( 'postman_problems', 'examples/seven_bridges/edgelist_seven_bridges.csv') START_NODE = 'D' # outputs PNG_PATH = pkg_resources.resource_filename( 'postman_problems', 'examples/seven_bridges/output/png/') CPP_VIZ_FILENAME = pkg_resources.resource_filename( 'postman_problems', 'examples/seven_bridges/output/cpp_graph') CPP_BASE_VIZ_FILENAME = pkg_resources.resource_filename( 'postman_problems', 'examples/seven_bridges/output/base_cpp_graph') CPP_GIF_FILENAME = pkg_resources.resource_filename( 'postman_problems', 'examples/seven_bridges/output/cpp_graph.gif') # setup logging logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) # SOLVE CPP ------------------------------------------------------------------------- logger.info('Solve CPP') circuit, graph = cpp(edgelist_filename=EDGELIST, start_node=START_NODE) logger.info('Print the CPP solution:') for e in circuit: logger.info(e) logger.info('Solution summary stats:') for k, v in calculate_postman_solution_stats(circuit).items(): logger.info(str(k) + ' : ' + str(v)) # VIZ ------------------------------------------------------------------------------- try: from postman_problems.viz import plot_circuit_graphviz, make_circuit_images, make_circuit_video logger.info('Creating single SVG of base graph') plot_circuit_graphviz(circuit=circuit, graph=graph, filename=CPP_BASE_VIZ_FILENAME, edge_label_attr='distance', format='svg', engine='circo', graph_attr={ 'label': 'Base Graph: Distances', 'labelloc': 't' }) logger.info('Creating single SVG of CPP solution') plot_circuit_graphviz(circuit=circuit, graph=graph, filename=CPP_VIZ_FILENAME, format='svg', engine='circo', graph_attr={ 'label': 'Base Graph: Chinese Postman Solution', 'labelloc': 't' }) logger.info('Creating PNG files for GIF') make_circuit_images(circuit=circuit, graph=graph, outfile_dir=PNG_PATH, format='png', engine='circo', graph_attr={ 'label': 'Base Graph: Chinese Postman Solution', 'labelloc': 't' }) logger.info('Creating GIF') video_message = make_circuit_video(infile_dir_images=PNG_PATH, outfile_movie=CPP_GIF_FILENAME, fps=0.5) logger.info(video_message) logger.info("and that's a wrap, checkout the output!") except FileNotFoundError(OSError) as e: print(e) print( "Sorry, looks like you don't have all the needed visualization dependencies." )