this repo has no description
at develop 7.1 kB view raw
1/**** , [ QCSP_Solver.cc ], 2 Copyright (c) 2008 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 "qsolver_qcsp.hh" 24#include <climits> 25 26inline vector<int> getTheValues(MySpace* sol,int vmin,int vmax) 27{ 28 vector<int> zevalues; 29 // cout<<sol<<" "<<vmin<<" "<<vmax<<endl; 30 if (vmax > (sol->nbVars())) cout<<"getTheValues mal appele"<<endl; 31 for (int i = vmin; i<=vmax; i++) { 32 // cout<<i<<" "; 33 // cout.flush(); 34 switch (sol->type_of_v[i]) { 35 case VTYPE_INT : 36 zevalues.push_back( (static_cast<IntVar*>(sol->v[i]))->val() ); 37 break; 38 case VTYPE_BOOL : 39 zevalues.push_back( (static_cast<BoolVar*>(sol->v[i]))->val() ); 40 break; 41 default : 42 cout<<"4Unknown variable type"<<endl; 43 abort(); 44 } 45 } 46 // cout<<endl; 47 48 return zevalues; 49} 50 51QCSP_Solver::QCSP_Solver(Qcop* sp) 52{ 53 this->sp = sp; 54 nbRanges=new int; 55} 56 57Strategy QCSP_Solver::solve(unsigned long int& nodes, unsigned int limit,bool allStrategies) 58{ 59 this->limit=limit; 60 MySpace* espace=sp->getSpace(0); 61 Options o; 62 Engine* solutions = new WorkerToEngine<Gecode::Search::Sequential::DFS>(espace,/*sizeof(MySpace),*/o); 63 return rSolve(sp,0,solutions,nodes,allStrategies); 64} 65 66 67 68Strategy QCSP_Solver::rSolve(Qcop* qs,int scope,Engine* L, unsigned long int& nodes,bool allStrategies) 69{ 70 nodes++; 71 MySpace* sol = static_cast<MySpace*>(L->next()); 72 Strategy ret=Strategy::Dummy(); 73 bool LwasEmpty = true; 74 bool atLeastOneExistential = false; 75 while ((sol != NULL) ) { 76 LwasEmpty=false; 77 vector<int> assignments = getTheValues(sol,0,sol->nbVars()-1); 78 Strategy result; 79 80 if (scope == (qs->spaces() - 1) ) { // last scope reached. Verify the goal... 81 MySpace* g = qs->getGoal(); 82 for (int i=0; i<g->nbVars(); i++) { 83 switch (g->type_of_v[i]) { 84 case VTYPE_INT : 85 rel(*g,*(static_cast<IntVar*>(g->v[i])) == assignments[i]); 86 break; 87 case VTYPE_BOOL : 88 rel(*g,*(static_cast<BoolVar*>(g->v[i])) == assignments[i]); 89 break; 90 default : 91 cout<<"Unknown variable type"<<endl; 92 abort(); 93 } 94 } 95 Gecode::DFS<MySpace> solutions(g); 96 MySpace* goalsol = solutions.next(); 97 if (goalsol == NULL) { 98 delete g; 99 result = Strategy::SFalse(); 100 } else { 101 int vmin = ( (scope==0)? 0 : (qs->nbVarInScope(scope-1)) ); 102 int vmax = (qs->nbVarInScope(scope))-1; 103 vector<int> zevalues=getTheValues(sol,vmin,vmax); 104 result = Strategy::STrue(); 105// result=Strategy(qs->quantification(scope),vmin,vmax,scope,zevalues); 106// result.attach(Strategy::STrue()); 107 delete g; 108 // delete sol; 109 delete goalsol; 110 } 111 } 112 113 else { // This is not the last scope... 114 MySpace* espace = qs->getSpace(scope+1); 115 for (int i=0; i<assignments.size(); i++) { 116 switch (espace->type_of_v[i]) { 117 case VTYPE_INT : 118 rel(*espace,*(static_cast<IntVar*>(espace->v[i])) == assignments[i]); 119 break; 120 case VTYPE_BOOL : 121 rel(*espace,*(static_cast<BoolVar*>(espace->v[i])) == assignments[i]); 122 break; 123 default : 124 cout<<"Unknown variable type"<<endl; 125 abort(); 126 } 127 } 128 Options o; 129 Engine* solutions = new WorkerToEngine<Gecode::Search::Sequential::DFS>(espace,/*sizeof(MySpace),*/o); 130 delete espace; 131 result=rSolve(qs,scope+1,solutions,nodes,allStrategies); 132 } 133 134 int vmin = ( (scope == 0) ? 0 : (qs->nbVarInScope(scope-1)) ); 135 int vmax = (qs->nbVarInScope(scope))-1; 136 vector<int> zevalues=getTheValues(sol,vmin,vmax); 137 delete sol; 138 if (qs->quantification(scope)) { // current scope is universal 139 if (result.isFalse()) { // one branch fails 140 delete L; 141 return Strategy::SFalse(); 142 } else { 143 Strategy toAttach(true,vmin,vmax,scope,zevalues); 144 toAttach.attach(result); 145 ret.attach(toAttach); 146 } 147 } else { //current scope is existential 148 if (!result.isFalse()) { // result is not the trivially false strategy... 149 atLeastOneExistential =true; 150 Strategy toAttach; 151 if (allStrategies) { 152 // We want to save every possible strategies. Each correct existential branch will be saved 153 if (scope >= limit) toAttach = Strategy::STrue(); 154 else { 155 toAttach = Strategy(qs->quantification(scope),vmin,vmax,scope,zevalues); 156 toAttach.attach(result); 157 } 158 ret.attach(toAttach); 159 } else { 160 //We want only one possible strategy. We found an assignment which leads to a valid substrategy. So, we return it immediately 161 delete L; 162 if (scope >= limit) return Strategy::STrue(); 163 ret = Strategy(qs->quantification(scope),vmin,vmax,scope,zevalues); 164 ret.attach(result); 165 return ret; 166 } 167 } 168 } 169 sol = static_cast<MySpace*>(L->next()); 170 } 171 delete L; 172 if (scope>limit) 173 ret = Strategy::STrue(); 174 if (qs->quantification(scope)) //universal scope 175 return (LwasEmpty ? Strategy::STrue() : ret); 176 else // existnetial Scope 177 return (atLeastOneExistential ? ret : Strategy::SFalse()); 178}