Generated on Thu Jan 16 2025 00:00:00 for Gecode by doxygen 1.14.0
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
40using namespace Gecode;
41
59class MagicSequence : public Script {
60private:
62 const int n;
65public:
67 enum {
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 }
92
93 virtual Space*
94 copy(void) {
95 return new MagicSequence(*this);
96 }
97
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
114int
115main(int argc, char* argv[]) {
116 SizeOptions opt("MagicSequence");
117 opt.solutions(0);
118 opt.iterations(4);
119 opt.size(500);
120 opt.propagation(MagicSequence::PROP_COUNT);
121 opt.propagation(MagicSequence::PROP_COUNT, "count");
122 opt.propagation(MagicSequence::PROP_GCC, "gcc");
123 opt.parse(argc,argv);
125 return 0;
126}
127
128// STATISTICS: example-any
129
static void run(const Options &opt, Script *s=NULL)
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:772
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 sequence.
MagicSequence(MagicSequence &e)
Constructor for cloning e.
@ PROP_COUNT
Use count constraints.
@ PROP_GCC
Use single global cardinality constraint.
MagicSequence(const SizeOptions &opt)
The actual model.
void parse(int argc, char *argv[])
Parse commandline arguments.
Definition test.cpp:120
Driver::ScriptBase< Driver::IgnoreStepOption< Space > > Script
Base-class for scripts.
Definition driver.hh:801
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 linear(Home home, const FloatVarArgs &x, FloatRelType frt, FloatVal c)
Post propagator for .
Definition linear.cpp:41
@ IRT_EQ
Equality ( )
Definition int.hh:941
Gecode toplevel namespace
IntVarBranch INT_VAR_NONE(void)
Select first unassigned variable.
Definition var.hpp:96
IntValBranch INT_VAL_MAX(void)
Select largest value.
Definition val.hpp:65