Esempio n. 1
0
def get_multiple_values(n, a, b, k, beta, repeat, action, qu):
    val_list = []
    for _ in range(repeat):
        if action == 'normal':
            G = sbm_graph(n, 2, a, b)
            val_list.append(get_average(G, a, b, beta))
        elif action == 'mixed':
            G = sbm_graph(n, k, a, b)
            val_list.append(get_average_k(G, a, b, k, beta))
    qu.put(val_list)
Esempio n. 2
0
def SDP_random_matrix():
    n = 500
    n2 = int(n / 2)
    a = 11.66
    b = 4
    p0 = 0.4
    p1 = 0.7
    gamma = 6.0 # 5.0
    m = int(gamma * np.log(n))
    # gamma = m / np.log(n) * 1.0
    h_vector = np.zeros([n])

    tmp_1 = np.random.binomial(m, p0, size=n2)
    tmp_1 = tmp_1 * np.log(p0 / p1) + (m - tmp_1) * np.log((1 - p0)/ (1 - p1))
    h_vector[:n2] = tmp_1 / np.log (a / b)
    tmp_1 = np.random.binomial(m, p1, size=n2)
    tmp_1 = tmp_1 * np.log(p0 / p1) + (m - tmp_1) * np.log((1 - p0)/ (1 - p1))
    h_vector[n2:] = tmp_1 / np.log (a / b)

    G = sbm_graph(n, 2, a, b)
    B_matrix = construct_B(G)
    g = np.ones([n])
    g[n2:] = -1.0
    tmp_m = np.kron(h_vector, g) + np.kron(B_matrix @ g, g)
    block_matrix = np.diag(np.diag(tmp_m.reshape([n, n]))) - B_matrix
    values, _ = np.linalg.eig(block_matrix)
    tmp_2 = np.diag(np.kron(B_matrix @ g, g).reshape([n, n]))
    values_2, _ = np.linalg.eig(np.diag(tmp_2) - B_matrix)
    print(np.min(values), np.min(values_2))
    print(np.all(h_vector * g > 0))
    return np.min(values) > 0
Esempio n. 3
0
def acc_task(params, num_of_times, qu):
    acc = 0
    n, m, a, b, p0, p1 = params
    for _ in range(num_of_times):
        graph = sbm_graph(n, 2, a, b)
        gt = get_ground_truth(graph)
        a_b_ratio = a / b
        data = generate_data(gt, n, m, p0, p1)
        if os.environ.get('USE_CVX_TOOLBOX'):
            result_label = solve_sdp_si_cvx(graph,
                                            data,
                                            p0,
                                            p1,
                                            a_b_ratio,
                                            rho=0.1,
                                            max_iter=100,
                                            tol=1e-5)
        else:
            result_label = sdp2_si(graph,
                                   data,
                                   p0,
                                   p1,
                                   a_b_ratio,
                                   rho=0.1,
                                   max_iter=5000,
                                   tol=1e-4)
        current_acc = int(compare(gt, result_label))
        acc += current_acc
    qu.put(acc)
Esempio n. 4
0
def get_acc_sdp2(repeat, n, a, b, kappa, queue=None):
    acc = 0
    for _ in range(repeat):
        graph = sbm_graph(n, 2, a, b)
        results = sdp2(graph, kappa)
        acc += float(exact_compare_k(results, 2))
    acc /= repeat
    if queue:
        queue.put(acc)
    return acc
def estimator_once(n, k, a, b, repeat):
    square_error = 0
    for _ in range(repeat):
        G = sbm_graph(n, k, a, b)
        a_hat, b_hat = estimate_a_b(G, k)
        square_error += (a - a_hat)**2 + (b - b_hat)**2
    square_error /= repeat
    logging.info('n: {0}, k: {1}, a: {2}, b: {3}, error: {4}'.format(
        n, k, a, b, square_error))
    return square_error
Esempio n. 6
0
def task(repeat, n, k, a, b, alpha, beta, num_of_sibm_samples, m, _N, qu=None):
    if has_cpp_wrapper:
        total_acc = task_cpp_wrapper(repeat, n, k, a, b, alpha, beta,
                                     num_of_sibm_samples, m, _N)
        if qu is not None:
            qu.put(total_acc)
            return
        else:
            return total_acc
    total_acc = 0
    for _ in range(repeat):
        G = sbm_graph(n, k, a, b)
        sibm_object_list = []
        for _ in range(m):
            if k == 2:
                sibm = SIBM2(G, alpha, beta)
            else:
                sibm = SIBMk(G, alpha, beta, k)
            sibm.metropolis(N=_N)
            sibm_object_list.append(sibm)
        acc = 0

        candidates_samples = []
        for sibm in sibm_object_list:
            candidates_samples.append(sibm.sigma)
        for _ in range(num_of_sibm_samples):
            for sibm in sibm_object_list:
                sibm._metropolis_single()
            if k == 2:
                inner_acc = int(
                    exact_compare(majority_voting(candidates_samples)))
            else:
                inner_acc = int(
                    exact_compare_k(majority_voting_k(candidates_samples, k),
                                    k))
            acc += inner_acc

        acc /= num_of_sibm_samples
        total_acc += acc
    total_acc /= repeat
    if qu is not None:
        qu.put(total_acc)
        return
    else:
        return total_acc
Esempio n. 7
0
def animation_metropolis(n, k, a, b):
    G = sbm_graph(n, k, a, b)
    sibm = SIBM(G, k)
    # draw initial configuration with random labels
    skip_draw_count = 5
    N = 250
    energy_list = []
    # get initial energy
    energy_list.append(sibm._get_Hamiltonian())
    for i in range(N):
        for _ in range(skip_draw_count):
            sibm._metropolis_single()
        _animation_metropolis_inner(G, sibm.sigma, i)
        energy_list.append(sibm._get_Hamiltonian())
        # plot energy versus time(i)
        plt.plot(list(range(i + 2)), energy_list)
        plt.savefig('build/am/energy-%03d.png' % (i + 1))
        plt.clf()
Esempio n. 8
0
 def test_sdp2_svx(self):
     G = sbm_graph(100, 2, 16, 4)
     results = solve_sdp_cvx(G)
     labels_true = get_ground_truth(G)
     self.assertAlmostEqual(compare(results, labels_true), 1.0)
Esempio n. 9
0
 def test_sdp2(self):
     G = sbm_graph(100, 2, 16, 4)
     results = sdp2(G)
     print(results)
     labels_true = get_ground_truth(G)
     self.assertAlmostEqual(compare(results, labels_true), 1.0)