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(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)) ) {
00097 treated.push(MARKER);
00098 return ret;
00099 }
00100 else {
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;
00111 if ( eval->score(qs,pos) == score[pos] ) return;
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 }