Beispiel #1
0
def hidden_lp(n, verbose=False, binary=False):
    print(f'n = {n}, Formulating LP')
    n_power = np.power(2, n)
    n_minus_power = np.power(2, n - 1)
    n_plus_power = np.power(2, n + 1)

    model = LpProblem("Hidden LP", LpMaximize)
    if binary:
        V = LpVariable.dicts("V", [i for i in range(n_power)],
                             lowBound=0,
                             cat='Binary')
    else:
        V = LpVariable.dicts("V", [i for i in range(n_power)], lowBound=0)

    model += (pulp.lpSum(V))

    model += (V[0] == 1)
    model += (V[n_power - 1] == 1)

    hidden_weight = get_hidden_weight_matrix(n)
    for col_idx in range(0, n_minus_power):
        col = hidden_weight[:, col_idx]
        edge_list = np.where(col > 0)[0]
        model += (pulp.lpSum([V[idx] for idx in edge_list]) <= 1)

    hidden_weight = get_hidden_weight_matrix(n + 1)
    for row_idx in range(0, n_plus_power):
        row = hidden_weight[row_idx, :]
        edge_list = np.where(row > 0)[0]
        model += (pulp.lpSum([V[idx] for idx in edge_list]) <= 1)

    # # add 3-cliques contraints
    # cliques = findclique(n)
    # for clique in cliques:
    #     model += (pulp.lpSum([V[idx] for idx in clique]) <= 1)

    print(f'n = {n}, Solving LP')
    model.solve()
    opt = value(model.objective)
    print(
        f'n={n}, opt={opt}, binary={binary}, status: {LpStatus[model.status]}')

    if verbose:
        cnt = 0
        for v in model.variables():
            if v.varValue > 0:
                bin_str = num_to_bin(int(v.name[2:]), n)
                vt_sum = compute_vt_sum(bin_str, raw=True)
                print(f'{v.name} = {v.varValue}, VT_sum = {vt_sum}')
                cnt += 1
        print(f'number of nonzeros = {cnt}')
Beispiel #2
0
def hidden_lp(n, verbose=False, binary=False):
    print(f'n = {n}, Formulating LP')
    n_power = np.power(2, n)
    n_minus_power = np.power(2, n - 1)

    model = LpProblem("Hidden LP", LpMaximize)
    if binary:
        V = LpVariable.dicts("V", [i for i in range(n_power)],
                             lowBound=0,
                             cat='Binary')
        fname = f'./lpsols/binary_lp_sol_n{n}.txt'
    else:
        V = LpVariable.dicts("V", [i for i in range(n_power)], lowBound=0)
        fname = f'./lpsols/lp_sol_n{n}.txt'

    model += (pulp.lpSum(V))

    for x in range(0, n_minus_power):
        bin_str = num_to_bin(x, n - 1)
        edge_list = []
        for idx in range(n):
            for bit in ['0', '1']:
                bin_added = bin_str[:idx] + bit + bin_str[idx:]
                num_added = bin_to_num(bin_added)
                if num_added not in edge_list:
                    edge_list.append(num_added)
        model += (pulp.lpSum([V[idx] for idx in edge_list]) <= 1)
    print(f'n = {n}, Solving LP')
    model.solve()
    opt = value(model.objective)
    print(f'n={n}, opt={opt}, status: {LpStatus[model.status]}')
    if verbose:
        cnt = 0
        for v in model.variables():
            print(v.name, '=', v.varValue)
            cnt += 1
        print(f'number of zeros = {cnt}')
    with open(fname, 'w') as f:
        f.write(f'n={n}, opt={opt}\n')
        for v in model.variables():
            f.write(f'{v.name}, {v.varValue}\n')
Beispiel #3
0
def edge_lp(n, verbose=False, binary=False):
    print(f'n = {n}, Formulating LP')
    n_power = np.power(2, n)
    n_minus_power = np.power(2, n-1)
    n_plus_power = np.power(2, n+1)

    model = LpProblem("Edge LP", LpMinimize)
    if binary:
        cat = 'Binary'
    else:
        cat = 'Continuous'

    E_minus = LpVariable.dicts("E_minus", [i for i in range(n_minus_power)], lowBound=0, cat=cat)
    E_plus = LpVariable.dicts("E_plus", [i for i in range(n_plus_power)], lowBound=0, cat=cat)

    model += (pulp.lpSum(E_minus) + pulp.lpSum(E_plus))
    
    minus_hidden_weight = get_hidden_weight_matrix(n)
    plus_hidden_weight = get_hidden_weight_matrix(n+1)
    for idx in range(0, n_power):
        minus_edge = minus_hidden_weight[idx, :]
        minus_edge_list = np.where(minus_edge>0)[0]

        plus_edge = plus_hidden_weight[:, idx]
        plus_edge_list = np.where(plus_edge>0)[0]

        bin_str = num_to_bin(idx, n)
        if compute_vt_sum(bin_str) == 0:
            model += (pulp.lpSum([E_minus[edge] for edge in minus_edge_list] + [E_plus[edge] for edge in plus_edge_list]) == 1)
        else:
            model += (pulp.lpSum([E_minus[edge] for edge in minus_edge_list] + [E_plus[edge] for edge in plus_edge_list]) == 1)

    print(f'n = {n}, Solving LP')
    model.solve()
    opt = value(model.objective)
    print(f'n={n}, opt={opt}, binary={binary}, status: {LpStatus[model.status]}')
Beispiel #4
0
def hidden_lp(n, verbose=False):
    print(f'n = {n}, Formulating LP')
    n_power = np.power(2, n)
    n_minus_power = np.power(2, n - 1)
    n_plus_power = np.power(2, n + 1)

    model = LpProblem("Hidden LP", LpMaximize)

    n_power_list = list(range(n_power))

    # v_bin = get_vt_general(n, [0, n//2])
    # v_bin = [i for i in range(n_power) if compute_vt_sum(num_to_bin(i, n)) in [0, n]]
    # v_bin = [i for i in range(n_power) if compute_vt_sum(num_to_bin(i, n), raw=True) <= n+1 or compute_vt_sum(num_to_bin(i, n), raw=True)>=(n-2)*(n+1)//2]
    # k = 4
    # v_bin = [i for i in range(n_power) if compute_vt_sum(num_to_bin(i, n), raw=True) <= k*(n+1)]
    v_bin = [
        i for i in range(n_power) if compute_vt_sum(num_to_bin(i, n)) == 0
        or compute_vt_sum(num_to_bin(i, n)) == 1
    ]
    # compare = [i for i in range(n_power) if compute_vt_sum(num_to_bin(i, n)) == 0 and compute_vt_sum(num_to_bin(i, n), raw=True) <= k*(n+1)]

    v_else = [i for i in range(n_power) if i not in v_bin]
    # v_else = []
    # v_else = [i for i in range(n_power) if compute_vt_sum(num_to_bin(i, n), raw=True) > k*(n+1) and compute_vt_sum(num_to_bin(i, n), raw=True) < (k+2)*(n+1)]
    Vbin = [i for i in v_bin]
    Velse = [i for i in v_else]

    VBIN = LpVariable.dicts("VBIN", [i for i in v_bin],
                            lowBound=0,
                            cat='Binary')
    VELSE = LpVariable.dicts("VELSE", [i for i in v_else],
                             lowBound=0,
                             upBound=1)

    model += (pulp.lpSum(VBIN) + pulp.lpSum(VELSE))

    if 0 in v_bin:
        model += (VBIN[0] == 1)
    else:
        model += (VELSE[0] == 1)

    if n_power - 1 in v_bin:
        model += (VBIN[n_power - 1] == 1)
    else:
        model += (VELSE[n_power - 1] == 1)

    hidden_weight = get_hidden_weight_matrix(n)
    for col_idx in range(0, n_minus_power):
        col = hidden_weight[:, col_idx]
        edge_list = np.where(col > 0)[0]
        v_bin_list = [edge for edge in edge_list if edge in v_bin]
        v_else_list = [edge for edge in edge_list if edge in v_else]
        model += ((pulp.lpSum([VBIN[idx] for idx in v_bin_list]) +
                   pulp.lpSum([VELSE[idx] for idx in v_else_list])) <= 1)

    hidden_weight = get_hidden_weight_matrix(n + 1)
    for row_idx in range(0, n_plus_power):
        row = hidden_weight[row_idx, :]
        edge_list = np.where(row > 0)[0]
        v_bin_list = [edge for edge in edge_list if edge in v_bin]
        v_else_list = [edge for edge in edge_list if edge in v_else]
        model += ((pulp.lpSum([VBIN[idx] for idx in v_bin_list]) +
                   pulp.lpSum([VELSE[idx] for idx in v_else_list])) <= 1)

    # add clique constraint
    # cliques = findclique(n)
    # for clique in cliques:
    #     clique_bin = [edge for edge in clique if edge in v_bin]
    #     clique_else = [edge for edge in clique if edge in v_else]
    #     model += ((pulp.lpSum([VBIN[idx] for idx in clique_bin])+pulp.lpSum([VELSE[idx] for idx in clique_else])) <= 1)

    print(f'n = {n}, Solving LP')
    model.solve()
    opt = value(model.objective)
    print(f'n={n}, opt={opt}, status: {LpStatus[model.status]}')
    # print(f'target = {len(compare)}')
    print(' ')

    if verbose:
        cnt = 0
        for v in model.variables():
            if v.varValue > 0:
                idx = v.name.find('_')
                bin_str = num_to_bin(int(v.name[idx + 1:]), n)
                vt_sum = compute_vt_sum(bin_str)
                print(f'{v.name} = {v.varValue}, VT_sum = {vt_sum}')
                cnt += 1
        print(f'number of nonzeros = {cnt}')
Beispiel #5
0
def loop_lp(n, verbose=False, v_bin=[0], v_zeros=[1], v_ones=[0]):
    print(f'n = {n}, Formulating LP')
    n_power = np.power(2, n)
    n_minus_power = np.power(2, n-1)
    n_plus_power = np.power(2, n+1)

    model = LpProblem("Loop LP", LpMaximize)

    v_else = [i for i in range(n_power) if i not in v_bin]

    Vbin = [i for i in v_bin]
    Velse = [i for i in v_else]

    VBIN = LpVariable.dicts("VBIN", [i for i in v_bin], lowBound=0, cat='Binary')
    VELSE = LpVariable.dicts("VELSE", [i for i in v_else], lowBound=0, upBound=1)

    model += (pulp.lpSum(VBIN)+pulp.lpSum(VELSE))
    
    for v in v_zeros:
        if v in v_bin:
            model += (VBIN[v] == 0)
        else:
            model += (VELSE[v] == 0)
    for v in v_ones:
        if v in v_bin:
            model += (VBIN[v] == 1)
        else:
            model += (VELSE[v] == 1)

    hidden_weight = get_hidden_weight_matrix(n+1)
    for row_idx in range(0, n_plus_power):
        row = hidden_weight[row_idx, :]
        edge_list = np.where(row>0)[0]
        v_bin_list = [edge for edge in edge_list if edge in v_bin]
        v_else_list = [edge for edge in edge_list if edge in v_else]
        model += ((pulp.lpSum([VBIN[idx] for idx in v_bin_list])+pulp.lpSum([VELSE[idx] for idx in v_else_list])) <= 1)

    hidden_weight = get_hidden_weight_matrix(n)
    for col_idx in range(0, n_minus_power):
        col = hidden_weight[:, col_idx]
        edge_list = np.where(col>0)[0]
        v_bin_list = [edge for edge in edge_list if edge in v_bin]
        v_else_list = [edge for edge in edge_list if edge in v_else]
        model += ((pulp.lpSum([VBIN[idx] for idx in v_bin_list])+pulp.lpSum([VELSE[idx] for idx in v_else_list])) <= 1)

    print(f'n = {n}, Solving LP')
    start = time.time()
    solver = solvers.COIN_CMD('cbc', threads=28)
    model.solve(solver)
    end = time.time()
    opt = value(model.objective)
    print(f'n = {n}, opt = {opt}, status: {LpStatus[model.status]}, it took {end-start} seconds')

    zeros = v_zeros
    ones = v_ones
    binary = v_bin
    temp_rule = 0
    temp_idx = -1
    for v in model.variables():
        idx = v.name.find('_')
        idx_int = int(v.name[idx+1:])
        bin_str = num_to_bin(idx_int, n)
        vt_sum = compute_vt_sum(bin_str)
        if v.varValue == 1:
            if verbose:
                print(f'{v.name} = {v.varValue}, VT_sum = {vt_sum}')
            if idx_int not in binary:
                binary.append(idx_int)
        elif v.varValue > 0:
            rule = v.varValue * sum(hidden_weight[idx_int, :])
            if rule > temp_rule:
                temp_idx = idx_int
                temp_max = v.varValue
                temp_rule = rule
            if verbose:
                print(f'{v.name} = {v.varValue}, VT_sum = {vt_sum}')

    if temp_idx >= 0:
        print(f'v_idx = {temp_idx}, '
              f'v_val = {temp_max}, '
              f'hidden_weight = {sum(hidden_weight[temp_idx, :])}')
        binary.append(temp_idx)
    return binary, opt 
Beispiel #6
0
def edges_from_vertex(x, n):
    bin_str = num_to_bin(x, n)
    edge_arr = np.zeros(np.power(2, n-1))
    deleted_bin_arr = [bin_str[:i]+bin_str[i+1:] for i in range(n)]
    edge_arr[v_bin_to_num(deleted_bin_arr)] = 1
    return edge_arr