00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "qsolver.hh"
00025 #include "implicative.hh"
00026
00027
00028 QSolver::QSolver(QSpace* qs, VariableHeuristic* ev, valueHeuristic* ve) {
00029 debug=false;
00030 eval=ev;
00031 valEval = ve;
00032 sp=qs;
00033 n=sp->nv();
00034 bh=new BranchingHeuristic(sp,eval);
00035 sp->indicateBranchingHeuristic(bh);
00036 nbRanges = new int;
00037 }
00038
00039
00040 bool QSolver::solve(unsigned long int& nodes, unsigned long int& propsteps) {
00041 int ret = sp->status(0,propsteps);
00042 return rSolve(sp,nodes,propsteps,0);
00043 }
00044
00045
00046 bool QSolver::rSolve(QSpace* qs, unsigned long int& nodes, unsigned long int& propsteps, int curvar) {
00047 unsigned int rien=0;
00048 nodes++;
00049
00050
00051
00052
00053
00054 int branchon = bh->nextVar(qs);
00056
00057
00058
00059 if (branchon == n) {
00060 int ret=qs->status(curvar,propsteps);
00061 int fret=qs->finalStatus(propsteps);
00062
00063 #ifdef QDEBUG
00064 cout<<"Solver : finalstatus appelé, résultat = "<<fret<<endl;
00065 qs->print();
00066 #endif
00067 qs->backtrack();
00068 bh->backtrack(qs);
00069 #ifdef QDEBUG
00070 cout<<"Solver : backtracking"<<endl;
00071 #endif
00072 return (fret==1)?true:false;
00073 }
00075
00076
00077
00078 int ret = qs->status(curvar,propsteps);
00079 #ifdef QDEBUG
00080 cout<<"Solver : status appelé, résultat = "<<ret<<endl;
00081 qs->print();
00082 #endif
00083 if (ret==1) {
00084 qs->backtrack();
00085 bh->backtrack(qs);
00086 #ifdef QDEBUG
00087 cout<<"Solver : backtracking"<<endl;
00088 #endif
00089 return true;
00090 }
00091
00092 if (ret==0) {
00093 qs->backtrack();
00094 bh->backtrack(qs);
00095 #ifdef QDEBUG
00096 cout<<"Solver : backtracking"<<endl;
00097 #endif
00098 return false;
00099 }
00101
00102
00103
00104 int** tab = valEval->subSet(static_cast<Implicative*>(qs),branchon,nbRanges);
00105 QSpace* subspace;
00106 subspace=qs->clone();
00107 switch (qs->type_of_v[branchon]) {
00108 case VTYPE_INT :
00109 subspace->assign_int(branchon,tab,*nbRanges);
00110 break;
00111 case VTYPE_BOOL :
00112 subspace->assign_bool(branchon,tab,*nbRanges);
00113 break;
00114 }
00115 #ifdef QDEBUG
00116 cout<<"Solver : first branch : v"<<branchon<<" = "<<the_set<<endl;
00117 #endif
00118 if ( rSolve(subspace,nodes, propsteps,branchon) ^ qs->quantification(branchon)) {
00119 delete subspace;
00120 qs->backtrack();
00121 bh->backtrack(qs);
00122 #ifdef QDEBUG
00123 cout<<"Solver : Backtrack"<<endl;
00124 #endif
00125 delete [] tab[0];
00126 delete [] tab[1];
00127 delete [] tab;
00128 return (!qs->quantification(branchon));
00129 }
00130 delete subspace;
00131 subspace=qs->clone();
00132 switch (subspace->type_of_v[branchon]) {
00133 case VTYPE_INT :
00134 subspace->remove_int(branchon,tab,*nbRanges);
00135 break;
00136 case VTYPE_BOOL :
00137 subspace->remove_bool(branchon,tab,*nbRanges);
00138 break;
00139 }
00140
00141 #ifdef QDEBUG
00142 cout<<"Solver : second branch : v"<<branchon<<" != "<<the_set<<endl;
00143 #endif
00144 if ( rSolve(subspace,nodes, propsteps, branchon) ^ qs->quantification(branchon)) {
00145 delete subspace;
00146 qs->backtrack();
00147 bh->backtrack(qs);
00148 #ifdef QDEBUG
00149 cout<<"Solver : Backtrack"<<endl;
00150 #endif
00151 delete [] tab[0];
00152 delete [] tab[1];
00153 delete [] tab;
00154 return (!qs->quantification(branchon));
00155 }
00156 delete subspace;
00157 qs->backtrack();
00158 bh->backtrack(qs);
00159 #ifdef QDEBUG
00160 cout<<"Solver : Backtrack"<<endl;
00161 #endif
00162 delete [] tab[0];
00163 delete [] tab[1];
00164 delete [] tab;
00165 return (qs->quantification(branchon));
00167 }
00168