this repo has no description
1/**** , [ QCOPPlus.cc ], 2 Copyright (c) 2009 Universite d'Orleans - Jeremie Vautard 3 4 Permission is hereby granted, free of charge, to any person obtaining a copy 5 of this software and associated documentation files (the "Software"), to deal 6 in the Software without restriction, including without limitation the rights 7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 copies of the Software, and to permit persons to whom the Software is 9 furnished to do so, subject to the following conditions: 10 11 The above copyright notice and this permission notice shall be included in 12 all copies or substantial portions of the Software. 13 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 THE SOFTWARE. 21 *************************************************************************/ 22 23#include "QCOPPlus.hh" 24 25Qcop::Qcop(int ns, bool* quant, int* nv) { 26 n=0; 27 this->nvar = nv; 28 for (int i=0;i<ns;i++) { 29 n += nv[i]; 30 } 31 nbSpaces=ns; 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 rules=new MySpace*[ns]; 38 nbVarBySpace[0]=nv[0]; 39 rules[0]=new MySpace(nbVarBySpace[0]); 40 for (int i=1;i<nbSpaces;i++) { 41 nbVarBySpace[i]=nbVarBySpace[i-1]+nv[i]; 42 rules[i]=new MySpace(nbVarBySpace[i]); 43 } 44 goal=new MySpace(n); 45 for(unsigned int i=0;i<n;i++) { 46 int lespace=0; 47 while (nbVarBySpace[lespace]<=i) lespace++; 48 whichSpaceOwns[i]=lespace; 49 } 50 51 52 varInitialised=new bool[n]; 53 for (unsigned int i=0;i<n;i++) varInitialised[i]=false; 54 currentDeclareSpace=0; 55 56 optim = new Opts[nbSpaces]; 57 for (int i=0;i<nbSpaces;i++) 58 optim[i].opt_type = 0; 59} 60 61 62Qcop::~Qcop() { 63 for (int i=0;i<nbSpaces;i++) { 64 delete rules[i]; 65 } 66 67 delete goal; 68 delete [] rules; 69} 70 71 72int Qcop::spaces() { 73 return nbSpaces; 74} 75 76 77void Qcop::QIntVar(int var,int min,int max) { 78 if (varInitialised[var]) { 79 cout<<"Variable "<<var<<" Already created !!"<<endl; 80 abort(); 81 } 82 83 for (int i=whichSpaceOwns[var];i<nbSpaces;i++) { 84 rules[i]->v[var] = new IntVar(*rules[i],min,max); 85 rules[i]->type_of_v[var] = VTYPE_INT; 86 } 87 goal->v[var] = new IntVar(*goal,min,max); 88 goal->type_of_v[var] = VTYPE_INT; 89 varInitialised[var]=true; 90 type_of_v[var]=VTYPE_INT; 91} 92 93 94void Qcop::QIntVar(int var,IntSet dom) { 95 if (varInitialised[var]) { 96 cout<<"Variable "<<var<<" Already created !!"<<endl; 97 abort(); 98 } 99 100 for (int i=whichSpaceOwns[var];i<nbSpaces;i++) { 101 rules[i]->v[var] = new IntVar(*rules[i],dom); 102 rules[i]->type_of_v[var] = VTYPE_INT; 103 } 104 goal->v[var] = new IntVar(*goal,dom); 105 goal->type_of_v[var] = VTYPE_INT; 106 varInitialised[var]=true; 107 type_of_v[var]=VTYPE_INT; 108} 109 110 111void Qcop::QBoolVar(int var) { 112 if (varInitialised[var]) { 113 cout<<"Variable "<<var<<" Already created !!"<<endl; 114 abort(); 115 } 116 117 for (int i=whichSpaceOwns[var];i<nbSpaces;i++) { 118 rules[i]->v[var] = new BoolVar(*rules[i],0,1); 119 rules[i]->type_of_v[var]=VTYPE_BOOL; 120 } 121 goal->v[var] = new BoolVar(*goal,0,1); 122 goal->type_of_v[var]=VTYPE_BOOL; 123 varInitialised[var]=true; 124 type_of_v[var]=VTYPE_BOOL; 125} 126 127MySpace* Qcop::space() { 128 if (currentDeclareSpace<nbSpaces) 129 return rules[currentDeclareSpace]; 130 if (currentDeclareSpace==nbSpaces) 131 return goal; 132 return NULL; 133} 134 135 136IntVar Qcop::var(int n) { 137 if (!varInitialised[n]) { 138 cout<<"Variable "<<n<<" not initialized !"<<endl; 139 abort(); 140 } 141 if (type_of_v[n] != VTYPE_INT) { 142 cout<<"Variable "<<n<<" is not INT"<<endl; 143 abort(); 144 } 145 return *(static_cast<IntVar*>(space()->v[n])); 146} 147 148 149BoolVar Qcop::bvar(int n) { 150 if (!varInitialised[n]) { 151 cout<<"Variable "<<n<<" not initialized !"<<endl; 152 abort(); 153 } 154 if (type_of_v[n] != VTYPE_BOOL){ 155 cout<<"Variable "<<n<<" is not BOOL"<<endl; 156 abort(); 157 } 158 return *(static_cast<BoolVar*>(space()->v[n])); 159} 160 161 162int Qcop::nextScope() { 163 if (currentDeclareSpace == -1) return -1; 164 currentDeclareSpace++; 165 if (currentDeclareSpace>nbSpaces) return -1; 166 return currentDeclareSpace; 167} 168 169void Qcop::makeStructure() { 170 for (unsigned int i=0;i<n;i++) { 171 if (varInitialised[i] == false) { 172 cout<<"Can't make structure : variable "<<i<<" not initialised"<<endl; 173 abort(); 174 } 175 } 176 177 for (int i=0;i<nbSpaces;i++) { 178 if (!Quantifiers[i]) 179 if (optim[i].vars.empty()) 180 optimize(i,0,getExistential( (i==0)?0:nbVarBySpace[i-1])); 181 } 182 183 for (unsigned int i=0;i<this->nbSpaces;i++) { 184 if (rules[i]->status() == SS_FAILED) { 185 cout<<"MakeStructure : rule space "<<i<<" is already failed."<<endl; 186 } 187 } 188 if (goal->status() == SS_FAILED) { 189 cout<<"MakeStructure : goal space is already failed."<<endl; 190 } 191 192} 193 194 195OptVar* Qcop::getAggregate(int scope, OptVar* opt, Aggregator* agg) { 196 if (!Quantifiers[scope]) {cout<<"Try to get aggregate on existential scope"<<endl;abort();} // aggregateur sur existentiel 197 if (opt->getScope() < scope) {cout<<"aggregated variable out of aggregator scope"<<endl;abort();} // Variable aggr�g�e avant aggregateur 198 for (int i=scope+1; i<opt->getScope();i++) // Universelle entre aggregateur et variable aggr�g�e 199 if (Quantifiers[i]) 200 {cout<<"Universal scope between variable and aggregator"<<endl;abort();} 201 202 UnivOptVar* zeopt = new UnivOptVar(scope,opt,agg); 203 optim[scope].vars.push_back(zeopt); 204 return zeopt; 205} 206 207 208OptVar* Qcop::getExistential(int var) { 209 if (Quantifiers[whichSpaceOwns[var]]) {cout<<"can't create existOptVar : variable "<<var<<" is universal"<<endl;abort();} // Var est universelle 210 ExistOptVar* opt = new ExistOptVar(var,whichSpaceOwns[var]); 211 return opt; 212} 213 214 215void Qcop::optimize(int scope, int optType,OptVar* var) { 216 if (var->getScope() < scope) {cout<<"optvar out of optimizing scope"<<endl;abort();} // Variable � optimiser avant d�cideur 217 for (int i=scope; i<var->getScope();i++) { 218 if (Quantifiers[i]) // universelle entre variable et d�cideur 219 { cout<<"universal scope between variable and optimizing scope"<<endl;abort();} 220 } 221 // cout<<"I add an optVar* for scope "<<scope<<endl; 222 optim[scope].vars.clear(); 223 optim[scope].vars.push_back(var); 224 optim[scope].opt_type=optType; 225} 226 227 228MySpace* Qcop::getSpace(int scope) { 229 if (scope<0 || scope>nbSpaces) { 230 cout<<"I return NULL coz of bad scope value (<0)"<<endl; 231 return NULL; 232 } 233 if (scope==nbSpaces) { 234 if (goal->status() == SS_FAILED) { 235 cout<<"I return NULL coz goal is failed"<<endl; 236 return NULL; 237 } 238 // cout<<"I return the goal"<<endl; 239 return static_cast<MySpace*>(goal->clone()); 240 } 241 if (rules[scope]->status() == SS_FAILED) { 242 cout<<"I return NULL coz scope "<<scope<<" is failed"<<endl; 243 return NULL; 244 } 245 // cout<<"I return the rule "<<scope<<endl; 246 return (static_cast<MySpace*>(rules[scope]->clone())); 247} 248 249 250MySpace* Qcop::getGoal() { 251 if (goal->status() == SS_FAILED) return NULL; 252 return static_cast<MySpace*>(goal->clone()); 253} 254 255 256int Qcop::getOptType(int scope) { 257 if (Quantifiers[scope]) {cout<<"Try to get OptType on universal scope"<<endl;abort();} 258 return optim[scope].opt_type; 259} 260 261 262OptVar* Qcop::getOptVar(int scope) { 263 if (Quantifiers[scope]) abort(); 264 // if (optim[scope].vars.size() == 0) cout<<"no optvar to return, ca va planter..."; 265 // cout<<"getoptvar returns optvar* of scope "<<scope; 266 return static_cast<OptVar*>(optim[scope].vars[0]); 267} 268 269Qcop* Qcop::clone() { 270 bool* qt = new bool[nbSpaces]; 271 int* nvv = new int[nbSpaces]; 272 MySpace** zerulz = new MySpace*[nbSpaces]; 273 Opts* opop = new Opts[nbSpaces]; 274 int* nbvbs = new int[nbSpaces]; 275 for (unsigned int i=0;i < nbSpaces;i++) { 276 qt[i] = this->Quantifiers[i]; 277 nvv[i] = this->nvar[i]; 278 zerulz[i] = static_cast<MySpace*>(this->rules[i]->clone(false)); 279 opop[i] = this->optim[i]; 280 nbvbs[i] = this->nbVarBySpace[i]; 281 } 282 283 // void** v = new void*[this->n]; 284 VarType* typeofv = new VarType[n]; 285 int* wso = new int[n]; 286 for (unsigned int i=0;i<n;i++) { 287 typeofv[i] = this->type_of_v[i]; 288 wso[i] = this->whichSpaceOwns[i]; 289 } 290 291 Qcop* ret = new Qcop(); 292 ret->nvar = nvv; 293 ret->n = this->n; 294 ret->nbSpaces = this->nbSpaces; 295 ret->type_of_v = typeofv; 296 ret->Quantifiers = qt; 297 ret->rules = zerulz; 298 ret->goal = static_cast<MySpace*>(this->goal->clone(false)); 299 ret->optim = opop; 300 ret->nbVarBySpace = nbvbs; 301 ret->whichSpaceOwns = wso; 302 ret->varInitialised = this->varInitialised; 303 currentDeclareSpace = this->currentDeclareSpace; 304 305return ret; 306}