magic-sequence.cpp
Go to the documentation of this file.
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
* Main authors:
* Christian Schulte <schulte@gecode.org>
* Guido Tack <tack@gecode.org>
*
* Copyright:
* Christian Schulte, 2001
* Guido Tack, 2006
*
* This file is part of Gecode, the generic constraint
* development environment:
* http://www.gecode.org
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
#include <gecode/driver.hh>
#include <gecode/int.hh>
#include <gecode/minimodel.hh>
using namespace Gecode;
class MagicSequence : public Script {
private:
const int n;
IntVarArray s;
public:
enum {
PROP_COUNT,
PROP_GCC
};
MagicSequence(const SizeOptions& opt)
: Script(opt), n(opt.size()), s(*this,n,0,n-1) {
switch (opt.propagation()) {
case PROP_COUNT:
for (int i=n; i--; )
count(*this, s, i, IRT_EQ, s[i]);
linear(*this, s, IRT_EQ, n);
break;
case PROP_GCC:
count(*this, s, s, opt.ipl());
break;
}
linear(*this, IntArgs::create(n,-1,1), s, IRT_EQ, 0);
branch(*this, s, INT_VAR_NONE(), INT_VAL_MAX());
}
MagicSequence(MagicSequence& e) : Script(e), n(e.n) {
s.update(*this, e.s);
}
virtual Space*
copy(void) {
return new MagicSequence(*this);
}
virtual
void print(std::ostream& os) const {
os << "\t";
for (int i = 0; i<n; i++) {
os << s[i] << ", ";
if ((i+1) % 20 == 0)
os << std::endl << "\t";
}
os << std::endl;
}
};
int
main(int argc, char* argv[]) {
SizeOptions opt("MagicSequence");
opt.solutions(0);
opt.iterations(4);
opt.size(500);
opt.propagation(MagicSequence::PROP_COUNT);
opt.propagation(MagicSequence::PROP_COUNT, "count");
opt.propagation(MagicSequence::PROP_GCC, "gcc");
opt.parse(argc,argv);
Script::run<MagicSequence,DFS,SizeOptions>(opt);
return 0;
}
// STATISTICS: example-any
Gecode::Driver::ScriptBase< Driver::IgnoreStepOption< Space > >::run
static void run(const Options &opt, Script *s=NULL)
Gecode::IntArgs::create
static IntArgs create(int n, int start, int inc=1)
Allocate array with n elements such that for all .
Definition array.hpp:76
Gecode::IntVarArray
Integer variable array.
Definition int.hh:763
Gecode::SizeOptions
Options for scripts with additional size parameter
Definition driver.hh:675
Gecode::Space
Computation spaces.
Definition core.hpp:1742
MagicSequence::main
int main(int argc, char *argv[])
Main-function.
Definition magic-sequence.cpp:115
MagicSequence::copy
virtual Space * copy(void)
Copy during cloning.
Definition magic-sequence.cpp:94
MagicSequence::print
virtual void print(std::ostream &os) const
Print sequence.
Definition magic-sequence.cpp:99
MagicSequence::MagicSequence
MagicSequence(MagicSequence &e)
Constructor for cloning e.
Definition magic-sequence.cpp:89
MagicSequence::PROP_COUNT
@ PROP_COUNT
Use count constraints.
Definition magic-sequence.cpp:68
MagicSequence::PROP_GCC
@ PROP_GCC
Use single global cardinality constraint.
Definition magic-sequence.cpp:69
MagicSequence::MagicSequence
MagicSequence(const SizeOptions &opt)
The actual model.
Definition magic-sequence.cpp:72
Test::Options::parse
void parse(int argc, char *argv[])
Parse commandline arguments.
Definition test.cpp:120
driver.hh
int.hh
Gecode::Script
Driver::ScriptBase< Driver::IgnoreStepOption< Space > > Script
Base-class for scripts.
Definition driver.hh:801
Gecode::branch
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
Gecode::linear
void linear(Home home, const FloatVarArgs &x, FloatRelType frt, FloatVal c)
Post propagator for .
Definition linear.cpp:41
Gecode::IRT_EQ
@ IRT_EQ
Equality ( ).
Definition int.hh:926
minimodel.hh
Gecode
Gecode toplevel namespace
Gecode::INT_VAR_NONE
IntVarBranch INT_VAR_NONE(void)
Select first unassigned variable.
Definition var.hpp:96
Gecode::INT_VAL_MAX
IntValBranch INT_VAL_MAX(void)
Select largest value.
Definition val.hpp:65