this repo has no description
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ 2/* 3 * Main authors: 4 * Christian Schulte <schulte@gecode.org> 5 * Guido Tack <tack@gecode.org> 6 * 7 * Copyright: 8 * Christian Schulte, 2006 9 * Guido Tack, 2011 10 * 11 * This file is part of Gecode, the generic constraint 12 * development environment: 13 * http://www.gecode.org 14 * 15 * Permission is hereby granted, free of charge, to any person obtaining 16 * a copy of this software and associated documentation files (the 17 * "Software"), to deal in the Software without restriction, including 18 * without limitation the rights to use, copy, modify, merge, publish, 19 * distribute, sublicense, and/or sell copies of the Software, and to 20 * permit persons to whom the Software is furnished to do so, subject to 21 * the following conditions: 22 * 23 * The above copyright notice and this permission notice shall be 24 * included in all copies or substantial portions of the Software. 25 * 26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 27 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 29 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 30 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 31 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 32 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 33 * 34 */ 35 36#include <gecode/int/circuit.hh> 37 38namespace Gecode { 39 40 void 41 circuit(Home home, int offset, const IntVarArgs& x, IntPropLevel ipl) { 42 Int::Limits::nonnegative(offset,"Int::circuit"); 43 if (x.size() == 0) 44 throw Int::TooFewArguments("Int::circuit"); 45 if (same(x)) 46 throw Int::ArgumentSame("Int::circuit"); 47 GECODE_POST; 48 ViewArray<Int::IntView> xv(home,x); 49 50 if (offset == 0) { 51 typedef Int::NoOffset<Int::IntView> NOV; 52 NOV no; 53 if (vbd(ipl) == IPL_DOM) { 54 GECODE_ES_FAIL((Int::Circuit::Dom<Int::IntView,NOV> 55 ::post(home,xv,no))); 56 } else { 57 GECODE_ES_FAIL((Int::Circuit::Val<Int::IntView,NOV> 58 ::post(home,xv,no))); 59 } 60 } else { 61 typedef Int::Offset OV; 62 OV off(-offset); 63 if (vbd(ipl) == IPL_DOM) { 64 GECODE_ES_FAIL((Int::Circuit::Dom<Int::IntView,OV> 65 ::post(home,xv,off))); 66 } else { 67 GECODE_ES_FAIL((Int::Circuit::Val<Int::IntView,OV> 68 ::post(home,xv,off))); 69 } 70 } 71 } 72 void 73 circuit(Home home, const IntVarArgs& x, IntPropLevel ipl) { 74 circuit(home,0,x,ipl); 75 } 76 77 void 78 circuit(Home home, const IntArgs& c, int offset, 79 const IntVarArgs& x, const IntVarArgs& y, IntVar z, 80 IntPropLevel ipl) { 81 Int::Limits::nonnegative(offset,"Int::circuit"); 82 int n = x.size(); 83 if (n == 0) 84 throw Int::TooFewArguments("Int::circuit"); 85 if (same(x)) 86 throw Int::ArgumentSame("Int::circuit"); 87 if ((y.size() != n) || (c.size() != n*n)) 88 throw Int::ArgumentSizeMismatch("Int::circuit"); 89 circuit(home, offset, x, ipl); 90 GECODE_POST; 91 IntArgs cx(offset+n); 92 for (int i=0; i<offset; i++) 93 cx[i] = 0; 94 for (int i=0; i<n; i++) { 95 for (int j=0; j<n; j++) 96 cx[offset+j] = c[i*n+j]; 97 element(home, cx, x[i], y[i]); 98 } 99 linear(home, y, IRT_EQ, z); 100 } 101 void 102 circuit(Home home, const IntArgs& c, 103 const IntVarArgs& x, const IntVarArgs& y, IntVar z, 104 IntPropLevel ipl) { 105 circuit(home,c,0,x,y,z,ipl); 106 } 107 void 108 circuit(Home home, const IntArgs& c, int offset, 109 const IntVarArgs& x, IntVar z, 110 IntPropLevel ipl) { 111 Int::Limits::nonnegative(offset,"Int::circuit"); 112 GECODE_POST; 113 IntVarArgs y(home, x.size(), Int::Limits::min, Int::Limits::max); 114 circuit(home, c, offset, x, y, z, ipl); 115 } 116 void 117 circuit(Home home, const IntArgs& c, 118 const IntVarArgs& x, IntVar z, 119 IntPropLevel ipl) { 120 circuit(home,c,0,x,z,ipl); 121 } 122 123 void 124 path(Home home, int offset, const IntVarArgs& x, IntVar s, IntVar e, 125 IntPropLevel ipl) { 126 Int::Limits::nonnegative(offset,"Int::path"); 127 int n=x.size(); 128 if (n == 0) 129 throw Int::TooFewArguments("Int::path"); 130 if (same(x)) 131 throw Int::ArgumentSame("Int::path"); 132 GECODE_POST; 133 ViewArray<Int::IntView> xv(home,n+1); 134 for (int i=0; i<n; i++) 135 xv[i] = Int::IntView(x[i]); 136 xv[n] = s; 137 138 if (offset == 0) { 139 element(home, x, e, n); 140 typedef Int::NoOffset<Int::IntView> NOV; 141 NOV no; 142 if (vbd(ipl) == IPL_DOM) { 143 GECODE_ES_FAIL((Int::Circuit::Dom<Int::IntView,NOV> 144 ::post(home,xv,no))); 145 } else { 146 GECODE_ES_FAIL((Int::Circuit::Val<Int::IntView,NOV> 147 ::post(home,xv,no))); 148 } 149 } else { 150 IntVarArgs ox(n+offset); 151 IntVar y(home, -1,-1); 152 for (int i=0; i<offset; i++) 153 ox[i] = y; 154 for (int i=0; i<n; i++) 155 ox[offset + i] = x[i]; 156 element(home, ox, e, offset+n); 157 typedef Int::Offset OV; 158 OV off(-offset); 159 if (vbd(ipl) == IPL_DOM) { 160 GECODE_ES_FAIL((Int::Circuit::Dom<Int::IntView,OV> 161 ::post(home,xv,off))); 162 } else { 163 GECODE_ES_FAIL((Int::Circuit::Val<Int::IntView,OV> 164 ::post(home,xv,off))); 165 } 166 } 167 } 168 void 169 path(Home home, const IntVarArgs& x, IntVar s, IntVar e, 170 IntPropLevel ipl) { 171 path(home,0,x,s,e,ipl); 172 } 173 174 void 175 path(Home home, const IntArgs& c, int offset, 176 const IntVarArgs& x, IntVar s, IntVar e, 177 const IntVarArgs& y, IntVar z, 178 IntPropLevel ipl) { 179 Int::Limits::nonnegative(offset,"Int::path"); 180 int n = x.size(); 181 if (n == 0) 182 throw Int::TooFewArguments("Int::path"); 183 if (same(x)) 184 throw Int::ArgumentSame("Int::path"); 185 if ((y.size() != n) || (c.size() != n*n)) 186 throw Int::ArgumentSizeMismatch("Int::path"); 187 GECODE_POST; 188 path(home, offset, x, s, e, ipl); 189 IntArgs cx(offset+n+1); 190 for (int i=0; i<offset; i++) 191 cx[i] = 0; 192 cx[offset+n] = 0; 193 for (int i=0; i<n; i++) { 194 for (int j=0; j<n; j++) 195 cx[offset+j] = c[i*n+j]; 196 element(home, cx, x[i], y[i]); 197 } 198 linear(home, y, IRT_EQ, z); 199 } 200 void 201 path(Home home, const IntArgs& c, 202 const IntVarArgs& x, IntVar s, IntVar e, 203 const IntVarArgs& y, IntVar z, 204 IntPropLevel ipl) { 205 path(home,c,0,x,s,e,y,z,ipl); 206 } 207 void 208 path(Home home, const IntArgs& c, int offset, 209 const IntVarArgs& x, IntVar s, IntVar e, IntVar z, 210 IntPropLevel ipl) { 211 Int::Limits::nonnegative(offset,"Int::path"); 212 GECODE_POST; 213 IntVarArgs y(home, x.size(), Int::Limits::min, Int::Limits::max); 214 path(home, c, offset, x, s, e, y, z, ipl); 215 } 216 void 217 path(Home home, const IntArgs& c, 218 const IntVarArgs& x, IntVar s, IntVar e, IntVar z, 219 IntPropLevel ipl) { 220 path(home,c,0,x,s,e,z,ipl); 221 } 222 223} 224 225// STATISTICS: int-post