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

#include <cstdlib>
#include <ctime>
#include <argv_options.h>
#include <random_generators.h>
#include <Poly.h>
#include <Matrix.h>
#include <Vector.h>

namespace {
   enum { seed };
}

namespace polymake { namespace polytope {

void rand(Poly& p_out, Poly& p_in, int n, const std::string& comment, const argv_option *options)
{
   time_t t;
   // initialize random rumber generator
   const long s=options[seed] ? atol(options[seed]) : (time(&t), t);
   UniformRNG<Rational> rg(s);

   const bool bounded=p_in.give("BOUNDED");
   if (!bounded)
      throw std::runtime_error("unbounded input polyhedron");

   const Matrix<Rational> V=p_in.give("VERTICES");
   const int
	 d=V.cols(),
	 n_vertices=V.rows();

   Vector<Rational> lambda(n_vertices);
   Matrix<Rational> Points_out(n,d);
   for (Entire< Rows< Matrix<Rational> > >::iterator p_i=entire(rows(Points_out)); !p_i.at_end(); ++p_i) {
      // get random partition of 1
      std::copy(rg.begin(), entire(lambda));
      lambda /= accumulate(lambda, operations::add());
      *p_i = lambda*V;
   }

   p_out.take("POINTS") << comment << "# seed=" << s << '\n'
			<< Points_out;
}

} }

using namespace polymake;

/** @file rand
 *
 *  Produce a polytope with $n$ random points from the input polytope.
 *
 *  Each generated point is a convex linear combination of the input vertices
 *  with uniformly distributed random coefficients. Thus, the output points can't in general
 *  be expected to be distributed uniformly within the input polytope; cf. @see unirand
 *  for this.
 *
 *  A seed value for the random number generator can be specified.
 *
 *  @synopsis rand <outfile> <infile> <n> [ -seed <s> ]
 *
 *  @writing  POINTS
 *
 *  @index clients Producing a new polyhedron from others
 *
 *  @author Carsten Jackisch
 */

int main(int argc, const char *argv[])
{
   argv_option options[]={ { "-seed", true } };
   if (!extract_options(argc,argv,2,options) || argc!=4) {
      cerr << "usage: " << argv[0] << " <outfile> <infile> <#points> [ -seed <s> ]" << endl;
      return 1;
   }
   try {
      Poly p_out(argv[1], ios::out | ios::trunc);
      Poly p_in(argv[2], ios::in);
      const int n = atoi(argv[3]);
      polytope::rand(p_out, p_in, n, std::string("# rand from ")+argv[2]+'\n', options);
   }
   catch (const std::exception& e) {
      cerr << e.what() << endl;
      return 1;
   }
   return 0;
}
