예제 #1
0
 def __init__(self, name=None, parityCheckMatrix=None, q=None):
     if parityCheckMatrix is not None:
         if utils.isStr(parityCheckMatrix):
             self.filename = os.path.expanduser(parityCheckMatrix)
             self._parityCheckMatrix = matrices.getNonbinaryMatrix(
                 self.filename)
             if name is None:
                 name = os.path.basename(self.filename)
         elif not isinstance(parityCheckMatrix, np.ndarray):
             self._parityCheckMatrix = matrices.getNonbinaryMatrix(
                 parityCheckMatrix)
         else:
             self._parityCheckMatrix = parityCheckMatrix
         if q is None:
             q = int(np.max(self.parityCheckMatrix) +
                     1)  # avoid having q stored as numpy int
         self.blocklength = self.parityCheckMatrix.shape[1]
         rank = gfqla.rank(self.parityCheckMatrix, q)
         self.infolength = self.blocklength - rank
         cols = np.hstack(
             (np.arange(self.infolength,
                        self.blocklength), np.arange(self.infolength)))
         self._generatorMatrix = gfqla.orthogonalComplement(
             self.parityCheckMatrix, columns=cols, q=q)
     LinearBlockCode.__init__(self, q, name)
예제 #2
0
파일: matrices.py 프로젝트: supermihi/lpdec
def getBinaryMatrix(source):
    """Creates a binary matrix of type :class:`np.ndarray` from either a file or a
    two-dimensional python list.

    If `source` is a file path, the file must either contain an explicit representation of the
    matrix (by means of whitespace-separated '0' and '1' characters) or be in the AList format
    (see alistToNumpy docstring).

    The file may be bzip2'ed, in which case it will be decompressed automatically.

    Returns
    -------
    np.ndarray[dtype=int]
        Numpy ndarray representation of the given matrix.
    """
    if isinstance(source, np.ndarray):
        return source.astype(np.int)
    if utils.isStr(source):
        source = os.path.expanduser(source)
        fileObj = bz2.BZ2File(source, 'r') if source.endswith('bz2') else open(source, 'rt')
        with fileObj as f:
            lines = [[int(x) for x in l.strip().split()]
                     for l in f.readlines()
                     if len(l.strip()) > 0]

    else:
        assert hasattr(source, '__iter__') and hasattr(source[0], '__iter__')
        lines = source
    if lines[0][0] in (0, 1):  # explicit 0/1 representation
        return np.array(lines, dtype=np.int)
    return alistToNumpy(lines)
예제 #3
0
파일: ip.py 프로젝트: zqcawzs/lpdec
    def __init__(self, code, gurobiParams=None, gurobiVersion=None, name=None):

        if name is None:
            name = 'GurobiIPDecoder'
        if utils.isStr(gurobiParams):
            # allow to specify tuning set parameters using gurobiParams='tuning1' or similar
            for i in ('1', '2', '3'):
                if i in gurobiParams:
                    gurobiParams = getattr(self, 'tuningSet' + i)
                    break
        elif gurobiParams is None:
            gurobiParams = self.tuningSet1
        gurobihelpers.GurobiDecoder.__init__(self, code, name, gurobiParams, gurobiVersion,
                                             integer=True)
        from gurobimh import GRB, quicksum
        matrix = code.parityCheckMatrix
        for i in range(code.blocklength):
            self.model.addConstr(quicksum(self.x[i, k] for k in range(1, code.q)),
                                 GRB.LESS_EQUAL, 1)
        self.z = []
        for i in range(matrix.shape[0]):
            ub = np.sum(matrix[i]) * (code.q - 1) // code.q
            self.z.append(self.model.addVar(0, ub, vtype=GRB.INTEGER, name='z{}'.format(i)))
        self.model.update()
        for z, row in zip(self.z, matrix):
            self.model.addConstr(quicksum(row[i]*k*self.x[i, k] for k in range(1, code.q) for i
                                          in np.flatnonzero(row)) - code.q * z, GRB.EQUAL, 0)
        self.model.update()
        self.mlCertificate = self.foundCodeword = True
예제 #4
0
def getBinaryMatrix(source):
    """Creates a binary matrix of type :class:`np.ndarray` from either a file or a
    two-dimensional python list.

    If `source` is a file path, the file must either contain an explicit representation of the
    matrix (by means of whitespace-separated '0' and '1' characters) or be in the AList format
    (see alistToNumpy docstring).

    The file may be bzip2'ed, in which case it will be decompressed automatically.

    Returns
    -------
    np.ndarray[dtype=int]
        Numpy ndarray representation of the given matrix.
    """
    if isinstance(source, np.ndarray):
        return source.astype(np.int)
    if utils.isStr(source):
        source = os.path.expanduser(source)
        fileObj = bz2.BZ2File(source, 'r') if source.endswith('bz2') else open(
            source, 'rt')
        with fileObj as f:
            lines = [[int(x) for x in l.strip().split()]
                     for l in f.readlines() if len(l.strip()) > 0]

    else:
        assert hasattr(source, '__iter__') and hasattr(source[0], '__iter__')
        lines = source
    if lines[0][0] in (0, 1):  # explicit 0/1 representation
        return np.array(lines, dtype=np.int)
    return alistToNumpy(lines)
예제 #5
0
파일: matrices.py 프로젝트: supermihi/lpdec
def getNonbinaryMatrix(source):
    """Reads a nonbinary matrix (no AList support)."""
    if utils.isStr(source):
        with open(source, 'rt') as f:
            lines = [[int(x) for x in l.strip().split()]
                     for l in f.readlines() if len(l.strip()) > 0]
        return np.array(lines, dtype=np.int)
    else:
        return np.array(source, dtype=np.int)
예제 #6
0
def getNonbinaryMatrix(source):
    """Reads a nonbinary matrix (no AList support)."""
    if utils.isStr(source):
        with open(source, 'rt') as f:
            lines = [[int(x) for x in l.strip().split()]
                     for l in f.readlines() if len(l.strip()) > 0]
        return np.array(lines, dtype=np.int)
    else:
        return np.array(source, dtype=np.int)
예제 #7
0
    def __init__(self, name=None, parityCheckMatrix=None, generatorMatrix=None):

        self._parityCheckMatrix = self._generatorMatrix = None
        if parityCheckMatrix is not None:
            assert generatorMatrix is None
            if utils.isStr(parityCheckMatrix) and name is None:
                name = os.path.basename(parityCheckMatrix)
            hmatrix = matrices.getBinaryMatrix(parityCheckMatrix)
            self._parityCheckMatrix = hmatrix
            cols = hmatrix.shape[1]
            rank = gfqla.rank(hmatrix)
            self.blocklength = cols
            self.infolength = cols - rank
        elif generatorMatrix is not None:
            assert name is not None
            gmatrix = matrices.getBinaryMatrix(generatorMatrix)
            self._generatorMatrix = gmatrix
            self.blocklength = gmatrix.shape[1]
            self.infolength = gmatrix.shape[0]
            assert gfqla.rank(gmatrix, 2) == self.infolength
        LinearBlockCode.__init__(self, 2,  name)
예제 #8
0
 def __init__(self, name=None, parityCheckMatrix=None, q=None):
     if parityCheckMatrix is not None:
         if utils.isStr(parityCheckMatrix):
             self.filename = os.path.expanduser(parityCheckMatrix)
             self._parityCheckMatrix = matrices.getNonbinaryMatrix(self.filename)
             if name is None:
                 name = os.path.basename(self.filename)
         elif not isinstance(parityCheckMatrix, np.ndarray):
                 self._parityCheckMatrix = matrices.getNonbinaryMatrix(parityCheckMatrix)
         else:
             self._parityCheckMatrix = parityCheckMatrix
         if q is None:
             q = int(np.max(self.parityCheckMatrix) + 1)  # avoid having q stored as numpy int
         self.blocklength = self.parityCheckMatrix.shape[1]
         rank = gfqla.rank(self.parityCheckMatrix, q)
         self.infolength = self.blocklength - rank
         cols = np.hstack((np.arange(self.infolength, self.blocklength),
                               np.arange(self.infolength)))
         self._generatorMatrix = gfqla.orthogonalComplement(self.parityCheckMatrix,
                                                            columns=cols, q=q)
     LinearBlockCode.__init__(self, q, name)
예제 #9
0
    def __init__(self,
                 name=None,
                 parityCheckMatrix=None,
                 generatorMatrix=None):

        self._parityCheckMatrix = self._generatorMatrix = None
        if parityCheckMatrix is not None:
            assert generatorMatrix is None
            if utils.isStr(parityCheckMatrix) and name is None:
                name = os.path.basename(parityCheckMatrix)
            hmatrix = matrices.getBinaryMatrix(parityCheckMatrix)
            self._parityCheckMatrix = hmatrix
            cols = hmatrix.shape[1]
            rank = gfqla.rank(hmatrix)
            self.blocklength = cols
            self.infolength = cols - rank
        elif generatorMatrix is not None:
            assert name is not None
            gmatrix = matrices.getBinaryMatrix(generatorMatrix)
            self._generatorMatrix = gmatrix
            self.blocklength = gmatrix.shape[1]
            self.infolength = gmatrix.shape[0]
            assert gfqla.rank(gmatrix, 2) == self.infolength
        LinearBlockCode.__init__(self, 2, name)