Generated on Sat Feb 7 2015 02:01:11 for Gecode by doxygen 1.8.9.1
magic-sequence.cpp
Go to the documentation of this file.
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  * Last modified:
12  * $Date: 2013-07-08 14:22:40 +0200 (Mon, 08 Jul 2013) $ by $Author: schulte $
13  * $Revision: 13820 $
14  *
15  * This file is part of Gecode, the generic constraint
16  * development environment:
17  * http://www.gecode.org
18  *
19  * Permission is hereby granted, free of charge, to any person obtaining
20  * a copy of this software and associated documentation files (the
21  * "Software"), to deal in the Software without restriction, including
22  * without limitation the rights to use, copy, modify, merge, publish,
23  * distribute, sublicense, and/or sell copies of the Software, and to
24  * permit persons to whom the Software is furnished to do so, subject to
25  * the following conditions:
26  *
27  * The above copyright notice and this permission notice shall be
28  * included in all copies or substantial portions of the Software.
29  *
30  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
31  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
32  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
33  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
34  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
35  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
36  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37  *
38  */
39 
40 #include <gecode/driver.hh>
41 #include <gecode/int.hh>
42 #include <gecode/minimodel.hh>
43 
44 using namespace Gecode;
45 
63 class MagicSequence : public Script {
64 private:
66  const int n;
68  IntVarArray s;
69 public:
71  enum {
73  PROP_GCC
74  };
77  : n(opt.size()), s(*this,n,0,n-1) {
78  switch (opt.propagation()) {
79  case PROP_COUNT:
80  for (int i=n; i--; )
81  count(*this, s, i, IRT_EQ, s[i]);
82  linear(*this, s, IRT_EQ, n);
83  break;
84  case PROP_GCC:
85  count(*this, s, s, opt.icl());
86  break;
87  }
88  linear(*this, IntArgs::create(n,-1,1), s, IRT_EQ, 0);
89  branch(*this, s, INT_VAR_NONE(), INT_VAL_MAX());
90  }
91 
93  MagicSequence(bool share, MagicSequence& e) : Script(share,e), n(e.n) {
94  s.update(*this, share, e.s);
95  }
97  virtual Space*
98  copy(bool share) {
99  return new MagicSequence(share,*this);
100  }
102  virtual
103  void print(std::ostream& os) const {
104  os << "\t";
105  for (int i = 0; i<n; i++) {
106  os << s[i] << ", ";
107  if ((i+1) % 20 == 0)
108  os << std::endl << "\t";
109  }
110  os << std::endl;
111  }
112 
113 };
114 
118 int
119 main(int argc, char* argv[]) {
120  SizeOptions opt("MagicSequence");
121  opt.solutions(0);
122  opt.iterations(4);
123  opt.size(500);
127  opt.parse(argc,argv);
128  Script::run<MagicSequence,DFS,SizeOptions>(opt);
129  return 0;
130 }
131 
132 // STATISTICS: example-any
133 
void size(unsigned int s)
Set default size.
Definition: options.hpp:467
static IntArgs create(int n, int start, int inc=1)
Allocate array with n elements such that for all .
Definition: array.hpp:72
Options for scripts with additional size parameter
Definition: driver.hh:567
IntVarBranch INT_VAR_NONE(void)
Select first unassigned variable.
Definition: var.hpp:108
void linear(Home home, const FloatVarArgs &x, FloatRelType frt, FloatNum c)
Post propagator for .
Definition: linear.cpp:45
void propagation(int v)
Set default propagation value.
Definition: options.hpp:181
virtual void print(std::ostream &os) const
Print sequence.
void parse(int &argc, char *argv[])
Parse options from arguments argv (number is argc)
Definition: options.cpp:435
Use single global cardinality constraint.
Example: Magic sequence
Integer variable array.
Definition: int.hh:741
Computation spaces.
Definition: core.hpp:1362
Parametric base-class for scripts.
Definition: driver.hh:622
void iterations(unsigned int i)
Set default number of iterations.
Definition: options.hpp:385
MagicSequence(bool share, MagicSequence &e)
Constructor for cloning e.
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
Equality ( )
Definition: int.hh:904
Options opt
The options.
Definition: test.cpp:101
virtual Space * copy(bool share)
Copy during cloning.
unsigned int size(I &i)
Size of all ranges of range iterator i.
IntValBranch INT_VAL_MAX(void)
Select largest value.
Definition: val.hpp:78
void count(Home home, const IntVarArgs &x, int n, IntRelType irt, int m, IntConLevel)
Post propagator for .
Definition: count.cpp:44
int main(int argc, char *argv[])
Main-function.
Use count constraints.
void solutions(unsigned int n)
Set default number of solutions to search for.
Definition: options.hpp:243
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
void icl(IntConLevel i)
Set default integer consistency level.
Definition: options.hpp:194
MagicSequence(const SizeOptions &opt)
The actual model.