Generated on Thu Mar 22 10:39:38 2012 for Gecode by doxygen 1.6.3

post.hpp

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, 2002
00008  *
00009  *  Last modified:
00010  *     $Date: 2011-08-12 15:27:20 +0200 (Fri, 12 Aug 2011) $ by $Author: tack $
00011  *     $Revision: 12285 $
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 <algorithm>
00039 #include <climits>
00040 
00041 namespace Gecode { namespace Int { namespace Linear {
00042 
00043   template<class View>
00044   inline void
00045   estimate(Term<View>* t, int n, int c, int& l, int &u) {
00046     double min = c;
00047     double max = c;
00048     for (int i=n; i--; ) {
00049       double a = t[i].a;
00050       if (a > 0) {
00051         min += a*t[i].x.min();
00052         max += a*t[i].x.max();
00053       } else {
00054         max += a*t[i].x.min();
00055         min += a*t[i].x.max();
00056       }
00057     }
00058     if (min < Limits::min)
00059       min = Limits::min;
00060     if (min > Limits::max)
00061       min = Limits::max;
00062     l = static_cast<int>(min);
00063     if (max < Limits::min)
00064       max = Limits::min;
00065     if (max > Limits::max)
00066       max = Limits::max;
00067     u = static_cast<int>(max);
00068   }
00069 
00071   template<class View>
00072   class TermLess {
00073   public:
00074     forceinline bool
00075     operator ()(const Term<View>& a, const Term<View>& b) {
00076       return before(a.x,b.x);
00077     }
00078   };
00079 
00080   template<class View>
00081   inline bool
00082   normalize(Term<View>* t, int &n,
00083             Term<View>* &t_p, int &n_p,
00084             Term<View>* &t_n, int &n_n) {
00085     /*
00086      * Join coefficients for aliased variables:
00087      *
00088      */
00089     {
00090       // Group same variables
00091       TermLess<View> tl;
00092       Support::quicksort<Term<View>,TermLess<View> >(t,n,tl);
00093 
00094       // Join adjacent variables
00095       int i = 0;
00096       int j = 0;
00097       while (i < n) {
00098         Limits::check(t[i].a,"Int::linear");
00099         double a = t[i].a;
00100         View x = t[i].x;
00101         while ((++i < n) && same(t[i].x,x)) {
00102           a += t[i].a;
00103           Limits::check(a,"Int::linear");
00104         }
00105         if (a != 0.0) {
00106           t[j].a = static_cast<int>(a); t[j].x = x; j++;
00107         }
00108       }
00109       n = j;
00110     }
00111 
00112     /*
00113      * Partition into positive/negative coefficents
00114      *
00115      */
00116     if (n > 0) {
00117       int i = 0;
00118       int j = n-1;
00119       while (true) {
00120         while ((t[j].a < 0) && (--j >= 0)) ;
00121         while ((t[i].a > 0) && (++i <  n)) ;
00122         if (j <= i) break;
00123         std::swap(t[i],t[j]);
00124       }
00125       t_p = t;     n_p = i;
00126       t_n = t+n_p; n_n = n-n_p;
00127     } else {
00128       t_p = t; n_p = 0;
00129       t_n = t; n_n = 0;
00130     }
00131 
00132     /*
00133      * Make all coefficients positive
00134      *
00135      */
00136     for (int i=n_n; i--; )
00137       t_n[i].a = -t_n[i].a;
00138 
00139     /*
00140      * Test for unit coefficients only
00141      *
00142      */
00143     for (int i=n; i--; )
00144       if (t[i].a != 1)
00145         return false;
00146     return true;
00147   }
00148 
00149 }}}
00150 
00151 // STATISTICS: int-post
00152