this repo has no description
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
41/**
42 * \brief %Options for %Hamming problems
43 *
44 */
45
46class HammingOptions : public Options {
47private:
48 /// Number of bits
49 Driver::UnsignedIntOption _bits;
50 /// Minimum distance
51 Driver::UnsignedIntOption _distance;
52 /// Number of symbols
53 Driver::UnsignedIntOption _size;
54
55public:
56 /// Initialize options for example with name \a s
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
66 /// Return number of bits
67 unsigned int bits(void) const { return _bits.value(); }
68 /// Return minimum distance
69 unsigned int distance(void) const { return _distance.value(); }
70 /// Return number of symbols
71 unsigned int size(void) const { return _size.value(); }
72
73};
74
75/**
76 * \brief %Example: Generating %Hamming codes
77 *
78 * Generate a Hamming code that fits in b-bit words to code n symbols where
79 * the Hamming distance between every two symbol codes is at least d.
80 * The Hamming distance between two words is the number of bit positions
81 * where they differ.
82 *
83 * \ingroup Example
84 *
85 */
86class Hamming : public Script {
87private:
88 /// The hamming code
89 SetVarArray x;
90public:
91 /// Actual model
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
113 /// Print solution
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
121 /// Constructor for copying \a s
122 Hamming(Hamming& s) : Script(s) {
123 x.update(*this, s.x);
124 }
125 /// Copy during cloning
126 virtual Space*
127 copy(void) {
128 return new Hamming(*this);
129 }
130
131};
132
133/** \brief Main-function
134 * \relates Hamming
135 */
136int
137main(int argc, char* argv[]) {
138 HammingOptions opt("Hamming",20,3,32);
139 opt.parse(argc,argv);
140 Script::run<Hamming,DFS,HammingOptions>(opt);
141 return 0;
142}
143
144
145// STATISTICS: example-any
146