def solver_nfl_explicit_lambda_1_dr():
    '''
    Network Lasso explict
    lam=1
    '''

    numNode = 100
    numRho = 7
    data = np.zeros((numNode, 2))
    for k in range(11):
        data[k] = data[k] + np.array([1, 1])
        data[k + 11] = data[k + 11] + np.array([-1, 1])
        data[k + 22] = data[k + 22] + np.array([2, 2])
        data[k + 33] = data[k + 33] + np.array([-1, -1])

    np.random.seed(1)
    noise = 0.5 * np.random.normal(0, 1, (numNode, 2))
    data = data + noise
    # print('sum of data:',np.sum(data))

    edgelist = [[k, k + 1] for k in range(numNode - 1)]

    G = nx.Graph()
    G.add_nodes_from(range(numNode))
    G.add_edges_from(edgelist)
    coloredge(G)
    G0, G1 = decompose_graph(G)

    objerror = []
    runtime = []
    _, obj, _, _ = solve_NetworkLasso(data, G, maxsteps=10000, rho=1, lam=1)
    optobj3 = obj[-1]

    obj = []
    t = time.time()
    x, obj, err, tevolution = solve_NetworkLasso(
        data, G, maxsteps=700, rho=2, lam=1, verbose=1)
    print('running time Network Lasso explict:',
          time.time() - t, ',rho: dynamic')
    objerror.append([i - optobj3 for i in obj])
    runtime.append(tevolution)

    # plotting
    plt.figure()
    plt.plot(runtime[0], objerror[0], label=r'$\rho=2^0$')
    plt.legend(loc='upper right')
    plt.xlim((0, 2))
    # plt.xlim((0,400))
    plt.ylim((1e-12, 1e2))
    plt.title(r'Network Lasso Algorithm ' r'(explicit, $\lambda=1$)', fontsize=12)
    plt.xlabel('Running time (seconds)', fontsize=12)
    plt.ylabel('Objective Value Error', fontsize=12)
    plt.yscale('log')
    plt.savefig('Figures/ChainGraph/dynamic_rho_Network_lamb=1_chain_graph_explicit',
                bbox_inches='tight')
Example #2
0
def network_lasso_explicit_2():
    ''' network lasso algorithm with explicit updates '''
    img = mpimg.imread('Data/ABC.png')
    imgsize1, imgsize2, _ = img.shape
    img_noise = img + 0.2 * np.random.normal(0, 1, (imgsize1, imgsize2, 3))
    data = np.reshape(img_noise, (-1, 3))
    # nodes = imgsize1 * imgsize2
    # graph settings

    graph = nx.grid_2d_graph(imgsize1, imgsize2)
    graph = nx.convert_node_labels_to_integers(graph)

    objerror = []
    runtime = []
    rho_set = [2**k for k in range(7)]
    # rho_set = [16]
    _, obj, _, _ = solve_NetworkLasso(data,
                                      graph,
                                      maxsteps=5000,
                                      rho=32,
                                      lam=10)
    optobj = obj[-1]

    for rho in rho_set:
        clock_time = time.time()
        _, obj, _, tevolution = solve_NetworkLasso(data,
                                                   graph,
                                                   maxsteps=1500,
                                                   rho=rho,
                                                   lam=10)
        print('running time network_lasso_explicit:',
              time.time() - clock_time, ',rho:', rho)
        objerror.append([i - optobj for i in obj])
        runtime.append(tevolution)

    # plotting
    plt.figure()
    plt.plot(runtime[0], objerror[0], label=r'$\rho=2^0$')
    plt.plot(runtime[1], objerror[1], label=r'$\rho=2^1$')
    plt.plot(runtime[2], objerror[2], label=r'$\rho=2^2$')
    plt.plot(runtime[3], objerror[3], label=r'$\rho=2^3$')
    plt.plot(runtime[4], objerror[4], label=r'$\rho=2^4$')
    plt.plot(runtime[5], objerror[5], label=r'$\rho=2^5$')
    plt.plot(runtime[6], objerror[6], label=r'$\rho=2^6$')
    plt.legend()
    plt.xlim((0, 250))
    plt.ylim((1e-12, 1e2))
    plt.title('Network Lasso Algorithm (explict)')
    plt.xlabel('Running time (seconds)', fontsize=12)
    # plt.xlabel('Number of Iterations')
    plt.ylabel('Objective Value Error', fontsize=12)
    plt.yscale('log')
    plt.savefig('Figures/GridGraph/Network_lamb=10_grid_graph_explicit',
                bbox_inches='tight')
Example #3
0
def image_show():
    ''' print original image and image recovered'''
    img = mpimg.imread('Data/ABC.png')
    imgsize1, imgsize2, _ = img.shape
    # img=img.astype(float)/255
    noise = 0.1 * np.random.normal(0, 1, (imgsize1, imgsize2, 3))
    imgnoise = img + noise
    data = np.reshape(imgnoise, (-1, 3))
    graph = nx.grid_2d_graph(imgsize1, imgsize2)
    graph = nx.convert_node_labels_to_integers(graph)

    x_hat, _, _, _ = solve_NetworkLasso(data,
                                        graph,
                                        maxsteps=100,
                                        rho=8,
                                        lam=0.2)

    img_recovered = np.reshape(x_hat, (imgsize1, imgsize2, 3))

    fig = plt.figure()
    fig.add_subplot(1, 3, 1)
    plt.imshow(img)
    fig.add_subplot(1, 3, 2)
    plt.imshow(imgnoise)
    fig.add_subplot(1, 3, 3)
    plt.imshow(img_recovered)
    plt.savefig('Figures/GridGraph/img_comparision', bbox_inches='tight')
Example #4
0
def solver_nfl_explicit_lambda_01():
    '''
    lam=0.1
    '''

    numNode = 100
    numRho = 7
    data = np.zeros((numNode, 2))
    for k in range(11):
        data[k] = data[k] + np.array([1, 1])
        data[k + 11] = data[k + 11] + np.array([-1, 1])
        data[k + 22] = data[k + 22] + np.array([2, 2])
        data[k + 33] = data[k + 33] + np.array([-1, -1])

    np.random.seed(1)
    noise = 0.5 * np.random.normal(0, 1, (numNode, 2))
    data = data + noise
    # print('sum of data:',np.sum(data))

    edgelist = [[k, k + 1] for k in range(numNode - 1)]

    G = nx.Graph()
    G.add_nodes_from(range(numNode))
    G.add_edges_from(edgelist)
    coloredge(G)
    G0, G1 = decompose_graph(G)

    objerror = []
    runtime = []
    _, obj, _, _ = solve_NetworkLasso(data, G, maxsteps=5000, rho=16, lam=0.1)
    optobj4 = obj[-1]

    for k in range(numRho):
        rho = 2**k
        obj = []
        t3 = time.time()
        x, obj, err, tevolution = solve_NetworkLasso(
            data, G, maxsteps=800, rho=2**k, lam=0.1)
        print('running time Network Lasso explict:',
              time.time() - t3, ',rho:', rho)
        objerror.append([i - optobj4 for i in obj])
        runtime.append(tevolution)

    # plotting
    plt.figure()
    plt.plot(runtime[0], objerror[0], label=r'$\rho=2^0$')
    plt.plot(runtime[1], objerror[1], label=r'$\rho=2^1$')
    plt.plot(runtime[2], objerror[2], label=r'$\rho=2^2$')
    plt.plot(runtime[3], objerror[3], label=r'$\rho=2^3$')
    plt.plot(runtime[4], objerror[4], label=r'$\rho=2^4$')
    plt.plot(runtime[5], objerror[5], label=r'$\rho=2^5$')
    plt.plot(runtime[6], objerror[6], label=r'$\rho=2^6$')
    plt.legend(loc='upper right')
    plt.xlim((0, 2))
    # plt.xlim((0,1000))
    plt.ylim((1e-12, 1e2))
    # plt.title('Network Lasso Algorithm (explict & lambda=0.1)')
    plt.title(r'Network Lasso Algorithm ' r'(explicit, $\lambda=0.1$)',fontsize=12)
    plt.xlabel('Running time (seconds)', fontsize=12)
    # plt.xlabel('Number of Iterations')
    plt.ylabel('Objective Value Error', fontsize=12)
    plt.yscale('log')
    plt.savefig('Figures/ChainGraph/Network_lamb=01_chain_graph_explict',
                bbox_inches='tight')
Example #5
0
def solver_network_cvx_lambda_1():
    '''
    1-D Chain Graph, y[i]=y*[i]+epsilon, y*[0:11]=(1,1),y*[11:22]=(-1,1),
    y*[22:33]=(2,2),y*[33:44]=(-1,-1),y*[44:]=(0,0)
    lambda = 1
    '''
    # Set parameters
    numNode = 100
    data = np.zeros((numNode, 2))
    for k in range(11):
        data[k] = data[k] + np.array([1, 1])
        data[k + 11] = data[k + 11] + np.array([-1, 1])
        data[k + 22] = data[k + 22] + np.array([2, 2])
        data[k + 33] = data[k + 33] + np.array([-1, -1])

    np.random.seed(1)
    noise = 0.5 * np.random.normal(0, 1, (numNode, 2))
    data = data + noise

    y = np.transpose(data)
    p, n = y.shape

    print("The number of data is %d, the dimension of each data is %d" %
          (n, p))
    # Creating graph
    G = TUNGraph.New()
    for k in range(numNode):
        G.AddNode(k)

    for i in range(numNode - 1):
        G.AddEdge(i, i + 1)

    print('Number of Edges', G.GetEdges())

    edge_weights = TIntPrFltH()
    for edge in G.Edges():
        temp = TIntPr(edge.GetSrcNId(), edge.GetDstNId())
        edge_weights.AddDat(temp, 1)
        # print("edge (%d, %d)" % (edge.GetSrcNId(), edge.GetDstNId()))
        # weight = edgeWeights.GetDat(TIntPr(edge.GetSrcNId(), edge.GetDstNId()))
        # print("edgeWeights:",weight)

    nodes = G.GetNodes()
    # Initialize variables to 0
    x_initial = np.zeros((p, nodes))
    u_initial = np.zeros((p, 2 * G.GetEdges()))
    z_initial = np.zeros((p, 2 * G.GetEdges()))

    runtime = []
    objerror = []
    rho_set = [2**k for k in range(7)]
    # rho_set = [1]
    '''
    find best obje value via explicit network lasso alogrithm
    '''

    graph = nx.Graph()
    graph.add_nodes_from(range(numNode))
    graph.add_edges_from([[k, k + 1] for k in range(numNode - 1)])

    _, obj, _, _ = solve_NetworkLasso(data, graph, maxsteps=4000, rho=4, lam=1)
    optobj = obj[-1]
    # print(x1)

    for rho in rho_set:
        clock_time = time.time()
        _, tevolution, obj, _ = runADMM(G1=G,
                                        sizeOptVar=p,
                                        sizeData=p,
                                        lamb=1,
                                        rho=rho,
                                        numiters=200,
                                        x=x_initial,
                                        u=u_initial,
                                        z=z_initial,
                                        a=y,
                                        edgeWeights=edge_weights,
                                        useConvex=1,
                                        epsilon=0.01,
                                        mu=0.5)
        print('NetworkLasso:', time.time() - clock_time, ',rho:', rho)

        objerror.append([i - optobj for i in obj])
        runtime.append(tevolution)
        # print(objerror)

    # plotting
    plt.figure()
    plt.plot(runtime[0], objerror[0], label=r'$\rho=2^0$')
    plt.plot(runtime[1], objerror[1], label=r'$\rho=2^1$')
    plt.plot(runtime[2], objerror[2], label=r'$\rho=2^2$')
    plt.plot(runtime[3], objerror[3], label=r'$\rho=2^3$')
    plt.plot(runtime[4], objerror[4], label=r'$\rho=2^4$')
    plt.plot(runtime[5], objerror[5], label=r'$\rho=2^5$')
    plt.plot(runtime[6], objerror[6], label=r'$\rho=2^6$')
    plt.legend(loc='upper right')
    plt.xlim((0, 50))
    plt.ylim((1e-5, 1e2))
    # plt.title('Network Lasso Algorithm-explict (lambda=1)')
    plt.title(r'Network Lasso Algorithm ' r'(cvx, $\lambda=1$)', fontsize=12)
    plt.xlabel('Running time (seconds)', fontsize=12)
    plt.ylabel('Objective Value Error', fontsize=12)
    plt.yscale('log')
    plt.savefig('Figures/ChainGraph_cvx/Network_lamb=1_chain_graph_cvx',
                bbox_inches='tight')
Example #6
0
def network_lasso_cvx_1():
    ''' network lasso algorithm with explicit updates '''
    # img = mpimg.imread('Data/ABC.png')
    # img = np.ones((10, 10, 3))
    img = np.zeros((10, 10, 3))
    for i in range(10):
        for j in range(10):
            if (i - 4)**2 + (j - 4)**2 < 5:
                img[i][j] = np.array([0.5, 0.5, 0.5])

    imgsize1, imgsize2, _ = img.shape
    np.random.seed(1)
    img_noise = img + 0.2 * np.random.normal(0, 1, (imgsize1, imgsize2, 3))
    data = np.reshape(img_noise, (-1, 3))
    y = np.transpose(data)
    nodes = imgsize1 * imgsize2
    # graph settings

    graph = nx.grid_2d_graph(imgsize1, imgsize2)
    graph = nx.convert_node_labels_to_integers(graph)

    graph_edges = list(graph.edges())
    graph_edges.sort()
    # print(graph_edges)
    # return 0

    G = TUNGraph.New()
    for k in range(nodes):
        G.AddNode(k)

    for edge in graph_edges:
        G.AddEdge(edge[0], edge[1])

    print('Number of Edges:', G.GetEdges())

    edge_weights = TIntPrFltH()
    for edge in G.Edges():
        temp = TIntPr(edge.GetSrcNId(), edge.GetDstNId())
        edge_weights.AddDat(temp, 1)

    # Initialize variables to 0
    x_initial = np.zeros((3, nodes))
    u_initial = np.zeros((3, 2 * G.GetEdges()))
    z_initial = np.zeros((3, 2 * G.GetEdges()))

    objerror = []
    runtime = []
    rho_set = [2**k for k in range(7)]
    # rho_set=[1]
    # optobj = 287.905779648668

    _, obj, _, _ = solve_NetworkLasso(
        y=data, G=graph, maxsteps=1500, rho=32, lam=0.5)

    optobj = obj[-1]

    # optobj3 = obj[-1]

    for rho in rho_set:
        clock_time = time.time()
        x_hat, tevolution, obj, _ = runADMM(G1=G, sizeOptVar=3, sizeData=3, lamb=0.5, rho=rho, numiters=300, x=x_initial,
                                            u=u_initial, z=z_initial, a=y, edgeWeights=edge_weights, useConvex=1, epsilon=0.01, mu=0.5)
        print('Network_lasso_cvx:',
              time.time() - clock_time, ',rho:', rho)
        objerror.append([i - optobj for i in obj])
        runtime.append(tevolution)

    # print('obj_cvx', obj)
    # print(runtime)
    # print(objerror)
    # plotting
    plt.figure()
    plt.plot(runtime[0], objerror[0], label=r'$\rho=2^0$')
    plt.plot(runtime[1], objerror[1], label=r'$\rho=2^1$')
    plt.plot(runtime[2], objerror[2], label=r'$\rho=2^2$')
    plt.plot(runtime[3], objerror[3], label=r'$\rho=2^3$')
    plt.plot(runtime[4], objerror[4], label=r'$\rho=2^4$')
    plt.plot(runtime[5], objerror[5], label=r'$\rho=2^5$')
    plt.plot(runtime[6], objerror[6], label=r'$\rho=2^6$')
    plt.legend(loc='upper right')
    plt.xlim((0, 120))
    plt.ylim((1e-6, 1e1))
    # plt.title('Network Lasso Algorithm (cvx)')
    plt.title(r'Network Lasso Algorithm ' r'(cvx, $\lambda=0.5$)', fontsize=12)
    plt.xlabel('Running time (seconds)', fontsize=12)
    # plt.xlabel('Number of Iterations')
    plt.ylabel('Objective Value Error', fontsize=12)
    plt.yscale('log')
    plt.savefig(
        'Figures/GridGraph_cvx/Network_lamb=05_grid_graph_cvx', bbox_inches='tight')
Example #7
0
def obj_find():
    ''' find optimal objective vlaue '''

    # img = mpimg.imread('Data/ABC.png')
    # imgsize1, imgsize2, _ = img.shape
    img = np.ones(16, 16, 3)

    np.random.seed(1)
    noise = 0.2 * np.random.normal(0, 1, (imgsize1, imgsize2, 3))
    imgnoise = img + noise
    data = np.reshape(imgnoise, (-1, 3))

    nodes = imgsize1 * imgsize2
    # graph settings

    graph = nx.grid_2d_graph(imgsize1, imgsize2)
    graph = nx.convert_node_labels_to_integers(graph)
    # print(list(graph.edges()))

    temp_edges = []
    for k in range(0, imgsize1 * imgsize2):
        for j in graph.neighbors(k):
            if j == k + 1:
                temp_edges.append([k, j])

    graph0_edges = []
    graph0_edges.append(temp_edges[0])
    for edge in temp_edges:
        if (edge[0] != graph0_edges[-1][1]) and (edge != graph0_edges[-1]):
            graph0_edges.append(edge)

    graph_edges = list(graph.edges())
    graph_edges.sort()
    graph1_edges = [edge for edge in list(
        graph.edges()) if edge not in graph0_edges]

    graph1_edges.sort()

    graph0_nodes = np.unique(np.array(graph0_edges))
    graph1_nodes = np.unique(np.array(graph1_edges))

    graph = nx.Graph()
    graph0 = nx.Graph()
    graph1 = nx.Graph()
    graph0.add_nodes_from(graph0_nodes)
    graph1.add_nodes_from(graph1_nodes)
    graph0.add_edges_from(graph0_edges)
    graph1.add_edges_from(graph1_edges)
    graph.add_nodes_from(range(imgsize1 * imgsize2))
    graph.add_edges_from(graph_edges)

    # print(list(graph.edges()))

    _, obj, _, _ = solve_GroupFL(
        y=data, G=graph, G0=graph0, G1=graph1, maxsteps=1000, rho=2, lam=10)

    optobj = obj[-1]
    print(obj)

    _, obj, _, _ = solve_NetworkLasso(
        y=data, G=graph, maxsteps=1000, rho=2, lam=10)
    optobj = obj[-1]
    print(obj)

    return
    plt.switch_backend('TkAgg')
    plt.figure()
    plt.plot(obj - obj[-1])
    # plt.ylim((1e-4, 1e3))
    plt.title('Graph Fused Lasso Algorithm (cvx)')
    plt.ylabel('error')
    plt.yscale('log')
    plt.show()

    with open('Figures/GridGraph_cvx/optobj.csv', 'w', newline='') as csvfile:
        objwriter = csv.writer(csvfile, delimiter=' ',
                               quotechar='|', quoting=csv.QUOTE_MINIMAL)
        objwriter.writerow([optobj] + ['lambda:'] + [10])