00001
00010
00011 #include <iostream>
00012 #include <string>
00013 #include <vector>
00014 #include <fstream>
00015
00016 #include <cstdio>
00017 #include <cstdlib>
00018 #include <cmath>
00019 #include <cassert>
00020
00021 #include "graphCharac+.hh"
00022
00023
00024 using namespace std;
00025
00026
00027 GraphManipulator::GraphManipulator()
00028 {
00029 noTree=true;
00030 noTreeNeg=true;
00031 }
00032
00033 GraphManipulator::~GraphManipulator()
00034 {
00035
00036 }
00037
00038
00039
00040 void GraphManipulator::InitGraphforMatch(int nb_samples,int firstSample1,float seuilDiv,int kMT1a,int kMT1b){
00041
00042 seuilDivergence=seuilDiv;
00043 nbSamples=nb_samples;
00044 float *t_delay=new float[nb_samples-1];
00045 for (int i=0;i<nb_samples-1;i++){t_delay[i]=TimeDelay[i+1+firstSample1];}
00046 graph1=new SubGraph[1](nb_dim,nb_samples,t_delay);
00047 graph2=new SubGraph[1](nb_dim,nb_samples,t_delay);
00048 delete [] t_delay;
00049 AddMTtoSubGraph(1,kMT1a,seuilDiv,firstSample1,nb_samples);
00050 AddMTtoSubGraph(2,kMT1a,seuilDiv,firstSample1,nb_samples);
00051 if (kMT1b!=-1){
00052 AddMTtoSubGraph(1,kMT1b,seuilDiv,firstSample1,nb_samples);
00053 AddMTtoSubGraph(2,kMT1b,seuilDiv,firstSample1,nb_samples);
00054 }
00055 }
00056
00057 void GraphManipulator::InitGraphforMatchNeg(int nb_samples,float seuilDiv, int b){
00058
00059 seuilDivergence=seuilDiv;
00060 nbSamples=nb_samples;
00061 float *t_delay=new float[nb_samples-1];
00062 int endList=kMT2aSorted.size()-1;
00063 int firstSample1;int kMT1a;int kMT1b;
00064 kMT1a=kMT2aSorted[endList];
00065 if(b!=-1){kMT1b=kMT2bSorted[endList];}
00066 else{kMT1b=-1;}
00067 firstSample1=firstSample2Sorted[endList];
00068 for (int i=0;i<nb_samples-1;i++){t_delay[i]=TimeDelay[i+1+firstSample1];}
00069 graph1=new SubGraph[1](nb_dim,nb_samples,t_delay);
00070 graph2=new SubGraph[1](nb_dim,nb_samples,t_delay);
00071 delete [] t_delay;
00072 AddMTtoSubGraph(1,kMT1a,seuilDiv,firstSample1,nb_samples);
00073 AddMTtoSubGraph(2,kMT1a,seuilDiv,firstSample1,nb_samples);
00074 if (kMT1b!=-1){
00075 AddMTtoSubGraph(1,kMT1b,seuilDiv,firstSample1,nb_samples);
00076 AddMTtoSubGraph(2,kMT1b,seuilDiv,firstSample1,nb_samples);
00077 }
00078
00079 }
00080
00081 void GraphManipulator::FixWeights(float weightMTinTLDiff,float TL1TL2Div,float flowDiff,float divDiff,float MIDiff,float divMTaMTbCost,float timeDiff,bool meanCalcul){
00082
00083
00084 matchCostObject[0].FixCostFuncWeigths(weightMTinTLDiff,TL1TL2Div,flowDiff,divDiff,MIDiff,divMTaMTbCost,timeDiff,meanCalcul);
00085 predefinedList=false;
00086 factorReducList=1.;
00087
00088 }
00089
00090 void GraphManipulator::FixLearnedWeights(int posOrNeg, bool meanCalcul,float factorReduc){
00091
00092 float weightMTinTLDiff;float TL1TL2Div;float flowDiff;float divDiff;float MIDiff;float divMTaMTbCost;float timeDiff;
00093 if (posOrNeg==1){
00094 weightMTinTLDiff=paraSearchObject.estimPara[0];
00095 TL1TL2Div=paraSearchObject.estimPara[1];
00096 flowDiff=paraSearchObject.estimPara[2];
00097 divDiff=paraSearchObject.estimPara[3];
00098 MIDiff=paraSearchObject.estimPara[4];
00099 divMTaMTbCost=paraSearchObject.estimPara[5];
00100 timeDiff=paraSearchObject.estimPara[6];
00101 }
00102 if (posOrNeg==-1){
00103 weightMTinTLDiff=paraSearchObjectNeg.estimPara[0];
00104 TL1TL2Div=paraSearchObjectNeg.estimPara[1];
00105 flowDiff=paraSearchObjectNeg.estimPara[2];
00106 divDiff=paraSearchObjectNeg.estimPara[3];
00107 MIDiff=paraSearchObjectNeg.estimPara[4];
00108 divMTaMTbCost=paraSearchObjectNeg.estimPara[5];
00109 timeDiff=paraSearchObjectNeg.estimPara[6];
00110 }
00111 matchCostObject[0].FixCostFuncWeigths(weightMTinTLDiff,TL1TL2Div,flowDiff,divDiff,MIDiff,divMTaMTbCost,timeDiff,meanCalcul);
00112 predefinedList=true;
00113 factorReducList=factorReduc;
00114
00115 }
00116
00117 void GraphManipulator::CreateMatchCostObject(){
00118
00119 matchCostObject=new MatchCost[1](graph1,graph2);
00120
00121 }
00122
00123 float GraphManipulator::Match(int posOrNeg, int min_Sample, int max_Sample,int nbMiniBranches){
00124
00125
00126 float cost,miniCost=Inf;
00127 bool searchEnd=false;bool calculate=true;
00128 int nb_samples=graph1[0].nbTimeSamp;
00129 float *t_delay=new float[nb_samples-1];
00130 for (int i=0;i<nb_samples-1;i++){t_delay[i]=1;}
00131 minSample=min_Sample; maxSample=max_Sample;
00132 maxSample+=1;
00133 int kMT2a=0;
00134 int kMT2b=1;
00135 int firstSample2=minSample;
00136 unsigned long int auxIndex;
00137
00138 int firstSample2Min=-1;
00139 int kMT2aMin=-1;
00140 int kMT2bMin=-1;
00141
00142 if (posOrNeg==1){
00143 if (!noTree){
00144 for (int i=0;i<nb_TimelessClasses;i++){
00145 for (int j=0;j<nb_TimelessClasses;j++){
00146 for (int k=0;k<maxSample;k++){
00147 delete [] searchTreeCollec[i][j][k];
00148 delete [] costs[i][j][k];
00149 }
00150 delete [] searchTreeCollec[i][j];
00151 delete [] costs[i][j];
00152 }
00153 delete [] searchTreeCollec[i];
00154 delete [] costs[i];
00155 }
00156 delete [] searchTreeCollec;
00157 delete [] costs;
00158
00159 costSorted.clear();
00160 kMT2aSorted.clear();
00161 kMT2bSorted.clear();
00162 firstSample2Sorted.clear();
00163 }
00164
00165
00166 searchTreeCollec= new SearchTree***[nb_TimelessClasses];
00167 costs=new float***[nb_TimelessClasses];
00168 for (int i=0;i<nb_TimelessClasses;i++){
00169 searchTreeCollec[i]=new SearchTree**[nb_TimelessClasses];
00170 costs[i]=new float**[nb_TimelessClasses];
00171 for (int j=0;j<nb_TimelessClasses;j++){
00172 searchTreeCollec[i][j]=new SearchTree*[maxSample];
00173 costs[i][j]=new float*[maxSample];
00174 for (int k=0;k<maxSample;k++){
00175 costs[i][j][k]=new float[8];
00176 searchTreeCollec[i][j][k]=new SearchTree[1];
00177 for (int l=0;l<8;l++){costs[i][j][k][l]=-1;}
00178 }
00179 }
00180 }
00181 noTree=false;
00182 }
00183
00184 if (posOrNeg==-1){
00185 predefinedList=true;
00186 if (!noTreeNeg){
00187 for (int i=0;i<nb_TimelessClasses;i++){
00188 for (int j=0;j<nb_TimelessClasses;j++){
00189 for (int k=0;k<maxSample;k++){
00190 delete [] searchTreeCollecNeg[i][j][k];
00191 delete [] costsNeg[i][j][k];
00192 }
00193 delete [] searchTreeCollecNeg[i][j];
00194 delete [] costsNeg[i][j];
00195 }
00196 delete [] searchTreeCollecNeg[i];
00197 delete [] costsNeg[i];
00198 }
00199 delete [] searchTreeCollecNeg;
00200 delete [] costsNeg;
00201
00202 }
00203
00204 searchTreeCollecNeg= new SearchTree***[nb_TimelessClasses];
00205 costsNeg=new float***[nb_TimelessClasses];
00206 for (int i=0;i<nb_TimelessClasses;i++){
00207 searchTreeCollecNeg[i]=new SearchTree**[nb_TimelessClasses];
00208 costsNeg[i]=new float**[nb_TimelessClasses];
00209 for (int j=0;j<nb_TimelessClasses;j++){
00210 searchTreeCollecNeg[i][j]=new SearchTree*[maxSample];
00211 costsNeg[i][j]=new float*[maxSample];
00212 for (int k=0;k<maxSample;k++){
00213 costsNeg[i][j][k]=new float[8];
00214 searchTreeCollecNeg[i][j][k]=new SearchTree[1];
00215 for (int l=0;l<8;l++){costsNeg[i][j][k][l]=-1;}
00216 }
00217 }
00218 }
00219 noTreeNeg=false;
00220 }
00221
00222 while (!searchEnd){
00223
00224 for (int i=0;i<nb_samples-1;i++){t_delay[i]=TimeDelay[i+1+firstSample2];}
00225 delete [] graph2;
00226 graph2=new SubGraph[1](nb_dim,nb_samples,t_delay);
00227
00228
00229 AddMTtoSubGraph(2,kMT2a,seuilDivergence,firstSample2,nb_samples);
00230 if (graph1[0].nbMTClusters==2){AddMTtoSubGraph(2,kMT2b,seuilDivergence,firstSample2,nb_samples);}
00231 else{kMT2b=kMT2a;}
00232 matchCostObject[0].UpdateGraph2(graph2);
00233 calculate=true;
00234
00235
00236 if (graph1[0].nbMTClusters==2&&predefinedList==false){
00237 calculate=false;factorReducList=4.;
00238 for (int i=0;i<sizeList;i++){
00239
00240 if(kMT2aSortedAux[i]==kMT2a&&firstSample2aSortedAux[i]==firstSample2){
00241 if (kMT2aCostAux[i]<Inf&& i<=(int)((float)sizeList/factorReducList)){
00242 for (int j=0;j<sizeList;j++){
00243 if(kMT2bSortedAux[j]==kMT2b&&firstSample2bSortedAux[j]==firstSample2){
00244 if (kMT2bCostAux[j]<Inf&& j<=(int)((float)sizeList/factorReducList)){calculate=true;}
00245 }
00246 }
00247 }
00248 }
00249 }
00250 for (int i=0;i<sizeList;i++){
00251 if(kMT2aSortedAux[i]==kMT2b&&firstSample2aSortedAux[i]==firstSample2){
00252 if (kMT2aCostAux[i]<Inf&& i<=(int)((float)sizeList/factorReducList)){
00253 for (int j=0;j<sizeList;j++){
00254 if(kMT2bSortedAux[j]==kMT2a&&firstSample2bSortedAux[j]==firstSample2){
00255 if (kMT2bCostAux[j]<Inf&& j<=(int)((float)sizeList/factorReducList)){calculate=true;}
00256 }
00257 }
00258 }
00259 }
00260 }
00261 factorReducList=1.;
00262 }
00263
00264 if (predefinedList){
00265 calculate=false;
00266 for (int i=0;i<sizeList;i++){
00267 if(kMT2aSortedAux[i]==kMT2a&&kMT2bSortedAux[i]==kMT2b&&firstSample2aSortedAux[i]==firstSample2){
00268 if (kMT2aCostAux[i]<Inf && i<=(int)((float)sizeList/factorReducList)){calculate=true;}
00269 if (i==(sizeList-1)){calculate=true;}
00270 }
00271 }
00272 }
00273
00274
00275 if (graph1[0].nbMTClusters==2&&kMT2a==kMT2b){calculate=false;}
00276
00277
00278 if (calculate){
00279 if (posOrNeg==1){ cost=matchCostObject[0].InexactMatchCost(nbMiniBranches,searchTreeCollec[kMT2a][kMT2b][firstSample2],costs[kMT2a][kMT2b][firstSample2]);
00280 }
00281 if (posOrNeg==-1){
00282 cost=matchCostObject[0].InexactMatchCost(nbMiniBranches,searchTreeCollecNeg[kMT2a][kMT2b][firstSample2],costsNeg[kMT2a][kMT2b][firstSample2]);
00283
00284 }
00285 }
00286 else{cost=Inf;}
00287
00288
00289 if (cost<miniCost){
00290 kMT2aMin=kMT2a;
00291 kMT2bMin=kMT2b;
00292 firstSample2Min=firstSample2;
00293 miniCost=cost;
00294 }
00295
00296 if (calculate){
00297 if (posOrNeg==1){
00298 int *auxkMT2aSorted=new int[1];
00299 int *auxkMT2bSorted=new int[1];
00300 int *auxfirstSample2Sorted=new int[1];
00301 float *auxcostSorted=new float[1];
00302 auxIndex=kMT2aSorted.size();
00303 for (unsigned long int k=0;k<kMT2aSorted.size();k++){
00304 if (cost<costSorted[k]){
00305 auxIndex=k;
00306 break;
00307 }
00308 }
00309 auxkMT2aSorted[0]=kMT2a;auxkMT2bSorted[0]=kMT2b;auxfirstSample2Sorted[0]=firstSample2;auxcostSorted[0]=cost;
00310 if (kMT2aSorted.size()!=0){
00311 vector<int>::iterator theIterator1 = kMT2aSorted.begin()+auxIndex;
00312 kMT2aSorted.insert(theIterator1,auxkMT2aSorted[0]);
00313 vector<int>::iterator theIterator2 = kMT2bSorted.begin()+auxIndex;
00314 kMT2bSorted.insert(theIterator2,auxkMT2bSorted[0]);
00315 vector<int>::iterator theIterator3 = firstSample2Sorted.begin()+auxIndex;
00316 firstSample2Sorted.insert(theIterator3,auxfirstSample2Sorted[0]);
00317 vector<float>::iterator theIterator4 = costSorted.begin()+auxIndex;
00318 costSorted.insert(theIterator4,auxcostSorted[0]);
00319 }
00320 else{
00321 kMT2aSorted.push_back(auxkMT2aSorted[0]);
00322 kMT2bSorted.push_back(auxkMT2bSorted[0]);
00323 firstSample2Sorted.push_back(auxfirstSample2Sorted[0]);
00324 costSorted.push_back(auxcostSorted[0]);
00325 }
00326 }
00327 }
00328
00329
00330 firstSample2++;
00331 if (kMT2a==(nb_TimelessClasses-1)&&(firstSample2+nb_samples)>(maxSample)){
00332 searchEnd=true;
00333 }
00334 else{
00335 if ((firstSample2+nb_samples)>(maxSample)){
00336 firstSample2=minSample;
00337 kMT2b++;
00338 if (graph1[0].nbMTClusters!=2){kMT2b=(nb_TimelessClasses-1);}
00339 if (kMT2b==(nb_TimelessClasses-1)){kMT2b=kMT2a+1;kMT2a++;}
00340 }
00341 }
00342
00343
00344 }
00345
00346
00347 delete [] t_delay;
00348 factorReducList=1;
00349 return cost;
00350 }
00351
00352 void GraphManipulator::SaveSortedList(int a,int b){
00353
00354 if (a==1&&b==0){
00355 sizeList=(int)kMT2aSorted.size();
00356 kMT2aSortedAux=new int[kMT2aSorted.size()];
00357 firstSample2aSortedAux=new int[kMT2aSorted.size()];
00358 kMT2aCostAux=new float[kMT2aSorted.size()];
00359 for (unsigned long int l=0;l<kMT2aSorted.size();l++){
00360 kMT2aSortedAux[l]=kMT2aSorted[l];
00361 firstSample2aSortedAux[l]=firstSample2Sorted[l];
00362 kMT2aCostAux[l]=costSorted[l];
00363 }
00364 }
00365 if (a==0&&b==1){
00366 sizeList=(int)kMT2aSorted.size();
00367 kMT2bSortedAux=new int[kMT2aSorted.size()];
00368 firstSample2bSortedAux=new int[kMT2aSorted.size()];
00369 kMT2bCostAux=new float[kMT2aSorted.size()];
00370 for (unsigned long int l=0;l<kMT2aSorted.size();l++){
00371 kMT2bSortedAux[l]=kMT2aSorted[l];
00372 firstSample2bSortedAux[l]=firstSample2Sorted[l];
00373 kMT2bCostAux[l]=costSorted[l];
00374 }
00375 }
00376 if (a==1&&b==1){
00377 sizeList=(int)kMT2aSorted.size();
00378 kMT2aSortedAux=new int[kMT2aSorted.size()];
00379 firstSample2aSortedAux=new int[kMT2aSorted.size()];
00380 kMT2aCostAux=new float[kMT2aSorted.size()];
00381 kMT2bSortedAux=new int[kMT2bSorted.size()];
00382 firstSample2bSortedAux=new int[kMT2bSorted.size()];
00383 kMT2bCostAux=new float[kMT2bSorted.size()];
00384 for (unsigned long int l=0;l<kMT2aSorted.size();l++){
00385 kMT2aSortedAux[l]=kMT2aSorted[l];
00386 kMT2bSortedAux[l]=kMT2bSorted[l];
00387 firstSample2aSortedAux[l]=firstSample2Sorted[l];
00388 kMT2aCostAux[l]=costSorted[l];
00389 }
00390
00391 }
00392 delete [] graph1;
00393 delete [] graph2;
00394 delete [] matchCostObject;
00395 }
00396
00397 void GraphManipulator::InitParaLearn(int a, int b){
00398
00399 int nbLevel=1000;
00400
00401
00402 if (a==1){
00403 paraSearchObject.Allocate(7,nbLevel,nb_TimelessClasses,graph1[0].nbMTClusters,minSample,maxSample,graph1[0].nbTimeSamp);
00404 paraSearchObject.Init(searchTreeCollec,costs,&kMT2aSorted,&kMT2bSorted,&firstSample2Sorted);
00405 paraSearchObject.MaxCostalues();
00406 }
00407
00408 if (b==1){ paraSearchObjectNeg.Allocate(7,nbLevel,nb_TimelessClasses,graph1[0].nbMTClusters,minSample,maxSample,graph1[0].nbTimeSamp);
00409 paraSearchObjectNeg.Init(searchTreeCollecNeg,costsNeg,&kMT2aSorted,&kMT2bSorted,&firstSample2Sorted);
00410 paraSearchObjectNeg.MaxCostalues();
00411 }
00412
00413 divergences=new float[7];
00414 for (int i=0;i<7;i++){divergences[i]=0;}
00415 sizeList=(int)kMT2aSorted.size();
00416
00417 }
00418
00419
00420 void GraphManipulator::ScaleParaLearnForSG(int a, int b,bool scale){
00421
00422
00423
00424
00425 if (a==1){
00426 paraSearchObject.Init(searchTreeCollec,costs,&kMT2aSorted,&kMT2bSorted,&firstSample2Sorted);
00427 if (scale) {paraSearchObject.MaxCostalues();}
00428 }
00429
00430 if (b==1){
00431 paraSearchObjectNeg.Init(searchTreeCollecNeg,costsNeg,&kMT2aSorted,&kMT2bSorted,&firstSample2Sorted);
00432 if (scale){paraSearchObjectNeg.MaxCostalues();}
00433 }
00434 sizeList=(int)kMT2aSorted.size();
00435
00436 }
00437
00438
00439 void GraphManipulator::LearnRefGraph(int posOrNeg){
00440
00441 if (posOrNeg==-1){
00442 int indexEnd=kMT2aSorted.size()-1;
00443 Learn(-1,kMT2aSorted[indexEnd],kMT2bSorted[indexEnd],firstSample2Sorted[indexEnd]);
00444
00445 }
00446 if (posOrNeg==1){
00447 Learn(1,kMT2aSorted[0],kMT2bSorted[0],firstSample2Sorted[0]);
00448
00449 }
00450 }
00451
00452 float GraphManipulator::Learn(int posOrNeg,int interestkMT2a,int interestkMT2b,int interestfirstSample2){
00453
00454
00455 if (posOrNeg>0){paraSearchObject.MMSEestim(interestkMT2a,interestkMT2b,interestfirstSample2);}
00456 if (posOrNeg<0){paraSearchObjectNeg.MMSEestim(interestkMT2a,interestkMT2b,interestfirstSample2);}
00457
00458
00459 kMT2aSorted.clear();
00460 kMT2bSorted.clear();
00461 firstSample2Sorted.clear();
00462 costSorted.clear();
00463 float cost,miniCost=Inf;
00464
00465 float negProb=0;float posProb=0;
00466 int firstSample2Min=-1;
00467 int kMT2aMin=-1;
00468 int kMT2bMin=-1;
00469 unsigned long int auxIndex;
00470
00471
00472
00473 float priorParaPos=0;
00474 for (int kPara=0;kPara<paraSearchObject.nbPara;kPara++){priorParaPos+=paraSearchObject.estimPara[kPara];}
00475 float priorParaNeg=0;
00476 for (int kPara=0;kPara<paraSearchObjectNeg.nbPara;kPara++){priorParaNeg+=paraSearchObjectNeg.estimPara[kPara];}
00477
00478
00479
00480
00481 float maxPosCost=-1;
00482 float maxNegCost=-1;
00483
00484 vector<float> costList;
00485 vector<float> costList2;
00486
00487
00488 for (int i=0;i<nb_TimelessClasses;i++){
00489 for (int j=0;j<nb_TimelessClasses;j++){
00490 for (int k=0;k<maxSample;k++){
00491 if (!paraSearchObject.treeCollec[i][j][k][0].emptyTree||!paraSearchObjectNeg.treeCollec[i][j][k][0].emptyTree){
00492
00493 if (priorParaPos!=0){
00494 float *auxCostList=new float[1];
00495 auxCostList[0]=paraSearchObject.ReevaluateMatch(i,j,k);
00496
00497 if (auxCostList[0]>maxPosCost&&auxCostList[0]<1e5){maxPosCost=auxCostList[0]*1.001;}
00498 costList.push_back(auxCostList[0]);
00499
00500 }
00501 if (priorParaNeg!=0){
00502 float *auxCostList2=new float[1];
00503 auxCostList2[0]=paraSearchObjectNeg.ReevaluateMatch(i,j,k);
00504
00505 if (auxCostList2[0]>maxNegCost&&auxCostList2[0]<1e5){maxNegCost=auxCostList2[0]*1.001;}
00506 costList2.push_back(auxCostList2[0]);
00507 }
00508 }
00509 }
00510 }
00511 }
00512
00513 float sumCostPos=0;float sumCostNeg=0;
00514 int costListIndex=0;int costListIndex2=0;
00515 for (int i=0;i<nb_TimelessClasses;i++){
00516 for (int j=0;j<nb_TimelessClasses;j++){
00517 for (int k=0;k<maxSample;k++){
00518 if (!paraSearchObject.treeCollec[i][j][k][0].emptyTree||!paraSearchObjectNeg.treeCollec[i][j][k][0].emptyTree){
00519
00520 if (priorParaPos!=0){
00521
00522 posProb=(float)(maxPosCost-costList[costListIndex])/(float)maxPosCost;
00523 if (posProb<0){posProb=0;}
00524 sumCostPos+=posProb;
00525 costListIndex++;
00526 }
00527
00528 if (priorParaNeg!=0){
00529
00530 negProb=(float)(maxNegCost-costList2[costListIndex2])/(float)maxNegCost;
00531 if (negProb<0){negProb=0;}
00532 sumCostNeg+=negProb;
00533 costListIndex2++;
00534 }
00535 }
00536 }
00537 }
00538 }
00539
00540 costListIndex=0;
00541 costListIndex2=0;
00542
00543 for (int i=0;i<nb_TimelessClasses;i++){
00544 for (int j=0;j<nb_TimelessClasses;j++){
00545 for (int k=0;k<maxSample;k++){
00546 if (!paraSearchObject.treeCollec[i][j][k][0].emptyTree||!paraSearchObjectNeg.treeCollec[i][j][k][0].emptyTree){
00547
00548
00549 if (priorParaPos!=0){
00550
00551 posProb=(float)(maxPosCost-costList[costListIndex])/(float)maxPosCost;
00552 if (posProb<0){posProb=0;}
00553 posProb/=(float)sumCostPos;
00554 costListIndex++;
00555 }
00556 else{posProb=1./(float)sizeList;}
00557
00558
00559 if (priorParaNeg!=0){
00560
00561 negProb=(float)(maxNegCost-costList2[costListIndex2])/(float)maxNegCost;
00562 if (negProb<0){negProb=0;}
00563 negProb/=(float)sumCostNeg;
00564 costListIndex2++;
00565 }
00566 else{negProb=1./(float)sizeList;}
00567
00568
00569 cost=posProb/(float)(negProb+posProb);
00570
00571
00572
00573 if (cost<miniCost){
00574 kMT2aMin=i;
00575 kMT2bMin=j;
00576 firstSample2Min=k;
00577 miniCost=cost;
00578 }
00579
00580 int *auxkMT2aSorted=new int[1];
00581 int *auxkMT2bSorted=new int[1];
00582 int *auxfirstSample2Sorted=new int[1];
00583 float *auxcostSorted=new float[1];
00584 auxIndex=kMT2aSorted.size();
00585 for (unsigned long int l=0;l<kMT2aSorted.size();l++){
00586 if (cost>costSorted[l]){
00587 auxIndex=l;
00588 break;
00589 }
00590 }
00591 auxkMT2aSorted[0]=i;auxkMT2bSorted[0]=j;auxfirstSample2Sorted[0]=k;auxcostSorted[0]=cost;
00592 if (kMT2aSorted.size()!=0){
00593 vector<int>::iterator theIterator1 = kMT2aSorted.begin()+auxIndex;
00594 kMT2aSorted.insert(theIterator1,auxkMT2aSorted[0]);
00595 vector<int>::iterator theIterator2 = kMT2bSorted.begin()+auxIndex;
00596 kMT2bSorted.insert(theIterator2,auxkMT2bSorted[0]);
00597 vector<int>::iterator theIterator3 = firstSample2Sorted.begin()+auxIndex;
00598 firstSample2Sorted.insert(theIterator3,auxfirstSample2Sorted[0]);
00599 vector<float>::iterator theIterator4 = costSorted.begin()+auxIndex;
00600 costSorted.insert(theIterator4,auxcostSorted[0]);
00601 }
00602 else{
00603 kMT2aSorted.push_back(auxkMT2aSorted[0]);
00604 kMT2bSorted.push_back(auxkMT2bSorted[0]);
00605 firstSample2Sorted.push_back(auxfirstSample2Sorted[0]);
00606 costSorted.push_back(auxcostSorted[0]);
00607 }
00608 }
00609 }
00610 }
00611 }
00612 StrengthProbLink();
00613 VariancePosterior();
00614 ConvergenceQuality();
00615 paraSearchObject.Init(&kMT2aSorted,&kMT2bSorted,&firstSample2Sorted);
00616 paraSearchObjectNeg.Init(&kMT2aSorted,&kMT2bSorted,&firstSample2Sorted);
00617
00618 costList2.clear();
00619 costList.clear();
00620
00621 return miniCost;
00622
00623 }
00624
00625 void GraphManipulator::EraseMemLearn(int posNeg){
00626
00627 if (posNeg==1||posNeg==2){paraSearchObject.ErasePdf();}
00628 if (posNeg==-1||posNeg==2){paraSearchObjectNeg.ErasePdf();}
00629 }
00630
00631
00632 void GraphManipulator::PrintAPostList(){
00633
00634
00635 for (int l=(int)(kMT2aSorted.size()-1);l>=0;l--){
00636 cout<<"graph posterior probability : "<< costSorted[l]<<" for MT class : "
00637 <<kMT2aSorted[l]<<"&"<<kMT2bSorted[l]<<" and first sample : " <<firstSample2Sorted[l]<<endl;
00638 }
00639
00640 }
00641
00642
00643 void GraphManipulator::WriteAPostList(){
00644
00645 char *path=new char[128];
00646 FILE *ifp;
00647 strcpy(path,"./APostList");
00648 ifp=NULL;
00649 assert (ifp=fopen(path,"w"));
00650
00651 if (paraSearchObject.estimPara[0]!=0||paraSearchObjectNeg.estimPara[0]!=0){fprintf(ifp,"%f %s",divergences[0]," ");}
00652 else{fprintf(ifp,"%f %s",-1.," ");}
00653 if (paraSearchObject.estimPara[1]!=0||paraSearchObjectNeg.estimPara[1]!=0){fprintf(ifp,"%f %s",divergences[1]," ");}
00654 else{fprintf(ifp,"%f %s",-1.," ");}
00655 if (paraSearchObject.estimPara[2]!=0||paraSearchObjectNeg.estimPara[2]!=0){fprintf(ifp,"%f %s",divergences[2]," ");}
00656 else{fprintf(ifp,"%f %s",-1.," ");}
00657 if (paraSearchObject.estimPara[3]!=0||paraSearchObjectNeg.estimPara[3]!=0){fprintf(ifp,"%f %s",divergences[3]," ");}
00658 else{fprintf(ifp,"%f %s",-1.," ");}
00659 if (paraSearchObject.estimPara[4]!=0||paraSearchObjectNeg.estimPara[4]!=0){fprintf(ifp,"%f %s",divergences[4]," ");}
00660 else{fprintf(ifp,"%f %s",-1.," ");}
00661 if (paraSearchObject.estimPara[5]!=0||paraSearchObjectNeg.estimPara[5]!=0){fprintf(ifp,"%f %s",divergences[5]," ");}
00662 else{fprintf(ifp,"%f %s",-1.," ");}
00663 if (paraSearchObject.estimPara[6]!=0||paraSearchObjectNeg.estimPara[6]!=0){fprintf(ifp,"%f\n",divergences[6]);}
00664 else{fprintf(ifp,"%f\n",-1.);}
00665 VariancePosterior();
00666 fprintf(ifp,"%f %s %f\n",meanPosterior," ",sqrt(varPosterior));
00667 fprintf(ifp,"%f %s %f\n",convQualityPos," ",convQualityNeg);
00668 fprintf(ifp,"%d\n",nbSamples);
00669 for (int l=0;l<(int)kMT2aSorted.size();l++){
00670 fprintf(ifp,"%d %s %d %s %d %s %f\n",kMT2aSorted[l]," ",kMT2bSorted[l]," ",firstSample2Sorted[l]," ", costSorted[l]);
00671 }
00672 assert (EOF!=fclose(ifp));
00673
00674 }
00675
00676
00677 void GraphManipulator::PrintProbaLink(){
00678
00679 cout<<"\nDIVERGENCES"<<endl;
00680 cout<<"_____________________________"<<endl;
00681
00682 if (paraSearchObject.estimPara[0]!=0||paraSearchObjectNeg.estimPara[0]!=0){cout<<"Cluster weights : "<<divergences[0]<<endl;}
00683 else{cout<<"Cluster weights : "<<"------"<<endl;}
00684 if (paraSearchObject.estimPara[1]!=0||paraSearchObjectNeg.estimPara[1]!=0){cout<<"Cluster Absolute Position : "<<divergences[1]<<endl;}
00685 else{cout<<"Cluster Absolute Position : "<<"------"<<endl;}
00686 if (paraSearchObject.estimPara[2]!=0||paraSearchObjectNeg.estimPara[2]!=0){cout<<"Cluster Flow : "<<divergences[2]<<endl;}
00687 else{cout<<"Cluster Flow : "<<"------"<<endl;}
00688 if (paraSearchObject.estimPara[3]!=0||paraSearchObjectNeg.estimPara[3]!=0){cout<<"Cluster change : "<<divergences[3]<<endl;}
00689 else{cout<<"Cluster change : "<<"------"<<endl;}
00690 if (paraSearchObject.estimPara[4]!=0||paraSearchObjectNeg.estimPara[4]!=0){cout<<"Cluster Mutual Information : "<<divergences[4]<<endl;}
00691 else{cout<<"Cluster Mutual Information : "<<"------"<<endl;}
00692 if (paraSearchObject.estimPara[5]!=0||paraSearchObjectNeg.estimPara[5]!=0){cout<<"MT Clusters Interaction : "<<divergences[5]<<endl;}
00693 else{cout<<"MT Clusters Interaction : "<<"------"<<endl;}
00694 if (paraSearchObject.estimPara[6]!=0||paraSearchObjectNeg.estimPara[6]!=0){cout<<"Time Delay : "<<divergences[6]<<endl;}
00695 else{cout<<"Time Delay : "<<"------"<<endl;}
00696 cout<<"\nPOSTERIOR DISTRIBUTION "<<endl;
00697 cout<<"_____________________________"<<endl;
00698 cout<<"Posterior mean : "<<meanPosterior<<endl;
00699 cout<<"Posterior Standard deviation : "<<sqrt(varPosterior)<<endl;
00700 cout<<"\nCONVERGENCE "<<endl;
00701 cout<<"_____________________________"<<endl;
00702 cout<<"Norm parameter vector+ difference: "<<convQualityPos<<endl;
00703 cout<<"Norm parameter vector- difference: "<<convQualityNeg<<endl;
00704 cout<<""<<endl;
00705 }
00706
00707
00708 void GraphManipulator::StrengthProbLink(){
00709
00710 float sumPos=0;float meanPos;float varPos;float hPos;
00711 float sumNeg=0;float meanNeg;float varNeg;float hNeg;
00712 float sumDiv=0;
00713 float p,q;
00714 float piCst=sqrt(2*M_PI);
00715 for (int kPara=0;kPara<paraSearchObject.nbPara;kPara++){
00716
00717
00718 sumPos=0;sumNeg=0;meanPos=0;meanNeg=0;varPos=0;varNeg=0;
00719 for (int i=0;i<paraSearchObject.nbQuantifLevel;i++){
00720 sumPos+=paraSearchObject.pdf[kPara][i];
00721 sumNeg+=paraSearchObjectNeg.pdf[kPara][i];
00722 }
00723 for (int i=0;i<paraSearchObject.nbQuantifLevel;i++){
00724 meanPos+=paraSearchObject.pdf[kPara][i]*i;
00725 meanNeg+=paraSearchObjectNeg.pdf[kPara][i]*i;
00726 }
00727 meanPos/=sumPos;
00728 meanNeg/=sumNeg;
00729 for (int i=0;i<paraSearchObject.nbQuantifLevel;i++){
00730 varPos+=(i-meanPos)*(i-meanPos)*paraSearchObject.pdf[kPara][i];
00731 varNeg+=(i-meanNeg)*(i-meanNeg)*paraSearchObjectNeg.pdf[kPara][i];
00732 }
00733 varPos/=sumPos;
00734 varNeg/=sumNeg;
00735 sumDiv=0;
00736
00737
00738 hPos=1.059*sqrt(varPos)/pow(sumPos,0.2);if (hPos==0){hPos=1.059/pow(sumPos,0.2);}
00739 hNeg=1.059*sqrt(varNeg)/pow(sumNeg,0.2);if (hNeg==0){hNeg=1.059/pow(sumNeg,0.2);}
00740
00741
00742 float auxSUMPos=0;float auxSUMNeg=0;
00743 for (int i=0;i<paraSearchObject.nbQuantifLevel;i++){
00744 p=0;q=0;
00745
00746 for (int k=0;k<paraSearchObject.nbQuantifLevel;k++){
00747 if (paraSearchObject.pdf[kPara][k]>0){p+=exp(-(i-k)*(i-k)/(2.*hPos*hPos))*paraSearchObject.pdf[kPara][k]+1./(float)paraSearchObject.nbQuantifLevel;}
00748 if (paraSearchObjectNeg.pdf[kPara][k]>0){q+=exp(-(i-k)*(i-k)/(2.*hNeg*hNeg))*paraSearchObjectNeg.pdf[kPara][k]+1./(float)paraSearchObject.nbQuantifLevel;}
00749 }
00750 p/=(piCst*hPos);p/=sumPos;
00751 q/=(piCst*hNeg);q/=sumNeg;
00752 if (sumPos==0){p=1./(float)paraSearchObject.nbQuantifLevel;}
00753 if (sumNeg==0){q=1./(float)paraSearchObject.nbQuantifLevel;}
00754
00755 auxSUMPos+=p;
00756 auxSUMNeg+=q;
00757
00758 }
00759
00760 for (int i=0;i<paraSearchObject.nbQuantifLevel;i++){
00761 p=0;q=0;
00762
00763 for (int k=0;k<paraSearchObject.nbQuantifLevel;k++){
00764 if (paraSearchObject.pdf[kPara][k]>0){p+=exp(-(i-k)*(i-k)/(2.*hPos*hPos))*paraSearchObject.pdf[kPara][k]+1./(float)paraSearchObject.nbQuantifLevel;;}
00765 if (paraSearchObjectNeg.pdf[kPara][k]>0){q+=exp(-(i-k)*(i-k)/(2.*hNeg*hNeg))*paraSearchObjectNeg.pdf[kPara][k]+1./(float)paraSearchObject.nbQuantifLevel;;}
00766 }
00767 p/=(piCst*hPos);p/=sumPos;p/=auxSUMPos;
00768 q/=(piCst*hNeg);q/=sumNeg;q/=auxSUMNeg;
00769 if (sumPos==0){p=1./(float)paraSearchObject.nbQuantifLevel;}
00770 if (sumNeg==0){q=1./(float)paraSearchObject.nbQuantifLevel;}
00771
00772 sumDiv+=(p-q)*log(p/q);
00773 }
00774 divergences[kPara]=sumDiv;
00775
00776 }
00777
00778 }
00779
00780
00781 void GraphManipulator::VariancePosterior(){
00782
00783 meanPosterior=0;varPosterior=0;
00784 for (unsigned long int l=0;l<kMT2aSorted.size();l++){meanPosterior+=costSorted[l];}
00785 meanPosterior/=kMT2aSorted.size();
00786 for (unsigned long int l=0;l<kMT2aSorted.size();l++){varPosterior+=(costSorted[l]-meanPosterior)*(costSorted[l]-meanPosterior);}
00787 varPosterior/=kMT2aSorted.size();
00788 }
00789
00790 void GraphManipulator::ConvergenceQuality(){
00791
00792 convQualityPos=0;
00793 convQualityNeg=0;
00794 float aux=0;
00795 for (int k=0;k<paraSearchObject.nbPara;k++){
00796 aux=(paraSearchObject.estimParaPrevious[k]-paraSearchObject.estimPara[k]);
00797 convQualityPos+=aux*aux;
00798 aux=(paraSearchObjectNeg.estimParaPrevious[k]-paraSearchObjectNeg.estimPara[k]);
00799 convQualityNeg+=aux*aux;
00800 }
00801 convQualityPos=sqrt(convQualityPos);
00802 convQualityNeg=sqrt(convQualityNeg);
00803
00804 }
00805
00806 void GraphManipulator::AddMTtoSubGraph(int subGraph,int kMT,float seuilDiv,int firstSample,int nb_samples){
00807
00808
00809 float **mean; float ***cov;unsigned long int weight;
00810 mean=new float*[nb_samples];cov=new float**[nb_samples];
00811 for (int i=0;i<nb_samples;i++){
00812 mean[i]=new float[nb_dim];
00813 cov[i]=new float*[nb_dim];
00814 for (int j=0;j<nb_dim;j++){cov[i][j]=new float[nb_dim];}
00815 }
00816 int nbClusters;
00817 int auxIndex,auxIndex1,auxIndex2;
00818 float minDiv;
00819 int *IndexkTLminDiv=new int[nb_samples+firstSample];
00820
00821
00822 for (int t=firstSample;t<nb_samples+firstSample;t++){
00823 weight=TmpClass[kMT].MTWeight[t][0];
00824 Index_CurrentImage=t;
00825 for (int j=0;j<nb_dim;j++){
00826 mean[t-firstSample][j]=TmpClass[kMT].MTCentroids[t][0][j];
00827 for (int l=0;l<nb_dim;l++){
00828 cov[t-firstSample][j][l]=TmpClass[kMT].MTCovariances[t][0][j][l];
00829 }
00830 }
00831 }
00832 if (subGraph==1)
00833 graph1[0].AddAttMT(mean,cov,weight);
00834 if (subGraph==2)
00835 graph2[0].AddAttMT(mean,cov,weight);
00836
00837 for (int i=0;i<nb_samples;i++){
00838 for (int j=0;j<nb_dim;j++){delete [] cov[i][j];}
00839 delete [] mean[i];delete [] cov[i];
00840 }
00841 delete [] mean;delete [] cov;
00842
00843
00844 for (int t=firstSample;t<nb_samples+firstSample;t++){
00845 nbClusters=0;
00846 auxIndex=0;
00847 minDiv=Inf;
00848 IndexkTLminDiv[t]=-1;
00849 int length=TmpClass[kMT].AssignedTimelessClass[t].size();
00850 for (int kTL=0;kTL<(int)length;kTL++){
00851 if ((float)TmpClass[kMT].DivergenceValue[t][kTL]<minDiv){
00852 minDiv=(float)TmpClass[kMT].DivergenceValue[t][kTL];
00853 IndexkTLminDiv[t]=kTL;
00854 }
00855 }
00856 for (int kTL=0;kTL<(int)length;kTL++){
00857 if ((float)TmpClass[kMT].DivergenceValue[t][kTL]*weight<seuilDiv||IndexkTLminDiv[t]==kTL){
00858 nbClusters++;
00859 }
00860 }
00861
00862 float **meanTL=new float*[nbClusters];float ***covTL=new float**[nbClusters];
00863 float *divTL=new float[nbClusters];unsigned long int *weightTL=new unsigned long int[nbClusters];
00864 for (int i=0;i<nbClusters;i++){
00865 meanTL[i]=new float[nb_dim];covTL[i]=new float*[nb_dim];
00866 for (int j=0;j<nb_dim;j++)
00867 covTL[i][j]=new float[nb_dim];
00868 }
00869
00870 for (int kTL=0;kTL<(int)length;kTL++){
00871 if ((float)TmpClass[kMT].DivergenceValue[t][kTL]*weight<seuilDiv||IndexkTLminDiv[t]==kTL){
00872 weightTL[auxIndex]=(unsigned long int)TmpClass[kMT].Weight[t][kTL];
00873 divTL[auxIndex]=(float)TmpClass[kMT].DivergenceValue[t][kTL]*weightTL[auxIndex];
00874 for (int i=0;i<nb_dim;i++){
00875 meanTL[auxIndex][i]=TmpClass[0].Centroids[t][TmpClass[kMT].AssignedTimelessClass[t][kTL]][i];
00876 for (int j=0;j<nb_dim;j++){
00877 covTL[auxIndex][i][j]=TmpClass[0].Covariances[t][TmpClass[kMT].AssignedTimelessClass[t][kTL]][i][j];
00878 }
00879 }
00880 auxIndex++;
00881 }
00882
00883 }
00884 if (subGraph==1)
00885 graph1[0].AddAttTLforMT(t-firstSample,nbClusters,meanTL,covTL,weightTL,divTL);
00886 if (subGraph==2)
00887 graph2[0].AddAttTLforMT(t-firstSample,nbClusters,meanTL,covTL,weightTL,divTL);
00888
00889 for (int i=0;i<nbClusters;i++){for (int j=0;j<nb_dim;j++){delete [] covTL[i][j];}delete [] meanTL[i];delete [] covTL[i];}
00890 delete [] covTL;delete [] meanTL;delete [] weightTL;delete [] divTL;
00891 }
00892
00893
00894 int length1;int length2;int nbClusters1;int nbClusters2;
00895 Divergence divObject(nb_dim);
00896 float *min_bound=new float[nb_dim];float *max_bound=new float[nb_dim];
00897 float *resolution=new float[nb_dim];
00898
00899 for (int t=firstSample+1;t<nb_samples+firstSample;t++){
00900 nbClusters1=0,nbClusters2=0;
00901 length1=(int)TmpClass[kMT].AssignedTimelessClass[t-1].size();
00902 length2=(int)TmpClass[kMT].AssignedTimelessClass[t].size();
00903 for (int kTLpre=0;kTLpre<length1;kTLpre++)
00904 if ((float)TmpClass[kMT].DivergenceValue[t-1][kTLpre]*weight<seuilDiv||IndexkTLminDiv[t-1]==kTLpre)
00905 nbClusters1++;
00906 for (int kTL=0;kTL<length2;kTL++)
00907 if ((float)TmpClass[kMT].DivergenceValue[t][kTL]*weight<seuilDiv||IndexkTLminDiv[t]==kTL)
00908 nbClusters2++;
00909 unsigned long int **flow=new unsigned long int*[nbClusters1];
00910 float **div=new float*[nbClusters1];
00911 float **MI=new float*[nbClusters1];
00912 for (int i=0;i<nbClusters1;i++){
00913 flow[i]=new unsigned long int[nbClusters2];
00914 div[i]=new float[nbClusters2];
00915 MI[i]=new float[nbClusters2];
00916 }
00917
00918 auxIndex2=0;
00919 for (int kTL=0;kTL<length2;kTL++){
00920 if ((float)TmpClass[kMT].DivergenceValue[t][kTL]*weight<seuilDiv||IndexkTLminDiv[t]==kTL){
00921 auxIndex1=0;
00922 for (int kTLpre=0;kTLpre<length1;kTLpre++){
00923 if ((float)TmpClass[kMT].DivergenceValue[t-1][kTLpre]*weight<seuilDiv||IndexkTLminDiv[t-1]==kTLpre){
00924 flow[auxIndex1][auxIndex2]=(unsigned long int)TmpClass[kMT].flow[t][kTLpre][kTL];
00925 div[auxIndex1][auxIndex2]=TmpClass[kMT].div[t][kTLpre][kTL];
00926 MI[auxIndex1][auxIndex2]=TmpClass[kMT].MutualInfo[t-1];
00927 auxIndex1++;
00928 }
00929 }
00930 auxIndex2++;
00931 }
00932 }
00933 if (subGraph==1)
00934 graph1[0].AddEdgesAttMT(t-firstSample,nbClusters1,nbClusters2,flow,div,MI);
00935 if (subGraph==2)
00936 graph2[0].AddEdgesAttMT(t-firstSample,nbClusters1,nbClusters2,flow,div,MI);
00937
00938 for (int i=0;i<nbClusters1;i++){delete [] flow[i];delete [] div[i];delete [] MI[i];}
00939 delete [] flow;delete [] div;delete [] MI;
00940 }
00941 delete [] min_bound; delete [] max_bound;
00942 delete [] resolution;
00943
00944
00945 }
00946
00947
00948