Generated on Thu Mar 22 10:39:32 2012 for Gecode by doxygen 1.6.3

gist.hpp

Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
00002 /*
00003  *  Main authors:
00004  *     Guido Tack <tack@gecode.org>
00005  *
00006  *  Copyright:
00007  *     Guido Tack, 2006
00008  *
00009  *  Last modified:
00010  *     $Date: 2011-08-25 10:43:31 +0200 (Thu, 25 Aug 2011) $ by $Author: tack $
00011  *     $Revision: 12352 $
00012  *
00013  *  This file is part of Gecode, the generic constraint
00014  *  development environment:
00015  *     http://www.gecode.org
00016  *
00017  * Permission is hereby granted, free of charge, to any person obtaining
00018  * a copy of this software and associated documentation files (the
00019  * "Software"), to deal in the Software without restriction, including
00020  * without limitation the rights to use, copy, modify, merge, publish,
00021  * distribute, sublicense, and/or sell copies of the Software, and to
00022  * permit persons to whom the Software is furnished to do so, subject to
00023  * the following conditions:
00024  *
00025  * The above copyright notice and this permission notice shall be
00026  * included in all copies or substantial portions of the Software.
00027  *
00028  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00029  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00030  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00031  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00032  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00033  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00034  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
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     flush();
00139     addHtml("</pre><hr />");
00140   }
00141 
00142   template<class S>
00143   std::string
00144   Print<S>::name(void) {
00145     return TextOutput::name();
00146   }
00147 
00148   template<class S>
00149   void
00150   Print<S>::finalize(void) {
00151     TextOutput::finalize();
00152   }
00153 
00154   forceinline
00155   Options::Options(void) {} 
00156 
00157   forceinline
00158   Options::_I::_I(void) : _click(heap,1), n_click(0),
00159     _solution(heap,1), n_solution(0),
00160     _move(heap,1), n_move(0), _compare(heap,1), n_compare(0) {}
00161 
00162   forceinline void
00163   Options::_I::click(Inspector* i) {
00164     _click[static_cast<int>(n_click++)] = i;
00165   }
00166   forceinline void
00167   Options::_I::solution(Inspector* i) {
00168     _solution[static_cast<int>(n_solution++)] = i;
00169   }
00170   forceinline void
00171   Options::_I::move(Inspector* i) {
00172     _move[static_cast<int>(n_move++)] = i;
00173   }
00174   forceinline void
00175   Options::_I::compare(Comparator* c) {
00176     _compare[static_cast<int>(n_compare++)] = c;
00177   }
00178   forceinline Inspector*
00179   Options::_I::click(unsigned int i) const {
00180     return (i < n_click) ? _click[i] : NULL;
00181   }
00182   forceinline Inspector*
00183   Options::_I::solution(unsigned int i) const {
00184     return (i < n_solution) ? _solution[i] : NULL;
00185   }
00186   forceinline Inspector*
00187   Options::_I::move(unsigned int i) const {
00188     return (i < n_move) ? _move[i] : NULL;
00189   }
00190   forceinline Comparator*
00191   Options::_I::compare(unsigned int i) const {
00192     return (i < n_compare) ? _compare[i] : NULL;
00193   }
00194 
00195   inline int
00196   dfs(Space* root, const Gist::Options& opt) {
00197     return explore(root, false, opt);
00198   }
00199 
00200   inline int
00201   bab(Space* root, const Gist::Options& opt) {
00202     return Gist::explore(root, true, opt);
00203   }
00204 
00205 }}
00206 
00207 // STATISTICS: gist-any