debug.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*-----------------------------------------------------------------------------
  2. | Copyright (c) 2013-2017, Nucleic Development Team.
  3. |
  4. | Distributed under the terms of the Modified BSD License.
  5. |
  6. | The full license is in the file COPYING.txt, distributed with this software.
  7. |----------------------------------------------------------------------------*/
  8. #pragma once
  9. #include <iostream>
  10. #include <sstream>
  11. #include <vector>
  12. #include "constraint.h"
  13. #include "solverimpl.h"
  14. #include "term.h"
  15. namespace kiwi
  16. {
  17. namespace impl
  18. {
  19. class DebugHelper
  20. {
  21. public:
  22. static void dump( const SolverImpl& solver, std::ostream& out )
  23. {
  24. out << "Objective" << std::endl;
  25. out << "---------" << std::endl;
  26. dump( *solver.m_objective, out );
  27. out << std::endl;
  28. out << "Tableau" << std::endl;
  29. out << "-------" << std::endl;
  30. dump( solver.m_rows, out );
  31. out << std::endl;
  32. out << "Infeasible" << std::endl;
  33. out << "----------" << std::endl;
  34. dump( solver.m_infeasible_rows, out );
  35. out << std::endl;
  36. out << "Variables" << std::endl;
  37. out << "---------" << std::endl;
  38. dump( solver.m_vars, out );
  39. out << std::endl;
  40. out << "Edit Variables" << std::endl;
  41. out << "--------------" << std::endl;
  42. dump( solver.m_edits, out );
  43. out << std::endl;
  44. out << "Constraints" << std::endl;
  45. out << "-----------" << std::endl;
  46. dump( solver.m_cns, out );
  47. out << std::endl;
  48. out << std::endl;
  49. }
  50. static void dump( const SolverImpl::RowMap& rows, std::ostream& out )
  51. {
  52. typedef SolverImpl::RowMap::const_iterator iter_t;
  53. iter_t end = rows.end();
  54. for( iter_t it = rows.begin(); it != end; ++it )
  55. {
  56. dump( it->first, out );
  57. out << " | ";
  58. dump( *it->second, out );
  59. }
  60. }
  61. static void dump( const std::vector<Symbol>& symbols, std::ostream& out )
  62. {
  63. typedef std::vector<Symbol>::const_iterator iter_t;
  64. iter_t end = symbols.end();
  65. for( iter_t it = symbols.begin(); it != end; ++it )
  66. {
  67. dump( *it, out );
  68. out << std::endl;
  69. }
  70. }
  71. static void dump( const SolverImpl::VarMap& vars, std::ostream& out )
  72. {
  73. typedef SolverImpl::VarMap::const_iterator iter_t;
  74. iter_t end = vars.end();
  75. for( iter_t it = vars.begin(); it != end; ++it )
  76. {
  77. out << it->first.name() << " = ";
  78. dump( it->second, out );
  79. out << std::endl;
  80. }
  81. }
  82. static void dump( const SolverImpl::CnMap& cns, std::ostream& out )
  83. {
  84. typedef SolverImpl::CnMap::const_iterator iter_t;
  85. iter_t end = cns.end();
  86. for( iter_t it = cns.begin(); it != end; ++it )
  87. dump( it->first, out );
  88. }
  89. static void dump( const SolverImpl::EditMap& edits, std::ostream& out )
  90. {
  91. typedef SolverImpl::EditMap::const_iterator iter_t;
  92. iter_t end = edits.end();
  93. for( iter_t it = edits.begin(); it != end; ++it )
  94. out << it->first.name() << std::endl;
  95. }
  96. static void dump( const Row& row, std::ostream& out )
  97. {
  98. typedef Row::CellMap::const_iterator iter_t;
  99. out << row.constant();
  100. iter_t end = row.cells().end();
  101. for( iter_t it = row.cells().begin(); it != end; ++it )
  102. {
  103. out << " + " << it->second << " * ";
  104. dump( it->first, out );
  105. }
  106. out << std::endl;
  107. }
  108. static void dump( const Symbol& symbol, std::ostream& out )
  109. {
  110. switch( symbol.type() )
  111. {
  112. case Symbol::Invalid:
  113. out << "i";
  114. break;
  115. case Symbol::External:
  116. out << "v";
  117. break;
  118. case Symbol::Slack:
  119. out << "s";
  120. break;
  121. case Symbol::Error:
  122. out << "e";
  123. break;
  124. case Symbol::Dummy:
  125. out << "d";
  126. break;
  127. default:
  128. break;
  129. }
  130. out << symbol.id();
  131. }
  132. static void dump( const Constraint& cn, std::ostream& out )
  133. {
  134. typedef std::vector<Term>::const_iterator iter_t;
  135. iter_t begin = cn.expression().terms().begin();
  136. iter_t end = cn.expression().terms().end();
  137. for( iter_t it = begin; it != end; ++it )
  138. {
  139. out << it->coefficient() << " * ";
  140. out << it->variable().name() << " + ";
  141. }
  142. out << cn.expression().constant();
  143. switch( cn.op() )
  144. {
  145. case OP_LE:
  146. out << " <= 0 ";
  147. break;
  148. case OP_GE:
  149. out << " >= 0 ";
  150. break;
  151. case OP_EQ:
  152. out << " == 0 ";
  153. break;
  154. default:
  155. break;
  156. }
  157. out << " | strength = " << cn.strength() << std::endl;
  158. }
  159. };
  160. } // namespace impl
  161. namespace debug
  162. {
  163. template<typename T>
  164. void dump( const T& value )
  165. {
  166. impl::DebugHelper::dump( value, std::cout );
  167. }
  168. template<typename T>
  169. void dump( const T& value, std::ostream& out )
  170. {
  171. impl::DebugHelper::dump( value, out );
  172. }
  173. template<typename T>
  174. std::string dumps( const T& value )
  175. {
  176. std::stringstream stream;
  177. impl::DebugHelper::dump( value, stream );
  178. return stream.str();
  179. }
  180. } // namespace debug
  181. } // namespace kiwi