00001
00011 #ifndef PARASEARCH_HH
00012 #define PARASEARCH_HH
00013
00014 #include <iostream>
00015 #include <fstream>
00016 #include <string>
00017 #include <vector>
00018
00019 #include <cstdio>
00020 #include <cstdlib>
00021 #include <cmath>
00022 #include <cassert>
00023
00024 #define InfCost 1e5
00025 #include "SearchTree.hh"
00026
00027
00028 using namespace std;
00032 class ParaSearch
00033 {
00034 public:
00035
00037 int nbPara;
00039 int nbQuantifLevel;
00041 int nbMTClasses;
00043 int nbSubGMTClasses;
00045 int minSample;
00047 int maxSample;
00049 int nbTimeSamples;
00050
00051
00053 SearchTree ****treeCollec;
00057 float ****costs;
00059 vector<int> kMTaSorted;
00061 vector<int> kMTbSorted;
00063 vector<int> firstSampleSorted;
00064
00065
00067 float **pdf;
00069 float *maxPdf;
00071 float *meanPdf;
00073 float *estimPara;
00075 float *estimParaPrevious;
00077 float *maxCostValue;
00078
00079
00080 public :
00081 ParaSearch(){}
00082 virtual~ParaSearch(){}
00084 void Allocate(int nb_parameters,int nbLevel,int nb_MTClasses, int nb_SubGMTClasses,int min_Sample,int max_Sample,int nb_TimeSamples){
00085
00086 nbPara=nb_parameters;
00087 nbQuantifLevel=nbLevel;
00088 pdf=new float*[nbPara];
00089 maxPdf=new float[nbPara];
00090 meanPdf=new float[nbPara];
00091 estimPara=new float[nbPara];for (int i=0;i<nbPara;i++){estimPara[i]=0;}
00092 estimParaPrevious=new float[nbPara];for (int i=0;i<nbPara;i++){estimParaPrevious[i]=0;}
00093 for (int i=0;i<nbPara;i++){pdf[i]=new float[nbQuantifLevel];}
00094 for (int i=0;i<nbPara;i++){for (int j=0;j<nbQuantifLevel;j++){pdf[i][j]=0;}}
00095 maxCostValue=new float[nbPara];
00096 nbMTClasses=nb_MTClasses;
00097 nbSubGMTClasses=nb_SubGMTClasses;
00098 minSample=min_Sample;
00099 maxSample=max_Sample;
00100 nbTimeSamples=nb_TimeSamples;
00101
00102 }
00104 void Init(SearchTree ****searchTreeCollec,float ****costss,vector<int>* kMTa,vector<int> *kMTb, vector<int> *firstSample){
00105
00106 treeCollec=searchTreeCollec;
00107 costs=costss;
00108 kMTaSorted=*kMTa;
00109 kMTbSorted=*kMTb;
00110 firstSampleSorted=*firstSample;
00111
00112 }
00114 void Init(vector<int>* kMTa,vector<int> *kMTb, vector<int> *firstSample){
00115
00116 kMTaSorted=*kMTa;
00117 kMTbSorted=*kMTb;
00118 firstSampleSorted=*firstSample;
00119
00120 }
00122 void MaxCostalues(){
00123 for (int kPara=0;kPara<nbPara;kPara++){
00124 maxCostValue[kPara]=0;
00125 for (int t=minSample;t<maxSample-nbTimeSamples+1;t++){
00126 for (int kMTa=0;kMTa<nbMTClasses;kMTa++){
00127 if (nbSubGMTClasses==2){for (int kMTb=0;kMTb<nbMTClasses;kMTb++){
00128 if (!treeCollec[kMTa][kMTb][t][0].emptyTree){
00129 if (maxCostValue[kPara]<costs[kMTa][kMTb][t][kPara]&&costs[kMTa][kMTb][t][kPara]<InfCost){maxCostValue[kPara]=costs[kMTa][kMTb][t][kPara];}
00130 }
00131 }}
00132 else{
00133 if (!treeCollec[kMTa][kMTa][t][0].emptyTree){
00134 if (maxCostValue[kPara]<costs[kMTa][kMTa][t][kPara]&&costs[kMTa][kMTa][t][kPara]<InfCost){maxCostValue[kPara]=costs[kMTa][kMTa][t][kPara];}
00135
00136 }
00137 }
00138 }
00139 }
00140
00141 }
00142
00143 }
00144
00146 void MMSEestim(int interestkMTa,int interestkMTb,int interestfirstSample){
00147
00148 int aux=0;
00149 float indexMMSE=0;
00150 float auxSum=0; float cardinality=0;
00151
00152 if (nbSubGMTClasses==1){interestkMTb=interestkMTa;}
00153 for (int kPara=0;kPara<nbPara;kPara++){
00154 estimParaPrevious[kPara]=estimPara[kPara];
00155
00156
00157
00158 if (costs[interestkMTa][interestkMTb][interestfirstSample][kPara]>=maxCostValue[kPara]){aux=nbQuantifLevel-1;}
00159 if (costs[interestkMTa][interestkMTb][interestfirstSample][kPara]<0){aux=0;}
00160 else{
00161 if (costs[interestkMTa][interestkMTb][interestfirstSample][kPara]<maxCostValue[kPara]){
00162 aux=(int)((float)costs[interestkMTa][interestkMTb][interestfirstSample][kPara]*nbQuantifLevel/(float)maxCostValue[kPara]+0.5);
00163 }
00164 }
00165
00166
00167 pdf[kPara][aux]+=1;
00168
00169
00170 indexMMSE=-1;auxSum=0;cardinality=0;
00171 for (int i=0;i<nbQuantifLevel;i++){auxSum+=pdf[kPara][i]*i;cardinality+=pdf[kPara][i];}
00172 indexMMSE=auxSum/cardinality;
00173 estimPara[kPara]=1.-(float)indexMMSE/(float)(nbQuantifLevel-1);
00174 meanPdf[kPara]=pdf[kPara][(int)(indexMMSE+0.5)];
00175
00176
00177 }
00178 auxSum=0;
00179 for (int kPara=0;kPara<nbPara;kPara++){
00180 auxSum+=estimPara[kPara];
00181 }
00182 for (int kPara=0;kPara<nbPara;kPara++){estimPara[kPara]/=auxSum;}
00183 }
00187 float ReevaluateMatch(int i,int j,int k){
00188
00189 float cost=-1;
00190 if (!treeCollec[i][j][k][0].emptyTree){
00191 cost=treeCollec[i][j][k][0].LowestCosts(costs[i][j][k],estimPara);
00192
00193 }
00194 return cost;
00195
00196
00197 }
00199 void ErasePdf(){
00200
00201 for (int i=0;i<nbPara;i++){for (int j=0;j<nbQuantifLevel;j++){pdf[i][j]=0;}}
00202 for (int kPara=0;kPara<nbPara;kPara++){estimPara[kPara]=0;}
00203 }
00204
00205
00206
00207 };
00208 #endif