this repo has no description
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2/*
3 * Main authors:
4 * Christian Schulte <schulte@gecode.org>
5 * Guido Tack <tack@gecode.org>
6 *
7 * Copyright:
8 * Christian Schulte, 2001
9 * Guido Tack, 2006
10 *
11 * This file is part of Gecode, the generic constraint
12 * development environment:
13 * http://www.gecode.org
14 *
15 * Permission is hereby granted, free of charge, to any person obtaining
16 * a copy of this software and associated documentation files (the
17 * "Software"), to deal in the Software without restriction, including
18 * without limitation the rights to use, copy, modify, merge, publish,
19 * distribute, sublicense, and/or sell copies of the Software, and to
20 * permit persons to whom the Software is furnished to do so, subject to
21 * the following conditions:
22 *
23 * The above copyright notice and this permission notice shall be
24 * included in all copies or substantial portions of the Software.
25 *
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 *
34 */
35
36#include <gecode/driver.hh>
37#include <gecode/int.hh>
38#include <gecode/minimodel.hh>
39
40using namespace Gecode;
41
42/**
43 * \brief %Example: Magic sequence
44 *
45 * Find a magic sequence of length \f$n\f$. A magic sequence of
46 * length \f$n\f$ is a sequence \f[x_0,x_1, \ldots, x_{n-1}\f]
47 * of integers such that for every \f$i=0,\ldots,n-1\f$:
48 * - \f$x_i\f$ is an integer between \f$0\f$ and \f$n-1\f$.
49 * - the number \f$i\f$ occurs exactly \f$x_i\f$ times in the sequence.
50 *
51 * See problem 19 at http://www.csplib.org/.
52 *
53 * Note that "Modeling and Programming with Gecode" uses this example
54 * as a case study.
55 *
56 * \ingroup Example
57 *
58 */
59class MagicSequence : public Script {
60private:
61 /// Length of sequence
62 const int n;
63 /// Sequence
64 IntVarArray s;
65public:
66 /// Propagation to use for model
67 enum {
68 PROP_COUNT, ///< Use count constraints
69 PROP_GCC ///< Use single global cardinality constraint
70 };
71 /// The actual model
72 MagicSequence(const SizeOptions& opt)
73 : Script(opt), n(opt.size()), s(*this,n,0,n-1) {
74 switch (opt.propagation()) {
75 case PROP_COUNT:
76 for (int i=n; i--; )
77 count(*this, s, i, IRT_EQ, s[i]);
78 linear(*this, s, IRT_EQ, n);
79 break;
80 case PROP_GCC:
81 count(*this, s, s, opt.ipl());
82 break;
83 }
84 linear(*this, IntArgs::create(n,-1,1), s, IRT_EQ, 0);
85 branch(*this, s, INT_VAR_NONE(), INT_VAL_MAX());
86 }
87
88 /// Constructor for cloning \a e
89 MagicSequence(MagicSequence& e) : Script(e), n(e.n) {
90 s.update(*this, e.s);
91 }
92 /// Copy during cloning
93 virtual Space*
94 copy(void) {
95 return new MagicSequence(*this);
96 }
97 /// Print sequence
98 virtual
99 void print(std::ostream& os) const {
100 os << "\t";
101 for (int i = 0; i<n; i++) {
102 os << s[i] << ", ";
103 if ((i+1) % 20 == 0)
104 os << std::endl << "\t";
105 }
106 os << std::endl;
107 }
108
109};
110
111/** \brief Main-function
112 * \relates MagicSequence
113 */
114int
115main(int argc, char* argv[]) {
116 SizeOptions opt("MagicSequence");
117 opt.solutions(0);
118 opt.iterations(4);
119 opt.size(500);
120 opt.propagation(MagicSequence::PROP_COUNT);
121 opt.propagation(MagicSequence::PROP_COUNT, "count");
122 opt.propagation(MagicSequence::PROP_GCC, "gcc");
123 opt.parse(argc,argv);
124 Script::run<MagicSequence,DFS,SizeOptions>(opt);
125 return 0;
126}
127
128// STATISTICS: example-any
129