Generated on Mon May 5 05:53:49 2008 for Gecode by doxygen 1.5.5

example.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, 2004
00008  *
00009  *  Last modified:
00010  *     $Date: 2008-01-23 17:23:28 +0100 (Wed, 23 Jan 2008) $ by $Author: tack $
00011  *     $Revision: 5957 $
00012  *
00013  *  This file is part of Gecode, the generic constraint
00014  *  development environment:
00015  *     http://www.gecode.org
00016  *
00017  *
00018  *  Permission is hereby granted, free of charge, to any person obtaining
00019  *  a copy of this software and associated documentation files (the
00020  *  "Software"), to deal in the Software without restriction, including
00021  *  without limitation the rights to use, copy, modify, merge, publish,
00022  *  distribute, sublicense, and/or sell copies of the Software, and to
00023  *  permit persons to whom the Software is furnished to do so, subject to
00024  *  the following conditions:
00025  *
00026  *  The above copyright notice and this permission notice shall be
00027  *  included in all copies or substantial portions of the Software.
00028  *
00029  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00030  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00031  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00032  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00033  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00034  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00035  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00036  *
00037  */
00038 
00039 #include "examples/support.hh"
00040 
00041 double
00042 am(double t[], int n) {
00043   if (n < 1)
00044     return 0.0;
00045   double s = 0;
00046   for (int i=n; i--; )
00047     s += t[i];
00048   return s / n;
00049 }
00050 
00051 double
00052 dev(double t[], int n) {
00053   if (n < 2)
00054     return 0.0;
00055   double m = am(t,n);
00056   double s = 0.0;
00057   for (int i=n; i--; ) {
00058     double d = t[i]-m;
00059     s += d*d;
00060   }
00061   return sqrt(s / (n-1)) / m;
00062 }
00063 
00064 void
00065 Example::sac_collect_vars() {
00066   // Collect variables in VarMap
00067   Reflection::VarMap vm;
00068   for (Reflection::ActorSpecIter si(this, vm); si(); ++si) {}
00069   // Enter collected variables into variable arrays
00070   for (Reflection::VarMapIter vmi(vm); vmi(); ++vmi) {
00071     if (vmi.spec().vti() == Int::BoolVarImp::vti) {
00072       BoolVar bv = vmi.var();
00073       _sac_bva.add(this, bv);
00074     } else if (vmi.spec().vti() == Int::IntVarImp::vti) {
00075       IntVar iv = vmi.var();
00076       _sac_iva.add(this, iv);
00077     }
00078   }
00079 }
00080 
00081 void
00082 Example::sac_remove_vars() {
00083   // ToDo: Empty _sac_iva and _sac_bva since they are no longer needed.
00084 }
00085 
00086 bool
00087 Example::sac(unsigned long int& p) {
00088   if (status(p) == SS_FAILED) return false;
00089 
00090   bool modified = false;
00091 
00092   // Process all Boolean variables
00093   for (int i = _sac_bva.size(); i--; ) {
00094     for (int val = 0; val <= 1; ++val) {
00095       if (_sac_bva[i].assigned()) break;
00096       Example* e = static_cast<Example*>(this->clone());
00097       rel(e, e->_sac_bva[i], IRT_EQ, val);
00098       if (e->status(p) == SS_FAILED) {
00099         modified = true;
00100         rel(this, this->_sac_bva[i], IRT_NQ, val);
00101         if (status(p) == SS_FAILED)
00102           return false;
00103       }
00104       delete e;
00105     }
00106   }
00107 
00108   // Process all integer variables
00109   for (int i = _sac_iva.size(); i--; ) {
00110     if (_sac_iva[i].assigned()) continue;
00111     IntArgs nq(_sac_iva[i].size());
00112     int nnq = 0;
00113     IntVarValues vv(_sac_iva[i]);
00114     while (vv()) {
00115       Example* e = static_cast<Example*>(this->clone());
00116       rel(e, e->_sac_iva[i], IRT_EQ, vv.val());
00117       if (e->status() == SS_FAILED) {
00118         nq[nnq++] = vv.val();
00119       }
00120       delete e;
00121       ++vv;
00122     }
00123     if (nnq > 0) modified = true;
00124     for (; nnq--; ) {
00125       rel(this, _sac_iva[i], IRT_NQ, nq[nnq]);
00126     }
00127     if (status(p) == SS_FAILED)
00128       return false;
00129   }
00130 
00131   return modified;
00132 }
00133 
00134 // STATISTICS: example-any