this repo has no description
at develop 7.0 kB view raw
1/**** , [ QCSPPlusUnblockable.cc ], 2Copyright (c) 2009 Universite d'Orleans - Jeremie Vautard 3 4Permission is hereby granted, free of charge, to any person obtaining a copy 5of this software and associated documentation files (the "Software"), to deal 6in the Software without restriction, including without limitation the rights 7to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8copies of the Software, and to permit persons to whom the Software is 9furnished to do so, subject to the following conditions: 10 11The above copyright notice and this permission notice shall be included in 12all copies or substantial portions of the Software. 13 14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20THE SOFTWARE. 21 *************************************************************************/ 22 23#include "QCSPPlusUnblockable.hh" 24 25QcspUnblockable::QcspUnblockable(int ns,bool* quant,int* nv) { 26 n=0; 27 for (int i=0;i<ns;i++) { 28 n += nv[i]; 29 } 30 nbSpaces=ns; 31 // cout<<"QCSPU construction : "<<n<<" vriables"<<endl; 32 v=new void*[n]; 33 type_of_v = new VarType[n]; 34 Quantifiers = quant; 35 whichSpaceOwns=new int[n]; 36 nbVarBySpace=new int[nbSpaces]; 37 // cout<<"Before rule creation : "<<arul<<endl; 38 arul=new MySpace(n); 39 // cout<<"after rule creation : "<<arul<<endl; 40 nbVarBySpace[0]=nv[0]; 41 for (int i=1;i<n;i++) { 42 nbVarBySpace[i]=nbVarBySpace[i-1]+nv[i]; 43 } 44 45 arul=new MySpace(n); 46 goal = new MySpace(n); 47 48 49 for(unsigned int i=0;i<n;i++) { 50 int lespace=0; 51 while (nbVarBySpace[lespace]<=i) lespace++; 52 whichSpaceOwns[i]=lespace; 53 } 54 55 varInitialised=new bool[n]; 56 for (unsigned int i=0;i<n;i++) varInitialised[i]=false; 57 currentDeclareSpace=0; 58 59 vars = new vector<int>[nbSpaces]; 60 bvars = new vector<int>[nbSpaces]; 61} 62 63 64QcspUnblockable::~QcspUnblockable() { 65 delete arul; 66 delete goal; 67} 68 69int QcspUnblockable::spaces() { 70 return nbSpaces; 71} 72 73void QcspUnblockable::QIntVar(int var,int min,int max) { 74 if (varInitialised[var]) { 75 cout<<"Variable "<<var<<" Already created !!"<<endl; 76 abort(); 77 } 78 // cout<<"Qintvar : arul = "<<arul<<endl; 79 80 arul->v[var] = new IntVar(*arul,min,max); 81 arul->type_of_v[var] = VTYPE_INT; 82 83 goal->v[var] = new IntVar(*goal,min,max); 84 goal->type_of_v[var] = VTYPE_INT; 85 varInitialised[var]=true; 86 type_of_v[var]=VTYPE_INT; 87} 88 89void QcspUnblockable::QIntVar(int var,IntSet dom) { 90 if (varInitialised[var]) { 91 cout<<"Variable "<<var<<" Already created !!"<<endl; 92 abort(); 93 } 94 95 arul->v[var] = new IntVar(*arul,dom); 96 arul->type_of_v[var] = VTYPE_INT; 97 goal->v[var] = new IntVar(*goal,dom); 98 goal->type_of_v[var] = VTYPE_INT; 99 varInitialised[var]=true; 100 type_of_v[var]=VTYPE_INT; 101} 102 103 104void QcspUnblockable::QBoolVar(int var) { 105 if (varInitialised[var]) { 106 cout<<"Variable "<<var<<" Already created !!"<<endl; 107 abort(); 108 } 109 110 arul->v[var] = new BoolVar(*arul,0,1); 111 arul->type_of_v[var]=VTYPE_BOOL; 112 goal->v[var] = new BoolVar(*goal,0,1); 113 goal->type_of_v[var]=VTYPE_BOOL; 114 varInitialised[var]=true; 115 type_of_v[var]=VTYPE_BOOL; 116} 117 118MySpace* QcspUnblockable::space() { 119 if (currentDeclareSpace<nbSpaces) { 120 //cout<<"Return space arul"<<endl; cout.flush(); 121 return arul; 122} 123 if (currentDeclareSpace==nbSpaces) { 124 //cout<<"Return space Goal"<<endl; cout.flush(); 125 return goal; 126} 127cout<<"Return null in space()"<<endl; 128 return NULL; 129} 130 131 132IntVar QcspUnblockable::var(int n) { 133 if (!varInitialised[n]) { 134 cout<<"Variable "<<n<<" not initialized !"<<endl; 135 abort(); 136 } 137 if (type_of_v[n] != VTYPE_INT) { 138 cout<<"Variable "<<n<<" is not INT"<<endl; 139 abort(); 140 } 141 return *(static_cast<IntVar*>(space()->v[n])); 142} 143 144BoolVar QcspUnblockable::bvar(int n) { 145 if (!varInitialised[n]) { 146 cout<<"Variable "<<n<<" not initialized !"<<endl; 147 abort(); 148 } 149 if (type_of_v[n] != VTYPE_BOOL){ 150 cout<<"Variable "<<n<<" is not BOOL"<<endl; 151 abort(); 152 } 153 return *(static_cast<BoolVar*>(space()->v[n])); 154} 155 156int QcspUnblockable::nextScope() { 157 if (currentDeclareSpace == -1) return -1; 158 currentDeclareSpace++; 159 if (currentDeclareSpace>nbSpaces) return -1; 160 return currentDeclareSpace; 161} 162 163void QcspUnblockable::makeStructure() { 164 for (unsigned int i=0;i<n;i++) { 165 if (varInitialised[i] == false) { 166 cout<<"Can't make structure : variable "<<i<<" not initialised"<<endl; 167 abort(); 168 } 169 } 170 for (unsigned int i=0;i<nbSpaces;i++) { 171 unsigned int nbint=0; 172 unsigned int nbbool=0; 173 for (unsigned int j=0;j<nbVarBySpace[i];j++) { 174 if (type_of_v[j] == VTYPE_INT) 175 nbint++; 176 else 177 nbbool++; 178 } 179 nbint=0;nbbool=0; 180 for (unsigned int j=0;j<nbVarBySpace[i];j++) { 181 if (type_of_v[j] == VTYPE_INT) { 182 (vars[i]).push_back(j); 183 } 184 else { 185 (bvars[i]).push_back(j); 186 } 187 } 188 } 189} 190 191forceinline bool QcspUnblockable::qt_of_var(int v) { 192 return Quantifiers[whichSpaceOwns[v]]; 193} 194 195MySpace* QcspUnblockable::getSpace(int scope) { 196 if (scope<0 || scope>nbSpaces) { 197 cout<<"I return NULL coz of bad scope value (<0)"<<endl; 198 return NULL; 199 } 200 if (scope==nbSpaces) { 201 if (goal->status() == SS_FAILED) { 202 cout<<"I return NULL coz goal is failed"<<endl; 203 return NULL; 204 } 205 // cout<<"I return the goal"<<endl; 206 return static_cast<MySpace*>(goal->clone()); 207 } 208 if (arul->status() == SS_FAILED) { 209 cout<<"I return NULL coz scope "<<scope<<" is failed"<<endl; 210 return NULL; 211 } 212 // cout<<"I return the rule "<<scope<<endl; 213 MySpace* ret= (static_cast<MySpace*>(arul->clone())); 214 IntVarArgs iva(vars[scope].size()); 215 BoolVarArgs bva(bvars[scope].size()); 216 // cout << "sizes : " <<iva.size() << " " << bva.size()<<endl; 217 for (int i=0;i<iva.size();i++) { 218 int idx = (vars[scope])[i]; 219 iva[i]=*( static_cast<IntVar*>(ret->v[idx]) ); 220 } 221 for (int i=0;i<bva.size();i++) { 222 int idx = (bvars[scope])[i]; 223 bva[i]=*( static_cast<BoolVar*>(ret->v[idx]) ); 224 } 225 br->branch(ret,iva,bva); 226 227 return ret; 228} 229 230 231MySpace* QcspUnblockable::getGoal() { 232 if (goal->status() == SS_FAILED) return NULL; 233 return static_cast<MySpace*>(goal->clone()); 234} 235 236 237void QcspUnblockable::branch(UnblockableBranching* b) { 238 br=b; 239}