gecode » int » linear

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: 2009-01-20 23:44:27 +0100 (Tue, 20 Jan 2009) $ by $Author: schulte $ 00011 * $Revision: 8082 $ 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 if (t[i].a > 0) { 00050 min += t[i].a*t[i].x.min(); 00051 max += t[i].a*t[i].x.max(); 00052 } else { 00053 max += t[i].a*t[i].x.min(); 00054 min += t[i].a*t[i].x.max(); 00055 } 00056 if (min < Limits::min) 00057 min = Limits::min; 00058 l = static_cast<int>(min); 00059 if (max > Limits::max) 00060 max = Limits::max; 00061 u = static_cast<int>(max); 00062 } 00063 00065 template<class View> 00066 class TermLess { 00067 public: 00068 forceinline bool 00069 operator ()(const Term<View>& a, const Term<View>& b) { 00070 return before(a.x,b.x); 00071 } 00072 }; 00073 00074 template<class View> 00075 inline bool 00076 normalize(Term<View>* t, int &n, 00077 Term<View>* &t_p, int &n_p, 00078 Term<View>* &t_n, int &n_n) { 00079 /* 00080 * Join coefficients for aliased variables: 00081 * 00082 */ 00083 { 00084 // Group same variables 00085 TermLess<View> tl; 00086 Support::quicksort<Term<View>,TermLess<View> >(t,n,tl); 00087 00088 // Join adjacent variables 00089 int i = 0; 00090 int j = 0; 00091 while (i < n) { 00092 Limits::check(t[i].a,"Int::linear"); 00093 double a = t[i].a; 00094 View x = t[i].x; 00095 while ((++i < n) && same(t[i].x,x)) { 00096 a += t[i].a; 00097 Limits::check(a,"Int::linear"); 00098 } 00099 if (a != 0.0) { 00100 t[j].a = static_cast<int>(a); t[j].x = x; j++; 00101 } 00102 } 00103 n = j; 00104 } 00105 00106 /* 00107 * Partition into positive/negative coefficents 00108 * 00109 */ 00110 if (n > 0) { 00111 int i = 0; 00112 int j = n-1; 00113 while (true) { 00114 while ((t[j].a < 0) && (--j >= 0)) ; 00115 while ((t[i].a > 0) && (++i < n)) ; 00116 if (j <= i) break; 00117 std::swap(t[i],t[j]); 00118 } 00119 t_p = t; n_p = i; 00120 t_n = t+n_p; n_n = n-n_p; 00121 } else { 00122 t_p = t; n_p = 0; 00123 t_n = t; n_n = 0; 00124 } 00125 00126 /* 00127 * Make all coefficients positive 00128 * 00129 */ 00130 for (int i=n_n; i--; ) 00131 t_n[i].a = -t_n[i].a; 00132 00133 /* 00134 * Test for unit coefficients only 00135 * 00136 */ 00137 for (int i=n; i--; ) 00138 if (t[i].a != 1) 00139 return false; 00140 return true; 00141 } 00142 00143 }}} 00144 00145 // STATISTICS: int-post 00146