Generated on Sat Apr 10 2021 00:00:00 for Gecode by doxygen 1.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  * This file is part of Gecode, the generic constraint
14  * development environment:
15  * http://www.gecode.org
16  *
17  * Permission is hereby granted, free of charge, to any person obtaining
18  * a copy of this software and associated documentation files (the
19  * "Software"), to deal in the Software without restriction, including
20  * without limitation the rights to use, copy, modify, merge, publish,
21  * distribute, sublicense, and/or sell copies of the Software, and to
22  * permit persons to whom the Software is furnished to do so, subject to
23  * the following conditions:
24  *
25  * The above copyright notice and this permission notice shall be
26  * included in all copies or substantial portions of the Software.
27  *
28  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35  *
36  */
37 
38 #include <gecode/driver.hh>
39 
40 #include <gecode/int.hh>
41 #include <gecode/minimodel.hh>
42 
43 #include "examples/scowl.hpp"
44 
45 using namespace Gecode;
46 
59 class WordSquare : public Script {
60 protected:
62  const int w_l;
65 public:
67  enum {
70  };
73  : Script(opt), w_l(opt.size()), letters(*this, w_l*w_l) {
74 
75  // Initialize letters
76  Matrix<IntVarArray> ml(letters, w_l, w_l);
77  for (int i=0; i<w_l; i++)
78  for (int j=i; j<w_l; j++)
79  ml(i,j) = ml(j,i) = IntVar(*this, 'a','z');
80 
81  // Number of words with that length
82  const int n_w = dict.words(w_l);
83 
84  // Initialize word array
85  IntVarArgs words(*this, w_l, 0, n_w-1);
86 
87  // All words must be different
88  distinct(*this, words);
89 
90  // Link words with letters
91  for (int i=0; i<w_l; i++) {
92  // Map each word to i-th letter in that word
93  IntSharedArray w2l(n_w);
94  for (int n=0; n<n_w; n++)
95  w2l[n]=dict.word(w_l,n)[i];
96  for (int j=0; j<w_l; j++)
97  element(*this, w2l, words[j], ml(i,j));
98  }
99 
100  // Symmetry breaking: the last word must be later in the wordlist
101  rel(*this, words[0], IRT_LE, words[w_l-1]);
102 
103  switch (opt.branching()) {
104  case BRANCH_WORDS:
105  // Branch by assigning words
106  branch(*this, words, INT_VAR_SIZE_MIN(), INT_VAL_SPLIT_MIN());
107  break;
108  case BRANCH_LETTERS:
109  // Branch by assigning letters
110  branch(*this, letters, INT_VAR_AFC_SIZE_MAX(opt.decay()), INT_VAL_MIN());
111  break;
112  }
113  }
116  : Script(s), w_l(s.w_l) {
117  letters.update(*this, s.letters);
118  }
120  virtual Space*
121  copy(void) {
122  return new WordSquare(*this);
123  }
125  virtual void
126  print(std::ostream& os) const {
127  Matrix<IntVarArray> ml(letters, w_l, w_l);
128  for (int i=0; i<w_l; i++) {
129  os << "\t\t";
130  for (int j=0; j<w_l; j++)
131  if (ml(i,j).assigned()) {
132  os << static_cast<char>(ml(i,j).val());
133  } else {
134  os << ".";
135  }
136  os << std::endl;
137  }
138  os << std::endl;
139  }
140 };
141 
142 
146 int
147 main(int argc, char* argv[]) {
148  FileSizeOptions opt("WordSquare");
149  opt.size(6);
153  opt.parse(argc,argv);
154  dict.init(opt.file());
155  if (opt.size() > static_cast<unsigned int>(dict.len())) {
156  std::cerr << "Error: size must be between 0 and "
157  << dict.len() << std::endl;
158  return 1;
159  }
160  Script::run<WordSquare,DFS,SizeOptions>(opt);
161  return 0;
162 }
163 
164 // STATISTICS: example-any
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:234
void init(const char *fn)
Perform actual initialization.
Definition: scowl.hpp:13482
int len(void) const
Return maximal length of a word.
Definition: scowl.hpp:13599
const char * word(int l, int i) const
Return word number i with length l.
Definition: scowl.hpp:13611
int words(void) const
Return total number of words.
Definition: scowl.hpp:13603
Parse an additional file option.
Definition: scowl.hpp:37
void parse(int &argc, char *argv[])
Parse options from arguments argv (number is argc)
Definition: options.cpp:548
Parametric base-class for scripts.
Definition: driver.hh:729
Passing integer variables.
Definition: int.hh:656
Integer variable array.
Definition: int.hh:763
Integer variables.
Definition: int.hh:371
Matrix-interface for arrays.
Definition: minimodel.hh:2161
void branching(int v)
Set default branching value.
Definition: options.hpp:225
Options for scripts with additional size parameter
Definition: driver.hh:675
Computation spaces.
Definition: core.hpp:1742
void update(Space &home, VarArray< Var > &a)
Update array to be a clone of array a.
Definition: array.hpp:1013
Example: Word-square puzzle
Definition: word-square.cpp:59
IntVarArray letters
The array of letters.
Definition: word-square.cpp:64
int main(int argc, char *argv[])
Main-function.
virtual void print(std::ostream &os) const
Print solution.
WordSquare(WordSquare &s)
Constructor for cloning s.
virtual Space * copy(void)
Copy during cloning.
WordSquare(const SizeOptions &opt)
Actual model.
Definition: word-square.cpp:72
@ BRANCH_LETTERS
Branch on letter variables.
Definition: word-square.cpp:69
@ BRANCH_WORDS
Branch on word variables.
Definition: word-square.cpp:68
const int w_l
Length of words.
Definition: word-square.cpp:62
void distinct(Home home, const IntVarArgs &x, IntPropLevel ipl)
Post propagator for for all .
Definition: distinct.cpp:46
void element(Home home, IntSharedArray c, IntVar x0, IntVar x1, IntPropLevel)
Post domain consistent propagator for .
Definition: element.cpp:39
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVal n)
Propagates .
Definition: rel.cpp:43
@ IRT_LE
Less ( )
Definition: int.hh:929
IntValBranch INT_VAL_SPLIT_MIN(void)
Select values not greater than mean of smallest and largest value.
Definition: val.hpp:75
IntValBranch INT_VAL_MIN(void)
Select smallest value.
Definition: val.hpp:55
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:236
IntVarBranch INT_VAR_SIZE_MIN(BranchTbl tbl)
Select variable with smallest domain size.
Definition: var.hpp:206
bool assigned(View x, int v)
Whether x is assigned to value v.
Definition: single.hpp:43
unsigned int size(I &i)
Size of all ranges of range iterator i.
Gecode::IntArgs i({1, 2, 3, 4})
Options opt
The options.
Definition: test.cpp:97
Dictionary dict
The dictionary to be used.
Definition: scowl.hpp:95