def validate_all_outputs(input_directory, output_directory, cost_dir, params=[]): input_files = utils.get_files_with_extension(input_directory, '.in') output_files = utils.get_files_with_extension(output_directory, '.out') if os.path.isfile(cost_dir): print('ERROR: ' + cost_dir + 'already exist!') return check_file = open(cost_dir, 'w') all_results = [] for input_file in tqdm.tqdm(input_files): output_file = utils.input_to_output(input_file, output_directory) print(input_file, output_file) if output_file not in output_files: print(f'No corresponding .out file for {input_file}') results = (None, None, f'No corresponding .out file for {input_file}') else: input_error, cost, results = validate_output(input_file, output_file, params=params) name = input_file.split('/')[-1] check_file.write(name + ':' + str(cost) + '\n') all_results.append((input_file, results)) check_file.close() return all_results
def validate_all_outputs(input_directory, output_directory, params=[]): input_files = utils.get_files_with_extension(input_directory, '.in') output_files = utils.get_files_with_extension(output_directory, '.out') all_results = [] for input_file in input_files: output_file = utils.input_to_output(input_file, output_directory) print(input_file, output_file) if output_file not in output_files: print(f'No corresponding .out file for {input_file}') results = (None, None, f'No corresponding .out file for {input_file}') else: if False: # delete invalid outputs try: results = validate_output(input_file, output_file, params=params) all_results.append((input_file, results)) except: os.remove(output_file) os.remove(output_file + ".optimal") else: results = validate_output(input_file, output_file, params=params) all_results.append((input_file, results)) return all_results
def validate_all_outputs(input_directory, output_directory, params=[]): input_files = utils.get_files_with_extension(input_directory, '.in') output_files = utils.get_files_with_extension(output_directory, '.out') for file in output_files: print(file) output_directory += '/' all_results = [] string = '' for input_file in input_files: basename, filename = os.path.split(input_file) if '200' in filename: continue output_file = utils.input_to_output(input_file, output_directory) print(input_file, output_file) if output_file not in output_files: print(f'No corresponding .out file for {input_file}') results = (None, None, f'No corresponding .out file for {input_file}') else: results = validate_output(input_file, output_file, params=params) string += filename + " " + results[2] + "\n" all_results.append((input_file, results)) utils.write_to_file('validations.out', string) return all_results
def validate_all_outputs(input_directory, output_directories): input_files = utils.get_files_with_extension(input_directory, '.in') output_files_list = [utils.get_files_with_extension(output_directory, '.out') for output_directory in output_directories] results = [] bestOutputs = [] for i in range(len(input_files)): input_file = input_files[i] result, bestOutput = _outputCost(input_file, output_directories) results.append(result) bestOutputs.append(bestOutput) masterOutput = "master/" + input_file.split("/")[-1].split(".")[0] + ".out" print(output_files_list[bestOutput][i]) copyfile(output_files_list[bestOutput][i], masterOutput) final_score = "Superscored " + str(len(output_directories)) + " directories with a score of: " + str(100 / len(results) * sum(results)) print(final_score) a = open(str(datetime.now()).split(".")[0].replace(":","-")+".log", "w") for r in results: a.write(str(r) + "\n") a.write("\n") for b in bestOutputs: a.write(str(b) + "\n") a.write("\n") for inp in inputs: a.write(str(inp) + "\n") a.write(final_score) a.close() return results
def validate_all_outputs(input_directory, output_directory, params=[]): input_files = utils.get_files_with_extension(input_directory, '.in') output_files = utils.get_files_with_extension(output_directory, '.out') i = 0 all_results = [] all_baseline = [] all_baseline = pickle.load(open("baselineCosts.p", "rb")) if not MULTICORE: inputs = Manager().list() for input_file in input_files: _outputCost(input_file, output_files, output_directory, all_baseline, i, all_results, inputs) i += 1 final_score = "Graded " + str( len(all_results)) + " results with a score of: " + str( 100 / len(all_results) * sum(all_results)) print(final_score) a = open( str(datetime.now()).split(".")[0].replace(":", "-") + ".log", "w") for r in all_results: a.write(str(r) + "\n") a.write("\n") for inp in inputs: a.write(str(inp) + "\n") a.write(final_score) a.close() return all_results else: print("MULTICORE IN PROCESS. DO NOT CONTROL-C.") all_results = Manager().list() inputs = Manager().list() tasks = [] i = 0 for input_file in input_files: tasks.append((input_file, output_files, output_directory, all_baseline, i, all_results, inputs)) i += 1 pool = Pool(num_thread - 1) results = [pool.apply_async(_outputCost, t) for t in tasks] pool.close() pool.join() final_score = "Graded " + str( len(all_results)) + " results with a score of: " + str( 100 / len(all_results) * sum(all_results)) print(final_score) a = open( str(datetime.now()).split(".")[0].replace(":", "-") + ".log", "w") for r in all_results: a.write(str(r) + "\n") a.write("\n") for inp in inputs: a.write(str(inp) + "\n") a.write(final_score) a.close() return all_results
def solve_all(input_directory, output_directory, params=[]): input_files = utils.get_files_with_extension(input_directory, 'in') output_files = set(utils.get_files_with_extension(output_directory, 'out')) print(output_files) for input_file in input_files: out_file_path = "outputs/" + input_file[7:-3] + ".out" if (out_file_path not in output_files): solve_from_file(input_file[7:], output_directory, params=params) output_files.add(out_file_path) print(input_file[:-3] + ".out")
def validate_all_outputs(input_directory, output_directory, params=[]): input_files = utils.get_files_with_extension(input_directory, '.in') output_files = utils.get_files_with_extension(output_directory, '.out') all_results = [] for input_file in input_files: output_file = utils.input_to_output(input_file, output_directory) if output_file not in output_files: print(f'No corresponding .out file for {input_file}') results = (None, None, f'No corresponding .out file for {input_file}') else: results = validate_output(input_file, output_file, params=params) all_results.append((input_file, results)) return all_results
def solve_all(input_directory, output_directory, params=[]): input_files = utils.get_files_with_extension(input_directory, 'in') counter = 0 for input_file in input_files: counter += 1 print(counter, "out of 90") solve_from_file(input_file, output_directory, params=params)
def remaining(filename): conn = sqlite3.connect(filename) c = conn.cursor() input_folder = input("Which folder to check against? ") inputs = [ file.split("/")[-1] for file in utils.get_files_with_extension(input_folder, 'in') ] results = [ file[0] for file in c.execute("SELECT input_file FROM models").fetchall() ] remaining = [] for i in inputs: if i not in results: remaining.append(i) print(i) conn.commit() conn.close() print(f"There are {len(remaining)} files remaining.") splitter('remaining', remaining)
def solve_all(input_directory, output_directory, params=[]): input_files = utils.get_files_with_extension(input_directory, 'in') #for i in range(260, 270): #input_file = "inputs//" + str(i) + ".in" #solve_from_file(input_file, output_directory, params=params) for input_file in input_files: solve_from_file(input_file, output_directory, params=params)
def solve_all(input_directory, output_directory, params=[]): input_files = utils.get_files_with_extension(input_directory, 'in') file = open("costs.csv", "w") for input_file in input_files: solve_from_file(input_file, output_directory, file, params=params) file.close()
def solve_all(input_directory, output_directory, params=[]): if params[0] == 'naive': print("Using naive method") print("Clearing logs") utils.clear_file('logs/naive.log') elif params[0] == 'greedy': print('Using greedy method') print("Clearning logs") utils.clear_file('logs/greedy.log') elif params[0] == 'three_opt': print('Using three_opt method') print("Clearning logs") utils.clear_file('logs/three_opt.log') elif params[0] == 'ant_colony': print("Using ant colony optimization") print("Clearing logs") utils.clear_file("logs/ant_colony.log") elif params[0] == 'greedy_clustering_three_opt': print("Using greedy clustering three opt") print("Clearing logs") utils.clear_file("logs/greedy_clustering_three_opt.log") elif params[0] == 'mst': print("Using mst method") print("Clearing logs") utils.clear_file("logs/mst.log") input_files = utils.get_files_with_extension(input_directory, 'in') for input_file in input_files: solve_from_file(input_file, output_directory, params=params) print()
def solve_all(input_directory, output_directory, params=[]): input_files = utils.get_files_with_extension(input_directory, 'in') count = 1 for input_file in input_files: print("Solving file " + str(count) + " out of " + str(len(input_files))) solve_from_file(input_file, output_directory, params=params) count+=1
def solve_all(input_directory, output_directory, params=[]): input_files = utils.get_files_with_extension(input_directory, 'in') for input_file in input_files: try: solve_from_file(input_file, output_directory, params=params) except: with open('./err.txt', 'a') as f: f.write(input_file + '\n')
def solve_all(input_directory, output_directory, params=[]): if params[0] != "ILP" and params[0] != "ANT" and params[0] != "TSP": print( 'Invalid algorithm. Please use one of the following algorithms: ILP, ANT, TSP.' ) return input_files = utils.get_files_with_extension(input_directory, 'in') for input_file in input_files: solve_from_file(input_file, output_directory, params=params)
def solve_all(input_directory, output_directory, params=[]): input_files = utils.get_files_with_extension(input_directory, 'in') file = open("order.csv", "w") for input_file in input_files: input_file = input_file[7:] input_file = input_file[:-3] file.write(input_file + "\n") # solve_from_file(input_file, output_directory, file, params=params) file.close()
def validate_all_outputs(input_directory, output_directory, check_dir, params=[]): input_files = utils.get_files_with_extension(input_directory, '.in') output_files = utils.get_files_with_extension(output_directory, '.out') # if os.path.isfile(check_dir): # print('ERROR: ' + check_dir + ' already exist!') # return check_file = open(check_dir, 'w') for input_file in tqdm.tqdm(input_files): output_file = utils.input_to_output(input_file, output_directory) # print(input_file, output_file) if output_file not in output_files: print(f'No corresponding .out file for {input_file}') else: cost = validate_output(input_file, output_file, params=params) name = input_file.split('/')[-1] check_file.write(name.replace('in', 'out') + ':' + str(cost) + '\n') check_file.close()
def compare_all(input_directory, comp1dir, comp2dir, output_directory, params=[]): input_files = utils.get_files_with_extension(input_directory, 'in') comp1_files = utils.get_files_with_extension(comp1dir, 'out') comp2_files = utils.get_files_with_extension(comp2dir, 'out') inputdirleng = len(input_directory) il =list(input_files) ill = [i[inputdirleng + 1:-3] for i in il] comp1dirleng = len(comp1dir) c1 =list(comp1_files) c1l =[i[comp1dirleng + 1:-4] for i in c1] comp2dirleng = len(comp2dir) c2 =list(comp2_files) c2l =[i[comp2dirleng + 1:-4] for i in c2] for i in c1l: if i in ill and i in c2l: compare_by_file(input_directory+"/"+i+'.in',comp1dir+"/"+i+'.out' ,comp2dir+"/"+i+'.out', output_directory,params=params)
def solve_all(input_directory, output_directory, params=[]): input_files = utils.get_files_with_extension(input_directory, 'in') for input_file in input_files: """ basename, filename = os.path.split(input_file) if '200' in filename: continue group, type = filename.split("_") if int(group) >= 10: continue """ solve_from_file(input_file, output_directory, params=params)
def solve_all(input_directory, output_directory, params=[]): input_files = utils.get_files_with_extension(input_directory, 'in') # with open("./new_input" ,'r') as f: # name = [] # while True: # line = f.readline() # if not line: # break # name.append(line[:line.rfind(".in")] + ".in") # # print(name) # input_files=input_files[250:] for input_file in input_files: # if input_file not in name: # continue solve_from_file(input_file, output_directory, params=params)
def solve_all(input_directory, output_directory, params=[]): input_files = utils.get_files_with_extension(input_directory, 'in') if not MULTICORE: for input_file in input_files: solve_from_file(input_file, output_directory, params=params) else: print("MULTICORE IN PROCESS. DO NOT CONTROL-C.") tasks = [] for input_file in input_files: tasks.append((input_file, output_directory, params)) pool = Pool(num_thread - 1) results = [pool.apply_async(solve_from_file, t) for t in tasks] pool.close() pool.join()
def solve_some(input_directory, output_directory, params=[]): input_files = utils.get_files_with_extension(input_directory, 'in') num_so_far = 0 score = 0 for input_file in input_files: num_so_far += 1 G, car_cycle, dropoff_mapping = solve_from_file_score( input_file, output_directory) this_score = student_utils.cost_of_solution(G, car_cycle, dropoff_mapping)[0] graph_total = sum(G[u][v]['weight'] for (u, v) in G.edges) score += graph_total / this_score if num_so_far == 10: print("score:") print(score) break
def solve_all(input_directory, output_directory, params=[]): input_files = utils.get_files_with_extension(input_directory, 'in') # control = int(params[0]) for input_file in input_files: # if control == 2: # if input_file.endswith('_200.in'): # continue # else: # if not input_file.endswith('_200.in'): # continue # else: # num = int(input_file[-8]) # if num % 2 != control: # continue solve_from_file(input_file, output_directory, params=params)
def solve_all(input_directory, output_directory, params=[]): input_files = utils.get_files_with_extension(input_directory, 'in') print("Started with the inputs: ") pool = multiprocessing.Pool() param_list = [] # for input_file in tqdm(input_files): # if input_file[input_file.index('_'):input_file.index('_') + 3] == '_50': # param_list.append([input_file, output_directory, params]) for input_file in tqdm(input_files): if input_file[input_file.index('_'):input_file.index('_') + 4] == '_100': param_list.append([input_file, output_directory, params]) for input_file in tqdm(input_files): if input_file[input_file.index('_'):input_file.index('_') + 4] == '_200': param_list.append([input_file, output_directory, params]) pool.map(solve_from_file_wrapper, param_list) pool.close() print("ENDING all the inputs.")
def analyze_all(output_directory, costs_directory, params=[]): input_files = set(utils.get_files_with_extension('inputs', 'in')) num_outputs = len(os.listdir(output_directory)) # writing into csv table # data = np.empty((num_outputs + 1, 5)) header = ['output_dir', 'output_filename', 'travel_cost', 'conquer_cost', 'cost'] costs_file = f'{costs_directory}/{output_directory}.csv' if not os.path.exists(costs_directory): os.makedirs(costs_directory) utils.write_data_to_file(costs_file, header, ',') for name in os.listdir(output_directory): if name.endswith("out"): if 'inputs/' + name.replace('.out', '.in') not in input_files: continue num, ext = name.split(".") t_cost, c_cost, cost = analyze_from_file(f'inputs/{num}.in', f'{output_directory}/{name}', params=params) if cost == -1: continue line_data = [output_directory, name, t_cost, c_cost, cost] utils.write_to_file(costs_file, '\n', append=True) utils.write_data_to_file(costs_file, line_data, ',', append=True)
def validate_all_inputs(input_directory, params=[]): input_files = utils.get_files_with_extension(input_directory, 'in') for input_file in input_files: validate_input(input_file, params=params)
def solve_all(input_directory, output_directory, params=[]): input_files = utils.get_files_with_extension(input_directory, (params[0] if len(params) > 0 else '') + '.in') for input_file in input_files: solve_from_file(input_file, output_directory, params=params)
def solve_all(input_directory, output_directory, params=[]): input_files = utils.get_files_with_extension(input_directory, 'in') for i, input_file in enumerate(input_files): print(f"~~ Solving file {i + 1} of {len(input_files)} ~~") solve_from_file(input_file, output_directory, params=params)
def solve_all(input_directory, output_directory, params=[]): input_files = utils.get_files_with_extension(input_directory, 'in') for input_file in input_files: solve_from_file(input_file, output_directory, params=params) os.remove(input_file)
# otherwise output the line on file2 and mark it with < sign elif f2_line != '': print("<", "Line-%d" % line_no, f2_line) # Print a blank line print() ''' #Read the next line from the file f1_line = f1.readline() f2_line = f2.readline() #Increment line counter line_no += 1 if not flag: print("file equal to ", fname2, " is ", fname1) # Close the files f1.close() f2.close() input_directory = "inputs/" input_files = utils.get_files_with_extension(input_directory, '.in') our_file_1 = "50.in" our_file_2 = "100.in" for input_file in input_files: #compare(input_file, our_file_1) compare(input_file, our_file_2)