Generated on Thu Jan 16 2025 00:00:00 for Gecode by doxygen 1.14.0
hamming.cpp
Go to the documentation of this file.
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2/*
3 * Main authors:
4 * Guido Tack <tack@gecode.org>
5 *
6 * Copyright:
7 * Guido Tack, 2004
8 *
9 * This file is part of Gecode, the generic constraint
10 * development environment:
11 * http://www.gecode.org
12 *
13 * Permission is hereby granted, free of charge, to any person obtaining
14 * a copy of this software and associated documentation files (the
15 * "Software"), to deal in the Software without restriction, including
16 * without limitation the rights to use, copy, modify, merge, publish,
17 * distribute, sublicense, and/or sell copies of the Software, and to
18 * permit persons to whom the Software is furnished to do so, subject to
19 * the following conditions:
20 *
21 * The above copyright notice and this permission notice shall be
22 * included in all copies or substantial portions of the Software.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 *
32 */
33
34#include <gecode/driver.hh>
35#include <gecode/int.hh>
36#include <gecode/minimodel.hh>
37#include <gecode/set.hh>
38
39using namespace Gecode;
40
45
46class HammingOptions : public Options {
47private:
54
55public:
57 HammingOptions(const char* s, unsigned int bits0,
58 unsigned int distance0, unsigned int size0)
59 : Options(s),
60 _bits("bits","word size in bits",bits0),
61 _distance("distance","minimum distance",distance0),
62 _size("size","number of symbols",size0) {
63 add(_bits); add(_distance); add(_size);
64 }
65
67 unsigned int bits(void) const { return _bits.value(); }
69 unsigned int distance(void) const { return _distance.value(); }
71 unsigned int size(void) const { return _size.value(); }
72
73};
74
86class Hamming : public Script {
87private:
90public:
92 Hamming(const HammingOptions& opt) :
93 Script(opt),
94 x(*this,opt.size(),IntSet::empty,1,opt.bits()) {
95
96 if (opt.trace() != 0)
97 trace(*this, x, opt.trace());
98
99 SetVarArgs cx(x.size());
100
101 for (int i=x.size(); i--;)
102 cx[i] = expr(*this, -x[i]);
103
104 for (int i=0; i<x.size(); i++)
105 for (int j=i+1; j<x.size(); j++)
106 rel(*this,
107 cardinality(x[j] & cx[i]) +
108 cardinality(x[i] & cx[j]) >= opt.distance());
109
110 branch(*this, x, SET_VAR_NONE(), SET_VAL_MIN_INC());
111 }
112
114 virtual void
115 print(std::ostream& os) const {
116 for (int i=0; i<x.size(); i++) {
117 os << "\t[" << i << "] = " << x[i] << std::endl;
118 }
119 }
120
123 x.update(*this, s.x);
124 }
125
126 virtual Space*
127 copy(void) {
128 return new Hamming(*this);
129 }
130
131};
132
136int
137main(int argc, char* argv[]) {
138 HammingOptions opt("Hamming",20,3,32);
139 opt.parse(argc,argv);
141 return 0;
142}
143
144
145// STATISTICS: example-any
146
int size(void) const
Return size of array (number of elements)
Definition array.hpp:1613
void add(Driver::BaseOption &o)
Add new option o.
Definition options.cpp:474
static void run(const Options &opt, Script *s=NULL)
Unsigned integer option.
Definition driver.hh:229
Integer sets.
Definition int.hh:174
Options(const char *s)
Initialize options for script with name s.
Definition options.cpp:576
Passing set variables.
Definition set.hh:491
Set variable array
Definition set.hh:573
Computation spaces.
Definition core.hpp:1744
Options for Hamming problems
Definition hamming.cpp:46
unsigned int bits(void) const
Return number of bits.
Definition hamming.cpp:67
unsigned int size(void) const
Return number of symbols.
Definition hamming.cpp:71
unsigned int distance(void) const
Return minimum distance.
Definition hamming.cpp:69
HammingOptions(const char *s, unsigned int bits0, unsigned int distance0, unsigned int size0)
Initialize options for example with name s.
Definition hamming.cpp:57
virtual void print(std::ostream &os) const
Print solution.
Definition hamming.cpp:115
int main(int argc, char *argv[])
Main-function.
Definition hamming.cpp:137
Hamming(Hamming &s)
Constructor for copying s.
Definition hamming.cpp:122
Hamming(const HammingOptions &opt)
Actual model.
Definition hamming.cpp:92
virtual Space * copy(void)
Copy during cloning.
Definition hamming.cpp:127
void parse(int argc, char *argv[])
Parse commandline arguments.
Definition test.cpp:120
Driver::ScriptBase< Driver::IgnoreStepOption< Space > > Script
Base-class for scripts.
Definition driver.hh:801
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
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
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVar x1)
Post propagator for .
Definition rel.cpp:68
Gecode toplevel namespace
Select first unassigned variable SetVarBranch SET_VAR_NONE(void)
Definition var.hpp:96
IntVar expr(Home home, const LinIntExpr &e, const IntPropLevels &ipls=IntPropLevels::def)
Post linear expression and return its value.
Definition int-expr.cpp:915
Include smallest element SetValBranch SET_VAL_MIN_INC(void)
Definition val.hpp:55
LinIntExpr cardinality(const SetExpr &)
Cardinality of set expression.
Definition set-expr.cpp:817