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

arithmetic.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, 2006
00008  *
00009  *  Last modified:
00010  *     $Date: 2008-02-21 08:37:19 +0100 (Thu, 21 Feb 2008) $ by $Author: schulte $
00011  *     $Revision: 6264 $
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 "gecode/minimodel.hh"
00039 
00040 #include <algorithm>
00041 
00042 namespace Gecode {
00043 
00044 #define GECODE_MM_RETURN_FAILED                 \
00045 if (home->failed()) {                           \
00046   IntVar _x(home,0,0); return _x;               \
00047 }
00048 
00049   IntVar
00050   abs(Space* home, IntVar x, IntConLevel icl, PropKind pk) {
00051     GECODE_MM_RETURN_FAILED;
00052     if ((icl == ICL_DOM) && (x.min() >= 0))
00053       return x;
00054     int min, max;
00055     if (x.min() >= 0) {
00056       min = x.min(); max = x.max();
00057     } else if (x.max() <= 0) {
00058       min = -x.max(); max = -x.min();
00059     } else {
00060       min = 0; max = std::max(-x.min(),x.max());
00061     }
00062     IntVar y(home, min, max);
00063     abs(home, x, y, icl, pk);
00064     return y;
00065   }
00066 
00067   IntVar
00068   min(Space* home, IntVar x, IntVar y, IntConLevel icl, PropKind pk) {
00069     GECODE_MM_RETURN_FAILED;
00070     IntVar z(home,
00071              std::min(x.min(),y.min()),
00072              std::min(x.max(),y.max()));
00073     min(home, x, y, z, icl, pk);
00074     return z;
00075   }
00076 
00077   IntVar
00078   min(Space* home, const IntVarArgs& x, IntConLevel icl, PropKind pk) {
00079     GECODE_MM_RETURN_FAILED;
00080     int min = Int::Limits::max;
00081     int max = Int::Limits::max;
00082     for (int i=x.size(); i--; ) {
00083       min = std::min(min,x[i].min());
00084       max = std::min(max,x[i].max());
00085     }
00086     IntVar y(home, min, max);
00087     Gecode::min(home, x, y, icl, pk);
00088     return y;
00089   }
00090 
00091   IntVar
00092   max(Space* home, IntVar x, IntVar y, IntConLevel icl, PropKind pk) {
00093     GECODE_MM_RETURN_FAILED;
00094     IntVar z(home,
00095              std::max(x.min(),y.min()),
00096              std::max(x.max(),y.max()));
00097     max(home, x, y, z, icl, pk);
00098     return z;
00099   }
00100 
00101   IntVar
00102   max(Space* home, const IntVarArgs& x, IntConLevel icl, PropKind pk) {
00103     GECODE_MM_RETURN_FAILED;
00104     int min = Int::Limits::min;
00105     int max = Int::Limits::min;
00106     for (int i=x.size(); i--; ) {
00107       min = std::max(min,x[i].min());
00108       max = std::max(max,x[i].max());
00109     }
00110     IntVar y(home, min, max);
00111     Gecode::max(home, x, y, icl, pk);
00112     return y;
00113   }
00114 
00115   IntVar
00116   mult(Space* home, IntVar x, IntVar y, IntConLevel icl, PropKind pk) {
00117     GECODE_MM_RETURN_FAILED;
00118     IntVar z(home, Int::Limits::min, Int::Limits::max);
00119     mult(home, x, y, z, icl, pk);
00120     return z;
00121   }
00122 
00123   IntVar
00124   sqr(Space* home, IntVar x, IntConLevel icl, PropKind pk) {
00125     GECODE_MM_RETURN_FAILED;
00126     IntVar y(home, 0, Int::Limits::max);
00127     sqr(home, x, y, icl, pk);
00128     return y;
00129   }
00130 
00131   IntVar
00132   sqrt(Space* home, IntVar x, IntConLevel icl, PropKind pk) {
00133     GECODE_MM_RETURN_FAILED;
00134     IntVar y(home, 0, std::max(0,x.max()));
00135     sqrt(home, x, y, icl, pk);
00136     return y;
00137   }
00138 
00139   IntVar
00140   plus(Space* home, IntVar x, IntVar y, IntConLevel icl, PropKind) {
00141     GECODE_MM_RETURN_FAILED;
00142     double min = static_cast<double>(x.min())+static_cast<double>(y.min());
00143     min = std::min(static_cast<double>(Int::Limits::max),
00144                    std::max(static_cast<double>(Int::Limits::min),min));
00145     double max = static_cast<double>(x.max())+static_cast<double>(y.max());
00146     max = std::min(static_cast<double>(Int::Limits::max),
00147                    std::max(static_cast<double>(Int::Limits::min),max));
00148     IntVar z(home, static_cast<int>(min), static_cast<int>(max));
00149     Int::Linear::Term<Int::IntView> ts[3];
00150     ts[0].a =  1; ts[0].x = x;
00151     ts[1].a =  1; ts[1].x = y;
00152     ts[2].a = -1; ts[2].x = z;
00153     Int::Linear::post(home, ts, 3, IRT_EQ, 0, icl);
00154     return z;
00155   }
00156 
00157   IntVar
00158   minus(Space* home, IntVar x, IntVar y, IntConLevel icl, PropKind) {
00159     GECODE_MM_RETURN_FAILED;
00160     double min = static_cast<double>(x.min())-static_cast<double>(y.max());
00161     min = std::min(static_cast<double>(Int::Limits::max),
00162                    std::max(static_cast<double>(Int::Limits::min),min));
00163     double max = static_cast<double>(x.max())-static_cast<double>(y.min());
00164     max = std::min(static_cast<double>(Int::Limits::max),
00165                    std::max(static_cast<double>(Int::Limits::min),max));
00166     IntVar z(home, static_cast<int>(min), static_cast<int>(max));
00167     Int::Linear::Term<Int::IntView> ts[3];
00168     ts[0].a =  1; ts[0].x = x;
00169     ts[1].a = -1; ts[1].x = y;
00170     ts[2].a = -1; ts[2].x = z;
00171     Int::Linear::post(home, ts, 3, IRT_EQ, 0, icl);
00172     return z;
00173   }
00174 
00175 #undef GECODE_MM_RETURN_FAILED
00176 
00177 }
00178 
00179 // STATISTICS: minimodel-any