[PPL-devel] [GIT] ppl/ppl(pip): Implemented display of solution trees.

François Galea francois.galea at uvsq.fr
Wed Sep 23 11:08:44 CEST 2009


Module: ppl/ppl
Branch: pip
Commit: 4eb68b6e2a67c8e0ae368cfa66b3cb8c567b4a94
URL:    http://www.cs.unipr.it/git/gitweb.cgi?p=ppl/ppl.git;a=commit;h=4eb68b6e2a67c8e0ae368cfa66b3cb8c567b4a94

Author: François Galea <francois.galea at uvsq.fr>
Date:   Wed Sep 23 10:53:15 2009 +0200

Implemented display of solution trees.

---

 src/PIP_Problem.cc              |    9 ++++++++
 src/PIP_Tree.cc                 |    4 +-
 src/PIP_Tree.defs.hh            |    2 +-
 tests/PIP_Problem/pipproblem.cc |   41 +++++++++++++++++++++++++++++++++++++++
 4 files changed, 53 insertions(+), 3 deletions(-)

diff --git a/src/PIP_Problem.cc b/src/PIP_Problem.cc
index 6d651fa..a8925b5 100644
--- a/src/PIP_Problem.cc
+++ b/src/PIP_Problem.cc
@@ -105,6 +105,15 @@ PPL::PIP_Problem::solve() const {
   throw std::runtime_error("PPL internal error");
 }
 
+PPL::PIP_Tree
+PPL::PIP_Problem::solution() const {
+  return current_solution;
+}
+
+PPL::PIP_Tree
+PPL::PIP_Problem::optimizing_solution() const {
+  return current_solution;
+}
 
 bool
 PPL::PIP_Problem::OK() const {
diff --git a/src/PIP_Tree.cc b/src/PIP_Tree.cc
index b8169b0..89f40f1 100644
--- a/src/PIP_Tree.cc
+++ b/src/PIP_Tree.cc
@@ -433,8 +433,8 @@ PIP_Solution_Node::ascii_load(std::istream& s) {
 }
 
 const Linear_Expression&
-PIP_Solution_Node::parametric_values(Variable v) {
-  update_solution();
+PIP_Solution_Node::parametric_values(const Variable &v) const {
+  const_cast<PIP_Solution_Node&>(*this).update_solution();
   Variables_Set& parameters = problem->parameters;
   dimension_type id = v.id();
   dimension_type j;
diff --git a/src/PIP_Tree.defs.hh b/src/PIP_Tree.defs.hh
index 6e9a954..1893dd6 100644
--- a/src/PIP_Tree.defs.hh
+++ b/src/PIP_Tree.defs.hh
@@ -163,7 +163,7 @@ public:
     Thrown if \p v is dimension-incompatible with \p *this
     or if \p v is a parameter.
   */
-  const Linear_Expression& parametric_values(Variable v);
+  const Linear_Expression& parametric_values(const Variable &v) const;
 
   void ascii_dump(std::ostream& s) const;
   bool ascii_load(std::istream& s);
diff --git a/tests/PIP_Problem/pipproblem.cc b/tests/PIP_Problem/pipproblem.cc
index df9f96f..8d84a95 100644
--- a/tests/PIP_Problem/pipproblem.cc
+++ b/tests/PIP_Problem/pipproblem.cc
@@ -24,6 +24,43 @@ site: http://www.cs.unipr.it/ppl/ . */
 
 namespace {
 
+void
+display_solution(const PIP_Tree pip, const Variables_Set &vars, int indent=0) {
+  using namespace std;
+  using namespace Parma_Polyhedra_Library::IO_Operators;
+  if (!pip) {
+    nout << setw(indent*2) << "" << "_|_" << endl;
+  } else {
+    const Constraint_System &constraints = pip->constraints();
+    if (!constraints.empty()) {
+      nout << setw(indent*2) << "" << "if ";
+      Constraint_System::const_iterator begin = constraints.begin();
+      Constraint_System::const_iterator end = constraints.end();
+      Constraint_System::const_iterator i;
+      for (i = begin; i != end; ++i)
+        nout << ((i==begin)?"":" and ") << *i;
+      nout << " then" << endl;
+    }
+    const PIP_Decision_Node* dn = pip->as_decision();
+    if (dn) {
+      display_solution(dn->child_node(true), vars, indent+1);
+      nout << setw(indent*2) << "" << "else" << endl;
+      display_solution(dn->child_node(false), vars, indent+1);
+    } else {
+      const PIP_Solution_Node* sn = pip->as_solution();
+      Variables_Set::const_iterator begin = vars.begin();
+      Variables_Set::const_iterator end = vars.end();
+      Variables_Set::const_iterator i;
+      nout << setw(indent*2+2) << "" << "{";
+      for (i=begin; i!=end; ++i)
+        nout << ((i==begin)?"":" ; ") << sn->parametric_values(Variable(*i));
+      nout << "}" << endl;
+      nout << setw(indent*2) << "" << "else" << endl;
+      nout << setw(indent*2+2) << "" << "_|_" << endl;
+    }
+  }
+}
+
 bool
 test01() {
   Variable X1(0);
@@ -43,6 +80,10 @@ test01() {
   PIP_Problem pip(cs.space_dimension(), cs.begin(), cs.end(), params);
 
   bool ok = (pip.solve() == OPTIMIZED_PIP_PROBLEM);
+  if (ok) {
+    const PIP_Tree solution = pip.solution();
+    display_solution(solution, Variables_Set(X1, X2));
+  }
 
   return ok;
 }




More information about the PPL-devel mailing list