post.hpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
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
00131 for (int i=0; i<n; i++)
00132 t[i].p = i;
00133
00134
00135
00136
00137
00138 {
00139
00140 TermByView<View> tl;
00141 Support::quicksort<Term<View>,TermByView<View>>(t,n,tl);
00142
00143
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
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
00185
00186
00187 for (int i=0; i<n_n; i++)
00188 t_n[i].a = -t_n[i].a;
00189
00190
00191
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
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
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
00227