Generated on Mon Aug 25 11:35:33 2008 for Gecode by doxygen 1.5.6

queens.cc

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  *
00006  *  Copyright:
00007  *     Christian Schulte, 2001
00008  *
00009  *  Last modified:
00010  *     $Date: 2008-01-08 16:09:27 +0100 (Tue, 08 Jan 2008) $ by $Author: nikopp $
00011  *     $Revision: 5789 $
00012  *
00013  *  This file is part of Gecode, the generic constraint
00014  *  development environment:
00015  *     http://www.gecode.org
00016  *
00017  *  Permission is hereby granted, free of charge, to any person obtaining
00018  *  a copy of this software and associated documentation files (the
00019  *  "Software"), to deal in the Software without restriction, including
00020  *  without limitation the rights to use, copy, modify, merge, publish,
00021  *  distribute, sublicense, and/or sell copies of the Software, and to
00022  *  permit persons to whom the Software is furnished to do so, subject to
00023  *  the following conditions:
00024  *
00025  *  The above copyright notice and this permission notice shall be
00026  *  included in all copies or substantial portions of the Software.
00027  *
00028  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00029  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00030  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00031  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00032  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00033  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00034  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00035  *
00036  */
00037 
00038 #include "examples/support.hh"
00039 #include "gecode/minimodel.hh"
00040 
00050 class Queens : public Example {
00051 protected:
00053   IntVarArray q;
00054 public:
00056   enum {
00057     PROP_BINARY,  
00058     PROP_MIXED,   
00059     PROP_DISTINCT 
00060   };
00062   Queens(const SizeOptions& opt)
00063     : q(this,opt.size(),0,opt.size()-1) {
00064     const int n = q.size();
00065     switch (opt.propagation()) {
00066     case PROP_BINARY:
00067       for (int i = 0; i<n; i++)
00068         for (int j = i+1; j<n; j++) {
00069           post(this, q[i] != q[j]);
00070           post(this, q[i]+i != q[j]+j);
00071           post(this, q[i]-i != q[j]-j);
00072         }
00073       break;
00074     case PROP_MIXED:
00075       for (int i = 0; i<n; i++)
00076         for (int j = i+1; j<n; j++) {
00077           post(this, q[i]+i != q[j]+j);
00078           post(this, q[i]-i != q[j]-j);
00079         }
00080       distinct(this, q, opt.icl());
00081       break;
00082     case PROP_DISTINCT:
00083       {
00084         IntArgs c(n);
00085         for (int i = n; i--; ) c[i] = i;
00086         distinct(this, c, q, opt.icl());
00087         for (int i = n; i--; ) c[i] = -i;
00088         distinct(this, c, q, opt.icl());
00089       }
00090       distinct(this, q, opt.icl());
00091       break;
00092     }
00093     branch(this, q, INT_VAR_SIZE_MIN, INT_VAL_MIN);
00094   }
00095 
00097   Queens(bool share, Queens& s) : Example(share,s) {
00098     q.update(this, share, s.q);
00099   }
00100 
00102   virtual Space*
00103   copy(bool share) {
00104     return new Queens(share,*this);
00105   }
00106 
00108   virtual void
00109   print(std::ostream& os) {
00110     os << "\t";
00111     for (int i = 0; i < q.size(); i++) {
00112       os << q[i] << ", ";
00113       if ((i+1) % 10 == 0)
00114         os << std::endl << "\t";
00115     }
00116     os << std::endl;
00117   }
00118   
00120   virtual void
00121   getVars(Gecode::Reflection::VarMap& vm, bool registerOnly) {
00122     vm.putArray(this,q,"q", registerOnly);
00123   }
00124 };
00125 
00129 int
00130 main(int argc, char* argv[]) {
00131   SizeOptions opt("Queens");
00132   opt.iterations(500);
00133   opt.size(100);
00134   opt.propagation(Queens::PROP_DISTINCT);
00135   opt.propagation(Queens::PROP_BINARY, "binary",
00136                       "only binary disequality constraints");
00137   opt.propagation(Queens::PROP_MIXED, "mixed",
00138                       "single distinct and binary disequality constraints");
00139   opt.propagation(Queens::PROP_DISTINCT, "distinct",
00140                       "three distinct constraints");
00141   opt.parse(argc,argv);
00142   Example::run<Queens,DFS,SizeOptions>(opt);
00143   return 0;
00144 }
00145 
00146 // STATISTICS: example-any
00147