def main(): ''' Entry point of Postnovo app. ''' start_time = time() test_argv = None userargs.setup(test_argv) if config.globals['Retrain']: classifier.train_models() else: input.parse() single_alg_prediction_df = singlealg.do_single_alg_procedure() ##REMOVE: for debugging #utils.save_pkl_objects( # config.globals['Output Directory'], # **{'single_alg_prediction_df.pkl': single_alg_prediction_df}) ##REMOVE: for debugging #single_alg_prediction_df = utils.load_pkl_objects( # config.globals['Output Directory'], # 'single_alg_prediction_df.pkl') consensus_prediction_df = consensus.do_consensus_procedure() ##REMOVE: for debugging #utils.save_pkl_objects( # config.globals['Output Directory'], # **{'consensus_prediction_df.pkl': consensus_prediction_df}) ##REMOVE: for debugging #consensus_prediction_df = utils.load_pkl_objects( # config.globals['Output Directory'], # 'consensus_prediction_df.pkl') prediction_df = pd.concat([ single_alg_prediction_df.reset_index(), consensus_prediction_df.reset_index() ], ignore_index=True) if 'index' in prediction_df.columns: prediction_df.drop('index', axis=1, inplace=True) prediction_df = masstol.do_mass_tol_procedure(prediction_df) prediction_df = interspec.do_interspec_procedure(prediction_df) classifier.classify(prediction_df) ##REMOVE: for debugging #classifier.classify(None) print('Postnovo successfully completed') utils.verbose_print('Total time elapsed:', time() - start_time) return
def main(): weights, values = parse(sys.argv[1]) weight_bound = int(prompt("Weight Bound")) matrix = knapsack(weights, values, weight_bound) score = matrix[-1][-1] ##Best score will always be in the last position items = walkback(weights, values, matrix) print "The best score is %d and the items are %s" % (score, str(items))
def main(): if len(sys.argv) != 2: usage() sys.exit(1) edges = dedupe(parse(get_lines(sys.argv[1]))) kruskals_output, _ = kruskals(edges) prims_output, _ = prims(edges) print_edges(prims_output, "primout.txt") print_edges(kruskals_output, "kruskalout.txt")
def __init__(self, filepath="b_should_be_easy.in"): data = parse(filepath) self.cars = [] for vehicle_id in range(data.get('vehicles')): self.cars.append(Car(vehicle_id)) self.rides = [] for ride_id in data.get('rides'): ride_info = data.get('rides').get(ride_id) self.rides.append(Ride(ride_id, **ride_info)) self.rows = data.get('rows') self.columns = data.get('columns') self.number_of_rides = data.get('number_of_rides') self.bonus = data.get('bonus') self.total_steps = data.get('total_steps')
def main(filename): with open(filename, 'r') as f: edges = parse(get_lines(filename)) graph = make_graph(edges) source = raw_input("Source vertex:") while not in_graph(graph, source): source = raw_input("Bad source\nSource vertex:") vec, back_edges = topological_sort(graph) negs = False for _, _, w in edges: if w < 0: negs = True cycle = len(back_edges) > 0 if not cycle: print "Graph is a DAG, running DAG SP algorithm" d, parent = dag_sp(graph, vec, source) elif not negs: print "Graph has no negative edges, running Dijkstra's algorithm" d, parent = dijkstra(graph, source) else: print "Running Bellman-Ford algorithm" d, parent = bellman_ford(graph, source) if d == None: ## Sufficent to assume parent is also None print "Graph contains a negative-weight cycle." sys.exit(0) while True: dest = raw_input("Destination vertex:") while not in_graph(graph, dest): dest = raw_input("Bad destination\nDestination vertex:") dist, order = get_shortest_path(d, parent, dest) if dist >= INFINITY: print "There is no way to reach the destination from the source" else: print "The distance between the vertices is %d" % dist print "The shortest path is %s" % pprint_path(order)
def main(): weights, values = parse(sys.argv[1]) weight_bound = int(prompt("Weight Bound")) items, score = knapsack(weights, values, weight_bound) print "The best score is %d and the items are %s" % (score, str(items))
#!/usr/bin/env python from input import get_lines, parse from graph import make_graph, topological_sort import sys from os.path import isfile filename = sys.argv[1] if not isfile(filename): exit(1) lines = get_lines(filename) edges = parse(lines) graph = make_graph(edges) path, back_edges = topological_sort(graph) if not back_edges: print "The path is: " + str(path) else: print "The graph is cyclic. Back edges: " + str(back_edges)
import input import knapsack import sys def pprint_2d_arr(arr): for row in arr[::-1]: s = "" for elem in row: s += "%3d," % elem print s tup = input.parse("knapsack.txt") output = knapsack.knapsack(*tup, weight_bound=int(sys.argv[1])) print output
from input import parse, get_lines, dedupe from graph import make_weighted_graph from sorts import merge_sort, PriorityQueue from mst import kruskals, prims edges = dedupe(parse(get_lines("graph.txt"))) print kruskals(edges) print prims(edges)
from input import get_lines, parse from graph import make_graph, topological_sort from sp import dag_sp, bellman_ford, dijkstra, get_shortest_path edges = parse(get_lines("dag.txt")) graph = make_graph(edges) vec, backs = topological_sort(graph) negs = False for _, _, w in edges: if w < 0: negs = True cycle = len(backs) > 0 if negs: print "Negatives" if cycle: print "Cyclic" print backs dag_sol = dag_sp(graph, vec, '1') bf_sol = bellman_ford(graph, '1') dijk_sol = dijkstra(graph, '1') print dag_sol print bf_sol print dijk_sol d, parent = dag_sol print get_shortest_path(d, parent, '5')
from input import parse, print_info, check_area, add_new_one, place_corral if __name__ == '__main__': # main function # CN = parse(input('Enter list of animals:')) # enter animal list from keyboard CN = parse(open('animals.txt', 'r').read()) # read animal list from file "animals.txt" place_corral(CN) # set corrals from animal list print(f'Corral needed: {len(set(CN))}') # how many corrals are needed print_info() # print to console info about animal and corral # checks whether it is possible to accommodate all newly arrived animals check_area( open('new_animals.txt', 'r').read()) # read animal list from file "new_animals.txt" while 1: # the user alternately accommodates each newly arrived animal info = input( "Enter 'e' for exit\nEnter animal kind and corral id(lion,1):") if info == 'e': break add_new_one(info.split(',')[0], int(info.split(',')[1])) print_info() # print to console info about animal and corral