Generated on Thu Apr 11 13:59:05 2019 for Gecode by doxygen 1.6.3

circuit.cpp

Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
00002 /*
00003  *  Main authors:
00004  *     Christian Schulte <schulte@gecode.org>
00005  *     Guido Tack <tack@gecode.org>
00006  *
00007  *  Copyright:
00008  *     Christian Schulte, 2006
00009  *     Guido Tack, 2011
00010  *
00011  *  This file is part of Gecode, the generic constraint
00012  *  development environment:
00013  *     http://www.gecode.org
00014  *
00015  *  Permission is hereby granted, free of charge, to any person obtaining
00016  *  a copy of this software and associated documentation files (the
00017  *  "Software"), to deal in the Software without restriction, including
00018  *  without limitation the rights to use, copy, modify, merge, publish,
00019  *  distribute, sublicense, and/or sell copies of the Software, and to
00020  *  permit persons to whom the Software is furnished to do so, subject to
00021  *  the following conditions:
00022  *
00023  *  The above copyright notice and this permission notice shall be
00024  *  included in all copies or substantial portions of the Software.
00025  *
00026  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00027  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00028  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00029  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00030  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00031  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00032  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00033  *
00034  */
00035 
00036 #include <gecode/int/circuit.hh>
00037 
00038 namespace Gecode {
00039 
00040   void
00041   circuit(Home home, int offset, const IntVarArgs& x, IntPropLevel ipl) {
00042     Int::Limits::nonnegative(offset,"Int::circuit");
00043     if (x.size() == 0)
00044       throw Int::TooFewArguments("Int::circuit");
00045     if (same(x))
00046       throw Int::ArgumentSame("Int::circuit");
00047     GECODE_POST;
00048     ViewArray<Int::IntView> xv(home,x);
00049 
00050     if (offset == 0) {
00051       typedef Int::NoOffset<Int::IntView> NOV;
00052       NOV no;
00053       if (vbd(ipl) == IPL_DOM) {
00054         GECODE_ES_FAIL((Int::Circuit::Dom<Int::IntView,NOV>
00055                         ::post(home,xv,no)));
00056       } else {
00057         GECODE_ES_FAIL((Int::Circuit::Val<Int::IntView,NOV>
00058                         ::post(home,xv,no)));
00059       }
00060     } else {
00061       typedef Int::Offset OV;
00062       OV off(-offset);
00063       if (vbd(ipl) == IPL_DOM) {
00064         GECODE_ES_FAIL((Int::Circuit::Dom<Int::IntView,OV>
00065                         ::post(home,xv,off)));
00066       } else {
00067         GECODE_ES_FAIL((Int::Circuit::Val<Int::IntView,OV>
00068                         ::post(home,xv,off)));
00069       }
00070     }
00071   }
00072   void
00073   circuit(Home home, const IntVarArgs& x, IntPropLevel ipl) {
00074     circuit(home,0,x,ipl);
00075   }
00076 
00077   void
00078   circuit(Home home, const IntArgs& c, int offset,
00079           const IntVarArgs& x, const IntVarArgs& y, IntVar z,
00080           IntPropLevel ipl) {
00081     Int::Limits::nonnegative(offset,"Int::circuit");
00082     int n = x.size();
00083     if (n == 0)
00084       throw Int::TooFewArguments("Int::circuit");
00085     if (same(x))
00086       throw Int::ArgumentSame("Int::circuit");
00087     if ((y.size() != n) || (c.size() != n*n))
00088       throw Int::ArgumentSizeMismatch("Int::circuit");
00089     circuit(home, offset, x, ipl);
00090     GECODE_POST;
00091     IntArgs cx(offset+n);
00092     for (int i=0; i<offset; i++)
00093       cx[i] = 0;
00094     for (int i=0; i<n; i++) {
00095       for (int j=0; j<n; j++)
00096         cx[offset+j] = c[i*n+j];
00097       element(home, cx, x[i], y[i]);
00098     }
00099     linear(home, y, IRT_EQ, z);
00100   }
00101   void
00102   circuit(Home home, const IntArgs& c,
00103           const IntVarArgs& x, const IntVarArgs& y, IntVar z,
00104           IntPropLevel ipl) {
00105     circuit(home,c,0,x,y,z,ipl);
00106   }
00107   void
00108   circuit(Home home, const IntArgs& c, int offset,
00109           const IntVarArgs& x, IntVar z,
00110           IntPropLevel ipl) {
00111     Int::Limits::nonnegative(offset,"Int::circuit");
00112     GECODE_POST;
00113     IntVarArgs y(home, x.size(), Int::Limits::min, Int::Limits::max);
00114     circuit(home, c, offset, x, y, z, ipl);
00115   }
00116   void
00117   circuit(Home home, const IntArgs& c,
00118           const IntVarArgs& x, IntVar z,
00119           IntPropLevel ipl) {
00120     circuit(home,c,0,x,z,ipl);
00121   }
00122 
00123   void
00124   path(Home home, int offset, const IntVarArgs& x, IntVar s, IntVar e,
00125        IntPropLevel ipl) {
00126     Int::Limits::nonnegative(offset,"Int::path");
00127     int n=x.size();
00128     if (n == 0)
00129       throw Int::TooFewArguments("Int::path");
00130     if (same(x))
00131       throw Int::ArgumentSame("Int::path");
00132     GECODE_POST;
00133     ViewArray<Int::IntView> xv(home,n+1);
00134     for (int i=0; i<n; i++)
00135       xv[i] = Int::IntView(x[i]);
00136     xv[n] = s;
00137 
00138     if (offset == 0) {
00139       element(home, x, e, n);
00140       typedef Int::NoOffset<Int::IntView> NOV;
00141       NOV no;
00142       if (vbd(ipl) == IPL_DOM) {
00143         GECODE_ES_FAIL((Int::Circuit::Dom<Int::IntView,NOV>
00144                         ::post(home,xv,no)));
00145       } else {
00146         GECODE_ES_FAIL((Int::Circuit::Val<Int::IntView,NOV>
00147                         ::post(home,xv,no)));
00148       }
00149     } else {
00150       IntVarArgs ox(n+offset);
00151       IntVar y(home, -1,-1);
00152       for (int i=0; i<offset; i++)
00153         ox[i] = y;
00154       for (int i=0; i<n; i++)
00155         ox[offset + i] = x[i];
00156       element(home, ox, e, offset+n);
00157       typedef Int::Offset OV;
00158       OV off(-offset);
00159       if (vbd(ipl) == IPL_DOM) {
00160         GECODE_ES_FAIL((Int::Circuit::Dom<Int::IntView,OV>
00161                         ::post(home,xv,off)));
00162       } else {
00163         GECODE_ES_FAIL((Int::Circuit::Val<Int::IntView,OV>
00164                         ::post(home,xv,off)));
00165       }
00166     }
00167   }
00168   void
00169   path(Home home, const IntVarArgs& x, IntVar s, IntVar e,
00170        IntPropLevel ipl) {
00171     path(home,0,x,s,e,ipl);
00172   }
00173 
00174   void
00175   path(Home home, const IntArgs& c, int offset,
00176        const IntVarArgs& x, IntVar s, IntVar e,
00177        const IntVarArgs& y, IntVar z,
00178        IntPropLevel ipl) {
00179     Int::Limits::nonnegative(offset,"Int::path");
00180     int n = x.size();
00181     if (n == 0)
00182       throw Int::TooFewArguments("Int::path");
00183     if (same(x))
00184       throw Int::ArgumentSame("Int::path");
00185     if ((y.size() != n) || (c.size() != n*n))
00186       throw Int::ArgumentSizeMismatch("Int::path");
00187     GECODE_POST;
00188     path(home, offset, x, s, e, ipl);
00189     IntArgs cx(offset+n+1);
00190     for (int i=0; i<offset; i++)
00191       cx[i] = 0;
00192     cx[offset+n] = 0;
00193     for (int i=0; i<n; i++) {
00194       for (int j=0; j<n; j++)
00195         cx[offset+j] = c[i*n+j];
00196       element(home, cx, x[i], y[i]);
00197     }
00198     linear(home, y, IRT_EQ, z);
00199   }
00200   void
00201   path(Home home, const IntArgs& c,
00202        const IntVarArgs& x, IntVar s, IntVar e,
00203        const IntVarArgs& y, IntVar z,
00204        IntPropLevel ipl) {
00205     path(home,c,0,x,s,e,y,z,ipl);
00206   }
00207   void
00208   path(Home home, const IntArgs& c, int offset,
00209        const IntVarArgs& x, IntVar s, IntVar e, IntVar z,
00210        IntPropLevel ipl) {
00211     Int::Limits::nonnegative(offset,"Int::path");
00212     GECODE_POST;
00213     IntVarArgs y(home, x.size(), Int::Limits::min, Int::Limits::max);
00214     path(home, c, offset, x, s, e, y, z, ipl);
00215   }
00216   void
00217   path(Home home, const IntArgs& c,
00218        const IntVarArgs& x, IntVar s, IntVar e, IntVar z,
00219        IntPropLevel ipl) {
00220     path(home,c,0,x,s,e,z,ipl);
00221   }
00222 
00223 }
00224 
00225 // STATISTICS: int-post