/*******************************************************************************
 *
 *  pseudo-codeword search for linear programming decoding
 *  Copyright (C) 2011 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
 *    http://www.gnu.org/licenses/gpl.txt
 *  for more details.
 *
 ******************************************************************************/

/*******************************************************************************
 *
 *  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]
 *  and improved in
 *    M. Chertkov, M. Stepanov, Polytope of correct (linear programming)
 *    decoding and low-weight pseudo-codewords --- in IEEE 2011 Intl. Symp.
 *    Inf. Theory (St. Petersburg, Russia, July 31--August 5, 2011).
 *      [arxiv: cs.IT/1102.3902]
 *  "./lp_search2 0" outputs a low-weight pseudo-codeword;
 *  "./lp_search2 -1 w" outputs (if found) a pseudo-codeword with weight <= w;
 *  "./lp_search2 n" with n > 0 outputs n [unsorted] pseudo-codeword weights.
 *
 ******************************************************************************/

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

#define LDPCC_RANDOM yes
#define LDPCC_LP yes
#define LDPCC_SMALL_VALUE 1.e-10

#include "../ldpcc/3.2.3/ldpcc.c"

int main(int argc, char **argv)
 {
  channel C;
  code H;
  real *xi, w, wtmp;
  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_cone_cross_section(&H);
  glp_term_out(GLP_OFF);

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

  n = atoi(argv[1]); if (n < -1) ERROR("lp_search2: invalid n\n");
  switch (n)
   {
    case  0: lp_search2(&H, &C, xi);
             for (i = 0; i < H.bits; i++) printf("%22.16e\n", xi[i]);
             break;
    case -1: if ((w = atof(argv[2])) <= 0) ERROR("lp_search2: invalid w\n");
             for (;;)
              {
               wtmp = lp_search2(&H, &C, xi);
               if (wtmp < w + LDPCC_SMALL_VALUE)
                {
                 for (i = 0; i < H.bits; i++) printf("%22.16e\n", xi[i]);
                 printf("\n# %22.16e\n", wtmp); break;
                }
              }
             break;
    default: for (j = 0; j < n; j++)
               { printf("%22.16e\n", lp_search2(&H, &C, xi)); fflush(stdout); }
             break;
   }

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

  return 0;
 }

