/*******************************************************************************
 *
 *  pseudo-codeword search for linear programming decoding
 *  Copyright (C) 2010 Misha Stepanov
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 ******************************************************************************/

/* Finding low weight pseudocodewords using LP decoding and the
 * procedure described in
 *   M. Chertkov, M.G. Stepanov, An efficient pseudocodeword search
 *   algorithm for linear programming decoding of LDPC codes, IEEE
 *   Transactions on Information Theory 54 (4) 1514-1520 (2008).
 *     [arxiv: cs.IT/0601113]
 * "./lp_search 0" outputs a low-weight pseudocodeword;
 * "./lp_search n" with n > 0 outputs n [unsorted] pseudocodeword weights. */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>

#define LDPCC_RANDOM yes
#define LDPCC_LP yes

#include "../ldpcc/3.2.2/ldpcc.c"

int main(int argc, char **argv)
 {
  channel C;
  code H;
  real *xi;
  int i, j, n;

  C.type = isotropic_erasure_channel; C.SNR = 1.e+2;
  C.RNG_grown = 0; grow_RNG(&C);

  H.HiHa_grown = H.ID_grown = H.LP_grown = 0;
  read_H_matrix("matrix_H", &H);
  grow_lp_decoder(&H);
  glp_term_out(GLP_OFF);

  ALLOCATE(xi, H.bits, real, real *)

  n = atoi(argv[1]); if (n < 0) ERROR("lp_search: negative n\n");
  if (n == 0)
   {
    lp_search(&H, &C, xi);
    for (i = 0; i < H.bits; i++) printf("%22.16e\n", xi[i]);
   }
   else
    for (j = 0; j < n; j++)
      { printf("%22.16e\n", lp_search(&H, &C, xi)); fflush(stdout); }

  kill_RNG(&C);
  kill_lp_decoder(&H);
  kill_H_matrix(&H);

  return 0;
 }

