def draw_histogram(out_dir, multigoal_rule, k, percentages, distribution, reps, n, m, method, threshold=None): distribution_name = get_distribution_name(distribution) perc = '_'.join([str(p) for p in percentages]) winner_filename = '{}_{}_{}_k{}_n{}_m{}_{}'.format(multigoal_rule.__name__, distribution_name, perc, k, n, m, method) file_path = os.path.join(out_dir, winner_filename) mw2d_generate_histogram(file_path, reps) histogram_filename = file_path + '.hist' mw2d_draw_histogram(histogram_filename, threshold) os.remove(histogram_filename)
def draw_pareto_chart_from_winner_files(current_dir, m, n, k, multigoal_rule, distribution, distribution_params=None): print("{}, k={}".format(multigoal_rule.__name__, k)) # We assume that there are "repetitions" files generated for each threshold. rule_name = multigoal_rule.__name__ distribution_name = get_distribution_name(distribution) rules = get_multigoal_rules(multigoal_rule) if not rules: return xy = {} for dir_name in os.listdir(current_dir): dir_pattern = '{}_{}_(\d+)_\d+_k{}_n{}_m{}'.format(rule_name, distribution_name, k, n, m) r1_match = re.match(dir_pattern, dir_name) if r1_match is None: continue r1 = r1_match.group(1) for filename in os.listdir(os.path.join(current_dir, dir_name)): rep = get_repetition_from_filename(dir_name, filename) if not rep: continue win_filename = os.path.join(current_dir, dir_name, filename) best_filename = '{}_{}.best'.format(dir_name, rep) best_filename = os.path.join(current_dir, dir_name, best_filename) scores = read_scores(win_filename) best = read_scores(best_filename) approx = scores[1] / best[1] if r1 in xy: xy[r1].append(approx) else: xy[r1] = [approx] xy_list = list(xy.items()) xy_list = sorted(xy_list, key=lambda e: int(e[0])) x = [int(x) for x, _ in xy_list] y_min = [np.min(ys) for _, ys in xy_list] if distribution_params is None: filename = '{}_{}_k{}_n{}_m{}'.format(rule_name, distribution_name, k, n, m) else: distribution_params_string = '_'.join([dpk + str(dpv) for dpk, dpv in distribution_params.items()]) distribution_params_string = distribution_params_string.replace('.', '') filename = '{}_{}_{}_k{}_n{}_m{}'.format(rule_name, distribution_name, distribution_params_string, k, n, m) title = "voters: {}, candidates: {}, committee size: {}".format(n, m, k) plot(filename, x, y_min, rules, title=title)
def draw_approximation_charts(experiment, ms, k_percs, distribution, approximations): out_dir = experiment.get_generated_dir_path() distribution_name = get_distribution_name(distribution) for im, m in enumerate(ms): filename = os.path.join(out_dir, 'approx_{}_m{}'.format(distribution_name, m)) ks = k_percs * m axes = plt.gca() axes.set_xlim([0, m]) axes.set_ylim([0, 1.1]) plt.xlabel('k') plt.ylabel('approximation') plt.plot(ks, approximations['Approx_Greedy'][im, :]) plt.plot(ks, approximations['Approx_P'][im, :]) plt.legend(['Greedy', 'P']) plt.title('Approximation in CC+kB') plt.savefig(filename) plt.clf()
def draw_transition_from_winner_files(current_dir, m, n, k, multigoal_rule, distribution, repetitions): print("{}, k={}".format(multigoal_rule.__name__, k)) # We assume that there are "repetitions" files generated for each threshold. rule_name = multigoal_rule.__name__ distribution_name = get_distribution_name(distribution) rules = get_multigoal_rules(multigoal_rule) if not rules: return for dir_name in os.listdir(current_dir): dir_path = os.path.join(current_dir, dir_name) if not os.path.isdir(dir_path): continue dir_pattern = '{}_{}_(\d+)_(\d+)_k{}_n{}_m{}'.format(rule_name, distribution_name, k, n, m) r1_r2_match = re.match(dir_pattern, dir_name) if r1_r2_match is None: continue percentages = r1_r2_match.group(1), r1_r2_match.group(2) draw_histogram(dir_path, multigoal_rule, k, percentages, distribution, repetitions, n, m, 'ILP')
m = 100 # candidates number n = 100 # voters number repetitions = 60 k_ccs = range(0, k + 1) methods = ['Approx_P', 'Approx_Greedy'] multigoal_rule = MultigoalCCBorda distribution = generate_uniform experiments = [] for _ in range(repetitions): config = MultigoalExperimentConfig() config.add_candidates(distribution(-3, -3, 3, 3, m, 'None')) config.add_voters(distribution(-3, -3, 3, 3, n, 'None')) config.set_distribution_name(get_distribution_name(distribution)) experiment = MultigoalExperiment(config) experiment.set_multigoal_election(MultigoalCCBorda, k, percent_thresholds=[0, 0]) experiments.append(experiment) for repetition, experiment in enumerate(experiments): for i, k_cc in enumerate(k_ccs): file_prefix = 'kcc' + str(k_cc) experiment.set_filename(file_prefix) experiment.run(n=1, n_start=repetition + 1, methods=methods, k_cc=k_cc,
draw_pareto_chart_from_winner_files from pmp.multigoal.helpers import get_distribution_name from pmp.rules import MultigoalBlocBorda current_file = os.path.abspath(__file__) current_dir = os.path.dirname(current_file) repetitions = 4 k = 10 n = 100 m = 100 phi = 0.1 multigoal_rule = MultigoalBlocBorda distribution = mallows distribution_name = get_distribution_name(distribution) start = 90 step = 2 dir_name = 'results_{}_{}_{}_{}_phi{}_k{}_n{}_m{}'.format( start, step, multigoal_rule.__name__, distribution_name, phi, k, n, m) configs = [] for _ in range(repetitions): config = MultigoalExperimentConfig() config.mallows(phi, m, n) config.set_distribution_name(distribution_name) configs.append(config) generate_winner_files_for_pareto(dir_name,