Beispiel #1
0
def compute_qubo_en(size, index_f, use_qa=False):
    matching = get_smti(index_f, size)

    solver = QUBO_SMTI(matching, mode="np").pre_process()
    solution_lp = LP_smti(matching).solve()

    x = ut.compute_qubo_vector_lp(solution_lp, solver)
    lp_en = solver.compute_energy(x)

    if use_qa:
        qa_solution = get_solution_qa(size, index_f, "smti")
        qa_en = min(qa_solution["energy"])
    else:
        qa_en = 0.0

    solution = QUBO_SMTI(matching).solve()

    qbsolv_en = solution.energy

    (opt_stable, opt_size) = solution_lp.is_stable()
    (qubo_stable, qubo_size) = solution.is_stable()
    return {"size": size, "index_f": index_f,
            "qa_en": qa_en, "lp_en": lp_en, "qbsolv_en": qbsolv_en,
            "opt_size": opt_size, "opt_stable": opt_stable, "qubo_size": qubo_size, "qubo_stable": qubo_stable,
            "opt_en": solver.get_optimal_energy(opt_size), "min_valid_en": solver.get_optimal_energy(0)}
Beispiel #2
0
def measure_time_instance(size, index_f, times_repeat=10):
    """
    Measure one instance with all available algorithms:
        - qubo
        - lp
        - shiftbrk
        - kiraly2
    time gets measured by timeit in seconds
    :param size:
    :param index_f:
    :param times_repeat:
    :return:
    """
    solver_types = ["qubo", "lp", "shiftbrk", "kiraly"]
    setup = create_setup(size, index_f)
    result = {"size": size, "index_f": index_f}
    for solver_type in solver_types:
        d_t, d_t_var = measure_solving(solver_type,
                                       setup,
                                       times_repeat=times_repeat)
        solution = eval_algorithm(get_smti(index_f=index_f, size=size),
                                  solver_type)
        (stable, size_res) = solution.is_stable()
        result[f"{solver_type}_dt[s]"] = d_t
        result[f"{solver_type}_dt_var[%]"] = 100 * d_t_var / d_t
        result[f"{solver_type}_stable"] = stable
        result[f"{solver_type}_size"] = size_res
    return result
def plot_equidistribution_smti(sizes, samples_per_size):
    """
    A plot that shows the distribution of sizes/ties per size category for smti dataset
    """

    ###########################################
    # Plot SIZES
    ###########################################
    average_sizes = {size: [] for size in sizes}
    for size in sizes:
        for index_f in range(samples_per_size):
            matching = get_smti(index_f, size)
            average_sizes[size].append(matching.average_pref_list_len())
    index = 3
    fig, ax = plt.subplots()
    for size in sizes:
        ax.scatter(average_sizes[size], [index for _ in average_sizes[size]])
        index += 1
    plt.xlim((0, sizes_smti[-1]))
    plt.xlabel("average preflist size")
    plt.ylabel("Matching size")
    plt.show()

    ###########################################
    # Plot TIES
    ###########################################
    average_ties = {size: [] for size in sizes}
    for size in sizes:
        for index_f in range(samples_per_size):
            matching = get_smti(index_f, size)
            average_ties[size].append(matching.average_tie_len())
    index = 3
    fig, ax = plt.subplots()
    for size in sizes:
        ax.scatter(average_ties[size], [index for _ in average_sizes[size]])
        index += 1
    plt.xlim((0, sizes_smti[-1]))
    plt.xlabel("average tie size per instance")
    plt.ylabel("Matching size")
    plt.show()
Beispiel #4
0
    def test_storage_smti(self):
        size = 5
        p1 = 0.5
        p2 = 0.5
        num = 100

        for index_f in range(num):
            m_created = create_and_save_smti(index_f, size, p1, p2)
            m_read = get_smti(index_f, size)
            self.assertEqual(m_created.males, m_read.males)
            self.assertEqual(m_created.females, m_read.females)
            self.assertEqual(m_created.males_pref, m_read.males_pref)
            self.assertEqual(m_created.females_pref, m_read.females_pref)
            self.assertEqual(m_created.solutions, m_read.solutions)
            self.assertEqual(m_created.size, m_read.size)
            self.assertEqual(m_read.meta, {"p1": p1, "p2": p2})
Beispiel #5
0
def eval_algorithm(size, index_f, solver_type):
    matching = get_smti(index_f, size)
    if solver_type == "qbsolv":
        return QUBO_SMTI(matching).solve().is_stable()
    elif solver_type == "qa":
        solutions = QUBO_SMTI(matching).solve_qa(verbose=False)
        store_qa_solution(solutions, size, index_f, "smti")
        min_solution = solutions[solutions.energy == solutions.energy.min()]
        log.info(solutions)
        log.info(min_solution)
        return Solution(matching, min_solution["match"].to_numpy()[0]).is_stable()
    elif solver_type == "lp":
        return LP_smti(matching).solve().is_stable()
    elif solver_type == "shiftbrk":
        return ShiftBrk(matching).solve().is_stable()
    elif solver_type == "kiraly":
        return Kirialy2(matching).solve().is_stable()
    else:
        raise Exception(f"unknown solver_type: {solver_type}")