Generated on Wed Jul 27 2011 15:08:35 for Gecode by doxygen 1.7.4
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: 2010-04-07 14:24:53 +0200 (Wed, 07 Apr 2010) $ by $Author: tack $
00011  *     $Revision: 10668 $
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     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 // STATISTICS: gist-any