def _run_inner(prefix, test_name, runs, g, c, args1, args2, args3, args4, args5, args6, args7, args8, args9): runner = TSPSRunner(g) add_solver(runner, args1, c, runs) add_solver(runner, args2, c, runs) add_solver(runner, args3, c, runs) add_solver(runner, args4, c, runs) add_solver(runner, args5, c, runs) add_solver(runner, args6, c, runs) add_solver(runner, args7, c, runs) add_solver(runner, args8, c, runs) add_solver(runner, args9, c, runs) runner.run() fig = GenericFig(size=120) result = TestRunResult(runner, fig, test_name + prefix, base_folder) def plot(): result.averaged_x_values(args1.key, '-b') result.averaged_x_values(args2.key, red_color(0)) result.averaged_x_values(args3.key, red_color(1)) result.averaged_x_values(args4.key, red_color(2)) result.averaged_x_values(args5.key, red_color(3)) result.averaged_x_values(args6.key, red_color(4)) result.averaged_x_values(args7.key, green_color(0)) result.averaged_x_values(args8.key, green_color(2)) result.averaged_x_values(args9.key, green_color(4)) result.next_subplot(test_name) plot() result.legend() result.ylabel() result.next_log_subplot(u"(log skaleje)") plot() result.save()
def _genetic_acs_sa_test_type_2_(graph, runs, iterations, chromosomes, selection, subf="a"): cities = len(graph.nodes()) filename = str(cities) + "_genetic_acs_sa_test_1_" + selection output_folder = base_folder + "2/" + subf c1, g1 = ga_pars(graph, iterations, chromosomes, selection, 0.01) c2, g2 = ga_pars(graph, iterations, chromosomes, selection, 0.5) c3, g3 = ga_pars(graph, iterations, chromosomes, selection, 0.99) runner = TSPSRunner(graph) runner.add_tsps(Genetic, g1, c1, runs, 'ga-acs-sa1') runner.add_tsps(Genetic, g2, c2, runs, 'ga-acs-sa2') runner.add_tsps(Genetic, g3, c3, runs, 'ga-acs-sa3') runner.run() runner.save_results(filename=filename, output_folder=output_folder) fig = GenericFig(ylabel="Vidutinis atstumas", size=120) fig.next_subplot("GA-ACS-SA, " + str(cities) + " miestu, n = " + str(runs)) fig.add_averaged_x_plot_values( runner.results['ga-acs-sa1']['all_distances'], line='r-', label='mr-0.01') fig.add_averaged_x_plot_values( runner.results['ga-acs-sa2']['all_distances'], line='g-', label='mr-0.5') fig.add_averaged_x_plot_values( runner.results['ga-acs-sa3']['all_distances'], line='b-', label='mr-0.99') fig.set_legend() fig.set_ylabel('') fig.next_subplot("GA-ACS-SA, " + str(cities) + " miestu, n = " + str(runs)) fig.add_averaged_x_plot_values( runner.results['ga-acs-sa1']['all_distances'], line='r-', label='mr-0.01') fig.add_averaged_x_plot_values( runner.results['ga-acs-sa2']['all_distances'], line='g-', label='mr-0.5') fig.add_averaged_x_plot_values( runner.results['ga-acs-sa3']['all_distances'], line='b-', label='mr-0.99') fig.set_xscale('log') fig.save(filename=filename, output_folder=output_folder)
def _genetic_test_type_2_(graph, runs, iterations, chromosomes, mr, subf="a"): cities = len(graph.nodes()) filename = str(cities) + "_genetic_test_2_pluslog_mr[" + str(mr) + "]_" output_folder = base_folder + "2/" + subf c = cond() c[CONDITION_GENERATION] = iterations gp3 = { 'chromosomes': chromosomes, 'cr': 0.05, 'mr': mr, 'selection_method': SELECTION_ROULETE, 'crossover_method': CROSSOVER_OX, 'mutation_method': MUTATION_SWAP } gp4 = { 'chromosomes': chromosomes, 'cr': 0.05, 'mr': mr, 'selection_method': SELECTION_RANK, 'crossover_method': CROSSOVER_OX, 'mutation_method': MUTATION_SWAP } runner = TSPSRunner(graph) runner.add_tsps(Genetic, gp3, c, runs, 'roulete-ox') runner.add_tsps(Genetic, gp4, c, runs, 'rank-ox') runner.run() runner.save_results(filename=filename, output_folder=output_folder) fig = GenericFig(ylabel="Vidutinis atstumas", size=120) fig.next_subplot( "GA, " + str(cities) + " miestu, n = " + str(runs)) fig.add_averaged_x_plot_values(runner.results['roulete-ox'] ['all_distances'], line='c-', label='roulete-ox') fig.add_averaged_x_plot_values(runner.results['rank-ox'] ['all_distances'], line='y-', label='rank-ox') fig.set_legend() fig.set_ylabel('') fig.next_subplot( "GA, " + str(cities) + " miestu, n = " + str(runs)) fig.add_averaged_x_plot_values(runner.results['roulete-ox'] ['all_distances'], line='c-', label='roulete-ox') fig.add_averaged_x_plot_values(runner.results['rank-ox'] ['all_distances'], line='y-', label='rank-ox') fig.set_xscale('log') fig.save(filename=filename, output_folder=output_folder)
def _sa_test_type_2_(graph, runs, decrement, no_progress): cities = len(graph.nodes()) filename = str(cities) + "_sa_test_sa-1_" + \ str(decrement) + "_no_progress_" + str(no_progress) + "_" output_folder = base_folder + "2" c = cond() c[CONDITION_NO_PROGRESS] = no_progress sp1 = {'initial_temp': 100, 'decrement': decrement} runner = TSPSRunner(graph) runner.add_tsps(SimulatedAnnealing, sp1, c, runs, 'sa-1') runner.run() runner.save_results(filename=filename, output_folder=output_folder) rez = 15 xs, ys, zs, maxs = sh_to_contour_heatmap_scatter( runner.results['sa-1']['all_distances'], rez, True) fig = GenericFig() fig.next_subplot("SA, miestu - " + str(cities) + ", alpha = " + str(decrement) + ", n = " + str(runs)) fig.add_scatter_heatmap(xs, ys, zs, rez) fig.set_xlim(0, maxs[0]) fig.save(filename=filename + "_a", output_folder=output_folder) fig = GenericFig() fig.next_subplot("SA, miestu - " + str(cities) + ", alpha = " + str(decrement) + ", n = " + str(runs)) fig.add_contour_heatmap(xs, ys, zs, rez) fig.save(filename=filename + "_b", output_folder=output_folder)
def _sa_test_type_1_(graph, runs): cities = len(graph.nodes()) filename = str(cities) + "_sa_test_sa-1" output_folder = base_folder + "1" c = cond() sp1 = {'initial_temp': 100, 'decrement': 0.99} sp2 = {'initial_temp': 100, 'decrement': 0.9999} runner = TSPSRunner(graph) runner.add_tsps(SimulatedAnnealing, sp1, c, runs, 'sa-1') runner.add_tsps(SimulatedAnnealing, sp2, c, runs, 'sa-2') runner.run() runner.save_results(filename=filename, output_folder=output_folder) fig = GenericFig(size=120) fig.next_subplot("SA, miestu - " + str(cities) + ", n = " + str(runs)) fig.add_averaged_x_plot_values(runner.results['sa-1']['all_distances'], line='r-', label='decrement-0.99') fig.add_averaged_x_plot_values(runner.results['sa-2']['all_distances'], line='g-', label='decrement-0.9999') fig.set_legend() fig.set_ylabel('') fig.next_subplot("GA, " + str(cities) + " miestu, n = " + str(runs)) fig.add_averaged_x_plot_values(runner.results['sa-1']['all_distances'], line='r-', label='decrement-0.99') fig.add_averaged_x_plot_values(runner.results['sa-2']['all_distances'], line='g-', label='decrement-0.9999') fig.set_xscale('log') fig.save(filename=filename, output_folder=output_folder)
def _genetic_acs_sa_test_type_1_(graph, runs, iterations, chromosomes, mr, selection, subf="a"): cities = len(graph.nodes()) filename = str(cities) + "_genetic_acs_sa_test_1_" + selection output_folder = base_folder + "1/" + subf c0 = cond() c0[CONDITION_GENERATION] = iterations g0 = g_pars(chromosomes, selection, mr) c1, g1 = ga_pars(graph, iterations, chromosomes, selection, mr) runner = TSPSRunner(graph) runner.add_tsps(Genetic, g0, c0, runs, 'ga') runner.add_tsps(Genetic, g1, c1, runs, 'ga-acs-sa') runner.run() runner.save_results(filename=filename, output_folder=output_folder) fig = GenericFig(ylabel="Vidutinis atstumas", size=120) fig.next_subplot("GA-ACS-SA, " + str(cities) + " miestu, n = " + str(runs)) fig.add_averaged_x_plot_values(runner.results['ga']['all_distances'], line='r-', label='ga') fig.add_averaged_x_plot_values( runner.results['ga-acs-sa']['all_distances'], line='g-', label='ga-acs-sa') fig.set_legend() fig.set_ylabel('') fig.next_subplot("GA-ACS-SA, " + str(cities) + " miestu, n = " + str(runs)) fig.add_averaged_x_plot_values(runner.results['ga']['all_distances'], line='r-', label='ga') fig.add_averaged_x_plot_values( runner.results['ga-acs-sa']['all_distances'], line='g-', label='ga-acs-sa') fig.set_xscale('log') fig.save(filename=filename, output_folder=output_folder)
def _acs_test_type_1_(graph, runs, base_pars, beta, antz, iters, subf="a"): cities = len(graph.nodes()) filename = str(cities) + "_acs_test_1__args[" + str(beta) + "][" + str( base_pars['alpha']) + "][" + str(base_pars['ro']) + "]" output_folder = base_folder + "1/" + subf base_pars['beta'] = beta c1 = cond() c1[CONDITION_GENERATION] = iters ap1 = deepcopy(base_pars) ap1['ants'] = antz[0] c2 = cond() c2[CONDITION_GENERATION] = iters ap2 = deepcopy(base_pars) ap2['ants'] = antz[1] runner = TSPSRunner(graph) runner.add_tsps(AntColonySystem, ap1, c1, runs, 'acs-1') runner.add_tsps(AntColonySystem, ap2, c2, runs, 'acs-2') runner.run() runner.save_results(filename=filename, output_folder=output_folder) fig = GenericFig(ylabel="Vidutinis atstumas", size=120) fig.next_subplot("ACS, miestu - " + str(cities) + ", n = " + str(runs)) fig.add_averaged_x_plot_values(runner.results['acs-1']['all_distances'], line='r-', label="ants = " + str(antz[0])) fig.add_averaged_x_plot_values(runner.results['acs-2']['all_distances'], line='g-', label="ants = " + str(antz[1])) fig.set_legend() fig.set_ylabel('') fig.next_subplot("ACS, miestu - " + str(cities) + ", n = " + str(runs)) fig.add_averaged_x_plot_values(runner.results['acs-1']['all_distances'], line='r-', label="ants = " + str(antz[0])) fig.add_averaged_x_plot_values(runner.results['acs-2']['all_distances'], line='g-', label="ants = " + str(antz[1])) fig.set_xscale('log') fig.save(filename=filename + "_a", output_folder=output_folder)
def runner(): return TSPSRunner(g_vln())
def _genetic_acs_test_type_1_(graph, runs, ants, chromosomes, iterations, ant_iterations=1, subf="a"): cities = len(graph.nodes()) filename = str(cities) + "_genetic_acs_test" output_folder = base_folder + "1/" + subf c1 = cond() c1[CONDITION_GENERATION] = iterations c2 = cond() c2[CONDITION_GENERATION] = ant_iterations ap = { 'beta': 30, 'alpha': 0.1, 'ro': 0.1, 'tau0': 10 } ap1 = deepcopy(ap) ap1['ants'] = ants acs1 = AntColonySystem(graph, ap1, c2, absolute_mute=True) ap2 = deepcopy(ap) ap2['ants'] = int(ants / 2) acs2 = AntColonySystem(graph, ap2, c2, absolute_mute=True) gp = { 'chromosomes': chromosomes, 'cr': 0.1, 'mr': 0.5, 'crossover_method': CROSSOVER_OX, 'mutation_method': MUTATION_SWAP } gp11 = deepcopy(gp) gp11['selection_method'] = SELECTION_ROULETE gp11['use_acs'] = acs1 gp12 = deepcopy(gp) gp12['selection_method'] = SELECTION_ROULETE gp12['use_acs'] = acs2 gp13 = deepcopy(gp) gp13['selection_method'] = SELECTION_ROULETE gp21 = deepcopy(gp) gp21['selection_method'] = SELECTION_RANK gp21['use_acs'] = acs1 gp22 = deepcopy(gp) gp22['selection_method'] = SELECTION_RANK gp22['use_acs'] = acs2 gp23 = deepcopy(gp) gp23['selection_method'] = SELECTION_RANK runner = TSPSRunner(graph) runner.add_tsps(Genetic, gp11, c1, runs, 'ga-acs-1.0-roulete') runner.add_tsps(Genetic, gp12, c1, runs, 'ga-acs-0.5-roulete') runner.add_tsps(Genetic, gp13, c1, runs, 'ga-roulete') runner.add_tsps(Genetic, gp21, c1, runs, 'ga-acs-1.0-rank') runner.add_tsps(Genetic, gp22, c1, runs, 'ga-acs-0.5-rank') runner.add_tsps(Genetic, gp23, c1, runs, 'ga-rank') runner.run() runner.save_results(filename=filename, output_folder=output_folder) fig = GenericFig(ylabel="Vidutinis atstumas", size=120) fig.next_subplot("GA-ACS, miestu - " + str(cities) + ", ant-iteration = " + str(ant_iterations) + ",n = " + str(runs)) fig.add_averaged_x_plot_values(runner.results['ga-acs-1.0-roulete'] ['all_distances'], line='r-', label='roulete_ants_' + str(ants)) fig.add_averaged_x_plot_values(runner.results['ga-acs-0.5-roulete'] ['all_distances'], line='r--', label='roulete_ants_' + str(ants / 2)) fig.add_averaged_x_plot_values(runner.results['ga-roulete'] ['all_distances'], line=':r', label='roulete') fig.add_averaged_x_plot_values(runner.results['ga-acs-1.0-rank'] ['all_distances'], line='g-', label='rank_ants_' + str(ants)) fig.add_averaged_x_plot_values(runner.results['ga-acs-0.5-rank'] ['all_distances'], line='g--', label='rank_ants_' + str(ants / 2)) fig.add_averaged_x_plot_values(runner.results['ga-rank'] ['all_distances'], line=':g', label='rank') fig.set_legend() fig.set_ylabel('') fig.next_subplot() fig.add_averaged_x_plot_values(runner.results['ga-acs-1.0-roulete'] ['all_distances'], line='r-', label='roulete_ants_' + str(ants)) fig.add_averaged_x_plot_values(runner.results['ga-acs-0.5-roulete'] ['all_distances'], line='r--', label='roulete_ants_' + str(ants / 2)) fig.add_averaged_x_plot_values(runner.results['ga-roulete'] ['all_distances'], line=':r', label='roulete') fig.add_averaged_x_plot_values(runner.results['ga-acs-1.0-rank'] ['all_distances'], line='g-', label='rank_ants_' + str(ants)) fig.add_averaged_x_plot_values(runner.results['ga-acs-0.5-rank'] ['all_distances'], line='g--', label='rank_ants_' + str(ants / 2)) fig.add_averaged_x_plot_values(runner.results['ga-rank'] ['all_distances'], line=':g', label='rank') fig.set_xscale('log') fig.save(filename=filename, output_folder=output_folder) fig = GenericFig(ylabel="Vidutinis atstumas", size=120) fig.next_subplot("GA-ACS, miestu - " + str(cities) + ", ant-iteration = " + str(ant_iterations) + ",n = " + str(runs)) fig.add_averaged_x_plot_values(runner.results['ga-acs-1.0-roulete'] ['all_distances'], line='r-', label='roulete_ants_' + str(ants)) fig.add_averaged_x_plot_values(runner.results['ga-acs-0.5-roulete'] ['all_distances'], line='r--', label='roulete_ants_' + str(ants / 2)) fig.add_averaged_x_plot_values(runner.results['ga-acs-1.0-rank'] ['all_distances'], line='g-', label='rank_ants_' + str(ants)) fig.add_averaged_x_plot_values(runner.results['ga-acs-0.5-rank'] ['all_distances'], line='g--', label='rank_ants_' + str(ants / 2)) fig.set_legend() fig.set_ylabel('') fig.next_subplot() fig.add_averaged_x_plot_values(runner.results['ga-acs-1.0-roulete'] ['all_distances'], line='r-', label='roulete_ants_' + str(ants)) fig.add_averaged_x_plot_values(runner.results['ga-acs-0.5-roulete'] ['all_distances'], line='r--', label='roulete_ants_' + str(ants / 2)) fig.add_averaged_x_plot_values(runner.results['ga-acs-1.0-rank'] ['all_distances'], line='g-', label='rank_ants_' + str(ants)) fig.add_averaged_x_plot_values(runner.results['ga-acs-0.5-rank'] ['all_distances'], line='g--', label='rank_ants_' + str(ants / 2)) fig.set_xscale('log') fig.save(filename=filename + "_b", output_folder=output_folder)
def _genetic_sa_test_type_1_(graph, runs, iterations, chromosomes, selection, subf="a", mr=0.5): cities = len(graph.nodes()) filename = str(cities) + "_genetic_sa_test_1_" + selection output_folder = base_folder + "1/" + subf c = cond() c[CONDITION_GENERATION] = iterations gp1 = g_pars(chromosomes, MUTATION_SWAP, selection, mr) gp2 = g_pars(chromosomes, MUTATION_SA, selection, mr) gp3 = g_pars(chromosomes, MUTATION_SA_INSERT, selection, mr) gp4 = g_pars(chromosomes, MUTATION_SA_REVERSE, selection, mr) runner = TSPSRunner(graph) runner.add_tsps(Genetic, gp1, c, runs, 'm-swap') runner.add_tsps(Genetic, gp2, c, runs, 'm-sa') runner.add_tsps(Genetic, gp3, c, runs, 'm-sa-i') runner.add_tsps(Genetic, gp4, c, runs, 'm-sa-r') runner.run() runner.save_results(filename=filename, output_folder=output_folder) fig = GenericFig(ylabel="Vidutinis atstumas", size=120) fig.next_subplot("GA-SA, " + str(cities) + " miestu, n = " + str(runs)) fig.add_averaged_x_plot_values(runner.results['m-swap']['all_distances'], line='r-', label='m-swap') fig.add_averaged_x_plot_values(runner.results['m-sa']['all_distances'], line='g-', label='m-sa') fig.add_averaged_x_plot_values(runner.results['m-sa-i']['all_distances'], line='c-', label='m-sa-i') fig.add_averaged_x_plot_values(runner.results['m-sa-r']['all_distances'], line='y-', label='m-sa-r') fig.set_legend() fig.set_ylabel('') fig.next_subplot("GA-SA, " + str(cities) + " miestu, n = " + str(runs)) fig.add_averaged_x_plot_values(runner.results['m-swap']['all_distances'], line='r-', label='m-swap') fig.add_averaged_x_plot_values(runner.results['m-sa']['all_distances'], line='g-', label='m-sa') fig.add_averaged_x_plot_values(runner.results['m-sa-i']['all_distances'], line='c-', label='m-sa-i') fig.add_averaged_x_plot_values(runner.results['m-sa-r']['all_distances'], line='y-', label='m-sa-r') fig.set_xscale('log') fig.save(filename=filename, output_folder=output_folder)