def multiple_runs_long_tracks(number, algorithm, data): best_result = 0, [] # set starting testing variables max_track_duration_max = 40 bound_max = 15 variables = [[0, "max_track_duration"], [12, "bound"]] data.set_test_variables(variables) # for i in range(max_track_duration_max): # data.bound = 0 # for y in range(bound_max): # print(data.max_track_duration, data.bound) # # new_result = algorithm(data, gh.invalid_long_tracks) # # if new_result[0] > best_result[0]: # best_result = new_result # print(best_result) # best_result_var = ([new_result[0], data.max_track_duration, data.bound]) # # data.bound = data.bound + 1 # # data.max_track_duration = data.max_track_duration + 1 # # print(data.max_track_duration) new_result = algorithm(data, gh.invalid_long_tracks) vis.print_results(algorithm, new_result, data)
def multiple_runs_duration(number, algorithm, data, start_duration=180): best_result = 0, [] x_values = [] y_values = [] data.max_duration = start_duration for i in range(number): new_result = algorithm(data) if new_result[0] > best_result[0]: best_result = new_result y_values.append(new_result[0]) x_values.append(data.max_duration) data.max_duration = data.max_duration + (5) print(data.max_duration) if data.max_duration == 180: break vis.print_results(algorithm, best_result, data) vis.plot_line(x_values, y_values)
def multiple_runs_max_tracks(number, algorithm, data): best_result = 0, [] best_track_count = 0 x_values = [] y_values = [] variables = [[1, "max_tracks"]] data.set_test_variables(variables) for i in range(number): new_result = algorithm(data, helper.lookup_score, gh.invalid_max_tracks) if new_result[0] > best_result[0]: best_result = new_result best_track_count = data.max_tracks y_values.append(new_result[0]) x_values.append(data.max_tracks) data.max_tracks = data.max_tracks + 1 vis.print_results(algorithm, best_result, data) print(best_track_count) vis.plot_line(x_values, y_values)
def run_random(algorithm, data, times=10000, print_lines=True, map=False, line=False, hist=False): """ runs a random algorithm multiple times :argument algorithm: the algorithm that is to be used, in this case of the greedy family :argument data: a class with all important static information about run, such as max_duration :argument times: times the algorithm is run :argument print_lines: determines if lines will be printed :argument map: determines if map will be printed :argument line: determines if line graph will be printed :argument hist: determines if histogram will be printed :returns best solution with the score and the generated lines. """ best_solution = None best_solutions_scores = [] new_solutions_scores = [] print("generating random solution...", end='', flush=True) for i in range(times): new_solution = algorithm(data) if not best_solution: best_solution = new_solution elif new_solution.score > best_solution.score: best_solution = new_solution new_solutions_scores.append(new_solution.score) best_solutions_scores.append(best_solution.score) print("\t DONE") if print_lines: vis.print_results(algorithm, best_solution, data) if map: vis.draw_map(data, best_solution.lines) if hist: vis.hist(new_solutions_scores, best_solution.lines) if line: vis.plot_line(best_solutions_scores) return best_solution
def multiple_runs_hill_climb(number, algorithm, data): best_result = 0, [] x_values = [] y_values = [] for i in range(4000): new_result = algorithm(number, data) if new_result[0] > best_result[0]: best_result = new_result y_values.append(best_result[0]) x_values.append(i) print(i, best_result[0]) vis.print_results(algorithm, best_result, data) vis.plot_line(x_values, y_values)
def multiple_runs(number, algorithm, data): best_result = 0, [] best_results = [] new_results = [] x_values = [] for i in range(number): new_result = algorithm(data) if new_result[0] > best_result[0]: best_result = new_result best_results.append(best_result[0]) new_results.append(new_result[0]) x_values.append(i) print(i, best_result[0]) vis.print_results(algorithm, best_result, data) vis.hist(new_results, best_result[1], data) vis.plot_line(x_values, best_results)
def run_greedy(algorithm, data, times=10000, lookup_function=helper.lookup_score, invalid_function=helper.invalid, print_lines=True, map=False, line=False, hist=False): """ runs a greedy algorithm multiple times :argument algorithm: the algorithm that is to be used, in this case of the greedy family :argument data: a class with all important static information about run, such as max_duration :argument times: times the algorithm is run :argument lookup_function: function for which lookup table is used :argument invalid_function: function that determines whichs lines are correct :argument print_lines: determines if lines will be printed :argument map: determines if map will be printed :argument line: determines if line graph will be printed :argument hist: determines if histogram will be printed :returns best solution with the score and the generated lines. """ # set possible heuristics data.lookup_table_function = lookup_function data.invalid_function = invalid_function if algorithm.__name__ == "recalculating_greedy" and\ lookup_function.__name__ != "lookup_score_random": times = 1 best_solution = None best_solutions_scores = [] new_solutions_scores = [] print("generating greedy solution...", end='', flush=True) for i in range(times): new_solution = algorithm(data) if not best_solution: best_solution = new_solution elif new_solution.score > best_solution.score: best_solution = new_solution new_solutions_scores.append(new_solution.score) best_solutions_scores.append(best_solution.score) print("\t DONE") if print_lines: vis.print_results(algorithm, best_solution, data) if map: vis.draw_map(data, best_solution.lines) if hist: vis.hist(new_solutions_scores, best_solution.lines) if line: vis.plot_line(best_solutions_scores) return best_solution
def run_hill_climber(algorithm, data, times=10, steps=10000, start_solution=[], change_amount=2, print_lines=True, map=False, line=False, hist=False): """ runs a hill-climber algorithm multiple times :argument algorithm: the algorithm that is to be used, in this case of the greedy family :argument data: a class with all important static information about run, such as max_duration :argument times: times the algorithm is run :argument steps: amount of steps the hill-climber takes :argument start_solution: an already created solution serving as start point for the hill climber :argument change_amount: amount of tracks that will be swapped in hill_climber_multi :argument print_lines: determines if lines will be printed :argument map: determines if map will be printed :argument line: determines if line graph will be printed :argument hist: determines if histogram will be printed :returns best solution with the score and the generated lines. """ best_solution = None best_solutions_scores = [] new_solutions_scores = [] for i in range(times): if algorithm.__name__[-8:-12] == "multi": new_solution = algorithm(steps, data, start_solution, change_amount) else: new_solution = algorithm(steps, data, start_solution) if not best_solution: best_solution = new_solution elif new_solution.score > best_solution.score: best_solution = new_solution new_solutions_scores.append(new_solution.score) best_solutions_scores.append(best_solution.score) print("run:", i, best_solution.score) if print_lines: vis.print_results(algorithm, best_solution, data) if map: vis.draw_map(data, best_solution.lines) if hist: vis.hist(new_solutions_scores, best_solution.lines) if line: vis.plot_line(best_solutions_scores) return best_solution