Generated on Thu Apr 11 13:59:06 2019 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  *  This file is part of Gecode, the generic constraint
00010  *  development environment:
00011  *     http://www.gecode.org
00012  *
00013  *  Permission is hereby granted, free of charge, to any person obtaining
00014  *  a copy of this software and associated documentation files (the
00015  *  "Software"), to deal in the Software without restriction, including
00016  *  without limitation the rights to use, copy, modify, merge, publish,
00017  *  distribute, sublicense, and/or sell copies of the Software, and to
00018  *  permit persons to whom the Software is furnished to do so, subject to
00019  *  the following conditions:
00020  *
00021  *  The above copyright notice and this permission notice shall be
00022  *  included in all copies or substantial portions of the Software.
00023  *
00024  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00025  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00026  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00027  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00028  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00029  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00030  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00031  *
00032  */
00033 
00034 #include <algorithm>
00035 #include <climits>
00036 
00037 namespace Gecode { namespace Int { namespace Linear {
00038 
00039   template<class View>
00040   inline void
00041   estimate(Term<View>* t, int n, int c, int& l, int &u) {
00042     long long int min = c;
00043     long long int max = c;
00044     for (int i=0; i<n; i++) {
00045       long long int a = t[i].a;
00046       if (a > 0) {
00047         min += a*t[i].x.min();
00048         max += a*t[i].x.max();
00049       } else {
00050         max += a*t[i].x.min();
00051         min += a*t[i].x.max();
00052       }
00053     }
00054     if (min < Limits::min)
00055       min = Limits::min;
00056     if (min > Limits::max)
00057       min = Limits::max;
00058     l = static_cast<int>(min);
00059     if (max < Limits::min)
00060       max = Limits::min;
00061     if (max > Limits::max)
00062       max = Limits::max;
00063     u = static_cast<int>(max);
00064   }
00065 
00067   template<class View>
00068   class TermByView {
00069   public:
00070     forceinline bool
00071     operator ()(const Term<View>& a, const Term<View>& b) {
00072       return a.x < b.x;
00073     }
00074   };
00075 
00077   template<class View>
00078   class TermBySizePos {
00079   public:
00080     forceinline bool
00081     operator ()(const Term<View>& a, const Term<View>& b) {
00082       assert((a.a > 0) && (b.a > 0));
00083       return (a.a > b.a) || ((a.a == b.a) && (a.p < b.p));
00084     }
00085   };
00086 
00088   inline int
00089   gcd(int a, int b) {
00090     if (b > a)
00091       std::swap(a,b);
00092     while (b > 0) {
00093       int t = b; b = a % b; a = t;
00094     }
00095     return a;
00096   }
00097 
00098 
00099 
00124   template<class View>
00125   inline bool
00126   normalize(Term<View>* t, int &n,
00127             Term<View>* &t_p, int &n_p,
00128             Term<View>* &t_n, int &n_n,
00129             int& g) {
00130     // Number terms by position
00131     for (int i=0; i<n; i++)
00132       t[i].p = i;
00133 
00134     /*
00135      * Join coefficients for aliased variables:
00136      *
00137      */
00138     {
00139       // Group same variables
00140       TermByView<View> tl;
00141       Support::quicksort<Term<View>,TermByView<View>>(t,n,tl);
00142 
00143       // Join adjacent variables
00144       int i = 0;
00145       int j = 0;
00146       while (i < n) {
00147         Limits::check(t[i].a,"Int::linear");
00148         long long int a = t[i].a;
00149         int p = t[i].p;
00150         View x = t[i].x;
00151         while ((++i < n) && (t[i].x == x)) {
00152           a += t[i].a;
00153           p = std::min(p,t[i].p);
00154           Limits::check(a,"Int::linear");
00155         }
00156         if (a != 0) {
00157           t[j].a = static_cast<int>(a); t[j].x = x; t[j].p = p; j++;
00158         }
00159       }
00160       n = j;
00161     }
00162 
00163     /*
00164      * Partition into positive/negative coefficents
00165      *
00166      */
00167     if (n > 0) {
00168       int i = 0;
00169       int j = n-1;
00170       while (true) {
00171         while ((t[j].a < 0) && (--j >= 0)) ;
00172         while ((t[i].a > 0) && (++i <  n)) ;
00173         if (j <= i) break;
00174         std::swap(t[i],t[j]);
00175       }
00176       t_p = t;     n_p = i;
00177       t_n = t+n_p; n_n = n-n_p;
00178     } else {
00179       t_p = t; n_p = 0;
00180       t_n = t; n_n = 0;
00181     }
00182 
00183     /*
00184      * Make all coefficients positive
00185      *
00186      */
00187     for (int i=0; i<n_n; i++)
00188       t_n[i].a = -t_n[i].a;
00189 
00190     /*
00191      * Sort terms into canonical order (to avoid indeterminstic order)
00192      */
00193     {
00194       TermBySizePos<View> tl;
00195       Support::quicksort<Term<View>,TermBySizePos<View>>(t_p,n_p,tl);
00196       Support::quicksort<Term<View>,TermBySizePos<View>>(t_n,n_n,tl);
00197     }
00198     
00199     /*
00200      * Compute greatest common divisor
00201      *
00202      */
00203     if ((n > 0) && (g > 0)) {
00204       g = t[0].a;
00205       for (int i=1; (g > 1) && (i < n); i++)
00206         g = gcd(t[i].a,g);
00207       if (g > 1)
00208         for (int i=n; i--; )
00209           t[i].a /= g;
00210     } else {
00211       g = 1;
00212     }
00213 
00214     /*
00215      * Test for unit coefficients only
00216      *
00217      */
00218     for (int i=0; i<n; i++)
00219       if (t[i].a != 1)
00220         return false;
00221     return true;
00222   }
00223 
00224 }}}
00225 
00226 // STATISTICS: int-post
00227