00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
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)) ) {
00096 treated.push(MARKER);
00097 return ret;
00098 }
00099 else {
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;
00110 if ( eval->score(qs,pos) == score[pos] ) return;
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 }