Generated on Sat Feb 7 2015 02:01:13 for Gecode by doxygen 1.8.9.1
word-square.cpp
Go to the documentation of this file.
1 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /*
3  * Main authors:
4  * Håkan Kjellerstrand <hakank@bonetmail.com>
5  * Mikael Lagerkvist <lagerkvist@gecode.org>
6  * Christian Schulte <schulte@gecode.org>
7  *
8  * Copyright:
9  * Håkan Kjellerstrand, 2009
10  * Mikael Lagerkvist, 2009
11  * Christian Schulte, 2009
12  *
13  * Last modified:
14  * $Date: 2013-07-08 14:22:40 +0200 (Mon, 08 Jul 2013) $ by $Author: schulte $
15  * $Revision: 13820 $
16  *
17  * This file is part of Gecode, the generic constraint
18  * development environment:
19  * http://www.gecode.org
20  *
21  * Permission is hereby granted, free of charge, to any person obtaining
22  * a copy of this software and associated documentation files (the
23  * "Software"), to deal in the Software without restriction, including
24  * without limitation the rights to use, copy, modify, merge, publish,
25  * distribute, sublicense, and/or sell copies of the Software, and to
26  * permit persons to whom the Software is furnished to do so, subject to
27  * the following conditions:
28  *
29  * The above copyright notice and this permission notice shall be
30  * included in all copies or substantial portions of the Software.
31  *
32  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
33  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
34  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
35  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
36  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
37  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
38  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
39  *
40  */
41 
42 #include <gecode/driver.hh>
43 
44 #include <gecode/int.hh>
45 #include <gecode/minimodel.hh>
46 
47 #include "examples/scowl.hpp"
48 
49 using namespace Gecode;
50 
63 class WordSquare : public Script {
64 protected:
66  const int w_l;
69 public:
71  enum {
74  };
77  : w_l(opt.size()), letters(*this, w_l*w_l) {
78 
79  // Initialize letters
80  Matrix<IntVarArray> ml(letters, w_l, w_l);
81  for (int i=0; i<w_l; i++)
82  for (int j=i; j<w_l; j++)
83  ml(i,j) = ml(j,i) = IntVar(*this, 'a','z');
84 
85  // Number of words with that length
86  const int n_w = dict.words(w_l);
87 
88  // Initialize word array
89  IntVarArgs words(*this, w_l, 0, n_w-1);
90 
91  // All words must be different
92  distinct(*this, words);
93 
94  // Link words with letters
95  for (int i=0; i<w_l; i++) {
96  // Map each word to i-th letter in that word
97  IntSharedArray w2l(n_w);
98  for (int n=n_w; n--; )
99  w2l[n]=dict.word(w_l,n)[i];
100  for (int j=0; j<w_l; j++)
101  element(*this, w2l, words[j], ml(i,j));
102  }
103 
104  // Symmetry breaking: the last word must be later in the wordlist
105  rel(*this, words[0], IRT_LE, words[w_l-1]);
106 
107  switch (opt.branching()) {
108  case BRANCH_WORDS:
109  // Branch by assigning words
110  branch(*this, words, INT_VAR_SIZE_MIN(), INT_VAL_SPLIT_MIN());
111  break;
112  case BRANCH_LETTERS:
113  // Branch by assigning letters
114  branch(*this, letters, INT_VAR_AFC_SIZE_MAX(opt.decay()), INT_VAL_MIN());
115  break;
116  }
117  }
119  WordSquare(bool share, WordSquare& s)
120  : Script(share,s), w_l(s.w_l) {
121  letters.update(*this, share, s.letters);
122  }
124  virtual Space*
125  copy(bool share) {
126  return new WordSquare(share,*this);
127  }
129  virtual void
130  print(std::ostream& os) const {
131  Matrix<IntVarArray> ml(letters, w_l, w_l);
132  for (int i=0; i<w_l; i++) {
133  os << "\t\t";
134  for (int j=0; j<w_l; j++)
135  if (ml(i,j).assigned()) {
136  os << static_cast<char>(ml(i,j).val());
137  } else {
138  os << ".";
139  }
140  os << std::endl;
141  }
142  os << std::endl;
143  }
144 };
145 
146 
150 int
151 main(int argc, char* argv[]) {
152  FileSizeOptions opt("WordSquare");
153  opt.size(6);
157  opt.parse(argc,argv);
158  dict.init(opt.file());
159  if (opt.size() > static_cast<unsigned int>(dict.len())) {
160  std::cerr << "Error: size must be between 0 and "
161  << dict.len() << std::endl;
162  return 1;
163  }
164  Script::run<WordSquare,DFS,SizeOptions>(opt);
165  return 0;
166 }
167 
168 // STATISTICS: example-any
Parse an additional file option.
Definition: scowl.hpp:41
void init(const char *fn)
Perform actual initialization.
Definition: scowl.hpp:13486
virtual void print(std::ostream &os) const
Print solution.
Options for scripts with additional size parameter
Definition: driver.hh:567
Branch on word variables.
Definition: word-square.cpp:72
IntVarBranch INT_VAR_SIZE_MIN(BranchTbl tbl)
Select variable with smallest domain size.
Definition: var.hpp:212
Integer variable array.
Definition: int.hh:741
Branch on letter variables.
Definition: word-square.cpp:73
Computation spaces.
Definition: core.hpp:1362
Parametric base-class for scripts.
Definition: driver.hh:622
void decay(double d)
Set default decay factor.
Definition: options.hpp:216
const int w_l
Length of words.
Definition: word-square.cpp:66
void update(Space &, bool share, VarArray< Var > &a)
Update array to be a clone of array a.
Definition: array.hpp:1072
Gecode::IntArgs i(4, 1, 2, 3, 4)
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:238
Options opt
The options.
Definition: test.cpp:101
Dictionary dict
The dictionary to be used.
Definition: scowl.hpp:99
virtual Space * copy(bool share)
Copy during cloning.
IntValBranch INT_VAL_MIN(void)
Select smallest value.
Definition: val.hpp:68
unsigned int size(I &i)
Size of all ranges of range iterator i.
WordSquare(const SizeOptions &opt)
Actual model.
Definition: word-square.cpp:76
const char * word(int l, int i) const
Return word number i with length l.
Definition: scowl.hpp:13607
int len(void) const
Return maximal length of a word.
Definition: scowl.hpp:13595
IntVarArray letters
The array of letters.
Definition: word-square.cpp:68
Less ( )
Definition: int.hh:907
void branching(int v)
Set default branching value.
Definition: options.hpp:203
void element(Home home, IntSharedArray c, IntVar x0, IntVar x1, IntConLevel)
Post domain consistent propagator for .
Definition: element.cpp:43
Passing integer variables.
Definition: int.hh:636
void parse(int &argc, char *argv[])
Parse options from arguments argv (number is argc)
Definition: options.cpp:331
Integer variables.
Definition: int.hh:350
bool assigned(View x, int v)
Whether x is assigned to value v.
Definition: single.hpp:47
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVal n)
Propagates .
Definition: rel.cpp:47
IntVarBranch INT_VAR_AFC_SIZE_MAX(double d, BranchTbl tbl)
Select variable with largest accumulated failure count divided by domain size with decay factor d...
Definition: var.hpp:242
Example: Word-square puzzle
Definition: word-square.cpp:63
WordSquare(bool share, WordSquare &s)
Constructor for cloning s.
void distinct(Home home, const IntVarArgs &x, IntConLevel icl)
Post propagator for for all .
Definition: distinct.cpp:47
Matrix-interface for arrays.
Definition: minimodel.hh:1924
IntValBranch INT_VAL_SPLIT_MIN(void)
Select values not greater than mean of smallest and largest value.
Definition: val.hpp:88
int main(int argc, char *argv[])
Definition: test.cpp:202
Gecode toplevel namespace
BrancherHandle branch(Home home, const FloatVarArgs &x, FloatVarBranch vars, FloatValBranch vals, FloatBranchFilter bf, FloatVarValPrint vvp)
Branch over x with variable selection vars and value selection vals.
Definition: branch.cpp:43
int words(void) const
Return total number of words.
Definition: scowl.hpp:13599