1 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ 2 /* 3 * Main authors: 4 * Guido Tack <tack@gecode.org> 5 * 6 * Copyright: 7 * Guido Tack, 2006 8 * 9 * This file is part of Gecode, the generic constraint 10 * development environment: 11 * http://www.gecode.org 12 * 13 * Permission is hereby granted, free of charge, to any person obtaining 14 * a copy of this software and associated documentation files (the 15 * "Software"), to deal in the Software without restriction, including 16 * without limitation the rights to use, copy, modify, merge, publish, 17 * distribute, sublicense, and/or sell copies of the Software, and to 18 * permit persons to whom the Software is furnished to do so, subject to 19 * the following conditions: 20 * 21 * The above copyright notice and this permission notice shall be 22 * included in all copies or substantial portions of the Software. 23 * 24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 28 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 29 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 30 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 31 * 32 */ 33 34 namespace Gecode { namespace Gist { 35 36 template<class S> VarComparator(std::string name)37 VarComparator<S>::VarComparator(std::string name) 38 : TextOutput(name) {} 39 40 template<class S> 41 void compare(const Space & s0,const Space & s1)42 VarComparator<S>::compare(const Space& s0, const Space& s1) { 43 std::ostringstream result; 44 dynamic_cast<const S&>(s0).compare(s1,result); 45 if (result.str() != "") { 46 init(); 47 addHtml("<pre>\n"); 48 getStream() << result.str() << std::endl; 49 addHtml("</pre><hr />"); 50 } 51 } 52 53 template<class S> 54 std::string name(void)55 VarComparator<S>::name(void) { 56 return TextOutput::name(); 57 } 58 59 template<class S> 60 void finalize(void)61 VarComparator<S>::finalize(void) { 62 TextOutput::finalize(); 63 } 64 65 inline std::string compare(std::string x_n,IntVar x,IntVar y)66 Comparator::compare(std::string x_n, IntVar x, IntVar y) { 67 IntVarRanges xr(x), yr(y); 68 if (!Iter::Ranges::equal(xr,yr)) { 69 std::ostringstream ret; 70 ret << x_n << "=" << x << " -> " << y; 71 return ret.str(); 72 } 73 return ""; 74 } 75 inline std::string compare(std::string x_n,BoolVar x,BoolVar y)76 Comparator::compare(std::string x_n, BoolVar x, BoolVar y) { 77 if (! (x.min() == y.min() && x.max() == y.max()) ) { 78 std::ostringstream ret; 79 ret << x_n << "=" << x << " -> " << y; 80 return ret.str(); 81 } 82 return ""; 83 } 84 #ifdef GECODE_HAS_SET_VARS 85 inline std::string compare(std::string x_n,SetVar x,SetVar y)86 Comparator::compare(std::string x_n, SetVar x, SetVar y) { 87 SetVarGlbRanges xglbr(x), yglbr(y); 88 SetVarLubRanges xlubr(x), ylubr(y); 89 if (! (Iter::Ranges::equal(xglbr,yglbr) && 90 Iter::Ranges::equal(xlubr,ylubr) && 91 x.cardMin() == y.cardMin() && 92 y.cardMax() == y.cardMax()) ) { 93 std::ostringstream ret; 94 ret << x_n << "=" << x << " -> " << y; 95 return ret.str(); 96 } 97 return ""; 98 } 99 #endif 100 #ifdef GECODE_HAS_FLOAT_VARS 101 inline std::string compare(std::string x_n,FloatVar x,FloatVar y)102 Comparator::compare(std::string x_n, FloatVar x, FloatVar y) { 103 if (! (x.min() == y.min() && x.max() == y.max()) ) { 104 std::ostringstream ret; 105 ret << x_n << "=" << x << " -> " << y; 106 return ret.str(); 107 } 108 return ""; 109 } 110 #endif 111 template<class Var> 112 std::string compare(std::string x_n,const VarArgArray<Var> & x,const VarArgArray<Var> & y)113 Comparator::compare(std::string x_n, const VarArgArray<Var>& x, 114 const VarArgArray<Var>& y) { 115 if (x.size() != y.size()) 116 return "Error: array size mismatch"; 117 std::ostringstream ret; 118 bool first = true; 119 for (int i=0; i<x.size(); i++) { 120 std::ostringstream xni; 121 xni << x_n << "[" << i << "]"; 122 std::string cmp = compare(xni.str(),x[i],y[i]); 123 if (cmp != "") { 124 if (!first) { 125 ret << ", "; 126 } else { 127 first = false; 128 } 129 ret << cmp; 130 } 131 } 132 return ret.str(); 133 } 134 135 template<class S> Print(const std::string & name)136 Print<S>::Print(const std::string& name) 137 : TextOutput(name) {} 138 139 template<class S> 140 void inspect(const Space & node)141 Print<S>::inspect(const Space& node) { 142 init(); 143 addHtml("<pre>\n"); 144 dynamic_cast<const S&>(node).print(getStream()); 145 flush(); 146 addHtml("</pre><hr />"); 147 } 148 149 template<class S> 150 std::string name(void)151 Print<S>::name(void) { 152 return TextOutput::name(); 153 } 154 155 template<class S> 156 void finalize(void)157 Print<S>::finalize(void) { 158 TextOutput::finalize(); 159 } 160 161 forceinline Options(void)162 Options::Options(void) {} 163 164 forceinline I_(void)165 Options::I_::I_(void) : _click(heap,1), n_click(0), 166 _solution(heap,1), n_solution(0), 167 _move(heap,1), n_move(0), _compare(heap,1), n_compare(0) {} 168 169 forceinline void click(Inspector * i)170 Options::I_::click(Inspector* i) { 171 _click[static_cast<int>(n_click++)] = i; 172 } 173 forceinline void solution(Inspector * i)174 Options::I_::solution(Inspector* i) { 175 _solution[static_cast<int>(n_solution++)] = i; 176 } 177 forceinline void move(Inspector * i)178 Options::I_::move(Inspector* i) { 179 _move[static_cast<int>(n_move++)] = i; 180 } 181 forceinline void compare(Comparator * c)182 Options::I_::compare(Comparator* c) { 183 _compare[static_cast<int>(n_compare++)] = c; 184 } 185 forceinline Inspector* click(unsigned int i) const186 Options::I_::click(unsigned int i) const { 187 return (i < n_click) ? _click[i] : nullptr; 188 } 189 forceinline Inspector* solution(unsigned int i) const190 Options::I_::solution(unsigned int i) const { 191 return (i < n_solution) ? _solution[i] : nullptr; 192 } 193 forceinline Inspector* move(unsigned int i) const194 Options::I_::move(unsigned int i) const { 195 return (i < n_move) ? _move[i] : nullptr; 196 } 197 forceinline Comparator* compare(unsigned int i) const198 Options::I_::compare(unsigned int i) const { 199 return (i < n_compare) ? _compare[i] : nullptr; 200 } 201 202 inline int dfs(Space * root,const Gist::Options & opt)203 dfs(Space* root, const Gist::Options& opt) { 204 return explore(root, false, opt); 205 } 206 207 inline int bab(Space * root,const Gist::Options & opt)208 bab(Space* root, const Gist::Options& opt) { 209 return Gist::explore(root, true, opt); 210 } 211 212 }} 213 214 // STATISTICS: gist-any 215