/Users/jamian/Desktop/gecode-2.0.1/contribs/qecode/qsolver.cc

Go to the documentation of this file.
00001 /*****************************************************************[qsolver.cc]
00002 Copyright (c) 2007, Universite d'Orleans - Jeremie Vautard, Marco Benedetti,
00003 Arnaud Lallouet.
00004 
00005 Permission is hereby granted, free of charge, to any person obtaining a copy
00006 of this software and associated documentation files (the "Software"), to deal
00007 in the Software without restriction, including without limitation the rights
00008 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00009 copies of the Software, and to permit persons to whom the Software is
00010 furnished to do so, subject to the following conditions:
00011 
00012 The above copyright notice and this permission notice shall be included in
00013 all copies or substantial portions of the Software.
00014 
00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00016 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00017 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00018 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00019 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00020 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00021 THE SOFTWARE.
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 //    if (debug) cout<<"Solver : Rsolve appelé..."<<endl;
00051     
00052     // Step 0 : we ask to the branching heuristic /////////////////////////////////
00053     // for the next variable to branch on. ////////////////////////////////////////
00054     int branchon = bh->nextVar(qs);                                              //
00056     
00057     // Step 1 : if there is no more variables to branch on, we ask for the ////////
00058     // final status of the QCSP, and return its result. ///////////////////////////
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     // Step 2 : if we must branch on a variable, first we check the status of  ////
00077     // the problem (it could be given at this moment) /////////////////////////////
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) { // If the answer is TRUE...                                    //
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) { // If the answer is FALSE...                                   //
00093         qs->backtrack();                                                         //
00094         bh->backtrack(qs);                                                       //
00095 #ifdef QDEBUG        
00096         cout<<"Solver : backtracking"<<endl;
00097 #endif
00098         return false;                                                            //
00099     }                                                                            //
00101     
00102     
00103     // Step 3 : the answer is "Don't know yet". We branch on the given variable. //
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 

Generated on Thu Feb 7 14:33:45 2008 for qecode by  doxygen 1.5.2