[gecode-users] Avoid the appearance of several simultaneous values

inspecteur inspecteur.rick at gmail.com
Tue Dec 24 15:59:21 CET 2013


Hi
Sorry for my english, I'm french and beginner on gecode.

I want to create an array of integers (4 * 4) with the constraints:
1) Possible valuesbetween 1 and 6
2) no repeating values on lines
3) The values 3 and 4 must not be on the same line
4) No more than 2 times the same value on each column

I stumble on the constraint number 3)

If someone could put me on the track


Here is my code

class CarreMagique : public Script {
private:
   const int n;
   IntVarArray carreMag;

   public:
   CarreMagique(const SizeOptions& opt)
     : n(opt.size()), carreMag(*this,n*4,1,6) {

     Matrix<IntVarArray> tab(carreMag, n, 4);
     IntVarArgs d1(4);
         for (int i = 0; i < n; i++) {
             for (int j = 0; j < 4; j++) {
                 d1[j]=tab(i,j);
             }
             distinct(*this, d1);
         }

   IntVarArgs d2(n);
         for (int j = 0; j < 4; j++) {
             for (int i = 0; i < n; i++) {
                 d2[i]=tab(i,j);
             }
             count(*this, d2, IntSet(0,2), IntArgs::create(6,1,1));
         }

     }
     branch(*this, carreMag, INT_VAR_NONE(), INT_VAL_SPLIT_MIN());
   }

   CarreMagique(bool share, CarreMagique& s) : Script(share,s), n(s.n) {
     carreMag.update(*this, share, s.carreMag);
   }

   virtual Space* copy(bool share) {
     return new CarreMagique(share,*this);
   }

   virtual void print(std::ostream& os) const {
     // Pour acceder au tableau comme à une matrice
     Matrix<IntVarArray> m(carreMag, n, 4);
     for (int i = 0; i < n; i++) {
       os << "\t";
       for (int j = 0; j < 4; j++) {
         os.width(2);
         os << m(i,j) << "  ";
       }
       os << std::endl;
     }
   }

};

int main(int argc, char* argv[]) {
   SizeOptions opt("CarreMagique");
   opt.size(4);
   Script::run<CarreMagique,DFS,SizeOptions>(opt);
   return 0;
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.gecode.org/pipermail/users/attachments/20131224/91e4748c/attachment.html>


More information about the users mailing list