def getWeightMatrix(self):
        """
        Return the weight matrix in dense format. Warning: should not be used
        unless sufficient memory is available to store the dense matrix.

        :returns: A numpy.ndarray weight matrix.
        """
        W = PysparseMatrix(matrix=self.W)
        return W.getNumpyArray()
    def inDegreeSequence(self):
        """
        Return a vector of the (in)degree sequence for each vertex.
        """
        A = self.nativeAdjacencyMatrix()
        j = spmatrix.ll_mat(self.vList.getNumVertices(), 1)
        j[:, 0] = 1

        degrees = spmatrix.dot(A, j)
        degrees = PysparseMatrix(matrix=degrees)
        degrees = numpy.array(degrees.getNumpyArray().ravel(), numpy.int)
        return degrees
예제 #3
0
 def setUp(self):
     self.n = 50000
     self.A = PysparseMatrix(matrix=poisson.poisson1d_sym(self.n))
     
     self.x_exact = numpy.ones(self.n)/math.sqrt(self.n)
     self.normx = 1.0/math.sqrt(self.n)
     self.b = self.A * self.x_exact
     
     lmbd_min = 4.0 * math.sin(math.pi/2.0/self.n) ** 2
     lmbd_max = 4.0 * math.sin((self.n - 1)*math.pi/2.0/self.n) ** 2
     cond = lmbd_max/lmbd_min
     self.tol = cond * macheps()
     self.LU = None
     self.relerr = 0.0
     self.descr = ''
     self.fmt = '\t%10s  %8.2e  %8.2e  %8d  %8d  %6.2f  %6.2f\n'
예제 #4
0
파일: cqp.py 프로젝트: vishalbelsare/nlpy
    def initialize_kkt_matrix(self):
        # [ -(Q+ρI)      0             A1' ] [∆x]   [c + Q x - A1' y     ]
        # [  0      -(S^{-1} Z + ρI)   A2' ] [∆s] = [- A2' y - µ S^{-1} e]
        # [  A1          A2            δI  ] [∆y]   [b - A1 x - A2 s     ]
        m, n = self.A.shape
        on = self.qp.original_n
        H = PysparseMatrix(size=n + m,
                           sizeHint=n + m + self.A.nnz + self.Q.nnz,
                           symmetric=True)

        # The (1,1) block will always be Q (save for its diagonal).
        H[:on, :on] = -self.Q

        # The (3,1) and (3,2) blocks will always be A.
        # We store it now once and for all.
        H[n:, :n] = self.A
        return H
예제 #5
0
파일: cqp.py 프로젝트: vishalbelsare/nlpy
    def initialize_kkt_matrix(self):
        # [ -(Q+ρI)   0        A1'     0   ]
        # [  0       -ρI       A2'  Z^{1/2}]
        # [  A1       A2       δI      0   ]
        # [  0        Z^{1/2}  0       S   ]
        m, n = self.A.shape
        on = self.qp.original_n
        H = PysparseMatrix(size=2 * n + m - on,
                           sizeHint=4 * on + m + self.A.nnz + self.Q.nnz,
                           symmetric=True)

        # The (1,1) block will always be Q (save for its diagonal).
        H[:on, :on] = -self.Q

        # The (2,1) block will always be A. We store it now once and for all.
        H[n:n + m, :n] = self.A
        return H
예제 #6
0
    def setUp(self):
        self.n = 200
        self.A = PysparseMatrix(matrix=poisson.poisson2d_sym_blk(self.n))
        
        self.x_exact = numpy.ones(self.n*self.n)/self.n
        self.normx = 1.0/self.n
        self.b = self.A * self.x_exact

        h = 1.0 / self.n
        lmbd_min = 4.0/h/h * (math.sin(math.pi*h/2.0) ** 2 +
                              math.sin(math.pi*h/2.0) ** 2)
        lmbd_max = 4.0/h/h * (math.sin((self.n - 1)*math.pi*h/2.0) ** 2 +
                              math.sin((self.n - 1)*math.pi*h/2.0) ** 2)
        cond = lmbd_max/lmbd_min
        self.tol = cond * macheps()
        self.LU = None
        self.relerr = 0.0
        self.descr = ''
        self.fmt = '\t%10s  %8.2e  %8.2e  %8d  %8d  %6.2f  %6.2f\n'
예제 #7
0
파일: lp.py 프로젝트: b45ch1/nlpy
    def __init__(self, lp, **kwargs):
        """
        Solve a linear program of the form::

            minimize c' x   subject to  A1 x + A2 s = b  and  s >= 0,      (LP)

        where the variables x are the original problem variables and s are
        slack variables. Any linear program may be converted to the above form
        by instantiation of the `SlackFramework` class. The conversion to the
        slack formulation is mandatory in this implementation.

        The method is a variant of Mehrotra's predictor-corrector method where
        steps are computed by solving the primal-dual system in augmented form.

        Primal and dual regularization parameters may be specified by the user
        via the opional keyword arguments `regpr` and `regdu`. Both should be
        positive real numbers and should not be "too large". By default they are
        set to 1.0 and updated at each iteration.

        If `scale` is set to `True`, (LP) is scaled automatically prior to
        solution so as to equilibrate the rows and columns of the constraint
        matrix [A1 A2].

        Advantages of this method are that it is not sensitive to dense columns
        in A, no special treatment of the unbounded variables x is required, and
        a sparse symmetric quasi-definite system of equations is solved at each
        iteration. The latter, although indefinite, possesses a Cholesky-like
        factorization. Those properties makes the method typically more robust
        that a standard predictor-corrector implementation and the linear system
        solves are often much faster than in a traditional interior-point method
        in augmented form.

        :keywords:
            :scale: Perform row and column equilibration of the constraint
                    matrix [A1 A2] prior to solution (default: `True`).

            :stabilize: Scale the linear system to be solved at each iteration
                        (default: `True`).

            :regpr: Initial value of primal regularization parameter
                    (default: `1.0`).

            :regdu: Initial value of dual regularization parameter
                    (default: `1.0`).

            :verbose: Turn on verbose mode (default `False`).
        """

        if not isinstance(lp, SlackFramework):
            msg = 'Input problem must be an instance of SlackFramework'
            raise ValueError, msg

        scale = kwargs.get('scale', True)
        self.verbose = kwargs.get('verbose', True)
        self.stabilize = kwargs.get('stabilize', True)

        self.lp = lp
        self.A = lp.A()               # Constraint matrix
        if not isinstance(self.A, PysparseMatrix):
            self.A = PysparseMatrix(matrix=self.A)

        m, n = self.A.shape
        # Record number of slack variables in LP
        self.nSlacks  = lp.n - lp.original_n

        # Constant vectors
        zero = np.zeros(n)
        self.b = -lp.cons(zero)     # Right-hand side
        self.c0 = lp.obj(zero)      # Constant term in objective
        self.c =  lp.grad(zero[:lp.original_n]) #lp.cost()  # Cost vector

        # Apply in-place problem scaling if requested.
        self.prob_scaled = False
        if scale:
            self.t_scale = cputime()
            self.scale()
            self.t_scale = cputime() - self.t_scale

        self.normb  = norm2(self.b)
        self.normc  = norm2(self.c)
        self.normbc = 1 + max(self.normb, self.normc)

        # Initialize augmented matrix
        self.H = PysparseMatrix(size=n+m,
                                sizeHint=n+m+self.A.nnz,
                                symmetric=True)

        # We perform the analyze phase on the augmented system only once.
        # self.LBL will be initialized in set_initial_guess().
        self.LBL = None

        self.regpr = kwargs.get('regpr', 1.0) ; self.regpr_min = 1.0e-8
        self.regdu = kwargs.get('regdu', 1.0) ; self.regdu_min = 1.0e-8

        # Check input parameters.
        if self.regpr < 0.0: self.regpr = 0.0
        if self.regdu < 0.0: self.regdu = 0.0

        # Dual regularization is necessary for stabilization.
        if self.regdu == 0.0:
            sys.stderr.write('Warning: No dual regularization in effect\n')
            sys.stderr.write('         Stabilization has been turned off\n')
            self.stabilize = False

        # Initialize format strings for display
        fmt_hdr = '%-4s  %9s' + '  %-8s'*6 + '  %-7s  %-4s  %-4s' + '  %-8s'*8
        self.header = fmt_hdr % ('Iter', 'Cost', 'pResid', 'dResid', 'cResid',
                                 'rGap', 'qNorm', 'rNorm', 'Mu', 'AlPr', 'AlDu',
                                 'LS Resid', 'RegPr', 'RegDu', 'Rho q', 'Del r',
                                 'Min(s)', 'Min(z)', 'Max(s)')
        self.format1  = '%-4d  %9.2e'
        self.format1 += '  %-8.2e' * 6
        self.format2  = '  %-7.1e  %-4.2f  %-4.2f'
        self.format2 += '  %-8.2e' * 8 + '\n'

        if self.verbose: self.display_stats()

        return
예제 #8
0
class TraubDataViz(FigureCanvas):
    """Class for visualizing data saved in custom HDF5 files in Traub model simulations."""
    def __init__(self, filename):
        FigureCanvas.__init__(self, Figure())
        self.spike_axes = self.figure.add_subplot(131)
        self.spike_axes.set_title('Spike trains')
        self.vm_axes = self.figure.add_subplot(132)
        self.vm_axes.set_title('Vm')
        self.ca_axes = self.figure.add_subplot(133)
        self.ca_axes.set_title('[Ca2+]')
        self.spike_axes_bg = self.copy_from_bbox(self.spike_axes.bbox)
        self.vm_axes_bg = self.copy_from_bbox(self.vm_axes.bbox)
        self.ca_axes_bg = self.copy_from_bbox(self.ca_axes.bbox)
        self.datafilename = filename
        self.spiketrain_dict = {}
        self.vm_dict = {}
        self.ca_dict = {}
        self.spike_matrix = None
        self.simtime = None
        self.simdt = None
        self.plotdt = None
        self.timestamp = None
        self.frame_count = 0
        self.timepoints = []
        self.cell_index_map = {}
        self.index_cell_map = {}
        self._read_data()
        self.draw()

    def _read_data(self):
        """Read spiketime serieses, Vm serieses and [Ca2+] serieses from data file."""
        with tables.openFile(self.datafilename) as h5file:
            try:
                self.simtime = h5file.root._v_attrs.simtime
                self.simdt = h5file.root._v_attrs.simdt
                self.plotdt = h5file.root._v_attrs.plotdt
                self.timestamp = h5file.root._v_attrs.timestamp
            except AttributeError, e:
                print e
            try:
                count = 0
                for train in h5file.root.spiketimes:
                    cell_name = train.name[:train.name.rfind('_')]
                    self.spiketrain_dict[cell_name] = train[:]
                    self.cell_index_map[cell_name] = count
                    self.index_cell_map[count] = cell_name
                    count += 1
            except tables.NoSuchNodeError:
                print 'No node called /spiketimes'
            try:
                for vm_array in h5file.root.Vm:
                    cell_name = vm_array.name[:vm_array.name.rfind('_')]
                    self.vm_dict[cell_name] = vm_array[:]
            except tables.NoSuchNodeError:
                print 'No node called /Vm'

            try:
                for ca_array in h5file.root.Ca:
                    cell_name = ca_array.name[:ca_array.name.rfind('_')]
                    self.ca_dict[cell_name] = ca_array[:]
            except tables.NoSuchNodeError:
                print 'No node called /Ca'
        print 'SIMULATION DONE ON', self.timestamp, '-- SIMTIME:', self.simtime, ', SIMDT:', self.simdt, ', PLOTDT:', self.plotdt

        if self.vm_dict:
            for cell_name, vm_array in self.vm_dict.items():
                self.frame_count = len(vm_array)
                break
        elif self.simtime and self.plotdt:
            self.frame_count = int(self.simtime / self.plotdt + 0.5)
        if not self.simtime:
            self.simtime = 1.0
        self.timepoints = np.linspace(0, self.simtime, self.frame_count)
        if self.spiketrain_dict:
            spike_mat = PysparseMatrix(nrow=len(self.spiketrain_dict.keys()),
                                       ncol=len(self.timepoints))
            for index in range(len(self.index_cell_map.keys())):
                cell_name = self.index_cell_map[index]
                try:
                    spiketrain = self.spiketrain_dict[cell_name]
                    spike_mat.put(
                        1.0, np.array([index] * len(spiketrain),
                                      dtype='int32'),
                        np.cast['int32'](spiketrain / self.plotdt + 0.5))
                except KeyError:
                    print 'No cell corresponding to index', index
            self.spike_matrix = spike_mat.getNumpyArray()
        self.vm_axes.set_xlim(self.simtime)
        self.ca_axes.set_xlim(self.simtime)
예제 #9
0
    def __init__(self, qp, **kwargs):
        """
        Solve a convex quadratic program of the form::

           minimize    c' x + 1/2 x' Q x
           subject to  A1 x + A2 s = b,                                 (QP)
                       s >= 0,

        where Q is a symmetric positive semi-definite matrix, the variables
        x are the original problem variables and s are slack variables. Any
        quadratic program may be converted to the above form by instantiation
        of the `SlackFramework` class. The conversion to the slack formulation
        is mandatory in this implementation.

        The method is a variant of Mehrotra's predictor-corrector method where
        steps are computed by solving the primal-dual system in augmented form.

        Primal and dual regularization parameters may be specified by the user
        via the opional keyword arguments `regpr` and `regdu`. Both should be
        positive real numbers and should not be "too large". By default they are
        set to 1.0 and updated at each iteration.

        If `scale` is set to `True`, (QP) is scaled automatically prior to
        solution so as to equilibrate the rows and columns of the constraint
        matrix [A1 A2].

        Advantages of this method are that it is not sensitive to dense columns
        in A, no special treatment of the unbounded variables x is required, and
        a sparse symmetric quasi-definite system of equations is solved at each
        iteration. The latter, although indefinite, possesses a Cholesky-like
        factorization. Those properties makes the method typically more robust
        that a standard predictor-corrector implementation and the linear system
        solves are often much faster than in a traditional interior-point method
        in augmented form.

        :keywords:
            :scale: Perform row and column equilibration of the constraint
                    matrix [A1 A2] prior to solution (default: `True`).

            :regpr: Initial value of primal regularization parameter
                    (default: `1.0`).

            :regdu: Initial value of dual regularization parameter
                    (default: `1.0`).

            :verbose: Turn on verbose mode (default `False`).
        """

        if not isinstance(qp, SlackFramework):
            msg = 'Input problem must be an instance of SlackFramework'
            raise ValueError, msg

        self.verbose = kwargs.get('verbose', True)
        scale = kwargs.get('scale', True)

        self.qp = qp
        self.A = qp.A()  # Constraint matrix
        if not isinstance(self.A, PysparseMatrix):
            self.A = PysparseMatrix(matrix=self.A)

        m, n = self.A.shape
        on = qp.original_n
        # Record number of slack variables in QP
        self.nSlacks = qp.n - on

        # Collect basic info about the problem.
        zero = np.zeros(n)

        self.b = -qp.cons(zero)  # Right-hand side
        self.c0 = qp.obj(zero)  # Constant term in objective
        self.c = qp.grad(zero[:on])  # Cost vector
        self.Q = PysparseMatrix(
            matrix=qp.hess(zero[:on], np.zeros(qp.original_m)))

        # Apply in-place problem scaling if requested.
        self.prob_scaled = False
        if scale:
            self.t_scale = cputime()
            self.scale()
            self.t_scale = cputime() - self.t_scale
        else:
            # self.scale() sets self.normQ to the Frobenius norm of Q
            # as a by-product. If we're not scaling, set normQ manually.
            self.normQ = self.Q.matrix.norm('fro')

        self.normb = norm_infty(self.b)
        self.normc = norm_infty(self.c)
        self.normbc = 1 + max(self.normb, self.normc)

        # Initialize augmented matrix
        self.H = PysparseMatrix(size=n + m,
                                sizeHint=n + m + self.A.nnz + self.Q.nnz,
                                symmetric=True)

        # The (1,1) block will always be Q (save for its diagonal).
        self.H[:on, :on] = -self.Q

        # The (2,1) block will always be A. We store it now once and for all.
        self.H[n:, :n] = self.A

        # It will be more efficient to keep the diagonal of Q around.
        self.diagQ = self.Q.take(range(qp.original_n))

        # We perform the analyze phase on the augmented system only once.
        # self.LBL will be initialized in solve().
        self.LBL = None

        # Set regularization parameters.
        self.regpr = kwargs.get('regpr', 1.0)
        self.regpr_min = 1.0e-8
        self.regdu = kwargs.get('regdu', 1.0)
        self.regdu_min = 1.0e-8

        # Check input parameters.
        if self.regpr < 0.0: self.regpr = 0.0
        if self.regdu < 0.0: self.regdu = 0.0

        # Initialize format strings for display
        fmt_hdr = '%-4s  %9s' + '  %-8s' * 6 + '  %-7s  %-4s  %-4s' + '  %-8s' * 8
        self.header = fmt_hdr % ('Iter', 'Cost', 'pResid', 'dResid', 'cResid',
                                 'rGap', 'qNorm', 'rNorm', 'Mu', 'AlPr',
                                 'AlDu', 'LS Resid', 'RegPr', 'RegDu', 'Rho q',
                                 'Del r', 'Min(s)', 'Min(z)', 'Max(s)')
        self.format1 = '%-4d  %9.2e'
        self.format1 += '  %-8.2e' * 6
        self.format2 = '  %-7.1e  %-4.2f  %-4.2f'
        self.format2 += '  %-8.2e' * 8 + '\n'

        if self.verbose: self.display_stats()

        return
예제 #10
0
    return krylov.bicgstab(*args, **kwargs)


@Deprecated('Use pysparse.itsolvers.Gmres instead.')
def gmres(*args, **kwargs):
    return krylov.gmres(*args, **kwargs)


if __name__ == '__main__':
    import numpy
    from pysparse.precon import precon
    from pysparse.tools import poisson

    n = 100
    n2 = n * n
    A = PysparseMatrix(matrix=poisson.poisson2d_sym(n))
    b = numpy.ones(n2)
    b /= numpy.linalg.norm(b)
    x = numpy.empty(n2)
    K = precon.ssor(A.matrix.to_sss())
    fmt = '%8s  %7.1e  %2d  %4d'

    def resid(A, b, x):
        r = b - A * x
        return numpy.linalg.norm(r)

    for Solver in [Pcg, Minres, Cgs, Qmrs, Gmres, Bicgstab]:
        solver = Solver(A)
        solver.solve(b, x, 1.0e-6, 3 * n)
        print fmt % (solver.name, resid(
            A, b, x), solver.nofCalled, solver.totalIterations)
예제 #11
0
 def jac(self, x):
     """
     Return the Jacobian matrix of the equality constraints at x as a PysparseMatrix.
     """
     _J = self.nlp.jac(x, store_zeros=True)  # Keep explicit zeros.
     return PysparseMatrix(matrix=_J)