hamming.cpp
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, 2004 00008 * 00009 * Last modified: 00010 * $Date: 2010-10-07 11:52:01 +0200 (Thu, 07 Oct 2010) $ by $Author: schulte $ 00011 * $Revision: 11473 $ 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 #include <gecode/driver.hh> 00039 #include <gecode/int.hh> 00040 #include <gecode/minimodel.hh> 00041 #include <gecode/set.hh> 00042 00043 using namespace Gecode; 00044 00050 class HammingOptions : public Options { 00051 private: 00053 Driver::UnsignedIntOption _bits; 00055 Driver::UnsignedIntOption _distance; 00057 Driver::UnsignedIntOption _size; 00058 00059 public: 00061 HammingOptions(const char* s, unsigned int bits0, 00062 unsigned int distance0, unsigned int size0) 00063 : Options(s), 00064 _bits("-bits","word size in bits",bits0), 00065 _distance("-distance","minimum distance",distance0), 00066 _size("-size","number of symbols",size0) { 00067 add(_bits); add(_distance); add(_size); 00068 } 00069 00071 unsigned int bits(void) const { return _bits.value(); } 00073 unsigned int distance(void) const { return _distance.value(); } 00075 unsigned int size(void) const { return _size.value(); } 00076 00077 }; 00078 00090 class Hamming : public Script { 00091 private: 00093 SetVarArray x; 00094 public: 00096 Hamming(const HammingOptions& opt) : 00097 x(*this,opt.size(),IntSet::empty,1,opt.bits()) { 00098 SetVarArgs cx(x.size()); 00099 for (int i=x.size(); i--;) 00100 cx[i] = expr(*this, -x[i]); 00101 00102 for (int i=0; i<x.size(); i++) 00103 for (int j=i+1; j<x.size(); j++) 00104 rel(*this, 00105 cardinality(x[j] & cx[i]) + 00106 cardinality(x[i] & cx[j]) >= opt.distance()); 00107 00108 branch(*this, x, SET_VAR_NONE, SET_VAL_MIN_INC); 00109 } 00110 00112 virtual void 00113 print(std::ostream& os) const { 00114 for (int i=0; i<x.size(); i++) { 00115 os << "\t[" << i << "] = " << x[i] << std::endl; 00116 } 00117 } 00118 00120 Hamming(bool share, Hamming& s) : Script(share,s) { 00121 x.update(*this, share, s.x); 00122 } 00124 virtual Space* 00125 copy(bool share) { 00126 return new Hamming(share,*this); 00127 } 00128 00129 }; 00130 00134 int 00135 main(int argc, char* argv[]) { 00136 HammingOptions opt("Hamming",20,3,32); 00137 opt.parse(argc,argv); 00138 Script::run<Hamming,DFS,HammingOptions>(opt); 00139 return 0; 00140 } 00141 00142 00143 // STATISTICS: example-any 00144