gist.hpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 namespace Gecode { namespace Gist {
00039
00040 template<class S>
00041 VarComparator<S>::VarComparator(std::string name)
00042 : TextOutput(name) {}
00043
00044 template<class S>
00045 void
00046 VarComparator<S>::compare(const Space& s0, const Space& s1) {
00047 std::ostringstream result;
00048 dynamic_cast<const S&>(s0).compare(s1,result);
00049 if (result.str() != "") {
00050 init();
00051 addHtml("<pre>\n");
00052 getStream() << result.str() << std::endl;
00053 addHtml("</pre><hr />");
00054 }
00055 }
00056
00057 template<class S>
00058 std::string
00059 VarComparator<S>::name(void) {
00060 return TextOutput::name();
00061 }
00062
00063 template<class S>
00064 void
00065 VarComparator<S>::finalize(void) {
00066 TextOutput::finalize();
00067 }
00068
00069 inline std::string
00070 Comparator::compare(std::string x_n, IntVar x, IntVar y) {
00071 IntVarRanges xr(x), yr(y);
00072 if (!Iter::Ranges::equal(xr,yr)) {
00073 std::ostringstream ret;
00074 ret << x_n << "=" << x << " -> " << y;
00075 return ret.str();
00076 }
00077 return "";
00078 }
00079 inline std::string
00080 Comparator::compare(std::string x_n, BoolVar x, BoolVar y) {
00081 if (! (x.min() == y.min() && x.max() == y.max()) ) {
00082 std::ostringstream ret;
00083 ret << x_n << "=" << x << " -> " << y;
00084 return ret.str();
00085 }
00086 return "";
00087 }
00088 #ifdef GECODE_HAS_SET_VARS
00089 inline std::string
00090 Comparator::compare(std::string x_n, SetVar x, SetVar y) {
00091 SetVarGlbRanges xglbr(x), yglbr(y);
00092 SetVarLubRanges xlubr(x), ylubr(y);
00093 if (! (Iter::Ranges::equal(xglbr,yglbr) &&
00094 Iter::Ranges::equal(xlubr,ylubr) &&
00095 x.cardMin() == y.cardMin() &&
00096 y.cardMax() == y.cardMax()) ) {
00097 std::ostringstream ret;
00098 ret << x_n << "=" << x << " -> " << y;
00099 return ret.str();
00100 }
00101 return "";
00102 }
00103 #endif
00104 template<class Var>
00105 std::string
00106 Comparator::compare(std::string x_n, const VarArgArray<Var>& x,
00107 const VarArgArray<Var>& y) {
00108 if (x.size() != y.size())
00109 return "Error: array size mismatch";
00110 std::ostringstream ret;
00111 bool first = true;
00112 for (int i=0; i<x.size(); i++) {
00113 std::ostringstream xni;
00114 xni << x_n << "[" << i << "]";
00115 std::string cmp = compare(xni.str(),x[i],y[i]);
00116 if (cmp != "") {
00117 if (!first) {
00118 ret << ", ";
00119 } else {
00120 first = false;
00121 }
00122 ret << cmp;
00123 }
00124 }
00125 return ret.str();
00126 }
00127
00128 template<class S>
00129 Print<S>::Print(const std::string& name)
00130 : TextOutput(name) {}
00131
00132 template<class S>
00133 void
00134 Print<S>::inspect(const Space& node) {
00135 init();
00136 addHtml("<pre>\n");
00137 dynamic_cast<const S&>(node).print(getStream());
00138 addHtml("</pre><hr />");
00139 }
00140
00141 template<class S>
00142 std::string
00143 Print<S>::name(void) {
00144 return TextOutput::name();
00145 }
00146
00147 template<class S>
00148 void
00149 Print<S>::finalize(void) {
00150 TextOutput::finalize();
00151 }
00152
00153 forceinline
00154 Options::Options(void) {}
00155
00156 forceinline
00157 Options::_I::_I(void) : _click(heap,1), n_click(0),
00158 _solution(heap,1), n_solution(0),
00159 _move(heap,1), n_move(0), _compare(heap,1), n_compare(0) {}
00160
00161 forceinline void
00162 Options::_I::click(Inspector* i) {
00163 _click[n_click++] = i;
00164 }
00165 forceinline void
00166 Options::_I::solution(Inspector* i) {
00167 _solution[n_solution++] = i;
00168 }
00169 forceinline void
00170 Options::_I::move(Inspector* i) {
00171 _move[n_move++] = i;
00172 }
00173 forceinline void
00174 Options::_I::compare(Comparator* c) {
00175 _compare[n_compare++] = c;
00176 }
00177 forceinline Inspector*
00178 Options::_I::click(unsigned int i) const {
00179 return (i < n_click) ? _click[i] : NULL;
00180 }
00181 forceinline Inspector*
00182 Options::_I::solution(unsigned int i) const {
00183 return (i < n_solution) ? _solution[i] : NULL;
00184 }
00185 forceinline Inspector*
00186 Options::_I::move(unsigned int i) const {
00187 return (i < n_move) ? _move[i] : NULL;
00188 }
00189 forceinline Comparator*
00190 Options::_I::compare(unsigned int i) const {
00191 return (i < n_compare) ? _compare[i] : NULL;
00192 }
00193
00194 inline int
00195 dfs(Space* root, const Gist::Options& opt) {
00196 return explore(root, false, opt);
00197 }
00198
00199 inline int
00200 bab(Space* root, const Gist::Options& opt) {
00201 return Gist::explore(root, true, opt);
00202 }
00203
00204 }}
00205
00206