Generated on Thu Jan 16 2025 00:00:00 for Gecode by doxygen 1.14.0
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
40using namespace Gecode;
41
63protected:
66public:
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 }
118
119 virtual Space*
120 copy(void) {
121 return new GolombRuler(*this);
122 }
123};
124
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)
137 return 0;
138}
139
140// STATISTICS: example-any
141
Passing integer variables.
Definition int.hh:662
Integer variable array.
Definition int.hh:772
Integer variables.
Definition int.hh:371
Options for scripts with additional size parameter
Definition driver.hh:675
Computation spaces.
Definition core.hpp:1744
int main(int argc, char *argv[])
Main-function.
virtual Space * copy(void)
Copy during cloning.
virtual void print(std::ostream &os) const
Print solution.
IntVarArray m
Array for ruler marks.
GolombRuler(const SizeOptions &opt)
Actual model.
GolombRuler(GolombRuler &s)
Constructor for cloning s.
virtual IntVar cost(void) const
Return cost.
void parse(int argc, char *argv[])
Parse commandline arguments.
Definition test.cpp:120
Driver::ScriptBase< Driver::IgnoreStepOption< IntMinimizeSpace > > IntMinimizeScript
Base-class for scripts for finding solution of lowest integer cost.
Definition driver.hh:808
void branch(Home home, const FloatVarArgs &x, FloatVarBranch vars, FloatValBranch vals, FloatBranchFilter bf=nullptr, FloatVarValPrint vvp=nullptr)
Branch over x with variable selection vars and value selection vals.
Definition branch.cpp:39
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVar x1)
Post propagator for .
Definition rel.cpp:68
@ IRT_EQ
Equality ( )
Definition int.hh:941
@ IRT_GQ
Greater or equal ( )
Definition int.hh:945
@ IRT_LE
Less ( )
Definition int.hh:944
@ IPL_BND
Bounds propagation.
Definition int.hh:993
Numerical limits for integer variables.
Definition int.hh:114
Finite domain integers.
Gecode toplevel namespace
IntVar expr(Home home, const LinIntExpr &e, const IntPropLevels &ipls=IntPropLevels::def)
Post linear expression and return its value.
Definition int-expr.cpp:915
void distinct(Home home, const IntVarArgs &x, IntPropLevel ipl=IPL_DEF)
Post propagator for for all .
Definition distinct.cpp:46
IntVarBranch INT_VAR_NONE(void)
Select first unassigned variable.
Definition var.hpp:96
IntValBranch INT_VAL_MIN(void)
Select smallest value.
Definition val.hpp:55
void max(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .