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

Go to the documentation of this file.
00001 /*****************************************************************[qecore.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 #include "qecore.hh"
00024 
00025 #include <iostream>
00026 
00027 
00028 QSpace::QSpace() {}
00029 
00030 QSpace::QSpace(QSpace& qs) {
00031   n=qs.n;
00032   ps=qs.ps;
00033   v = new void*[n];
00034   type_of_v = qs.type_of_v;
00035 }
00036 
00037 
00038 #define MARKER size
00039 
00040 BranchingHeuristic::BranchingHeuristic(QSpace* qs,VariableHeuristic* ev) {
00041   
00042   eval=ev;
00043   size=qs->nv();
00044   score=new int[size];
00045   touched.push(MARKER);
00046   bloc=new int[size];
00047   
00048   blocks=1; bool q=qs->quantification(0);
00049   for (int i=0;i<size;i++) {
00050     if ((qs->quantification(i)) != q) {
00051       q=qs->quantification(i);
00052       blocks++;
00053     }
00054     bloc[i]=(blocks-1);
00055   }
00056 
00057   treated.push(MARKER);
00058 
00059   for (int i=0;i<size;i++) {
00060     score[i]=eval->score(qs,i);
00061   }
00062 
00063   ExtensiveComparator* c = new ExtensiveComparator(size,score);
00064 
00065   heaps=new cheap[blocks];
00066   for (int i=0;i<blocks;i++) {
00067     heaps[i]=cheap(c);
00068     heaps[i].setBounds(size);
00069   }
00070 
00071   
00072   int whichblock=0; bool qf=(qs->quantification(0));
00073   for (int i=0;i<size;i++) {
00074     if (qf != (qs->quantification(i)) ) {
00075       whichblock++; 
00076       qf=(qs->quantification(i));
00077     }
00078     heaps[whichblock].insert(i);
00079   }
00080   
00081   curHeap=0;
00082 }
00083 
00084 int BranchingHeuristic::nextVar(QSpace* qs) {
00085   int ret;
00086   touched.push(MARKER);
00087   while (! (heaps[blocks-1]).empty() ) {
00088     if (heaps[curHeap].empty()) {
00089       curHeap++;
00090     }
00091     if (curHeap >= blocks ) break;
00092     ret=heaps[curHeap].getminwopop();
00093     treated.push(ret);
00094 
00095     if( !(qs->subsumed(ret)) ) {      // If the variable is a good one to branch on.../!\ A INSTANCIATED VARIABLE MUST BE SUBSUMED
00096       treated.push(MARKER);
00097       return ret;
00098     }
00099     else {    // If the variable is subsumed, we delete it fron the heap...
00100         int rien=heaps[curHeap].getmin();
00101     }
00102   }
00103   treated.push(MARKER);
00104   return MARKER;
00105 }
00106 
00107 void BranchingHeuristic::updateVar(int pos, QSpace* qs) {
00108 
00109   if (!(heaps[bloc[pos]].inHeap(pos)) ) return;         // If the variable is not in the heap (so, it is already assigned or ignored), do nothing
00110   if ( eval->score(qs,pos) == score[pos] ) return;      // if no update is necessary, do nothing
00111 
00112   if ( eval->score(qs,pos) < score[pos] ) {
00113     score[pos]=eval->score(qs,pos);
00114     heaps[bloc[pos]].increase(pos);
00115   }
00116   else {
00117     score[pos]=eval->score(qs,pos);
00118     heaps[bloc[pos]].decrease(pos);
00119   }
00120 }
00121 
00122 void BranchingHeuristic::vartouched(int pos,QSpace* qs) {
00123   touched.push(pos);
00124   updateVar(pos,qs);
00125 }
00126 
00127 void BranchingHeuristic::backtrack(QSpace* qs) {
00128   if (treated.top() != MARKER ) {
00129     cout<<"Branching Heuristic : Probleme de pile... "<<endl;
00130     abort();
00131   }
00132   else {
00133     treated.pop();
00134   }
00135   while (treated.top() != MARKER ) {
00136     heaps[bloc[treated.top()]].insert(treated.top() );
00137     curHeap=bloc[treated.top()];
00138     treated.pop();
00139   }
00140   
00141   while (touched.top() != MARKER ) {
00142     updateVar(touched.top(),qs);
00143     touched.pop();
00144   }
00145 }

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