00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00031 #include "examples/support.hh"
00032 #include "gecode/minimodel.hh"
00033
00034 class AllInterval : public Example {
00035 private:
00036 int n;
00037 IntVarArray x;
00038 IntVarArray d;
00039 public:
00040
00046 void adiff_sn(Space* home, IntVarArray& x) {
00047
00048
00049 IntVarArray sn(home, n);
00050
00051 for (int i = n; i--; ) {
00052 sn[i].init(home, i, i);
00053 }
00054
00055 sortedness(home, x, sn);
00056 }
00057
00063 void adiff_sn_star(Space* home, IntVarArray& x) {
00064
00065 IntVarArray snstar(home, n - 1);
00066
00067 for (int i = n - 1; i--; ) {
00068 snstar[i].init(home, i + 1, i + 1);
00069 }
00070 sortedness(home, x, snstar);
00071 }
00072
00078 void difference(Space* home, IntVarArray& x, IntVarArray& d) {
00079
00080 IntVarArray diff(this, n - 1);
00081 for (int i = 0; i < n - 1; i++) {
00082 diff[i].init(this, 1 - n, n - 1);
00083 }
00084
00085 for (int i = 0; i < n - 1; i++) {
00086 post(this, x[i + 1] - x[i] == diff[i]);
00087 abs(this, diff[i], d[i]);
00088 }
00089 }
00090
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111 void break_negation(Space* home, IntVarArray& x) {
00112 rel(home, x[0], IRT_LE, x[1]);
00113 }
00114
00115
00116
00117
00118
00119
00120
00121
00122 void break_reversal(Space* home, IntVarArray& d) {
00123 rel(home, d[0], IRT_LE, d[n - 2]);
00124 }
00126
00127 AllInterval(const Options& op) :
00128 n(op.size),
00129 x(this, n),
00130 d(this, n - 1) {
00131
00132 IntSet dom_zn (0, n - 1);
00133 IntSet dom_zns (1, n - 1);
00134
00135
00136 for (int i = 0; i < n; i++) {
00137 x[i].init(this, dom_zn);
00138 if (i < n - 1)
00139 d[i].init(this, dom_zns);
00140 }
00141
00142 difference(this, x, d);
00143
00144
00145 break_negation(this, x);
00146
00147
00148 break_reversal(this, d);
00149
00150
00151 adiff_sn(this, x);
00152 adiff_sn_star(this, d);
00153
00154 branch(this, x, BVAR_SIZE_MIN, BVAL_MIN);
00155
00156 }
00157
00158 AllInterval(bool share, AllInterval& a)
00159 : Example(share, a),
00160 n(a.n) {
00161 x.update(this, share, a.x);
00162 d.update(this, share, a.d);
00163 }
00164
00165 virtual Space*
00166 copy(bool share) {
00167 return new AllInterval(share, *this);
00168 }
00169
00170 virtual void print(void){
00171 std::cout << "Sol:\n";
00172 std::cout << "x: ";
00173 for (int i = 0; i < n; i++) {
00174 std::cout << x[i] << " ";
00175 }
00176 std::cout <<"\n";
00177
00178 std::cout << "d: ";
00179 for (int i = 0; i < n - 1; i++) {
00180 std::cout << d[i] << " ";
00181 }
00182 std::cout <<"\n";
00183
00184 }
00185 };
00186
00187
00188 int main(int argc, char** argv){
00189 Options opt("All-Interval Series ");
00190 opt.size = 12;
00191 opt.parse(argc, argv);
00192 if (opt.size < 2) {
00193 std::cerr << "n must be at least 2!" << std::endl;
00194 return -1;
00195 }
00196 Example::run<AllInterval, DFS>(opt);
00197 return 0;
00198 }
00199
00200
00201