/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ /* * Main authors: * Patrick Pekczynski * Mikael Lagerkvist * Christian Schulte * * Copyright: * Patrick Pekczynski, 2004 * Mikael Lagerkvist, 2006 * Christian Schulte, 2007 * * Last modified: * $Date: 2010-10-07 20:52:01 +1100 (Thu, 07 Oct 2010) $ by $Author: schulte $ * $Revision: 11473 $ * * 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 #include #include using namespace Gecode; /** * \brief Options taking two additional parameters * * \relates LangfordNumber */ class LangfordNumberOptions : public Options { public: int k, n; /// Parameters to be given on the command line /// Initialize options for example with name \a s LangfordNumberOptions(const char* s, int k0, int n0) : Options(s), k(k0), n(n0) {} /// Parse options from arguments \a argv (number is \a argc) void parse(int& argc, char* argv[]) { Options::parse(argc,argv); if (argc < 3) return; n = atoi(argv[1]); k = atoi(argv[2]); } /// Print help message virtual void help(void) { Options::help(); std::cerr << "\t(unsigned int) default: " << n << std::endl << "\t\tparameter n" << std::endl << "\t(unsigned int) default: " << k << std::endl << "\t\tparameter k" << std::endl; } }; /** * \brief %Example: Langford's number problem * * See problem 24 at http://www.csplib.org/. * * \ingroup Example */ class LangfordNumber : public Script { protected: int k, n; ///< Problem parameters IntVarArray y; ///< Sequence variables public: /// Propagation to use for model enum { PROP_REIFIED, ///< Use reified constraints PROP_EXTENSIONAL, ///< Use extensional constraints PROP_EXTENSIONAL_CHANNEL ///< Use extensional and channel constraints }; /// Construct model LangfordNumber(const LangfordNumberOptions& opt) : k(opt.k), n(opt.n), y(*this,k*n,1,n) { switch (opt.propagation()) { case PROP_REIFIED: { // Position of values in sequence IntVarArgs pv(*this,k*n,0,k*n-1); Matrix p(pv,n,k); /* * The occurences of v in the Langford sequence are v numbers apart. * * Let \#(i, v) denote the position of the i-th occurence of * value v in the Langford Sequence. Then * * \f$ \forall i, j \in \{1, \dots, k\}, i \neq j: * \forall v \in \{1, \dots, n\}: \#(i, v) + (v + 1) = \#(j, v)\f$ * */ for (int i=0; i values for (int i=0; i 1) a[v-2]=v-1; REG ra(a), rv(v); extensional(*this, y, *ra+rv+(ra(v,v)+rv)(k-1,k-1)+*ra); } } break; case PROP_EXTENSIONAL_CHANNEL: { // Boolean variables for channeling BoolVarArgs bv(*this,k*n*n,0,1); Matrix b(bv,k*n,n); // Post channel constraints for (int i=0; i opt.n) { std::cerr << "n must be at least k!" << std::endl; return 1; } Script::run(opt); return 0; } // STATISTICS: example-any