def pickmax(input_path, output_files): best_score = -999 best_solution = None for out in output_files: if os.path.exists(out): score, msg = score_output(input_path, out) else: score = -1000 if score > best_score: best_score = score best_solution = out return best_solution
def score_graph(self): """ Quick and dirty set_score for the graph. Note that there is a lot of file writing so that we could use the included output_scorer. :return: Score of the current solution on the current graph. """ if not os.path.exists("temp"): os.makedirs("temp") self.write_solution("temp") self.write_input("graph", "parameters") score = output_scorer.score_output("temp", "temp/temp.out") return score
def score_all_outputs(output_folder, silent=True): all_scores = {} for size in size_categories: category_scores = {} category_path = path_to_inputs + "/" + size output_category_path = output_folder + "/" + size loaded_cached = False ''' cached_filename = output_folder + "/" + size + "_cached_scores.json" try: print("Looking for cached file: {}".format(cached_filename)) with open(cached_filename, "r") as cached_file: print("Loading from cached evaluation scores for " + size + "...") category_scores = json.load(cached_file) print("Read {} successfully.".format(cached_filename)) loaded_cached = True except FileNotFoundError as e: pass ''' if not loaded_cached: category_dir = os.fsencode(category_path) if not os.path.isdir(output_category_path): os.mkdir(output_category_path) for input_folder in os.listdir(category_dir): input_name = os.fsdecode(input_folder) #print(input_name) score, msg = score_output( category_path + "/" + input_name, output_category_path + "/" + input_name + ".out") #if not silent: #print("{}-{} scored {}".format(size, input_name, score)) category_scores[input_name] = max(score, 0) ''' with open(cached_filename, "w") as cached_file: json.dump(category_scores, cached_file) ''' all_scores[size] = category_scores return all_scores
def autograde(path_to_inputs, path_to_outputs): size_categories = ['small', 'medium', 'large'] total_score = 0.0 total_inputs = 0 for size in size_categories: category_path = path_to_inputs + "/" + size category_dir = os.fsencode(category_path) folders = os.listdir(category_dir) for input_folder in folders: input_name = os.fsdecode(input_folder) output_file = path_to_outputs + "/" + size + "/" + input_name + ".out" score, msg = score_output(category_path + "/" + input_name, output_file) total_score += score total_inputs += 1 print('Processing input %s %s' % (input_name, size)) average = total_score/total_inputs print('Graded score: %f' % average)
def take_max_scores(path1, path2): keep_file_path = "./true_outputs" size_categories = ["small", "medium", "large"] path_to_inputs = "./all_inputs" outputs = [] outputs.append(path2) outputs.append(path1) for size in size_categories: category_path = path_to_inputs + "/" + size category_dir = os.fsencode(category_path) for input_folder in os.listdir(category_dir): category_path = path_to_inputs + "/" + size input_name = os.fsdecode(input_folder) max = 0.0 file_to_keep = None for path_to_outputs in outputs: print("looking at" + path_to_outputs) output_category_path = path_to_outputs + "/" + size if input_name == ".DS_Store": continue output_file = output_category_path + "/" + input_name + ".out" try: score, msg = output_scorer.score_output( category_path + "/" + input_name, output_file) except FileNotFoundError: score = 0 print("score = " + str(score)) if score and score > max: max = score file_to_keep = output_file print(file_to_keep) if not file_to_keep: file_to_keep = "./old_outputs/" + size + "/" + input_name + ".out" print("copyng from" + file_to_keep + "to" + keep_file_path + "/" + size) copy(file_to_keep, keep_file_path + "/outputs/" + size)
import os from output_scorer import score_output path_to_inputs = "./all_inputs" path_to_outputs = "./outputs" avg = 0.0 index = 0 size_categories = ["small", "medium", "large"] if not os.path.isdir(path_to_outputs): os.mkdir(path_to_outputs) for size in size_categories: category_path = path_to_inputs + "/" + size output_category_path = path_to_outputs + "/" + size category_dir = os.fsencode(category_path) for input_folder in os.listdir(category_dir): input_name = os.fsdecode(input_folder) print('group', input_name) thisscore = score_output( category_path + "/" + input_name, output_category_path + "/" + input_name + ".out")[0] avg = (avg * index + thisscore) / (index + 1) index += 1 print(avg)
def solver_main(input_type): iname = "all_inputs/" + input_type + "/" oname = "all_outputs/" + input_type + "/" files = list(os.walk(iname))[0][1] scores = [] files.sort(reverse=True) for f in files: print(f) try: main(iname + f + "/", oname, f + "1", 1) score1, msg = score_output(iname + "/" + f + "/", oname + str(f) + "1.out") except: score1 = 0 open(oname + str(f) + "1.out", 'a').close() try: main(iname + f + "/", oname, f + "2", 2) score2, msg = score_output(iname + "/" + f + "/", oname + str(f) + "2.out") except: score2 == 0 open(oname + str(f) + "2.out", 'a').close() try: main(iname + f + "/", oname, f + "3", 3) score3, msg = score_output(iname + "/" + f + "/", oname + str(f) + "3.out") except: score3 = 0 open(oname + str(f) + "3.out", 'a').close() try: main(iname + f + "/", oname, f + "4", 4) score4, msg = score_output(iname + "/" + f + "/", oname + str(f) + "4.out") except: score4 = 0 open(oname + str(f) + "4.out", 'a').close() dummyScores = [] for i in range(5): dummy.main(f, iType=input_type, counter=i) scoreDum, msg = score_output(iname + "/" + f + "/", oname + str(f) + str(i) + "5.out") dummyScores.append(scoreDum) score5 = max(dummyScores) index_dum = dummyScores.index(score5) os.rename(oname + str(f) + str(dummyScores.index(score5)) + "5.out", oname + str(f) + "5.out") if index_dum == 0: os.remove(oname + str(f) + "15.out") os.remove(oname + str(f) + "25.out") os.remove(oname + str(f) + "35.out") os.remove(oname + str(f) + "45.out") elif index_dum == 1: os.remove(oname + str(f) + "05.out") os.remove(oname + str(f) + "25.out") os.remove(oname + str(f) + "35.out") os.remove(oname + str(f) + "45.out") elif index_dum == 2: os.remove(oname + str(f) + "05.out") os.remove(oname + str(f) + "15.out") os.remove(oname + str(f) + "35.out") os.remove(oname + str(f) + "45.out") elif index_dum == 3: os.remove(oname + str(f) + "05.out") os.remove(oname + str(f) + "15.out") os.remove(oname + str(f) + "25.out") os.remove(oname + str(f) + "45.out") elif index_dum == 4: os.remove(oname + str(f) + "05.out") os.remove(oname + str(f) + "15.out") os.remove(oname + str(f) + "25.out") os.remove(oname + str(f) + "35.out") score = max(score1, score2, score3, score4, score5) if score == score1: os.rename(oname + str(f) + "1.out", oname + str(f) + ".out") os.remove(oname + str(f) + "2.out") os.remove(oname + str(f) + "3.out") os.remove(oname + str(f) + "4.out") os.remove(oname + str(f) + "5.out") elif score == score2: os.rename(oname + str(f) + "2.out", oname + str(f) + ".out") os.remove(oname + str(f) + "1.out") os.remove(oname + str(f) + "3.out") os.remove(oname + str(f) + "4.out") os.remove(oname + str(f) + "5.out") elif score == score3: os.rename(oname + str(f) + "3.out", oname + str(f) + ".out") os.remove(oname + str(f) + "1.out") os.remove(oname + str(f) + "2.out") os.remove(oname + str(f) + "4.out") os.remove(oname + str(f) + "5.out") elif score == score4: os.rename(oname + str(f) + "4.out", oname + str(f) + ".out") os.remove(oname + str(f) + "1.out") os.remove(oname + str(f) + "2.out") os.remove(oname + str(f) + "3.out") os.remove(oname + str(f) + "5.out") elif score == score5: os.rename(oname + str(f) + "4.out", oname + str(f) + ".out") os.remove(oname + str(f) + "1.out") os.remove(oname + str(f) + "2.out") os.remove(oname + str(f) + "3.out") os.remove(oname + str(f) + "5.out") count = 0 while score == 0: if count > 10: break os.remove(oname + str(f) + ".out") dummy.main(f, iType=input_type) os.rename(oname + str(f) + "05.out", oname + str(f) + ".out") score, msg = score_output(iname + "/" + f + "/", oname + str(f) + ".out") count += 1 print("Score: ", score * 100, "%") try: if score >= 0: scores.append(score) except: continue print("Average: ", sum(scores) / len(scores)) print(len(files)) print(len(scores))
def main(): ''' Main method which iterates over all inputs and calls `solve` on each. The student should modify `solve` to return their solution and modify the portion which writes it to a file to make sure their output is formatted correctly. ''' # size_categories = ["small", "medium", "large"] size_categories = ["small", "medium", "large"] file_counts = {"small": 331, "medium": 331, "large": 100} total_percentage = 0.0 total_num_files = sum([file_counts[size] for size in size_categories]) if not os.path.isdir(path_to_outputs): os.mkdir(path_to_outputs) for size in size_categories: # Print stuff start > print(f"Directory: {size}, Files: {file_counts[size]}") count = 0 start_time = time.time() # Print stuff end < category_path = path_to_inputs + "/" + size output_category_path = path_to_outputs + "/" + size category_dir = os.fsencode(category_path) if not os.path.isdir(output_category_path): os.mkdir(output_category_path) for input_folder in os.listdir(category_dir): input_name = os.fsdecode(input_folder) graph, num_buses, size_bus, constraints = parse_input( category_path + "/" + input_name) # print("Working on {}".format(input_name)) solution = solve(input_name, graph, num_buses, size_bus, constraints) output_file = open( output_category_path + "/" + input_name + ".out", "w") #TODO: modify this to write your solution to your # file properly as it might not be correct to # just write the variable solution to a file # output_file.write(solution) for lst in solution: output_file.write(str(list(lst))) output_file.write("\n") output_file.close() score, msg = scorer.score_output( category_path + "/" + input_name, output_category_path + "/" + input_name + ".out") if score == -1: print(f"File {input_name}: {msg}") total_percentage += score # Print stuff start > uncomment when actually running the algorithm count += 1 if DisplayCount: print(f"Finished {count}/{file_counts[size]}", end="\r") end_time = time.time() print(f"Done with {size}. Took {end_time - start_time} seconds.") # Print stuff end < print( f"Done with all sizes, avg percentage is: {total_percentage/total_num_files}" )
for smaller in partition(people[1:]): # insert `first` in each of the subpartition's subsets for n, subset in enumerate(smaller): yield smaller[:n] + [[first] + subset] + smaller[n + 1:] # put `first` in its own subset yield [[first]] + smaller groups = list() for i in range(0, NUM_PPL): groups.append(str(i)) best_score = 0 best_p = [] count = 0 for n, p in enumerate(partition(groups), 1): # p corresponds to an individual partition if len(p) == NUM_BUS: count += 1 if count % 5000 == 0: print(count) score, msg = score_output("./folder", p) if score > best_score: best_p = [] if score >= best_score: best_score = score best_p.append(p) print("The best score is: " + str(best_score)) print(best_p)