Example #1
0
    def baseline3(self, t, two_k):
        assert self.seeds is None
        self.load_baseline3_transition_matrix()
        self.graph = self.graph.tocsc()
        begin = time.time()
        self.seeds = np.zeros(self.total_nodes)
        c = np.array(self.partitions, dtype='float64')
        for i in range(len(c)):
            if c[i] < 0:
                c[i] = 0
        c *= self.targets
        for i in range(t):
            c = csc_matrix.dot(c, self.graph)
        l = [None] * self.total_nodes
        for i in range(self.total_nodes):
            l[i] = (i, c[i])
        cnt = 0
        for elem in sorted(l, key=lambda x: x[1], reverse=True):
            if elem[1] <= 0:
                break
            #seeds.append(elem[0])
            self.seeds[elem[0]] = 1
            cnt += 1
            if cnt >= two_k / 2:
                break

        c = np.array(self.partitions, dtype='float64')
        for i in range(len(c)):
            if c[i] > 0:
                c[i] = 0
            else:
                c[i] *= -1
        c *= self.targets
        for i in range(t):
            c = csc_matrix.dot(c, self.graph)
        l = [None] * self.total_nodes
        for i in range(self.total_nodes):
            l[i] = (i, c[i])
        cnt = 0
        for elem in sorted(l, key=lambda x: x[1], reverse=True):
            if elem[1] <= 0:
                break
            #seeds.append(-1*elem[0])
            self.seeds[elem[0]] = -1
            cnt += 1
            if cnt >= two_k / 2:
                break

        #for i in range(two_k):
        #    if self.partitions[seeds[i]] < 0:
        #        self.seeds[seeds[i]] = -1
        #    else:
        #        self.seeds[seeds[i]] = 1
        end = time.time()
        #print('\nB3 ---------------')
        #print(self.seeds)
        self.load_transition_matrix()
        return end - begin
Example #2
0
    def explain(self, X):
        """
        Return local explanation

        Parameters
        ----------
        X : tensor: `torch.Tensor`
            Input data

        Returns
        -------
        M_explain : matrix
            Importance per sample, per columns.
        masks : matrix
            Sparse matrix showing attention masks used by network.
        """
        try:
            self.model.eval()

            dataloader = DataLoader(
                PredictDataset(X),
                batch_size=self.batch_size,
                shuffle=False,
            )

            res_explain = []
            reducing_matrix = create_explain_matrix(self.model.input_dim, 0,
                                                    [], self.model.input_dim)
            for batch_nb, data in enumerate(dataloader):
                data = data.to(self.device).float()

                M_explain, masks = self.model.forward_masks(data)
                for key, value in masks.items():
                    masks[key] = csc_matrix.dot(value.cpu().detach().numpy(),
                                                reducing_matrix)

                res_explain.append(
                    csc_matrix.dot(M_explain.cpu().detach().numpy(),
                                   reducing_matrix))

                if batch_nb == 0:
                    res_masks = masks
                else:
                    for key, value in masks.items():
                        res_masks[key] = np.vstack([res_masks[key], value])

            res_explain = np.vstack(res_explain)

            return res_explain, res_masks
        finally:
            self.model.train()
Example #3
0
    def compute_feature_importances(self, X):
        """Compute global feature importance.

        Parameters
        ----------
        loader : `torch.utils.data.Dataloader`
            Pytorch dataloader.

        """

        try:
            X_tensor = torch.FloatTensor(X)
            dataset = torch.utils.data.TensorDataset(X_tensor)
            loader = torch.utils.data.DataLoader(dataset,
                                                 batch_size=self.batch_size)
            self.model.eval()
            reducing_matrix = create_explain_matrix(self.model.input_dim, 0,
                                                    [], self.model.input_dim)

            feature_importances_ = np.zeros((self.model.input_dim))
            for data, in loader:
                data = data.to(self.device).float()
                M_explain, masks = self.model.forward_masks(data)
                feature_importances_ += M_explain.sum(
                    dim=0).cpu().detach().numpy()

            feature_importances_ = csc_matrix.dot(feature_importances_,
                                                  reducing_matrix)
            self.feature_importances_ = feature_importances_ / np.sum(
                feature_importances_)
        finally:
            self.model.train()
Example #4
0
 def influ_max(self, t, two_k):
     assert self.seeds is None
     assert self.graph_type == 1
     self.graph = self.graph.tocsc()
     final_walk_distribution = np.array(self.partitions, dtype='float64')
     final_walk_distribution *= self.targets
     begin = time.time()
     for tm in range(t):
         final_walk_distribution = csc_matrix.dot(final_walk_distribution,
                                                  self.graph)
     l = [None] * self.total_nodes
     for i in range(len(final_walk_distribution)):
         l[i] = (i, final_walk_distribution[i],
                 abs(final_walk_distribution[i]))
     cnt = 0
     total = 0
     self.seeds = np.zeros(self.total_nodes)
     for elem in sorted(l, key=lambda x: x[2], reverse=True):
         if elem[1] < 0:
             total += -1 * elem[1]
             self.seeds[elem[0]] = -1
         else:
             total += elem[1]
             self.seeds[elem[0]] = 1
         cnt += 1
         if cnt >= two_k:
             break
     end = time.time()
     #print(self.seeds)
     return end - begin
Example #5
0
    def explain(self, X):
        """
        Return local explanation

        Parameters
        ----------
            data: a :tensor: `torch.Tensor`
                Input data
            target: a :tensor: `torch.Tensor`
                Target data

        Returns
        -------
            M_explain: matrix
                Importance per sample, per columns.
            masks: matrix
                Sparse matrix showing attention masks used by network.
        """
        self.network.eval()

        dataloader = DataLoader(PredictDataset(X),
                                batch_size=self.batch_size,
                                shuffle=False)

        for batch_nb, data in enumerate(dataloader):
            data = data.to(self.device).float()

            output, M_loss, M_explain, masks = self.network(data)
            for key, value in masks.items():
                masks[key] = csc_matrix.dot(value.cpu().detach().numpy(),
                                            self.reducing_matrix)

            if batch_nb == 0:
                res_explain = csc_matrix.dot(M_explain.cpu().detach().numpy(),
                                             self.reducing_matrix)
                res_masks = masks
            else:
                res_explain = np.vstack([
                    res_explain,
                    csc_matrix.dot(M_explain.cpu().detach().numpy(),
                                   self.reducing_matrix)
                ])
                for key, value in masks.items():
                    res_masks[key] = np.vstack([res_masks[key], value])
        return res_explain, res_masks
Example #6
0
def tabnet_explain(model: TabNetModel, dl: TabDataLoader):
    "Get explain values for a set of predictions"
    dec_y = []
    model.eval()
    for batch_nb, data in enumerate(dl):
        with torch.no_grad():
            M_explain, masks = model.forward_masks(data[0], data[1])
        for key, value in masks.items():
            masks[key] = csc_matrix.dot(value.cpu().numpy(), model.emb_reducer)

        explain = csc_matrix.dot(M_explain.cpu().numpy(), model.emb_reducer)
        if batch_nb == 0:
            res_explain = explain
            res_masks = masks
        else:
            res_explain = np.vstack([res_explain, explain])
            for key, value in masks.items():
                res_masks[key] = np.vstack([res_masks[key], value])
    return res_explain, res_masks
Example #7
0
def tabnet_feature_importances(model: TabNetModel, dl: TabDataLoader):
    model.eval()
    feature_importances_ = np.zeros((model.post_emb))
    for batch_nb, data in enumerate(dl):
        M_explain, masks = model.forward_masks(data[0], data[1])
        feature_importances_ += M_explain.sum(dim=0).cpu().detach().numpy()

    feature_importances_ = csc_matrix.dot(feature_importances_,
                                          model.emb_reducer)
    return feature_importances_ / np.sum(feature_importances_)
Example #8
0
    def _compute_feature_importances(self, loader):
        self.network.eval()
        feature_importances_ = np.zeros((self.network.post_embed_dim))
        for data, targets in loader:
            data = data.to(self.device).float()
            M_explain, masks = self.network.forward_masks(data)
            feature_importances_ += M_explain.sum(dim=0).cpu().detach().numpy()

        feature_importances_ = csc_matrix.dot(feature_importances_,
                                              self.reducing_matrix)
        self.feature_importances_ = feature_importances_ / np.sum(feature_importances_)
Example #9
0
    def train_epoch(self, train_loader):
        """
        Trains one epoch of the network in self.network

        Parameters
        ----------
            train_loader: a :class: `torch.utils.data.Dataloader`
                DataLoader with train set
        """

        self.network.train()
        y_preds = []
        ys = []
        total_loss = 0
        feature_importances_ = np.zeros((self.network.post_embed_dim))

        for data, targets in train_loader:
            batch_outs = self.train_batch(data, targets)
            if self.output_dim == 2:
                y_preds.append(
                    torch.nn.Softmax(dim=1)(
                        batch_outs["y_preds"])[:, 1].cpu().detach().numpy())
            else:
                values, indices = torch.max(batch_outs["y_preds"], dim=1)
                y_preds.append(indices.cpu().detach().numpy())
            ys.append(batch_outs["y"].cpu().detach().numpy())
            total_loss += batch_outs["loss"]
            feature_importances_ += batch_outs['batch_importance']

        # Reduce to initial input_dim
        feature_importances_ = csc_matrix.dot(feature_importances_,
                                              self.reducing_matrix)
        # Normalize feature_importances_
        feature_importances_ = feature_importances_ / np.sum(
            feature_importances_)

        y_preds = np.hstack(y_preds)
        ys = np.hstack(ys)

        if self.output_dim == 2:
            stopping_loss = -roc_auc_score(y_true=ys, y_score=y_preds)
        else:
            stopping_loss = -accuracy_score(y_true=ys, y_pred=y_preds)
        total_loss = total_loss / len(train_loader)
        epoch_metrics = {
            'loss_avg': total_loss,
            'stopping_loss': stopping_loss,
            'feature_importances_': feature_importances_
        }

        if self.scheduler is not None:
            self.scheduler.step()
        return epoch_metrics
Example #10
0
    def get_ek(self, psi):
        assert isinstance(psi, Wavefunction)
        v = self.v
        dv = self.grid.dv
        nelect = self.system.nelect
        psi = psi.get_psi()

        # T = psi'*Lap3*psi
        T1 = csr_matrix.dot(v, psi)
        T2 = csc_matrix.dot(csc_matrix(psi), T1)
        T = T2 * nelect * dv
        T = T[0]
        return T
Example #11
0
def stopcriterion(A, A_T, v, u, b, xi, r, s, r_norm, s_norm, p, n, ABSTOL,
                  RELTOL, rho, k):
    pri_evalf = np.array([
        np.linalg.norm(csr_matrix.dot(A, v[k + 1])),
        np.linalg.norm(u[k + 1]),
        np.linalg.norm(b)
    ])
    eps_pri = np.sqrt(p) * ABSTOL + RELTOL * np.amax(pri_evalf)

    dual_evalf = rho[k] * csc_matrix.dot(A_T, xi[k + 1])
    eps_dual = np.sqrt(n) * ABSTOL + RELTOL * np.linalg.norm(dual_evalf)

    r_norm.append(np.linalg.norm(r[k + 1]))
    s_norm.append(np.linalg.norm(s[k + 1]))
    if r_norm[k + 1] <= eps_pri and s_norm[k + 1] <= eps_dual:
        return 'break'
Example #12
0
    def train_epoch(self, train_loader):
        """
        Trains one epoch of the network in self.network

        Parameters
        ----------
            train_loader: a :class: `torch.utils.data.Dataloader`
                DataLoader with train set
        """

        self.network.train()
        y_preds = []
        ys = []
        total_loss = 0
        feature_importances_ = np.zeros((self.network.post_embed_dim))

        for data, targets in train_loader:
            batch_outs = self.train_batch(data, targets)
            y_preds.append(
                batch_outs["y_preds"].cpu().detach().numpy().flatten())
            ys.append(batch_outs["y"].cpu().detach().numpy())
            total_loss += batch_outs["loss"]
            feature_importances_ += batch_outs['batch_importance']

        # Reduce to initial input_dim
        feature_importances_ = csc_matrix.dot(feature_importances_,
                                              self.reducing_matrix)
        # Normalize feature_importances_
        feature_importances_ = feature_importances_ / np.sum(
            feature_importances_)

        y_preds = np.hstack(y_preds)
        ys = np.hstack(ys)

        stopping_loss = mean_squared_error(y_true=ys, y_pred=y_preds)
        total_loss = total_loss / len(train_loader)
        epoch_metrics = {
            'loss_avg': total_loss,
            'stopping_loss': stopping_loss,
            'feature_importances_': feature_importances_
        }

        if self.scheduler is not None:
            self.scheduler.step()
            print("Current learning rate: ",
                  self.optimizer.param_groups[-1]["lr"])
        return epoch_metrics
Example #13
0
    def _compute_feature_importances(self, loader):
        """Compute global feature importance.
        Parameters
        ----------
        loader : `torch.utils.data.Dataloader`
            Pytorch dataloader.
        """
        self.network.eval()
        feature_importances_ = np.zeros((self.network.post_embed_dim))
        for data, targets in loader:
            data = data.to(self.device).float()
            M_explain, masks = self.network.forward_masks(data)
            feature_importances_ += M_explain.sum(dim=0).cpu().detach().numpy()

        feature_importances_ = csc_matrix.dot(
            feature_importances_, self.reducing_matrix
        )
        self.feature_importances_ = feature_importances_ / np.sum(feature_importances_)
def rand_eig_function(A, k):

    n = A.shape[1]

    Omega = np.random.rand(n, k)  # returns n x k matrix
    Y = csr_matrix.dot(A, Omega)

    q, r = np.linalg.qr(
        Y)  # Orthogonal-triangular decomposition (QR decomposition)

    B = csc_matrix.dot((A.T), q).T  # Compute Laplacian
    B = B.dot(q)

    S, U_ = np.linalg.eig(B)
    sortidx = S.argsort()[::-1]
    X = U_[:, sortidx][:, 0:k]

    U = q.dot(X)

    return U, S
Example #15
0
    def compute_feature_importances(self, X):
        """Compute global feature importance.
        Parameters
        ----------
        loader : `torch.utils.data.Dataloader`
            Pytorch dataloader.
        """
        dataloader = DataLoader(
            PredictDataset(X),
            batch_size=self.batch_size,
            shuffle=False,
        )
        self.network.eval()
        feature_importances_ = np.zeros((self.network.post_embed_dim))
        for data in dataloader:
            data = data.to(self.device).float()
            M_explain, masks = self.network.forward_masks(data)
            feature_importances_ += M_explain.sum(dim=0).cpu().detach().numpy()

        feature_importances_ = csc_matrix.dot(feature_importances_,
                                              self.reducing_matrix)
        feature_importances_ = feature_importances_ / np.sum(
            feature_importances_)
        return feature_importances_
Example #16
0
def cp_RR(problem_data, rho_method):

    ######################
    ## IMPORT LIBRARIES ##
    ######################

    #Math libraries
    import numpy as np
    from scipy.sparse import csc_matrix
    from scipy.sparse import csr_matrix
    from scipy.sparse import linalg

    #Timing
    import time

    #Import data
    from Data.read_fclib import *

    #Plot residuals
    from Solver.ADMM_iteration.Numerics.plot import *

    #Initial penalty parameter
    import Solver.Rho.Optimal

    #Max iterations and kind of tolerance
    from Solver.Tolerance.iter_totaltolerance import *

    #Acceleration
    from Solver.Acceleration.plusr import *

    #b = Es matrix
    from Data.Es_matrix import *

    #Projection onto second order cone
    from Solver.ADMM_iteration.Numerics.projection import *

    #####################################################
    ############# TERMS / NOT A FUNCTION YET ############
    #####################################################

    start = time.clock()
    problem = hdf5_file(problem_data)

    M = problem.M.tocsc()
    f = problem.f
    A = csc_matrix.transpose(problem.H.tocsc())
    A_T = csr_matrix.transpose(A)
    w = problem.w
    mu = problem.mu

    #Dimensions (normal,tangential,tangential)
    dim1 = 3
    dim2 = np.shape(w)[0]

    #Problem size
    n = np.shape(M)[0]
    p = np.shape(w)[0]

    b = [
        1 / linalg.norm(A, 'fro') * Es_matrix(w, mu, np.zeros([
            p,
        ])) / np.linalg.norm(Es_matrix(w, mu, np.ones([
            p,
        ])))
    ]

    #################################
    ############# SET-UP ############
    #################################

    #Set-up of vectors
    v = [np.zeros([
        n,
    ])]
    u = [
        np.zeros([
            p,
        ])
    ]  #this is u tilde, but in the notation of the paper is used as hat [np.zeros([10,0])]
    u_hat = [np.zeros([
        p,
    ])]  #u_hat[0] #in the notation of the paper this used with a underline
    xi = [np.zeros([
        p,
    ])]
    xi_hat = [np.zeros([
        p,
    ])]
    r = [np.zeros([
        p,
    ])]  #primal residual
    s = [np.zeros([
        p,
    ])]  #dual residual
    r_norm = [0]
    s_norm = [0]
    tau = [1]  #over-relaxation
    e = []  #restart

    #Optimal penalty parameter
    rho_string = 'Solver.Rho.Optimal.' + rho_method + '(A,M,A_T)'
    rho = eval(rho_string)

    #Plot
    r_plot = []
    s_plot = []
    b_plot = []
    u_bin_plot = []
    xi_bin_plot = []

    ########################################
    ## TERMS COMMON TO ALL THE ITERATIONS ##
    ########################################

    #Super LU factorization of M + rho * dot(M_T,M)
    P = M + rho * csc_matrix.dot(A_T, A)
    LU = linalg.splu(P)

    ################
    ## ITERATIONS ##
    ################
    for j in range(20):
        print j

        len_u = len(u) - 1
        for k in range(len_u, MAXITER):

            ################
            ## v - update ##
            ################
            RHS = -f + rho * csc_matrix.dot(A_T,
                                            -w - b[j] - xi_hat[k] + u_hat[k])
            v.append(LU.solve(RHS))  #v[k+1]

            ################
            ## u - update ##
            ################
            Av = csr_matrix.dot(A, v[k + 1])
            vector = Av + xi_hat[k] + w + b[j]
            u.append(projection(vector, mu, dim1, dim2))  #u[k+1]

            ########################
            ## residuals - update ##
            ########################
            s.append(rho * csc_matrix.dot(A_T, (u[k + 1] - u_hat[k])))  #s[k+1]
            r.append(Av - u[k + 1] + w + b[j])  #r[k+1]

            #################
            ## xi - update ##
            #################
            xi.append(xi_hat[k] + r[k + 1])  #xi[k+1]

            ###################################
            ## accelerated ADMM with restart ##
            ###################################
            plusr(tau, u, u_hat, xi, xi_hat, k, e, rho)

            ####################
            ## stop criterion ##
            ####################
            pri_evalf = np.amax(
                np.array([
                    np.linalg.norm(csr_matrix.dot(A, v[k + 1])),
                    np.linalg.norm(u[k + 1]),
                    np.linalg.norm(w + b[j])
                ]))
            eps_pri = np.sqrt(p) * ABSTOL + RELTOL * pri_evalf

            dual_evalf = np.linalg.norm(rho * csc_matrix.dot(A_T, xi[k + 1]))
            eps_dual = np.sqrt(n) * ABSTOL + RELTOL * dual_evalf

            r_norm.append(np.linalg.norm(r[k + 1]))
            s_norm.append(np.linalg.norm(s[k + 1]))
            if r_norm[k + 1] <= eps_pri and s_norm[k + 1] <= eps_dual:

                for element in range(len(u)):
                    #Relative velocity
                    u_proj = projection(u[element], mu, dim1, dim2)

                    u_proj_contact = np.split(u_proj, dim2 / dim1)
                    u_contact = np.split(u[element], dim2 / dim1)

                    u_count = 0.0
                    for contact in range(dim2 / dim1):
                        if np.array_equiv(u_contact[contact],
                                          u_proj_contact[contact]):
                            u_count += 1.0

                    u_bin = 100 * u_count / (dim2 / dim1)
                    u_bin_plot.append(u_bin)

                    #Reaction
                    xi_proj = projection(xi[element], 1 / mu, dim1, dim2)

                    xi_proj_contact = np.split(xi_proj, dim2 / dim1)
                    xi_contact = np.split(xi[element], dim2 / dim1)

                    xi_count = 0.0
                    for contact in range(dim2 / dim1):
                        if np.array_equiv(xi_contact[contact],
                                          xi_proj_contact[contact]):
                            xi_count += 1.0

                    xi_bin = 100 * xi_count / (dim2 / dim1)
                    xi_bin_plot.append(xi_bin)

                for element in range(len(r_norm)):
                    r_plot.append(r_norm[element])
                    s_plot.append(s_norm[element])
                    b_plot.append(np.linalg.norm(b[j]))

                #print 'First contact'
                #print rho*xi[k+1][:3]
                #uy = projection(rho*xi[k+1],1/mu,dim1,dim2)
                #print uy[:3]
                #print 'Last contact'
                #print rho*xi[k+1][-3:]
                #print uy[-3:]

                #print u[k+1]

                #R = rho*xi[k+1]
                #N1 = csc_matrix.dot(M, v[k+1]) - csc_matrix.dot(A_T, R) + f
                #N2 = R - projection(R - u[k+1], 1/mu, dim1, dim2)
                #N1_norm = np.linalg.norm(N1)
                #N2_norm = np.linalg.norm(N2)

                #print np.sqrt( N1_norm**2 + N2_norm**2 )
                break

        #b(s) stop criterion
        b.append(Es_matrix(w, mu, Av + w))

        if j == 0:
            pass
        else:
            b_per_contact_j1 = np.split(b[j + 1], dim2 / dim1)
            b_per_contact_j0 = np.split(b[j], dim2 / dim1)
            count = 0
            for i in range(dim2 / dim1):
                if np.linalg.norm(b_per_contact_j1[i] -
                                  b_per_contact_j0[i]) / np.linalg.norm(
                                      b_per_contact_j0[i]) > 1e-03:
                    count += 1
            if count < 1:
                break

        v = [np.zeros([
            n,
        ])]
        u = [
            np.zeros([
                p,
            ])
        ]  #this is u tilde, but in the notation of the paper is used as hat [np.zeros([10,0])]
        u_hat = [np.zeros([
            p,
        ])]  #u_hat[0] #in the notation of the paper this used with a underline
        xi = [np.zeros([
            p,
        ])]
        xi_hat = [np.zeros([
            p,
        ])]
        r = [np.zeros([
            p,
        ])]  #primal residual
        s = [np.zeros([
            p,
        ])]  #dual residual
        r_norm = [0]
        s_norm = [0]
        tau = [1]  #over-relaxation
        e = []  #restart

    end = time.clock()
    ####################
    ## REPORTING DATA ##
    ####################

    f, axarr = plt.subplots(4, sharex=True)
    f.suptitle('External update with cp_RR (Acary)')

    axarr[0].semilogy(b_plot)
    axarr[0].set(ylabel='||Phi(s)||')

    axarr[1].axhline(y=rho)
    axarr[1].set(ylabel='Rho')

    axarr[2].semilogy(r_plot, label='||r||')
    axarr[2].semilogy(s_plot, label='||s||')
    axarr[2].set(ylabel='Residuals')

    axarr[3].plot(u_bin_plot, label='u in K*')
    axarr[3].plot(xi_bin_plot, label='xi in K')
    axarr[3].legend()
    axarr[3].set(xlabel='Iteration', ylabel='Projection (%)')
    plt.show()

    #plotit(r,b,start,end,'With acceleration / With restarting for '+problem_data+' for rho: '+rho_method)

    time = end - start
    print 'Total time: ', time
    return time
                                   1), np.arange(0, A.shape[1], 1))))
L = D - A  # Compute Laplacian

S, V = np.linalg.eig(np.array(L.todense()).squeeze()
                     )  # Compute k smallest eigenvalues and eigenvec. of L
sortidx = S.argsort()
V = V[:, sortidx]

c_idx_un = kmeans.kmeans_python(V[:, 0:k], k)  # Partition X by k-means

# [B] Normalized case
# c_idx: Clusters identified using normalized Laplacian version

# Degree Matrix (normalized)
D = np.diag(1 / np.sqrt(sumMat))
L = csc_matrix.dot(csr_matrix.tocsc(A.T), D.T).T  # Compute Laplacian
L = L.dot(D)
S, V = np.linalg.eig(L)  # Compute k largest eigenvalues and eigenvec. of L
sortidx = S.argsort()[::-1]
X = V[:, sortidx][:, 0:k]
norm2 = np.power(X, 2).sum(axis=1)  # Normalize X row-wise
norm2.shape = (norm2.shape[0], 1)
X = X / (np.sqrt(norm2))
c_idx = kmeans.kmeans_python(X, k)  # Partition X by k-means

# Clustering algorithm End

## Get node labels
idx2names = {}
for line in open('inverse_teams.txt'):
    (index, name) = line.split("\t")
Example #18
0
def vp_N_He(problem_data, rho_method):

    ######################
    ## IMPORT LIBRARIES ##
    ######################

    #Math libraries
    import numpy as np
    from scipy.sparse import csc_matrix
    from scipy.sparse import csr_matrix
    from scipy.sparse import linalg

    #Timing
    import time

    #Import data
    from Data.read_fclib import *

    #Plot residuals
    from Solver.ADMM_iteration.Numerics.plot import *

    #Initial penalty parameter
    import Solver.Rho.Optimal

    #Max iterations and kind of tolerance
    from Solver.Tolerance.iter_totaltolerance import *

    #Varying penalty parameter
    from Solver.Rho.Varying.He import *

    #b = Es matrix
    from Data.Es_matrix import *

    #Projection onto second order cone
    from Solver.ADMM_iteration.Numerics.projection import *

    ##################################
    ############# REQUIRE ############
    ##################################

    start = time.clock()
    problem = hdf5_file(problem_data)

    M = problem.M.tocsc()
    f = problem.f
    A = csc_matrix.transpose(problem.H.tocsc())
    A_T = csr_matrix.transpose(A)
    w = problem.w
    mu = problem.mu

    #Dimensions (normal,tangential,tangential)
    dim1 = 3
    dim2 = np.shape(w)[0]

    #Problem size
    n = np.shape(M)[0]
    p = np.shape(w)[0]

    b = 0.1 * Es_matrix(w, mu, np.ones([
        p,
    ])) / np.linalg.norm(Es_matrix(w, mu, np.ones([
        p,
    ])))

    #################################
    ############# SET-UP ############
    #################################

    #Set-up of vectors
    v = [np.zeros([
        n,
    ])]
    u = [
        np.zeros([
            p,
        ])
    ]  #this is u tilde, but in the notation of the paper is used as hat [np.zeros([10,0])]
    xi = [np.zeros([
        p,
    ])]
    r = [np.zeros([
        p,
    ])]  #primal residual
    s = [np.zeros([
        p,
    ])]  #dual residual
    r_norm = [0]
    s_norm = [0]
    e = []  #restart
    rho = []

    #Optimal penalty parameter
    rho_string = 'Solver.Rho.Optimal.' + rho_method + '(A,M,A_T)'
    rh = eval(rho_string)
    rho.append(rh)  #rho[0]

    ################
    ## ITERATIONS ##
    ################

    for k in range(MAXITER):

        #Super LU factorization of M + rho * dot(M_T,M)
        if rho[k] != rho[k - 1] or k == 0:
            P = M + rho[k] * csc_matrix.dot(A_T, A)
            LU = linalg.splu(P)
            LU_old = LU
        else:
            LU = LU_old

        ################
        ## v - update ##
        ################
        RHS = -f + rho[k] * csc_matrix.dot(A_T, -w - b - xi[k] + u[k])
        v.append(LU.solve(RHS))  #v[k+1]

        ################
        ## u - update ##
        ################
        Av = csr_matrix.dot(A, v[k + 1])
        vector = Av + xi[k] + w + b
        u.append(projection(vector, mu, dim1, dim2))  #u[k+1]

        ########################
        ## residuals - update ##
        ########################
        s.append(rho[k] * csc_matrix.dot(A_T, (u[k + 1] - u[k])))  #s[k+1]
        r.append(Av - u[k + 1] + w + b)  #r[k+1]

        #################
        ## xi - update ##
        #################
        ratio = rho[k -
                    1] / rho[k]  #update of dual scaled variable with new rho
        xi.append(ratio * (xi[k] + r[k + 1]))  #xi[k+1]

        ####################
        ## stop criterion ##
        ####################
        pri_evalf = np.amax(
            np.array([
                np.linalg.norm(csr_matrix.dot(A, v[k + 1])),
                np.linalg.norm(u[k + 1]),
                np.linalg.norm(w + b)
            ]))
        eps_pri = np.sqrt(p) * ABSTOL + RELTOL * pri_evalf

        dual_evalf = np.linalg.norm(rho[k] * csc_matrix.dot(A_T, xi[k + 1]))
        eps_dual = np.sqrt(n) * ABSTOL + RELTOL * dual_evalf

        r_norm.append(np.linalg.norm(r[k + 1]))
        s_norm.append(np.linalg.norm(s[k + 1]))
        if r_norm[k + 1] <= eps_pri and s_norm[k + 1] <= eps_dual:
            break

        ################################
        ## penalty parameter - update ##
        ################################
        rho.append(penalty(rho[k], r_norm[k + 1], s_norm[k + 1]))

        #end rutine

    end = time.clock()
    ####################
    ## REPORTING DATA ##
    ####################
    #plotit(r,s,start,end,'Without acceleration / Without restarting for '+problem_data+' for rho: '+rho_method)

    time = end - start
    return time
Example #19
0
def cp_RR(problem_data, rho_method):

	######################
	## IMPORT LIBRARIES ##
	######################

	#Math libraries
	import numpy as np
	from scipy.sparse import csc_matrix
	from scipy.sparse import csr_matrix
	from scipy.sparse import linalg

	#Timing
	import time

	#Import data
	from Data.read_fclib import *

	#Plot residuals
	from Solver.ADMM_iteration.Numerics.plot import *

	#Initial penalty parameter
	import Solver.Rho.Optimal

	#Max iterations and kind of tolerance
	from Solver.Tolerance.iter_totaltolerance import *

	#Acceleration
	from Solver.Acceleration.plusr import *

	#b = Es matrix
	from Data.Es_matrix import *

	#Projection onto second order cone
	from Solver.ADMM_iteration.Numerics.projection import *

	#####################################################
	############# TERMS / NOT A FUNCTION YET ############
	#####################################################

	start = time.clock()
	problem = hdf5_file(problem_data)

	M = problem.M.tocsc()
	f = problem.f
	A = csc_matrix.transpose(problem.H.tocsc())
	A_T = csr_matrix.transpose(A)
	w = problem.w
	mu = problem.mu

	#Dimensions (normal,tangential,tangential)
	dim1 = 3 
	dim2 = np.shape(w)[0]

	#Problem size
	n = np.shape(M)[0]
	p = np.shape(w)[0]

	b = 0.1 * Es_matrix(w,mu,np.ones([p,])) / np.linalg.norm(Es_matrix(w,mu,np.ones([p,])))

	#################################
	############# SET-UP ############
	#################################

	#Set-up of vectors
	v = [np.zeros([n,])]
	u = [np.zeros([p,])] #this is u tilde, but in the notation of the paper is used as hat [np.zeros([10,0])]
	u_hat = [np.zeros([p,])] #u_hat[0] #in the notation of the paper this used with a underline
	xi = [np.zeros([p,])] 
	xi_hat = [np.zeros([p,])]
	r = [np.zeros([p,])] #primal residual
	s = [np.zeros([p,])] #dual residual
	r_norm = [0]
	s_norm = [0]
	tau = [1] #over-relaxation
	e = [] #restart

	#Optimal penalty parameter
	rho_string = 'Solver.Rho.Optimal.' + rho_method + '(A,M,A_T)'
	rho = eval(rho_string)

	########################################
	## TERMS COMMON TO ALL THE ITERATIONS ##
	########################################

	#Super LU factorization of M + rho * dot(M_T,M)
	P = M + rho * csc_matrix.dot(A_T,A)
	LU = linalg.splu(P)

	################
	## ITERATIONS ##
	################
	for k in range(MAXITER):
	
		################
		## v - update ##
		################
		RHS = -f + rho * csc_matrix.dot(A_T, -w - b - xi_hat[k] + u_hat[k])
		v.append(LU.solve(RHS)) #v[k+1]

		################
		## u - update ##
		################
		Av = csr_matrix.dot(A,v[k+1])
		vector = Av + xi_hat[k] + w + b
		u.append(projection(vector,mu,dim1,dim2)) #u[k+1]

		########################
		## residuals - update ##
		########################
		s.append(rho * csc_matrix.dot(A_T,(u[k+1]-u_hat[k]))) #s[k+1]
		r.append(Av - u[k+1] + w + b) #r[k+1]

		#################
		## xi - update ##
		#################
		xi.append(xi_hat[k] + r[k+1]) #xi[k+1]

		####################
		## stop criterion ##
		####################
		pri_evalf = np.amax(np.array([np.linalg.norm(csr_matrix.dot(A,v[k+1])),np.linalg.norm(u[k+1]),np.linalg.norm(w + b)]))
		eps_pri = np.sqrt(p)*ABSTOL + RELTOL*pri_evalf

		dual_evalf = np.linalg.norm(rho * csc_matrix.dot(A_T,xi[k+1]))
		eps_dual = np.sqrt(n)*ABSTOL + RELTOL*dual_evalf

		r_norm.append(np.linalg.norm(r[k+1]))
		s_norm.append(np.linalg.norm(s[k+1]))
		if r_norm[k+1]<=eps_pri and s_norm[k+1]<=eps_dual:
			break	

		###################################
		## accelerated ADMM with restart ##
		###################################
		plusr(tau,u,u_hat,xi,xi_hat,k,e,rho)
	
		#end rutine

	end = time.clock()
	####################
	## REPORTING DATA ##
	####################
	#plotit(r,s,start,end,'With acceleration / With restarting for '+problem_data+' for rho: '+rho_method)

	time = end - start
	return time
Example #20
0
def cp_N(problem_data, rho_method):

    ######################
    ## IMPORT LIBRARIES ##
    ######################

    #Math libraries
    import numpy as np
    from scipy.sparse import csc_matrix
    from scipy.sparse import csr_matrix
    from scipy.sparse import linalg

    #Timing
    import time

    #Import data
    from Data.read_fclib import *

    #Plot residuals
    from Solver.ADMM_iteration.Numerics.plot import *

    #Initial penalty parameter
    import Solver.Rho.Optimal

    #Max iterations and kind of tolerance
    from Solver.Tolerance.iter_totaltolerance import *

    #b = Es matrix
    from Data.Es_matrix import *

    #Projection onto second order cone
    from Solver.ADMM_iteration.Numerics.projection import *

    ##################################
    ############# REQUIRE ############
    ##################################

    start = time.clock()
    problem = hdf5_file(problem_data)

    M = problem.M.tocsc()
    f = problem.f
    A = csc_matrix.transpose(problem.H.tocsc())
    A_T = csr_matrix.transpose(A)
    w = problem.w
    mu = problem.mu

    #Dimensions (normal,tangential,tangential)
    dim1 = 3
    dim2 = np.shape(w)[0]

    #Problem size
    n = np.shape(M)[0]
    p = np.shape(w)[0]

    b = [
        1 / linalg.norm(A, 'fro') * Es_matrix(w, mu, np.ones([
            p,
        ])) / np.linalg.norm(Es_matrix(w, mu, np.ones([
            p,
        ])))
    ]

    #################################
    ############# SET-UP ############
    #################################

    #Set-up of vectors
    v = [np.zeros([
        n,
    ])]
    u = [
        np.zeros([
            p,
        ])
    ]  #this is u tilde, but in the notation of the paper is used as hat [np.zeros([10,0])]
    xi = [np.zeros([
        p,
    ])]
    r = [np.zeros([
        p,
    ])]  #primal residual
    s = [np.zeros([
        p,
    ])]  #dual residual
    r_norm = [0]
    s_norm = [0]
    e = []  #restart

    #Optimal penalty parameter
    start = time.clock()
    rho_string = 'Solver.Rho.Optimal.' + rho_method + '(A,M,A_T)'
    rho = eval(rho_string)

    ########################################
    ## TERMS COMMON TO ALL THE ITERATIONS ##
    ########################################

    #Super LU factorization of M + rho * dot(M_T,M)
    P = M + rho * csc_matrix.dot(A_T, A)
    LU = linalg.splu(P)

    ################
    ## ITERATIONS ##
    ################
    for j in range(MAXITER):

        for k in range(len(u) - 1, MAXITER):

            ################
            ## v - update ##
            ################
            RHS = -f + rho * csc_matrix.dot(A_T, -w - b[j] - xi[k] + u[k])
            v.append(LU.solve(RHS))  #v[k+1]

            ################
            ## u - update ##
            ################
            Av = csr_matrix.dot(A, v[k + 1])
            vector = Av + xi[k] + w + b[j]
            u.append(projection(vector, mu, dim1, dim2))  #u[k+1]

            ########################
            ## residuals - update ##
            ########################
            s.append(rho * csc_matrix.dot(A_T, (u[k + 1] - u[k])))  #s[k+1]
            r.append(Av - u[k + 1] + w + b[j])  #r[k+1]

            #################
            ## xi - update ##
            #################
            xi.append(xi[k] + r[k + 1])  #xi[k+1]

            ####################
            ## stop criterion ##
            ####################
            pri_evalf = np.amax(
                np.array([
                    np.linalg.norm(csr_matrix.dot(A, v[k + 1])),
                    np.linalg.norm(u[k + 1]),
                    np.linalg.norm(w + b[j])
                ]))
            eps_pri = np.sqrt(p) * ABSTOL + RELTOL * pri_evalf

            dual_evalf = np.linalg.norm(rho * csc_matrix.dot(A_T, xi[k + 1]))
            eps_dual = np.sqrt(n) * ABSTOL + RELTOL * dual_evalf

            r_norm.append(np.linalg.norm(r[k + 1]))
            s_norm.append(np.linalg.norm(s[k + 1]))
            if r_norm[k + 1] <= eps_pri and s_norm[k + 1] <= eps_dual:
                break

            #end rutine

        #b(s) stop criterion
        b.append(Es_matrix(w, mu, csr_matrix.dot(A, v[k + 1])))

        if j == 0:
            pass
        else:
            b_per_contact_j1 = np.split(b[j + 1], dim2 / dim1)
            b_per_contact_j0 = np.split(b[j], dim2 / dim1)
            count = 0
            for i in range(dim2 / dim1):
                if np.linalg.norm(b_per_contact_j1[i] -
                                  b_per_contact_j0[i]) / np.linalg.norm(
                                      b_per_contact_j0[i]) > 1e-02:
                    count += 1
            if count < 1:
                break

        v.append(np.zeros([
            n,
        ]))
        u.append(
            np.zeros([
                p,
            ])
        )  #this is u tilde, but in the notation of the paper is used as hat [np.zeros([10,0])]
        xi.append(np.zeros([
            p,
        ]))
        r.append(np.zeros([
            p,
        ]))  #primal residual
        s.append(np.zeros([
            p,
        ]))  #dual residual
        r_norm.append(0)
        s_norm.append(0)

    #print(1 / linalg.norm(A,'fro'))
    #print(1 / linalg.norm(A,1))
    print(b[-1])

    end = time.clock()
    ####################
    ## REPORTING DATA ##
    ####################
    plotit(
        b, s, start, end, 'Without acceleration / Without restarting for ' +
        problem_data + ' for rho: ' + rho_method)

    time = end - start
    return time
Example #21
0
def birgRank(G,
             Rtrain,
             dH,
             alpha=.5,
             theta=.5,
             mu=.5,
             eps=0,
             max_iters=0,
             nodes=None,
             verbose=False):
    """
    *Rtrain*: Matrix containing 0s and 1s for annotations. 
        Rows are nodes, columns are goids
    *alpha*: teleportation parameter in Personalized PageRank.
    *theta*: (1-theta) percent of Rtrain used in seeding vectors.
    *mu*: (1-mu) percent of random walkers diffuse from G via Rtrain to H.
    *eps*: use power iteration to approximate the scores, and iterate until x_i - x_i-1 < eps. 
        Default is 0 which means the solution will be solved directly.
        Useful as scipy's solver struggles with these large matrices
    *max_iters*: maximum # of iterations to run power iteration.
    *nodes*: set of nodes for which to run RWR and get GO term scores.
        Only used if eps or max_iters is not 0

    *returns*: Xh - m*n matrix of scores for each GO term, protein pair, same size as Rtrain
    """

    print("Starting birgRank")
    m, n = Rtrain.shape

    # make seeding vectors (matrix)
    B = vstack([theta * eye(m), (1 - theta) * Rtrain.T]).tocsc()
    # column normalize the matrix
    B = alg_utils.normalizeGraphEdgeWeights(B, axis=0)
    B = (1 - alpha) * B

    # make transition matrix
    #G = G/2 + G.T/2  # make sure G is symmetric
    # final matrix is:
    # [G  0]
    # [RT H]
    P = vstack([
        hstack([mu * G, csc_matrix((m, n))]),
        hstack([(1 - mu) * Rtrain.T, dH])
    ]).tocsc()
    # try reversing the R connection and see if that makes a difference
    #P = vstack([hstack([mu*G, (1-mu)*Rtrain]), hstack([csc_matrix((n,m)), dH])]).tocsc()
    # normalize using the same normalization as AptRank
    P = alg_utils.normalizeGraphEdgeWeights(P, axis=0)  # column normalization
    P = alpha * P
    # make sure they're in csc format for the solvers
    P = P.tocsc()
    B = B.tocsc()

    start_time = time.process_time()
    if eps != 0 or max_iters != 0:
        print("\tstarting power iteration over each node individually")
        # Version of birgrank using a power iteration. Useful as scipy's solver struggles with these large matrices
        # looks like the real problem is the amount of ram needed to store the results in the large matrix X
        # rather than power iterate with the entire matrix B, run power iteration for column of B individually
        # and then merge only the Xh results
        Xh = lil_matrix((m, n))
        # much faster to only compute scores for a subset of nodes
        if nodes is None:
            nodes = list(range(B.shape[1]))
        for i in tqdm(nodes):
            e = B[:, i].toarray().flatten()
            x = e.copy()
            prev_x = x.copy()
            for iters in range(1, max_iters + 1):
                x = csc_matrix.dot(P, prev_x) + e

                max_d = (x - prev_x).max()
                #if verbose:
                #    tqdm.write("\t\tmax score change: %0.6f" % (max_d))
                if max_d < eps:
                    break
                prev_x = x.copy()
            Xh[i] = x[m:]
            if verbose:
                print(
                    "\tbirgRank converged after %d iterations. max_d: %0.2e, eps: %0.2e"
                    % (iters, max_d, eps))
        Xh = Xh.T

        total_time = time.process_time() - start_time
        # this only shows the # of iterations for the last prot
        print(
            "\tbirgRank converged after %d iterations (%0.2f sec) for node %d"
            % (iters, total_time, i))
    else:
        A = eye(m + n) - P
        A = A.tocsc()
        # solve PageRank linear system using 3-block solver
        print("\tsolving for Xg")
        # now solve the linear system X = A/B
        # split it up into two equations to solve
        # (I-alpha*G)Xg = (1-alpha)I
        Xg = spsolve(A[:m, :][:, :m], B[:m, :])
        print("\tsolving for Xh")
        # alpha*RT*Xg = (I - alpha*H)Xh
        Xh = spsolve(A[m:, :][:, m:], (B[m:, :] - A[m:, :][:, :m] * Xg))

        total_time = time.process_time() - start_time
        print("\tsolved birgRank using sparse linear system (%0.2f sec)" %
              (total_time))

    # transpose Xh so it has the same dimensions as Rtrain
    # prot rows, goid columns
    return Xh.T
Example #22
0
def vp_RR_He(problem_data, rho_method):

	######################
	## IMPORT LIBRARIES ##
	######################

	#Math libraries
	import numpy as np
	from scipy.sparse import csc_matrix
	from scipy.sparse import csr_matrix
	from scipy.sparse import linalg

	#Timing
	import time

	#Import data
	from Data.read_fclib import *

	#Plot residuals
	from Solver.ADMM_iteration.Numerics.plot import *

	#Initial penalty parameter
	import Solver.Rho.Optimal

	#Max iterations and kind of tolerance
	from Solver.Tolerance.iter_totaltolerance import *

	#Acceleration
	from Solver.Acceleration.plusr_vp import *

	#Varying penalty parameter
	from Solver.Rho.Varying.He import *

	#b = Es matrix
	from Data.Es_matrix import *

	#Db = DEs matrix (derivative)
	from Data.DEs_matrix import *

	#Projection onto second order cone
	from Solver.ADMM_iteration.Numerics.projection import *

	##################################
	############# REQUIRE ############
	##################################

	start = time.clock()
	problem = hdf5_file(problem_data)

	M = problem.M.tocsc()
	f = problem.f
	A = csc_matrix.transpose(problem.H.tocsc())
	A_T = csr_matrix.transpose(A)
	w = problem.w
	mu = problem.mu

	#Dimensions (normal,tangential,tangential)
	dim1 = 3 
	dim2 = np.shape(w)[0]

	#Problem size
	n = np.shape(M)[0]
	p = np.shape(w)[0]

	b = [1/linalg.norm(A,'fro') * Es_matrix(w,mu,np.zeros([p,])) / np.linalg.norm(Es_matrix(w,mu,np.ones([p,])))]

	#################################
	############# SET-UP ############
	#################################

	#Set-up of vectors
	v = [np.zeros([n,])]
	u = [np.zeros([p,])] #this is u tilde, but in the notation of the paper is used as hat [np.zeros([10,0])]
	u_hat = [np.zeros([p,])] #u_hat[0] #in the notation of the paper this used with a underline
	xi = [np.zeros([p,])] 
	xi_hat = [np.zeros([p,])]
	r = [np.zeros([p,])] #primal residual
	s = [np.zeros([p,])] #dual residual
	r_norm = [0]
	s_norm = [0]
	tau = [1] #over-relaxation
	e = [] #restart
	rho = []

	#Optimal penalty parameter
	rho_string = 'Solver.Rho.Optimal.' + rho_method + '(A,M,A_T)'
	rh = eval(rho_string)
	rho.append(rh) #rho[0]

	################
	## ITERATIONS ##
	################

	for k in range(MAXITER):
		print k
	
		#Super LU factorization of M + rho * dot(M_T,M)
		if k == 0:
			P = M + rho[k] * csc_matrix.dot(A_T,A)
			LU = linalg.splu(P)

		else:		
			DE = DEs_matrix(w, mu, Av + w, A.toarray())			
			P = M + rho[k] * csc_matrix.dot(A_T + DE,A)
			LU = linalg.splu(P)

		################
		## v - update ##
		################

		if k==0:
			RHS = -f + rho[k] * csc_matrix.dot(A_T, -w - b[k] - xi_hat[k] + u_hat[k])
			v.append(LU.solve(RHS)) #v[k+1]

		else:
			RHS = -f + rho[k] * csc_matrix.dot(A_T + DE, -w - b[k] - xi_hat[k] + u_hat[k])
			v.append(LU.solve(RHS)) #v[k+1]	

		################
		## u - update ##
		################
		Av = csr_matrix.dot(A,v[k+1])
		vector = Av + xi_hat[k] + w + b[k]
		u.append(projection(vector,mu,dim1,dim2)) #u[k+1]

		########################
		## residuals - update ##
		########################
		s.append(rho[k] * csc_matrix.dot(A_T,(u[k+1]-u_hat[k]))) #s[k+1]
		r.append(Av - u[k+1] + w + b[k]) #r[k+1]

		#################
		## xi - update ##
		#################
		ratio = rho[k-1]/rho[k] #update of dual scaled variable with new rho
		xi.append(ratio*(xi_hat[k] + r[k+1])) #xi[k+1]

		#b update
		b.append(Es_matrix(w,mu,Av + w))

		####################
		## stop criterion ##
		####################
		pri_evalf = np.amax(np.array([np.linalg.norm(csr_matrix.dot(A,v[k+1])),np.linalg.norm(u[k+1]),np.linalg.norm(w + b[k+1])]))
		eps_pri = np.sqrt(p)*ABSTOL + RELTOL*pri_evalf

		dual_evalf = np.linalg.norm(rho[k] * csc_matrix.dot(A_T,xi[k+1]))
		eps_dual = np.sqrt(n)*ABSTOL + RELTOL*dual_evalf

		r_norm.append(np.linalg.norm(r[k+1]))
		s_norm.append(np.linalg.norm(s[k+1]))
		if r_norm[k+1]<=eps_pri and s_norm[k+1]<=eps_dual:
			orthogonal = np.dot(u[-1],rho[-2]*xi[-1])
			print orthogonal
			break

		#b_per_contact_j1 = np.split(b[k+1],dim2/dim1)
		#b_per_contact_j0 = np.split(b[k],dim2/dim1)
		#count = 0
		#for j in range(dim2/dim1):
		#	if np.linalg.norm(b_per_contact_j1[j] - b_per_contact_j0[j]) / np.linalg.norm(b_per_contact_j0[j]) > 1e-03:
		#		count += 1
		#if count < 1:		
		#	break

		###################################
		## accelerated ADMM with restart ##
		###################################
		plusr(tau,u,u_hat,xi,xi_hat,k,e,rho,ratio)

		################################
		## penalty parameter - update ##
		################################
		rho.append(penalty(rho[k],r_norm[k+1],s_norm[k+1]))	
	
		#end rutine

	end = time.clock()

	####################
	## REPORTING DATA ##
	####################
	#print b[-1]
	#print np.linalg.norm(b[-1])
	#plotit(r,s,start,end,'With acceleration / Without restarting for '+problem_data+' for rho: '+rho_method)
	plotit(r,s,start,end,'Internal update with vp_RR_He (Di Cairano)')

	time = end - start
	print 'Total time: ', time
	return time
Example #23
0
def vp_RR_He(problem_data, rho_method):

    ######################
    ## IMPORT LIBRARIES ##
    ######################

    #Math libraries
    import numpy as np
    from scipy.sparse import csc_matrix
    from scipy.sparse import csr_matrix
    from scipy.sparse import linalg

    #Timing
    import time

    #Import data
    from Data.read_fclib import *

    #Plot residuals
    from Solver.ADMM_iteration.Numerics.plot import *

    #Initial penalty parameter
    import Solver.Rho.Optimal

    #Max iterations and kind of tolerance
    from Solver.Tolerance.iter_totaltolerance import *

    #Acceleration
    from Solver.Acceleration.plusr_vp import *

    #Varying penalty parameter
    from Solver.Rho.Varying.He import *

    #b = Es matrix
    from Data.Es_matrix import *

    #Projection onto second order cone
    from Solver.ADMM_iteration.Numerics.projection import *

    ##################################
    ############# REQUIRE ############
    ##################################

    start = time.clock()
    problem = hdf5_file(problem_data)

    M = problem.M.tocsc()
    f = problem.f
    A = csc_matrix.transpose(problem.H.tocsc())
    A_T = csr_matrix.transpose(A)
    w = problem.w
    mu = problem.mu

    #Dimensions (normal,tangential,tangential)
    dim1 = 3
    dim2 = np.shape(w)[0]

    #Problem size
    n = np.shape(M)[0]
    p = np.shape(w)[0]

    b = [
        1 / linalg.norm(A, 'fro') * Es_matrix(w, mu, np.zeros([
            p,
        ])) / np.linalg.norm(Es_matrix(w, mu, np.ones([
            p,
        ])))
    ]

    #################################
    ############# SET-UP ############
    #################################

    #Set-up of vectors
    v = [np.zeros([
        n,
    ])]
    u = [
        np.zeros([
            p,
        ])
    ]  #this is u tilde, but in the notation of the paper is used as hat [np.zeros([10,0])]
    u_hat = [np.zeros([
        p,
    ])]  #u_hat[0] #in the notation of the paper this used with a underline
    xi = [np.zeros([
        p,
    ])]
    xi_hat = [np.zeros([
        p,
    ])]
    r = [np.zeros([
        p,
    ])]  #primal residual
    s = [np.zeros([
        p,
    ])]  #dual residual
    r_norm = [0]
    s_norm = [0]
    tau = [1]  #over-relaxation
    e = []  #restart
    rho = []

    #Optimal penalty parameter
    rho_string = 'Solver.Rho.Optimal.' + rho_method + '(A,M,A_T)'
    rh = eval(rho_string)
    rho.append(rh)  #rho[0]

    ################
    ## ITERATIONS ##
    ################

    for j in range(15):
        print j

        len_u = len(u) - 1
        for k in range(len_u, MAXITER):

            #Super LU factorization of M + rho * dot(M_T,M)
            if k == 0:  #rho[k] != rho[k-1] or
                P = M + rho[k] * csc_matrix.dot(A_T, A)
                LU = linalg.splu(P)
                LU_old = LU

            else:
                LU = LU_old

            #rho[k] != rho[k-1] or
            ################
            ## v - update ##
            ################
            RHS = -f + rho[k] * csc_matrix.dot(
                A_T, -w - b[j] - xi_hat[k] + u_hat[k])
            v.append(LU.solve(RHS))  #v[k+1]

            #P = M + rho[k] * csc_matrix.dot(A_T,A)
            #RHS = -f + rho[k] * csc_matrix.dot(A_T, -w - b[j] - xi_hat[k] + u_hat[k])
            #v.append(linalg.spsolve(P,RHS)) #v[k+1]

            #P = M + rho[k] * csc_matrix.dot(A_T,A)
            #LU = linalg.factorized(P)
            #RHS = -f + rho[k] * csc_matrix.dot(A_T, -w - b[j] - xi_hat[k] + u_hat[k])
            #v.append(LU(RHS)) #v[k+1]

            ################
            ## u - update ##
            ################
            Av = csr_matrix.dot(A, v[k + 1])
            vector = Av + xi_hat[k] + w + b[j]
            u.append(projection(vector, mu, dim1, dim2))  #u[k+1]

            ########################
            ## residuals - update ##
            ########################
            s.append(rho[k] * csc_matrix.dot(A_T,
                                             (u[k + 1] - u_hat[k])))  #s[k+1]
            r.append(Av - u[k + 1] + w + b[j])  #r[k+1]

            #################
            ## xi - update ##
            #################
            ratio = rho[k - 1] / rho[
                k]  #update of dual scaled variable with new rho
            xi.append(ratio * (xi_hat[k] + r[k + 1]))  #xi[k+1]

            if j != 0 and k != 0 and j < 2:
                #from mpl_toolkits.mplot3d import Axes3D
                #soa = np.array([np.concatenate((xi[k+1][:3],np.array([0,0,0]))).tolist()])
                #np.concatenate((xi[k][:3],np.array([0,0,0]))).tolist()
                #print rho[k]*xi[k+1][:3] * 1e19
                print u[k + 1][:3]

                #X, Y, Z, U, V, W = zip(*soa)
                #fig = plt.figure()
                #ax = fig.add_subplot(111, projection='3d')
                #ax.quiver(X, Y, Z, U, V, W)
                #ax.set_xlim([-1, 1])
                #ax.set_ylim([-1, 1])
                #ax.set_zlim([-1, 1])
                #plt.show()

                #plotit(xi[-2:], xi[-2:], start, 5.0,'External update with vp_RR_He (Di Cairano)')

                #print Av[-3:] - u[k+1][-3:] + w[-3:]

            ###################################
            ## accelerated ADMM with restart ##
            ###################################
            plusr(tau, u, u_hat, xi, xi_hat, k, e, rho, ratio)

            ################################
            ## penalty parameter - update ##
            ################################
            r_norm.append(np.linalg.norm(r[k + 1]))
            s_norm.append(np.linalg.norm(s[k + 1]))
            rho.append(penalty(rho[k], r_norm[k + 1], s_norm[k + 1]))
            #rho.append(0.125)

            ####################
            ## stop criterion ##
            ####################
            pri_evalf = np.amax(
                np.array([
                    np.linalg.norm(csr_matrix.dot(A, v[k + 1])),
                    np.linalg.norm(u[k + 1]),
                    np.linalg.norm(w + b[j])
                ]))
            eps_pri = np.sqrt(p) * ABSTOL + RELTOL * pri_evalf

            dual_evalf = np.linalg.norm(rho[k] *
                                        csc_matrix.dot(A_T, xi[k + 1]))
            eps_dual = np.sqrt(n) * ABSTOL + RELTOL * dual_evalf

            if r_norm[k + 1] <= eps_pri and s_norm[k + 1] <= eps_dual:
                R = rho[k] * xi[k + 1]
                N1 = csc_matrix.dot(M, v[k + 1]) - csc_matrix.dot(A_T, R) + f
                N2 = R - projection(R - u[k + 1], 1 / mu, dim1, dim2)
                N1_norm = np.linalg.norm(N1)
                N2_norm = np.linalg.norm(N2)

                print np.sqrt(N1_norm**2 + N2_norm**2)
                #print rho[k]*xi[k+1]

                #plotit(r[len_u:], xi[len_u:], start, 5.0,'External update with vp_RR_He (Di Cairano)')

                break

            #end rutine

        #b(s) stop criterion
        b.append(Es_matrix(w, mu, Av + w))

        if j == 0:
            pass
        else:
            b_per_contact_j1 = np.split(b[j + 1], dim2 / dim1)
            b_per_contact_j0 = np.split(b[j], dim2 / dim1)
            count = 0
            for i in range(dim2 / dim1):
                if np.linalg.norm(b_per_contact_j1[i] -
                                  b_per_contact_j0[i]) / np.linalg.norm(
                                      b_per_contact_j0[i]) > 1e-03:
                    count += 1
            if count < 1:
                #orthogonal = np.dot(u[-1],rho[-2]*xi[-1])
                #print orthogonal
                break

        v.append(np.zeros([
            n,
        ]))
        u.append(np.zeros([
            p,
        ]))
        u_hat.append(np.zeros([
            p,
        ]))
        xi.append(np.zeros([
            p,
        ]))
        xi_hat.append(np.zeros([
            p,
        ]))
        r.append(np.zeros([
            p,
        ]))  #primal residual
        s.append(np.zeros([
            p,
        ]))  #dual residual
        r_norm.append(0)
        s_norm.append(0)
        tau.append(1)  #over-relaxation
        e.append(np.nan)  #restart
        rho.append(rho[-1])

    end = time.clock()
    time = end - start
    ####################
    ## REPORTING DATA ##
    ####################

    print P.nnz
    print np.shape(P)

    f, axarr = plt.subplots(2, sharex=True)
    f.suptitle('Sharing X axis')
    axarr[0].plot(rho, label='rho')
    axarr[1].semilogy(r_norm, label='||r||')
    axarr[1].semilogy(s_norm, label='||s||')

    plt.show()

    #plt.semilogy(r_norm, label='||r||')
    #plt.hold(True)
    #plt.semilogy(rho, label='||s||')
    #plt.hold(True)
    #plt.ylabel('Residuals')
    #plt.xlabel('Iteration')
    #plt.text(len(r)/2,np.log(np.amax(S)+np.amax(R))/10,'N_iter = '+str(len(r)-1))
    #plt.text(len(r)/2,np.log(np.amax(S)+np.amax(R))/100,'Total time = '+str((end-start)*10**3)+' ms')
    #plt.text(len(r)/2,np.log(np.amax(S)+np.amax(R))/1000,'Time_per_iter = '+str(((end-start)/(len(r)-1))*10**3)+' ms')
    #plt.title('External update with vp_RR_He (Di Cairano)')
    #plt.legend()
    plt.show()

    #print 'Total time: ',time
    return time
Example #24
0
    psi = ravel(psi)
    psi = psi / h**(3. / 2)
    n = 2 * psi**2

    #exchange
    vex = -(3. / pi)**(1. / 3) * n**(1. / 3)

    #hartree
    #Lap3 Vh = -4*pi*n
    #pdb.set_trace()
    vh = cgs(Lap3, -4 * pi * (n + ncomp))[0] - vcomp  #Poisson solver

    vtot = vex + vh + vext
    #pdb.set_trace()
    T1 = csr_matrix.dot(-0.5 * Lap3, psi)
    T2 = csc_matrix.dot(csc_matrix(psi), T1)
    T = T2 * 2 * h**3
    T = T[0]

    Eext = sum(n * vext) * h**3
    Eh = 0.5 * sum(n * vh) * h**3
    Ex = sum(-(3. / 4) * (3. / pi)**(1. / 3) * n**(4. / 3)) * h**3

    Etot = T + Eext + Eh + Ex

    ediff = abs(Eprev - Etot)
    Eprev = Etot
    E = E[0]
    #pdb.set_trace()
    print 'Helium', Etot, T, Eext, Eh, Ex, ediff
Example #25
0
def vp_RR_He(problem_data, rho_method):

    ######################
    ## IMPORT LIBRARIES ##
    ######################

    #Math libraries
    import numpy as np
    from scipy.sparse import csc_matrix
    from scipy.sparse import csr_matrix
    from scipy.sparse import linalg

    #Timing
    import time

    #Import data
    from Data.read_fclib import *

    #Plot residuals
    from Solver.ADMM_iteration.Numerics.plot import *

    #Initial penalty parameter
    import Solver.Rho.Optimal

    #Max iterations and kind of tolerance
    from Solver.Tolerance.iter_totaltolerance import *

    #Acceleration
    from Solver.Acceleration.plusr_vp import *

    #Varying penalty parameter
    from Solver.Rho.Varying.He import *

    #b = Es matrix
    from Data.Es_matrix import *

    #Projection onto second order cone
    from Solver.ADMM_iteration.Numerics.projection import *

    ##################################
    ############# REQUIRE ############
    ##################################

    start = time.clock()
    problem = hdf5_file(problem_data)

    M = problem.M.tocsc()
    f = problem.f
    A = csc_matrix.transpose(problem.H.tocsc())
    A_T = csr_matrix.transpose(A)
    w = problem.w
    mu = problem.mu

    #Dimensions (normal,tangential,tangential)
    dim1 = 3
    dim2 = np.shape(w)[0]

    #Problem size
    n = np.shape(M)[0]
    p = np.shape(w)[0]

    b = [Es_matrix(w, mu, np.zeros([
        p,
    ]))]

    #################################
    ############# SET-UP ############
    #################################

    #Set-up of vectors
    v = [np.zeros([
        n,
    ])]
    u = [
        np.zeros([
            p,
        ])
    ]  #this is u tilde, but in the notation of the paper is used as hat [np.zeros([10,0])]
    u_hat = [np.zeros([
        p,
    ])]  #u_hat[0] #in the notation of the paper this used with a underline
    xi = [np.zeros([
        p,
    ])]
    xi_hat = [np.zeros([
        p,
    ])]
    r = [np.zeros([
        p,
    ])]  #primal residual
    s = [np.zeros([
        p,
    ])]  #dual residual
    r_norm = [0]
    s_norm = [0]
    tau = [1]  #over-relaxation
    e = []  #restart
    rho = []

    #Optimal penalty parameter
    rho_string = 'Solver.Rho.Optimal.' + rho_method + '(A,M,A_T)'
    rh = eval(rho_string)
    rho.append(rh)  #rho[0]

    #Plot
    rho_plot = []
    r_plot = []
    s_plot = []
    b_plot = []
    u_bin_plot = []
    xi_bin_plot = []
    siconos_plot = []

    ################
    ## ITERATIONS ##
    ################

    for j in range(200):
        print j

        len_u = len(u) - 1
        for k in range(len_u, MAXITER):

            #Super LU factorization of M + rho * dot(M_T,M)
            if rho[k] != rho[k - 1] or k == len_u:  #rho[k] != rho[k-1] or
                P = M + rho[k] * csc_matrix.dot(A_T, A)
                LU = linalg.splu(P)
                LU_old = LU

            else:
                LU = LU_old

            ################
            ## v - update ##
            ################
            RHS = -f + rho[k] * csc_matrix.dot(
                A_T, -w - b[j] - xi_hat[k] + u_hat[k])
            v.append(LU.solve(RHS))  #v[k+1]

            ################
            ## u - update ##
            ################
            Av = csr_matrix.dot(A, v[k + 1])
            vector = Av + xi_hat[k] + w + b[j]
            u.append(projection(vector, mu, dim1, dim2))  #u[k+1]

            ########################
            ## residuals - update ##
            ########################
            s.append(rho[k] * csc_matrix.dot(A_T,
                                             (u[k + 1] - u_hat[k])))  #s[k+1]
            r.append(Av - u[k + 1] + w + b[j])  #r[k+1]

            #################
            ## xi - update ##
            #################
            ratio = rho[k - 1] / rho[
                k]  #update of dual scaled variable with new rho
            xi.append(ratio * (xi_hat[k] + r[k + 1]))  #xi[k+1]

            ###################################
            ## accelerated ADMM with restart ##
            ###################################
            plusr(tau, u, u_hat, xi, xi_hat, k, e, rho, ratio)

            ################################
            ## penalty parameter - update ##
            ################################
            r_norm.append(np.linalg.norm(r[k + 1]))
            s_norm.append(np.linalg.norm(s[k + 1]))
            rho.append(penalty(rho[k], r_norm[k + 1], s_norm[k + 1]))

            ####################
            ## stop criterion ##
            ####################
            pri_evalf = np.amax(
                np.array([
                    np.linalg.norm(csr_matrix.dot(A, v[k + 1])),
                    np.linalg.norm(u[k + 1]),
                    np.linalg.norm(w + b[j])
                ]))
            eps_pri = np.sqrt(p) * ABSTOL + RELTOL * pri_evalf

            dual_evalf = np.linalg.norm(rho[k] *
                                        csc_matrix.dot(A_T, xi[k + 1]))
            eps_dual = np.sqrt(n) * ABSTOL + RELTOL * dual_evalf

            R = -rho[k] * xi[k + 1]
            N1 = csc_matrix.dot(M, v[k + 1]) - csc_matrix.dot(A_T, R) + f
            N2 = u[k + 1] - projection(u[k + 1] - R, mu, dim1, dim2)
            N1_norm = np.linalg.norm(N1)
            N2_norm = np.linalg.norm(N2)
            siconos_plot.append(np.sqrt(N1_norm**2 + N2_norm**2))

            if r_norm[k + 1] <= eps_pri and s_norm[k + 1] <= eps_dual:
                #if k == len_u:

                for element in range(len(u)):
                    #Relative velocity
                    u_proj = projection(u[element], mu, dim1, dim2)

                    u_proj_contact = np.split(u_proj, dim2 / dim1)
                    u_contact = np.split(u[element], dim2 / dim1)

                    u_count = 0.0
                    for contact in range(dim2 / dim1):
                        #if np.linalg.norm(u_contact[contact] - u_proj_contact[contact]) / np.linalg.norm(u_contact[contact]) < 1e-01:
                        if np.allclose(u_contact[contact],
                                       u_proj_contact[contact],
                                       rtol=0.1,
                                       atol=0.0):
                            u_count += 1.0

                    u_bin = 100 * u_count / (dim2 / dim1)
                    u_bin_plot.append(u_bin)

                    #Reaction
                    xi_proj = projection(-1 * xi[element], 1 / mu, dim1, dim2)

                    xi_proj_contact = np.split(xi_proj, dim2 / dim1)
                    xi_contact = np.split(-1 * xi[element], dim2 / dim1)

                    xi_count = 0.0
                    for contact in range(dim2 / dim1):
                        #if np.linalg.norm(xi_contact[contact] - xi_proj_contact[contact]) / np.linalg.norm(xi_contact[contact]) < 1e-01:
                        if np.allclose(xi_contact[contact],
                                       xi_proj_contact[contact],
                                       rtol=0.1,
                                       atol=0.0):
                            xi_count += 1.0

                    xi_bin = 100 * xi_count / (dim2 / dim1)
                    xi_bin_plot.append(xi_bin)

                for element in range(len(r_norm)):
                    rho_plot.append(rho[element])
                    r_plot.append(r_norm[element])
                    s_plot.append(s_norm[element])
                    b_plot.append(np.linalg.norm(b[j]))

                #print 'First contact'
                #print -rho[k]*xi[k+1][:3]
                #uy = projection(-rho[k]*xi[k+1],1/mu,dim1,dim2)
                #print uy[:3]
                #print 'Last contact'
                #print -rho[k]*xi[k+1][-3:]
                #print uy[-3:]

                #R = -rho[k]*xi[k+1]
                #N1 = csc_matrix.dot(M, v[k+1]) - csc_matrix.dot(A_T, R) + f
                #N2 = u[k+1] - projection(u[k+1] - R, mu, dim1, dim2)
                #N1_norm = np.linalg.norm(N1)
                #N2_norm = np.linalg.norm(N2)

                #print np.sqrt( N1_norm**2 + N2_norm**2 )
                print b_plot[-1]
                print b[-1][:3]
                print b[-1][-3:]
                break

            #end rutine

        #b(s) stop criterion
        b.append(Es_matrix(w, mu, Av + w))

        if j == 0:
            pass
        else:
            b_per_contact_j1 = np.split(b[j + 1], dim2 / dim1)
            b_per_contact_j0 = np.split(b[j], dim2 / dim1)
            count = 0
            for i in range(dim2 / dim1):
                if np.linalg.norm(b_per_contact_j1[i] -
                                  b_per_contact_j0[i]) / np.linalg.norm(
                                      b_per_contact_j0[i]) > 1e-03:
                    count += 1
            if count < 1:
                break

        v = [np.zeros([
            n,
        ])]
        u = [
            np.zeros([
                p,
            ])
        ]  #this is u tilde, but in the notation of the paper is used as hat [np.zeros([10,0])]
        u_hat = [np.zeros([
            p,
        ])]  #u_hat[0] #in the notation of the paper this used with a underline
        xi = [np.zeros([
            p,
        ])]
        xi_hat = [np.zeros([
            p,
        ])]
        r = [np.zeros([
            p,
        ])]  #primal residual
        s = [np.zeros([
            p,
        ])]  #dual residual
        r_norm = [0]
        s_norm = [0]
        tau = [1]  #over-relaxation
        e = []  #restart
        rho = [rho[-1]]

    end = time.clock()
    time = end - start
    ####################
    ## REPORTING DATA ##
    ####################

    f, axarr = plt.subplots(5, sharex=True)
    f.suptitle('External update with vp_RR_He (Di Cairano)')

    axarr[0].semilogy(b_plot)
    axarr[0].set(ylabel='||Phi(s)||')

    axarr[1].plot(rho_plot)
    axarr[1].set(ylabel='Rho')

    axarr[2].semilogy(r_plot, label='||r||')
    axarr[2].semilogy(s_plot, label='||s||')
    axarr[2].legend()
    axarr[2].set(ylabel='Residuals')

    axarr[3].semilogy(siconos_plot)
    axarr[3].set(ylabel='SICONOS error')

    axarr[4].plot(u_bin_plot, label='u in K*')
    axarr[4].plot(xi_bin_plot, label='-xi in K')
    axarr[4].legend()
    axarr[4].set(xlabel='Iteration', ylabel='Projection (%)')

    plt.show()

    print 'Total time: ', time
    return time