File: langford-number.cpp

package info (click to toggle)
gecode-snapshot 6.2.0%2Bgit20240207-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 35,308 kB
  • sloc: cpp: 475,516; perl: 2,077; makefile: 1,816; sh: 198
file content (214 lines) | stat: -rw-r--r-- 6,439 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
 *  Main authors:
 *     Patrick Pekczynski <pekczynski@ps.uni-sb.de>
 *     Mikael Lagerkvist <lagerkvist@gecode.org>
 *     Christian Schulte <schulte@gecode.org>
 *
 *  Copyright:
 *     Patrick Pekczynski, 2004
 *     Mikael Lagerkvist, 2006
 *     Christian Schulte, 2007
 *
 *  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;

/**
 * \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)
    : Script(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<IntVarArgs> p(pv,n,k);

        /*
         * The occurrences of v in the Langford sequence are v numbers apart.
         *
         * Let \#(i, v) denote the position of the i-th occurrence 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<n; i++)
          for (int j=0; j<k-1; j++)
            rel(*this, p(i,j)+i+2 == p(i,j+1));

        distinct(*this, pv, opt.ipl());

        // Channel positions <-> values
        for (int i=0; i<n; i++)
          for (int j=0; j<k; j++)
            element(*this, y, p(i,j), i+1);
      }
      break;
    case PROP_EXTENSIONAL:
      {
        IntArgs a(n-1);
        for (int v=2; v<=n; v++)
          a[v-2]=v;
        for (int v=1; v<=n; v++) {
          // Construct regular expression for all symbols but v
          if (v > 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<BoolVarArgs> b(bv,k*n,n);

        // Post channel constraints
        for (int i=0; i<n*k; i++)
          channel(*this, b.col(i), y[i], 1);

        // For placing two numbers three steps apart, we construct the
        // regular expression 0*100010*, and apply it to the projection of
        // the sequence on the value.
        REG r0(0), r1(1);
        for (int v=1; v<=n; v++)
          extensional(*this, b.row(v-1),
                      *r0 + r1 + (r0(v,v) + r1)(k-1,k-1) + *r0);
      }
      break;
    }

    // Symmetry breaking
    rel(*this, y[0], IRT_LE, y[n*k-1]);

    // Branching
    branch(*this, y, INT_VAR_SIZE_MIN(), INT_VAL_MAX());
  }

  /// Print solution
  virtual void print(std::ostream& os) const {
    os << "\t" << y << std::endl;
  }

  /// Constructor for cloning \a l
  LangfordNumber(LangfordNumber& l)
    : Script(l), k(l.k), n(l.n) {
    y.update(*this, l.y);

  }
  /// Copy during cloning
  virtual Space*
  copy(void) {
    return new LangfordNumber(*this);
  }
};


/** \brief Main-function
 *  \relates LangfordNumber
 */
int
main(int argc, char* argv[]) {
  LangfordNumberOptions opt("Langford Numbers",3,9);
  opt.ipl(IPL_DOM);
  opt.propagation(LangfordNumber::PROP_EXTENSIONAL_CHANNEL);
  opt.propagation(LangfordNumber::PROP_REIFIED,
                  "reified");
  opt.propagation(LangfordNumber::PROP_EXTENSIONAL,
                  "extensional");
  opt.propagation(LangfordNumber::PROP_EXTENSIONAL_CHANNEL,
                  "extensional-channel");
  opt.parse(argc, argv);
  if (opt.k < 1) {
    std::cerr << "k must be at least 1!" << std::endl;
    return 1;
  }
  if (opt.k > opt.n) {
    std::cerr << "n must be at least k!" << std::endl;
    return 1;
  }
  Script::run<LangfordNumber,DFS,LangfordNumberOptions>(opt);
  return 0;
}

// STATISTICS: example-any