Generated on Tue Jan 26 2021 00:00:00 for Gecode by doxygen 1.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  * 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 
40 using namespace Gecode;
41 
59 class MagicSequence : public Script {
60 private:
62  const int n;
64  IntVarArray s;
65 public:
67  enum {
69  PROP_GCC
70  };
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 
90  s.update(*this, e.s);
91  }
93  virtual Space*
94  copy(void) {
95  return new MagicSequence(*this);
96  }
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 
114 int
115 main(int argc, char* argv[]) {
116  SizeOptions opt("MagicSequence");
117  opt.solutions(0);
118  opt.iterations(4);
119  opt.size(500);
123  opt.parse(argc,argv);
124  Script::run<MagicSequence,DFS,SizeOptions>(opt);
125  return 0;
126 }
127 
128 // STATISTICS: example-any
129 
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:234
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
static IntArgs create(int n, int start, int inc=1)
Allocate array with n elements such that for all .
Definition: array.hpp:76
Integer variable array.
Definition: int.hh:763
void propagation(int v)
Set default propagation value.
Definition: options.hpp:203
void iterations(unsigned int i)
Set default number of iterations.
Definition: options.hpp:461
void solutions(unsigned int n)
Set default number of solutions to search for.
Definition: options.hpp:283
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: Magic sequence
int main(int argc, char *argv[])
Main-function.
@ PROP_COUNT
Use count constraints.
@ PROP_GCC
Use single global cardinality constraint.
virtual void print(std::ostream &os) const
Print sequence.
virtual Space * copy(void)
Copy during cloning.
MagicSequence(MagicSequence &e)
Constructor for cloning e.
MagicSequence(const SizeOptions &opt)
The actual model.
void count(Home home, const IntVarArgs &x, int n, IntRelType irt, int m, IntPropLevel)
Post propagator for .
Definition: count.cpp:40
void linear(Home home, const FloatVarArgs &x, FloatRelType frt, FloatVal c)
Post propagator for .
Definition: linear.cpp:41
@ IRT_EQ
Equality ( )
Definition: int.hh:926
IntValBranch INT_VAL_MAX(void)
Select largest value.
Definition: val.hpp:65
IntVarBranch INT_VAR_NONE(void)
Select first unassigned variable.
Definition: var.hpp:96
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