hamming.cpp
Go to the documentation of this file.
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
* Main authors:
* Guido Tack <tack@gecode.org>
*
* Copyright:
* Guido Tack, 2004
*
* This file is part of Gecode, the generic constraint
* development environment:
* http://www.gecode.org
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
#include <gecode/driver.hh>
#include <gecode/int.hh>
#include <gecode/minimodel.hh>
#include <gecode/set.hh>
using namespace Gecode;
class HammingOptions : public Options {
private:
Driver::UnsignedIntOption _bits;
Driver::UnsignedIntOption _distance;
Driver::UnsignedIntOption _size;
public:
HammingOptions(const char* s, unsigned int bits0,
unsigned int distance0, unsigned int size0)
: Options(s),
_bits("bits","word size in bits",bits0),
_distance("distance","minimum distance",distance0),
_size("size","number of symbols",size0) {
add(_bits); add(_distance); add(_size);
}
unsigned int bits(void) const { return _bits.value(); }
unsigned int distance(void) const { return _distance.value(); }
unsigned int size(void) const { return _size.value(); }
};
class Hamming : public Script {
private:
SetVarArray x;
public:
Hamming(const HammingOptions& opt) :
Script(opt),
x(*this,opt.size(),IntSet::empty,1,opt.bits()) {
if (opt.trace() != 0)
trace(*this, x, opt.trace());
SetVarArgs cx(x.size());
for (int i=x.size(); i--;)
cx[i] = expr(*this, -x[i]);
for (int i=0; i<x.size(); i++)
for (int j=i+1; j<x.size(); j++)
rel(*this,
cardinality(x[j] & cx[i]) +
cardinality(x[i] & cx[j]) >= opt.distance());
branch(*this, x, SET_VAR_NONE(), SET_VAL_MIN_INC());
}
virtual void
print(std::ostream& os) const {
for (int i=0; i<x.size(); i++) {
os << "\t[" << i << "] = " << x[i] << std::endl;
}
}
Hamming(Hamming& s) : Script(s) {
x.update(*this, s.x);
}
virtual Space*
copy(void) {
return new Hamming(*this);
}
};
int
main(int argc, char* argv[]) {
HammingOptions opt("Hamming",20,3,32);
opt.parse(argc,argv);
Script::run<Hamming,DFS,HammingOptions>(opt);
return 0;
}
// STATISTICS: example-any
Gecode::ArgArrayBase::size
int size(void) const
Return size of array (number of elements).
Definition array.hpp:1607
Gecode::BaseOptions::add
void add(Driver::BaseOption &o)
Add new option o.
Definition options.cpp:474
Gecode::Driver::ScriptBase< Driver::IgnoreStepOption< Space > >::run
static void run(const Options &opt, Script *s=NULL)
Gecode::Driver::UnsignedIntOption
Unsigned integer option.
Definition driver.hh:229
Gecode::IntSet
Integer sets.
Definition int.hh:174
Gecode::Options::Options
Options(const char *s)
Initialize options for script with name s.
Definition options.cpp:576
Gecode::SetVarArgs
Passing set variables.
Definition set.hh:488
Gecode::SetVarArray
Set variable array
Definition set.hh:570
Gecode::Space
Computation spaces.
Definition core.hpp:1742
HammingOptions
Options for Hamming problems
Definition hamming.cpp:46
HammingOptions::bits
unsigned int bits(void) const
Return number of bits.
Definition hamming.cpp:67
HammingOptions::size
unsigned int size(void) const
Return number of symbols.
Definition hamming.cpp:71
HammingOptions::distance
unsigned int distance(void) const
Return minimum distance.
Definition hamming.cpp:69
HammingOptions::HammingOptions
HammingOptions(const char *s, unsigned int bits0, unsigned int distance0, unsigned int size0)
Initialize options for example with name s.
Definition hamming.cpp:57
Hamming::print
virtual void print(std::ostream &os) const
Print solution.
Definition hamming.cpp:115
Hamming::main
int main(int argc, char *argv[])
Main-function.
Definition hamming.cpp:137
Hamming::Hamming
Hamming(Hamming &s)
Constructor for copying s.
Definition hamming.cpp:122
Hamming::Hamming
Hamming(const HammingOptions &opt)
Actual model.
Definition hamming.cpp:92
Hamming::copy
virtual Space * copy(void)
Copy during cloning.
Definition hamming.cpp:127
Test::Options::parse
void parse(int argc, char *argv[])
Parse commandline arguments.
Definition test.cpp:120
driver.hh
int.hh
set.hh
Gecode::Script
Driver::ScriptBase< Driver::IgnoreStepOption< Space > > Script
Base-class for scripts.
Definition driver.hh:801
Gecode::trace
GECODE_FLOAT_EXPORT void trace(Home home, const FloatVarArgs &x, TraceFilter tf, int te=(TE_INIT|TE_PRUNE|TE_FIX|TE_FAIL|TE_DONE), FloatTracer &t=StdFloatTracer::def)
Create a tracer for float variables.
Definition trace.cpp:39
Gecode::branch
void branch(Home home, const FloatVarArgs &x, FloatVarBranch vars, FloatValBranch vals, FloatBranchFilter bf=nullptr, FloatVarValPrint vvp=nullptr)
Branch over x with variable selection vars and value selection vals.
Definition branch.cpp:39
Gecode::rel
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVar x1)
Post propagator for .
Definition rel.cpp:68
minimodel.hh
Gecode
Gecode toplevel namespace
Gecode::SET_VAR_NONE
SetVarBranch SET_VAR_NONE(void)
Select first unassigned variable.
Definition var.hpp:96
Gecode::expr
IntVar expr(Home home, const LinIntExpr &e, const IntPropLevels &ipls=IntPropLevels::def)
Post linear expression and return its value.
Definition int-expr.cpp:915
Gecode::SET_VAL_MIN_INC
SetValBranch SET_VAL_MIN_INC(void)
Include smallest element.
Definition val.hpp:55
Gecode::cardinality
LinIntExpr cardinality(const SetExpr &)
Cardinality of set expression.
Definition set-expr.cpp:817