Generated on Wed Nov 1 15:04:28 2006 for Gecode by doxygen 1.4.5

photo.cc

Go to the documentation of this file.
00001 /*
00002  *  Main authors:
00003  *     Christian Schulte <schulte@gecode.org>
00004  *
00005  *  Copyright:
00006  *     Christian Schulte, 2001
00007  *
00008  *  Last modified:
00009  *     $Date: 2006-08-04 16:06:52 +0200 (Fri, 04 Aug 2006) $ by $Author: schulte $
00010  *     $Revision: 3517 $
00011  *
00012  *  This file is part of Gecode, the generic constraint
00013  *  development environment:
00014  *     http://www.gecode.org
00015  *
00016  *  See the file "LICENSE" for information on usage and
00017  *  redistribution of this file, and for a
00018  *     DISCLAIMER OF ALL WARRANTIES.
00019  *
00020  */
00021 
00022 #include "examples/support.hh"
00023 #include "gecode/minimodel.hh"
00024 
00026 class PhotoSpec {
00027 public:
00028   const int  n_names; 
00029   const int  n_prefs; 
00030   const int* prefs;   
00031   PhotoSpec(const int n_n, const int n_p, const int* p)
00032     : n_names(n_n), n_prefs(n_p), prefs(p) {}
00033 };
00034 
00036 static const int s_prefs[] = {
00037   0,2, 1,4, 2,3, 2,4, 3,0, 4,3, 4,0, 4,1
00038 };
00040 static const PhotoSpec p_small(5, 8, s_prefs);
00041 
00043 static const int l_prefs[] = {
00044   0,2, 0,4, 0,7, 1,4, 1,8, 2,3, 2,4, 3,0, 3,4,
00045   4,5, 4,0, 5,0, 5,8, 6,2, 6,7, 7,8, 7,6
00046 };
00048 static const PhotoSpec p_large(9,17, l_prefs);
00049 
00061 class Photo : public Example {
00062 protected:
00064   const PhotoSpec& spec;
00066   IntVarArray      pos;
00068   IntVar           sat;
00069 
00070 public:
00072   Photo(const Options& opt) :
00073     spec(opt.size == 0 ? p_small : p_large),
00074     pos(this,spec.n_names, 0, spec.n_names-1),
00075     sat(this,0,spec.n_prefs)
00076   {
00077     BoolVarArgs ful(spec.n_prefs);
00078     // Map preferences to fulfilment
00079     for (int i = spec.n_prefs; i--; ) {
00080       int pa = spec.prefs[2*i+0];
00081       int pb = spec.prefs[2*i+1];
00082       ful[i] = post(this,
00083                     ~(pos[pb]-pos[pa] == 1) ^
00084                     ~(pos[pa]-pos[pb] == 1));
00085     }
00086     // Sum of fulfilment
00087     {
00088       IntVarArgs eq(spec.n_prefs+1);
00089       IntArgs    c(spec.n_prefs+1);
00090       eq[spec.n_prefs] = sat; c[spec.n_prefs] = -1;
00091       for (int i = spec.n_prefs; i--; ) {
00092         eq[i] = ful[i]; c[i] = 1;
00093       }
00094       linear(this, c, eq, IRT_EQ, 0);
00095     }
00096     distinct(this, pos, opt.icl);
00097 
00098     // Break some symmetries
00099     rel(this, pos[0], IRT_LE, pos[1]);
00100 
00101     if (opt.naive) {
00102       branch(this, pos, BVAR_NONE, BVAL_MIN);
00103     } else {
00104       branch(this, pos, BVAR_DEGREE_MAX, BVAL_MIN);
00105     }
00106   }
00107 
00109   Photo(bool share, Photo& s) :
00110     Example(share,s), spec(s.spec) {
00111     pos.update(this, share, s.pos);
00112     sat.update(this, share, s.sat);
00113   }
00114 
00116   virtual Space*
00117   copy(bool share) {
00118     return new Photo(share,*this);
00119   }
00120 
00122   virtual void
00123   print(void) {
00124     std::cout << "\tpos[] = {";
00125     for (int i = 0; i < spec.n_names; i++)
00126       std::cout << pos[i] << ((i<spec.n_names-1)?",":"};\n");
00127     std::cout << "\tsat: " << sat << std::endl;
00128   }
00129 
00131   void
00132   constrain(Space* s) {
00133     rel(this, sat, IRT_GR, static_cast<Photo*>(s)->sat.val());
00134   }
00135 
00136 };
00137 
00141 int
00142 main(int argc, char** argv) {
00143   Options opt("Photo");
00144   opt.solutions  = 0;
00145   opt.size       = 1;
00146   opt.iterations = 10;
00147   opt.icl        = ICL_BND;
00148   opt.parse(argc,argv);
00149   Example::run<Photo,BAB>(opt);
00150   return 0;
00151 }
00152 
00153 
00154 // STATISTICS: example-any
00155