Generated on Tue Feb 11 2025 17:33:26 for Gecode by doxygen 1.12.0
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
45using namespace Gecode;
46
59class WordSquare : public Script {
60protected:
62 const int w_l;
65public:
67 enum {
70 };
73 : Script(opt), w_l(opt.size()), letters(*this, w_l*w_l) {
74
75 // Initialize letters
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 {
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
146int
147main(int argc, char* argv[]) {
148 FileSizeOptions opt("WordSquare");
149 opt.size(6);
150 opt.branching(WordSquare::BRANCH_LETTERS);
151 opt.branching(WordSquare::BRANCH_WORDS, "words");
152 opt.branching(WordSquare::BRANCH_LETTERS, "letters");
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 }
161 return 0;
162}
163
164// STATISTICS: example-any
int n
Number of negative literals for node type.
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
Parametric base-class for scripts.
Definition driver.hh:729
static void run(const Options &opt, Script *s=NULL)
Definition script.hpp:290
Passing integer variables.
Definition int.hh:656
Integer variable array.
Definition int.hh:763
Integer variables.
Definition int.hh:371
Matrix-interface for arrays.
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
void parse(int argc, char *argv[])
Parse commandline arguments.
Definition test.cpp:120
Example: Word-square puzzle
IntVarArray letters
The array of letters.
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.
@ BRANCH_LETTERS
Branch on letter variables.
@ BRANCH_WORDS
Branch on word variables.
const int w_l
Length of words.
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
@ IRT_LE
Less ( )
Definition int.hh:929
Gecode toplevel namespace
IntValBranch INT_VAL_SPLIT_MIN(void)
Select values not greater than mean of smallest and largest value.
Definition val.hpp:75
IntVarBranch INT_VAR_AFC_SIZE_MAX(double d=1.0, BranchTbl tbl=nullptr)
Select variable with largest accumulated failure count divided by domain size with decay factor d.
Definition var.hpp:236
void distinct(Home home, const IntVarArgs &x, IntPropLevel ipl=IPL_DEF)
Post propagator for for all .
Definition distinct.cpp:46
void element(Home home, IntSharedArray n, IntVar x0, IntVar x1, IntPropLevel ipl=IPL_DEF)
Post domain consistent propagator for .
Definition element.cpp:39
IntValBranch INT_VAL_MIN(void)
Select smallest value.
Definition val.hpp:55
IntVarBranch INT_VAR_SIZE_MIN(BranchTbl tbl=nullptr)
Select variable with smallest domain size.
Definition var.hpp:206
Dictionary dict
The dictionary to be used.
Definition scowl.hpp:95