Пример #1
0
 def get_results(self):
     for f in self.parent.upper_frame.right_frame.list_files:
         structure = read_file(join("Data/all", f), f in listFiles)
         if self.parent.upper_frame.left_frame.algo.get() == 0:
             preferences = structure["preferences"]
             if self.parent.upper_frame.left_frame.filtered.get():
                 preferences, nb_voters, nb_unique = remove_last_ballots(preferences)
             candidates = structure["candidates"]
             ensemble, best = bnb(len(preferences), preferences, candidates)
             axes, card = find_axes2(best[0][0], candidates)
             axes = filter_symmetric_axes(axes)
             for axis in axes:
                 self.results.append((f, axis))
         else:
             if self.parent.upper_frame.left_frame.dissimilarity.get() == 0:
                 dissimilarity_function = dissimilarity_and_n
             elif self.parent.upper_frame.left_frame.dissimilarity.get() == 1:
                 dissimilarity_function = dissimilarity_and_or
             else:
                 dissimilarity_function = dissimilarity_over_over
             if f in listFiles:
                 t, permutations = find_axis_from_structure(structure, dissimilarity_function,
                                                            self.parent.upper_frame.left_frame.weighted.get())
             else:
                 t, permutations = find_axis_from_structure(structure, dissimilarity_function,
                                                            self.parent.upper_frame.left_frame.weighted.get(),
                                                            unwanted_candidates=[2, 3, 7, 11])
             permutations = filter_symmetric_axes(permutations[1])
             for permutation in permutations:
                 self.results.append((f, permutation))
 def get_results(self):
     for f in self.parent.upper_frame.right_frame.list_files:
         structure = read_file(join("Data/all", f), f in listFiles)
         if self.parent.upper_frame.left_frame.algo.get() == 0:
             preferences = structure["preferences"]
             if self.parent.upper_frame.left_frame.filtered.get():
                 preferences, nb_voters, nb_unique = remove_last_ballots(
                     preferences)
             candidates = structure["candidates"]
             ensemble, best = bnb(len(preferences), preferences, candidates)
             axes, card = find_axes2(best[0][0], candidates)
             axes = filter_symmetric_axes(axes)
             for axis in axes:
                 self.results.append((f, axis))
         else:
             if self.parent.upper_frame.left_frame.dissimilarity.get() == 0:
                 dissimilarity_function = dissimilarity_and_n
             elif self.parent.upper_frame.left_frame.dissimilarity.get(
             ) == 1:
                 dissimilarity_function = dissimilarity_and_or
             else:
                 dissimilarity_function = dissimilarity_over_over
             if f in listFiles:
                 t, permutations = find_axis_from_structure(
                     structure, dissimilarity_function,
                     self.parent.upper_frame.left_frame.weighted.get())
             else:
                 t, permutations = find_axis_from_structure(
                     structure,
                     dissimilarity_function,
                     self.parent.upper_frame.left_frame.weighted.get(),
                     unwanted_candidates=[2, 3, 7, 11])
             permutations = filter_symmetric_axes(permutations[1])
             for permutation in permutations:
                 self.results.append((f, permutation))
Пример #3
0
def all_files_to_latex(directory,
                       files_list,
                       axes_list,
                       names_list,
                       dissimilarity_function=dissimilarity_over_over,
                       weighted=False,
                       strict=None,
                       unwanted_candidates=[]):
    """
    Returns a string with all graphics from a list of files
    :param directory: path to the directory where all the files in files_list are
    :param files_list: list of files
    :param axes_list: axes corresponding to the files
    :param names_list: names to give to the figures, for instance the wards corresponding to the elections
    :param dissimilarity_function: function to use to calculate dissimilarity between 2 candidates
    :param weighted: if True, matrices scores are calculated with the weighted gradient
    :param strict: list of booleans, True if the corresponding file in the list is depicting strict preferences
    :param unwanted_candidates: list of candidates to exclude from the search
    :return: LaTeX code to display the graphs corresponding to all the files
    """
    if not strict:
        strict = [False] * len(files_list)
    res = ""
    for i in range(len(files_list)):
        print(files_list[i])
        structure = read_file(join(directory, files_list[i]), strict[i])
        res += axes_to_latex_graph(structure, axes_list[i], names_list[i],
                                   dissimilarity_function, weighted,
                                   unwanted_candidates)
    return res
Пример #4
0
def calculate_bnb(self, file, vrow, filtered):
    structure = read_file("Data/all/" + str(file), file in listFiles)
    if not filtered:
        preferences = structure["preferences"]
        candidates = structure["candidates"]
        Label(self, text=str(structure["nb_voters"])).grid(row=vrow, column=3)
        Label(self, text=str(structure["nb_unique_orders"])).grid(row=vrow,
                                                                  column=5)
    else:
        preferences_bis = structure["preferences"]
        preferences, nb_voters, nb_unique = remove_last_ballots(
            preferences_bis)
        candidates = structure["candidates"]
        Label(self, text=str(nb_voters)).grid(row=vrow, column=3)
        Label(self, text=str(nb_unique)).grid(row=vrow, column=5)

    t1 = time()
    ensemble, best = bnb(len(preferences), preferences, candidates)
    t2 = time()
    Label(self, text=str(best[1])).grid(row=vrow, column=7)
    Label(self, text=str(len(best[0][0]))).grid(row=vrow, column=9)
    if not filtered:
        Label(self, text=str(best[1] * 100.0 / structure["nb_voters"])).grid(
            row=vrow, column=11)
    else:
        Label(self, text=str(best[1] * 100.0 / nb_voters)).grid(row=vrow,
                                                                column=11)
    axes, card = find_axes2(best[0][0], candidates)

    Label(self, text=str(len(axes))).grid(row=vrow, column=13)
    Label(self, text=str(t2 - t1)).grid(row=vrow, column=15)
Пример #5
0
def calculate_bnb(self, file, vrow, filtered):
    structure = read_file("Data/all/" + str(file), file in listFiles)
    if not filtered:
        preferences = structure["preferences"]
        candidates = structure["candidates"]
        Label(self, text=str(structure["nb_voters"])).grid(row=vrow, column=3)
        Label(self, text=str(structure["nb_unique_orders"])).grid(row=vrow, column=5)
    else:
        preferences_bis = structure["preferences"]
        preferences, nb_voters, nb_unique = remove_last_ballots(preferences_bis)
        candidates = structure["candidates"]
        Label(self, text=str(nb_voters)).grid(row=vrow, column=3)
        Label(self, text=str(nb_unique)).grid(row=vrow, column=5)

    t1 = time()
    ensemble, best = bnb(len(preferences), preferences, candidates)
    t2 = time()
    Label(self, text=str(best[1])).grid(row=vrow, column=7)
    Label(self, text=str(len(best[0][0]))).grid(row=vrow, column=9)
    if not filtered:
        Label(self, text=str(best[1] * 100.0 / structure["nb_voters"])).grid(row=vrow, column=11)
    else:
        Label(self, text=str(best[1] * 100.0 / nb_voters)).grid(row=vrow, column=11)
    axes, card = find_axes2(best[0][0], candidates)

    Label(self, text=str(len(axes))).grid(row=vrow, column=13)
    Label(self, text=str(t2 - t1)).grid(row=vrow, column=15)
Пример #6
0
def axes_to_latex_graph_bnb(f, axis, name=None):
    """
    Returns the LaTeX code to display the axis
    :param structure: structure extracted from a file or directory
    :param axis: axis corresponding to the file, according to wikipedia
    :param name: name that will be given to the figures
    :param dissimilarity_function: function to use to calculate dissimilarity between 2 candidates
    :param weighted: if True, matrices scores are calculated with the weighted gradient
    :param unwanted_candidates: list of candidates to exclude from the search
    :return: string containing the LaTeX code
    """

    structure = read_file(f)
    preferences = structure["preferences"]
    candidates = [i + 1 for i in range(len(structure["candidates"]))]
    bb, best = bnb(len(preferences), preferences, candidates)
    permutations, card = find_axes2(best[0][0], candidates)

    matches = get_matches(axis)
    length_unit = str(len(permutations[0]) + 1)
    res = "\\section{" + str(name) + "}" if name else ""
    res += "\\begin{center}\n"

    for permutation in permutations:
        # beginning of the environment
        res += "\\begin{figure}\n"
        res += "\\begin{tikzpicture}[x=\\textwidth/" + length_unit + ", y=\\textwidth/" + length_unit + "]\n"
        # Adding the x and y axes
        res += "\\draw[->, >=latex] (0,0) -- (" + length_unit + ",0);\n"
        res += "\\draw[->, >=latex] (0,0) -- (0, 5.5);\n"
        #res += "\\draw[->, >=latex] (0,0) -- (" + length_unit + ",0);\n"
        #res += "\\draw[->, >=latex] (0,0) -- (0," + length_unit + ");\n"
        # Adding the legend below the x axis
        for i in range(len(permutation)):
            res += "\\draw (" + str(i + 1) + ",-0.1) node[below] {" + str(
                permutation[i]) + "};\n"

        res += "\\draw plot[ultra thick]coordinates{"
        temp = ""  # string for the circles marking the dots
        for i in range(len(permutation)):
            if permutation[i] in matches:
                res += "(" + str(i + 1) + "," + str(
                    matches[permutation[i]]) + ")"
                temp += "\\draw[fill=black] (" + str(i + 1) + "," + str(
                    matches[permutation[i]]) + ") circle (0.1);\n"
        res += "};\n"
        res += temp  # ading the circles
        res += "\\end{tikzpicture}\n"
        if name:
            res += "\\caption{Permutation " + str(
                permutations.index(permutation)) + " of " + name + "}\n"
        res += "\\end{figure}\n"

    res += "\\end{center}\n\\clearpage\n"

    return res
Пример #7
0
def calculate_seriation(self, file, vrow, dissimilarity, weighted):
    structure = read_file("Data/all/" + str(file), file in listFiles)
    Label(self, text=str(structure["nb_voters"])).grid(row=vrow, column=3)
    Label(self, text=str(structure["nb_unique_orders"])).grid(row=vrow, column=5)
    if file in listFiles:
        t, axes = find_axis_from_structure(structure, dissimilarity, weighted)
    else:
        t, axes = find_axis_from_structure(structure, dissimilarity, weighted, unwanted_candidates=[2, 3, 7, 11])
    Label(self, text=str(len(axes[1]))).grid(row=vrow, column=7)
    Label(self, text=str(t)).grid(row=vrow, column=9)
Пример #8
0
def axes_to_latex_graph_bnb(f, axis, name=None):
    """
    Returns the LaTeX code to display the axis
    :param structure: structure extracted from a file or directory
    :param axis: axis corresponding to the file, according to wikipedia
    :param name: name that will be given to the figures
    :param dissimilarity_function: function to use to calculate dissimilarity between 2 candidates
    :param weighted: if True, matrices scores are calculated with the weighted gradient
    :param unwanted_candidates: list of candidates to exclude from the search
    :return: string containing the LaTeX code
    """

    structure = read_file(f)
    preferences = structure["preferences"]
    candidates = [i+1 for i in range(len(structure["candidates"]))]
    bb, best = bnb(len(preferences), preferences, candidates)
    permutations, card = find_axes2(best[0][0], candidates)

    matches = get_matches(axis)
    length_unit = str(len(permutations[0]) + 1)
    res = "\\section{" + str(name) + "}" if name else ""
    res += "\\begin{center}\n"

    for permutation in permutations:
        # beginning of the environment
        res += "\\begin{figure}\n"
        res += "\\begin{tikzpicture}[x=\\textwidth/" + length_unit + ", y=\\textwidth/" + length_unit + "]\n"
        # Adding the x and y axes
        res += "\\draw[->, >=latex] (0,0) -- (" + length_unit + ",0);\n"
        res += "\\draw[->, >=latex] (0,0) -- (0, 5.5);\n"
        #res += "\\draw[->, >=latex] (0,0) -- (" + length_unit + ",0);\n"
        #res += "\\draw[->, >=latex] (0,0) -- (0," + length_unit + ");\n"
        # Adding the legend below the x axis
        for i in range(len(permutation)):
            res += "\\draw (" + str(i+1) + ",-0.1) node[below] {" + str(permutation[i]) + "};\n"

        res += "\\draw plot[ultra thick]coordinates{"
        temp = ""  # string for the circles marking the dots
        for i in range(len(permutation)):
            if permutation[i] in matches:
                res += "(" + str(i+1) + "," + str(matches[permutation[i]]) + ")"
                temp += "\\draw[fill=black] (" + str(i+1) + "," + str(matches[permutation[i]]) + ") circle (0.1);\n"
        res += "};\n"
        res += temp  # ading the circles
        res += "\\end{tikzpicture}\n"
        if name:
            res += "\\caption{Permutation " + str(permutations.index(permutation)) + " of " + name + "}\n"
        res += "\\end{figure}\n"

    res += "\\end{center}\n\\clearpage\n"

    return res
Пример #9
0
def calculate_seriation(self, file, vrow, dissimilarity, weighted):
    structure = read_file("Data/all/" + str(file), file in listFiles)
    Label(self, text=str(structure["nb_voters"])).grid(row=vrow, column=3)
    Label(self, text=str(structure["nb_unique_orders"])).grid(row=vrow,
                                                              column=5)
    if file in listFiles:
        t, axes = find_axis_from_structure(structure, dissimilarity, weighted)
    else:
        t, axes = find_axis_from_structure(structure,
                                           dissimilarity,
                                           weighted,
                                           unwanted_candidates=[2, 3, 7, 11])
    Label(self, text=str(len(axes[1]))).grid(row=vrow, column=7)
    Label(self, text=str(t)).grid(row=vrow, column=9)
Пример #10
0
def example_file():
    structure = read_file(sys.argv[1])
    preferences = structure["preferences"]
    candidates = [i+1 for i in range(len(structure["candidates"]))]
    print("Preferences : " + str(preferences))
    print("Candidats : " + str(candidates))
    t1 = time()
    bb, best = bnb(len(preferences), preferences, candidates)
    t2 = time()
    print("done")
    f = sys.argv[1].split(".")[0]  + "_resultat.txt"
    wfile = open(f, 'w')
    wfile.write("Plus large ensemble cohérent : ")
    for bull in best[0][0]:
        wfile.write(str(bull) + "\n")
    wfile.write("Resultat : " + str(best[1]) + "\n")
    wfile.write("Duration : " + str(t2-t1) + "\n")
    wfile.write("Axes :\n")
    axes, card = find_axes2(best[0][0], candidates)
    if axes:
        for a in axes:
            wfile.write(str(a)+"\n")
    wfile.close()
Пример #11
0
def all_files_to_latex(directory, files_list, axes_list, names_list,
                       dissimilarity_function=dissimilarity_over_over, weighted=False, strict=None,
                       unwanted_candidates=[]):
    """
    Returns a string with all graphics from a list of files
    :param directory: path to the directory where all the files in files_list are
    :param files_list: list of files
    :param axes_list: axes corresponding to the files
    :param names_list: names to give to the figures, for instance the wards corresponding to the elections
    :param dissimilarity_function: function to use to calculate dissimilarity between 2 candidates
    :param weighted: if True, matrices scores are calculated with the weighted gradient
    :param strict: list of booleans, True if the corresponding file in the list is depicting strict preferences
    :param unwanted_candidates: list of candidates to exclude from the search
    :return: LaTeX code to display the graphs corresponding to all the files
    """
    if not strict:
        strict = [False] * len(files_list)
    res = ""
    for i in range(len(files_list)):
        print(files_list[i])
        structure = read_file(join(directory, files_list[i]), strict[i])
        res += axes_to_latex_graph(structure, axes_list[i], names_list[i],
                                   dissimilarity_function, weighted, unwanted_candidates)
    return res