debug.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 LICENSE, 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. for (const auto &rowPair : rows)
  53. {
  54. dump(rowPair.first, out);
  55. out << " | ";
  56. dump(*rowPair.second, out);
  57. }
  58. }
  59. static void dump(const std::vector<Symbol> &symbols, std::ostream &out)
  60. {
  61. for (const auto &symbol : symbols)
  62. {
  63. dump(symbol, out);
  64. out << std::endl;
  65. }
  66. }
  67. static void dump(const SolverImpl::VarMap &vars, std::ostream &out)
  68. {
  69. for (const auto &varPair : vars)
  70. {
  71. out << varPair.first.name() << " = ";
  72. dump(varPair.second, out);
  73. out << std::endl;
  74. }
  75. }
  76. static void dump(const SolverImpl::CnMap &cns, std::ostream &out)
  77. {
  78. for (const auto &cnPair : cns)
  79. dump(cnPair.first, out);
  80. }
  81. static void dump(const SolverImpl::EditMap &edits, std::ostream &out)
  82. {
  83. for (const auto &editPair : edits)
  84. out << editPair.first.name() << std::endl;
  85. }
  86. static void dump(const Row &row, std::ostream &out)
  87. {
  88. for (const auto &rowPair : row.cells())
  89. {
  90. out << " + " << rowPair.second << " * ";
  91. dump(rowPair.first, out);
  92. }
  93. out << std::endl;
  94. }
  95. static void dump(const Symbol &symbol, std::ostream &out)
  96. {
  97. switch (symbol.type())
  98. {
  99. case Symbol::Invalid:
  100. out << "i";
  101. break;
  102. case Symbol::External:
  103. out << "v";
  104. break;
  105. case Symbol::Slack:
  106. out << "s";
  107. break;
  108. case Symbol::Error:
  109. out << "e";
  110. break;
  111. case Symbol::Dummy:
  112. out << "d";
  113. break;
  114. default:
  115. break;
  116. }
  117. out << symbol.id();
  118. }
  119. static void dump(const Constraint &cn, std::ostream &out)
  120. {
  121. for (const auto &term : cn.expression().terms())
  122. {
  123. out << term.coefficient() << " * ";
  124. out << term.variable().name() << " + ";
  125. }
  126. out << cn.expression().constant();
  127. switch (cn.op())
  128. {
  129. case OP_LE:
  130. out << " <= 0 ";
  131. break;
  132. case OP_GE:
  133. out << " >= 0 ";
  134. break;
  135. case OP_EQ:
  136. out << " == 0 ";
  137. break;
  138. default:
  139. break;
  140. }
  141. out << " | strength = " << cn.strength() << std::endl;
  142. }
  143. };
  144. } // namespace impl
  145. namespace debug
  146. {
  147. template <typename T>
  148. void dump(const T &value)
  149. {
  150. impl::DebugHelper::dump(value, std::cout);
  151. }
  152. template <typename T>
  153. void dump(const T &value, std::ostream &out)
  154. {
  155. impl::DebugHelper::dump(value, out);
  156. }
  157. template <typename T>
  158. std::string dumps(const T &value)
  159. {
  160. std::stringstream stream;
  161. impl::DebugHelper::dump(value, stream);
  162. return stream.str();
  163. }
  164. } // namespace debug
  165. } // namespace kiwi