/* 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: is_ball_or_sphere_h.cc,v 1.4 2004/12/08 09:34:56 gawrilow Exp $"

#include <Poly.h>
#include <is_sphere_h.h>
#include <argv_options.h>
#include <ctime>
#include <sstream>

namespace polymake {
namespace topaz {

namespace {

enum { in_seed, in_strategy, stable_rounds, quiet };

}

void is_ball_or_sphere_h(std::ostream& os, Poly& p, const argv_option *options)
{
   const HasseDiagram<> HD = p.give("HASSE_DIAGRAM");
   const bool is_pmf = p.give("PSEUDO_MANIFOLD");
   if (!is_pmf) {
      if (!options[quiet])
	 os << "\nis_ball_or_sphere_h: Complex is NOT a ball and NOT a sphere.\n\n";
      p.take("BALL_OR_SPHERE") << false;
      return;  
   }

   // get the time as initial seed
   time_t t;
   time(&t);
   const int seed = options[in_seed] ? atoi(options[in_seed]) : t;

   int strategy= options[in_strategy] ? atoi(options[in_strategy]) : 0;
   const int n_stable_rounds= options[stable_rounds] ? atoi(options[stable_rounds]) : (HD.dim()-1) * 1000;
   
   bool is_ball_or_sphere= is_ball_or_sphere_h(HD,seed,strategy,n_stable_rounds);
   ++strategy;
   while (!is_ball_or_sphere && strategy <= 1) {
      if (!options[quiet])
	 os << "\nis_ball_or_sphere_h: after " << n_stable_rounds
	    << " iterations without improvement:\nUnable to determine, whether the complex is a ball or a sphere.\n"
	    << "Trying strategy " << strategy << ".\n";
      is_ball_or_sphere = is_ball_or_sphere_h(HD,seed,strategy,n_stable_rounds);
      ++strategy;
   }

   if (!is_ball_or_sphere) {
      std::ostringstream e;
      if (!options[quiet])
	 e << "\nis_ball_or_sphere_h: after " << n_stable_rounds
	   << " iterations without improvement:\nUnable to determine, whether the complex is a ball or a sphere.\n"
	   << "Exiting without result.\n";
      throw std::runtime_error(e.str());
   }

   if (!options[quiet])
      os << "\nis_ball_or_sphere_h: Complex is BALL_OR_SPHERE.\n\n";
   p.take("BALL_OR_SPHERE") << true;
}

} }

using namespace polymake;

/** @file is_ball_or_sphere_h
 *
 *  Heuristic to determin if a complex is a ball or a ball or a sphere.
 *
 *  @synopsis is_ball_or_sphere_h <file> [ -seed <s> -strategy {0|1} -stable_rounds <r> -quiet ]
 *
 *  @reading  HASSE_DIAGRAM, PSEUDO_MANIFOLD
 *  @writing  may write BALL_OR_SPHERE
 */

int main(int argc, const char* argv[])
{
   argv_option options[]={ {"-seed", true}, {"-strategy", true},
			   {"-stable_rounds", true}, {"-quiet", false} };
   if (!extract_options(argc,argv,1,options) || argc != 2) {
      cerr << "usage: " << argv[0] << " <file> [ -seed <s> -strategy {0|1} -stable_rounds <r> -quiet ]" << endl;
      return 1;
   }
   try {
      Poly p(argv[1], ios::in | ios::out);
      topaz::is_ball_or_sphere_h(std::cout, p, options);
   } 
   catch (const std::exception& e) {
     cerr << e.what() << endl;
     return 1;
   }
   return 0;
}


// Local Variables:
// mode:C++
// c-basic-offset:3
// End:
