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

Generated on Fri Sep 21 16:36:36 2007 for qecode by  doxygen 1.5.2