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 *
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
40using namespace Gecode;
41
42/**
43 * \brief %Example: Finding optimal %Golomb rulers
44 *
45 * The script makes use of two lower bounds taken from:
46 * Barbara Smith, Kostas Stergiou, Toby Walsh,
47 * Modelling the Golomb Ruler Problem.
48 * In IJCAI 99 Workshop on Non-binary Constraints,
49 * 1999.
50 *
51 * See also problem 6 at http://www.csplib.org/.
52 *
53 * The upper bound used is from the trivial construction where
54 * distances between consecutive marks are increasing powers of two.
55 *
56 * Note that "Modeling and Programming with Gecode" uses this example
57 * as a case study.
58 *
59 * \ingroup Example
60 *
61 */
62class GolombRuler : public IntMinimizeScript {
63protected:
64 /// Array for ruler marks
65 IntVarArray m;
66public:
67 /// Actual model
68 GolombRuler(const SizeOptions& opt)
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
102 /// Return cost
103 virtual IntVar cost(void) const {
104 return m[m.size()-1];
105 }
106
107 /// Print solution
108 virtual void
109 print(std::ostream& os) const {
110 os << "\tm[" << m.size() << "] = " << m << std::endl;
111 }
112
113 /// Constructor for cloning \a s
114 GolombRuler(GolombRuler& s)
115 : IntMinimizeScript(s) {
116 m.update(*this, s.m);
117 }
118 /// Copy during cloning
119 virtual Space*
120 copy(void) {
121 return new GolombRuler(*this);
122 }
123};
124
125/** \brief Main-function
126 * \relates GolombRuler
127 */
128int
129main(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