hamming.cpp
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 #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
00144