/* Copyright (c) 1997-2004
   Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Berlin, Germany)
   http://www.math.tu-berlin.de/polymake,  mailto:polymake@math.tu-berlin.de

   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 2, or (at your option) any
   later version: http://www.gnu.org/licenses/gpl.txt.

   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.
*/

#ident "$Project: polymake $$Id: binary-markov-graph.cc,v 1.3 2006/02/03 16:46:26 joswig Exp $"

#include <cstdlib>
#include <Poly.h>
#include <Rational.h>
#include <Graph.h>
#include <IncidenceMatrix.h>
#include <Array.h>
#include <Vector.h>
#include <Matrix.h>
#include <linalg.h>

namespace polymake { namespace polytope {

   void binary_markov_graph(Poly& p, const std::string& observation) {
      const Vector<Rational> zero=zero_vector<Rational>(2);
      const Vector<Rational> x1=unit_vector<Rational>(2,0);
      const Vector<Rational> y1=unit_vector<Rational>(2,1);
      Vector<Rational> x2(2),y2(2); x2[0]=y2[1]=Rational(2);

      const int n=observation.size();
      typedef Graph< nothing, Vector<Rational>, directed > graph;
      graph G(2*n+2); // unique source=0, sink=2*n+1

      if (observation[0]=='0') {
	 G.edge(0,1)=x1;
	 G.edge(0,2)=zero;
      } else {
	 G.edge(0,1)=zero;
	 G.edge(0,2)=y1;
      }
      
      for (int i=1; i<n; ++i)
	 if (observation[i]=='0') {
	    G.edge(2*i-1,2*i+1)=x2;
	    G.edge(2*i-1,2*i+2)=zero;
	    G.edge(2*i,2*i+1)=x1;
	    G.edge(2*i,2*i+2)=y1;
	 } else {
	    G.edge(2*i-1,2*i+1)=x1;
	    G.edge(2*i-1,2*i+2)=y1;
	    G.edge(2*i,2*i+1)=zero;
	    G.edge(2*i,2*i+2)=y2;
	 }

      G.edge(2*n-1,2*n+1)=G.edge(2*n,2*n+1)=zero_vector<Rational>(2);

      p.take("SUM_PRODUCT_GRAPH") << G;
      p.take("AMBIENT_DIM") << 2;
   }

} }

using namespace polymake;

/** @file binary-markov-graph
 *
 *  Defines a very simple graph for a polytope propagation related to a Hidden Markov Model.
 *  The propagated polytope is always a polygon.
 *  For a detailed description see
 *  M. Joswig: Polytope propagation, in: Algebraic statistics and computational biology
 *  by L. Pachter and B. Sturmfels (eds.), Cambridge, 2005.
 *
 *  @synopsis binary-markov-graph <file> <n>
 *
 *  @reading  n/a
 *  @writing  SUM_PRODUCT_GRAPH, AMBIENT_DIM
 *
 *  @index clients Polytope Propagation
 */

int main(int argc, char *argv[]) {
   if (argc != 3) {
      cerr << "usage: " << argv[0] << " <file> <bitstring>" << endl;
      return 1;
   }
   try {
      Poly p(argv[1], ios::out | ios::trunc, "PropagatedPolytope");
      const std::string observation(argv[2]);
      polytope::binary_markov_graph(p,observation);
   }
   catch (const std::exception& e) {
      cerr << e.what() << endl;
      return 1;
   }
   return 0;
}
