Generated on Tue Jan 19 2021 06:15:49 for Gecode by doxygen 1.8.13
golomb-ruler.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  *
6  * Copyright:
7  * Christian Schulte, 2001
8  *
9  * This file is part of Gecode, the generic constraint
10  * development environment:
11  * http://www.gecode.org
12  *
13  * Permission is hereby granted, free of charge, to any person obtaining
14  * a copy of this software and associated documentation files (the
15  * "Software"), to deal in the Software without restriction, including
16  * without limitation the rights to use, copy, modify, merge, publish,
17  * distribute, sublicense, and/or sell copies of the Software, and to
18  * permit persons to whom the Software is furnished to do so, subject to
19  * the following conditions:
20  *
21  * The above copyright notice and this permission notice shall be
22  * included in all copies or substantial portions of the Software.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31  *
32  */
33 
34 #include <gecode/driver.hh>
35 #include <gecode/int.hh>
36 #include <gecode/minimodel.hh>
37 
38 #include <iomanip>
39 
40 using namespace Gecode;
41 
63 protected:
66 public:
69  : IntMinimizeScript(opt),
70  m(*this,opt.size(),0,
71  (opt.size() < 31) ? (1 << (opt.size()-1))-1 : Int::Limits::max) {
72 
73  // Assume first mark to be zero
74  rel(*this, m[0], IRT_EQ, 0);
75 
76  // Order marks
77  rel(*this, m, IRT_LE);
78 
79  // Number of marks and differences
80  const int n = m.size();
81  const int n_d = (n*n-n)/2;
82 
83  // Array of differences
84  IntVarArgs d(n_d);
85 
86  // Setup difference constraints
87  for (int k=0, i=0; i<n-1; i++)
88  for (int j=i+1; j<n; j++, k++)
89  // d[k] is m[j]-m[i] and must be at least sum of first j-i integers
90  rel(*this, d[k] = expr(*this, m[j]-m[i]),
91  IRT_GQ, (j-i)*(j-i+1)/2);
92 
93  distinct(*this, d, opt.ipl());
94 
95  // Symmetry breaking
96  if (n > 2)
97  rel(*this, d[0], IRT_LE, d[n_d-1]);
98 
99  branch(*this, m, INT_VAR_NONE(), INT_VAL_MIN());
100  }
101 
103  virtual IntVar cost(void) const {
104  return m[m.size()-1];
105  }
106 
108  virtual void
109  print(std::ostream& os) const {
110  os << "\tm[" << m.size() << "] = " << m << std::endl;
111  }
112 
115  : IntMinimizeScript(s) {
116  m.update(*this, s.m);
117  }
119  virtual Space*
120  copy(void) {
121  return new GolombRuler(*this);
122  }
123 };
124 
128 int
129 main(int argc, char* argv[]) {
130  SizeOptions opt("GolombRuler");
131  opt.solutions(0);
132  opt.size(10);
133  opt.ipl(IPL_BND);
134  opt.parse(argc,argv);
135  if (opt.size() > 0)
136  IntMinimizeScript::run<GolombRuler,BAB,SizeOptions>(opt);
137  return 0;
138 }
139 
140 // STATISTICS: example-any
141 
Bounds propagation.
Definition: int.hh:978
void size(unsigned int s)
Set default size.
Definition: options.hpp:586
Options for scripts with additional size parameter
Definition: driver.hh:675
IntVarBranch INT_VAR_NONE(void)
Select first unassigned variable.
Definition: var.hpp:96
void 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:39
int size(void) const
Return size of array (number of elements)
Definition: array.hpp:908
void update(Space &home, VarArray< Var > &a)
Update array to be a clone of array a.
Definition: array.hpp:995
virtual IntVar cost(void) const
Return cost.
void max(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Definition: arithmetic.cpp:49
void parse(int &argc, char *argv[])
Parse options from arguments argv (number is argc)
Definition: options.cpp:666
GolombRuler(const SizeOptions &opt)
Actual model.
Integer variable array.
Definition: int.hh:763
void ipl(IntPropLevel i)
Set default integer propagation level.
Definition: options.hpp:216
Example: Finding optimal Golomb rulers
virtual Space * copy(void)
Copy during cloning.
Computation spaces.
Definition: core.hpp:1701
Greater or equal ( )
Definition: int.hh:930
Parametric base-class for scripts.
Definition: driver.hh:729
Gecode::IntSet d(v, 7)
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:234
Equality ( )
Definition: int.hh:926
Options opt
The options.
Definition: test.cpp:97
Gecode::IntArgs i({1, 2, 3, 4})
IntValBranch INT_VAL_MIN(void)
Select smallest value.
Definition: val.hpp:55
unsigned int size(I &i)
Size of all ranges of range iterator i.
void distinct(Home home, const IntVarArgs &x, IntPropLevel ipl)
Post propagator for for all .
Definition: distinct.cpp:46
Less ( )
Definition: int.hh:929
Passing integer variables.
Definition: int.hh:656
IntVarArray m
Array for ruler marks.
BoolVar expr(Home home, const BoolExpr &e, IntPropLevel ipl)
Post Boolean expression and return its value.
Definition: bool-expr.cpp:627
Integer variables.
Definition: int.hh:371
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVal n)
Propagates .
Definition: rel.cpp:43
GolombRuler(GolombRuler &s)
Constructor for cloning s.
void solutions(unsigned int n)
Set default number of solutions to search for.
Definition: options.hpp:283
Gecode toplevel namespace
virtual void print(std::ostream &os) const
Print solution.
int main(int argc, char *argv[])
Main-function.