/* Copyright (c) 1997-2010
   Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Darmstadt, Germany)
   http://www.polymake.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.
--------------------------------------------------------------------------------
   $Project: polymake $$Id: lrs_lp_client.cc 9991 2010-12-12 23:36:28Z gawrilow $
*/

#include "polymake/client.h"
#include "polymake/Matrix.h"
#include "polymake/Vector.h"
#include "polymake/polytope/lrs_interface.h"

namespace polymake { namespace polytope {

void lrs_solve_lp(perl::Object p, perl::Object lp, bool maximize)
{
   typedef lrs_interface::solver Solver;
   const Matrix<Rational> H=p.give("FACETS | INEQUALITIES"),
      E=p.lookup("AFFINE_HULL | EQUATIONS");
   const Vector<Rational> Obj=lp.give("LINEAR_OBJECTIVE");
   int linearity_dim;

   try {
      Solver solver;
      Solver::lp_solution S=solver.solve_lp(H, E, Obj, maximize, &linearity_dim);
      lp.take(maximize ? "MAXIMAL_VALUE" : "MINIMAL_VALUE") << S.first;
      lp.take(maximize ? "MAXIMAL_VERTEX" : "MINIMAL_VERTEX") << S.second;
      p.take("FEASIBLE") << true;
      p.take("POINTED") << !linearity_dim;
      p.take("LINEALITY_DIM") << linearity_dim;
   }
   catch (unbounded) {
      if (maximize)
         lp.take("MAXIMAL_VALUE") << std::numeric_limits<Rational>::infinity();
      else
         lp.take("MINIMAL_VALUE") << -std::numeric_limits<Rational>::infinity();
      lp.take(maximize ? "MAXIMAL_VERTEX" : "MINIMAL_VERTEX") << perl::undefined();
      p.take("FEASIBLE") << true;
      p.take("POINTED") << !linearity_dim;
      p.take("LINEALITY_DIM") << linearity_dim;
   }
   catch (infeasible) {
      lp.take(maximize ? "MAXIMAL_VALUE" : "MINIMAL_VALUE") << perl::undefined();
      lp.take(maximize ? "MAXIMAL_VERTEX" : "MINIMAL_VERTEX") << perl::undefined();
      p.take("FEASIBLE") << false;
      p.take("POINTED") << perl::undefined();
      p.take("LINEALITY_DIM") << perl::undefined();
   }
}

void lrs_valid_point(perl::Object p)
{
   lrs_interface::solver solver;
   const Matrix<Rational> H=p.give("FACETS | INEQUALITIES"),
      E=p.lookup("LINEAR_SPAN | EQUATIONS");
   Vector<Rational> P;
   if (solver.check_feasibility(H,E,P)) {
      p.take("VALID_RAY", perl::temporary) << P;
   } else {
      p.take("VALID_RAY") << perl::undefined();
   }
}

Function4perl(&lrs_solve_lp, "lrs_solve_lp(Polytope<Rational>, LinearProgram<Rational>, $)");
Function4perl(&lrs_valid_point, "lrs_valid_point(Cone<Rational>)");

} }

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