BeliefValuePairPool.cpp
Go to the documentation of this file.
00001 #include "BeliefValuePairPool.h"
00002 #include <exception>
00003 using namespace std;
00004 
00005 
00006 REAL_VALUE BeliefValuePairPool::getValue(SharedPointer<Belief>& b) 
00007 {
00008         DEBUG_TRACE(cout << "UpperBoundBVpair::getValue" << endl;);
00009 
00010         REAL_VALUE val, min_val;
00011         REAL_VALUE ratio, min_ratio;
00012         vector<SparseVector_Entry>::const_iterator  bi, bend;
00013 
00014         vector<SparseVector_Entry>::const_iterator  ci, cend;
00015 
00016         SparseVector tmp, bhat;
00017         REAL_VALUE cval;
00018         REAL_VALUE inner_cornerPoints_b;
00019         REAL_VALUE inner_cornerPoints_c;
00020         bool bdone = false;
00021 
00022         inner_cornerPoints_b = inner_prod( cornerPoints, *b );
00023 
00024         
00025         DEBUG_TRACE( cout << "cornerPoints"<< endl; );
00026         DEBUG_TRACE( cornerPoints.write(cout) << endl; );
00027         DEBUG_TRACE( cout << "b"<< endl; );
00028         DEBUG_TRACE( b->write(cout) << endl; );
00029 
00030         DEBUG_TRACE( cout << "inner_cornerPoints_b "<< inner_cornerPoints_b << endl; );
00031 
00032         min_val = inner_cornerPoints_b;
00033 
00034         for(list<SharedPointer<BeliefValuePair> >::iterator iter = points.begin(); iter!=points.end(); iter ++) 
00035         {
00036 
00037                 if ((*iter)->disabled) 
00038                 {
00039                         //cout << "skipping" << endl;
00040                         continue;
00041                 }
00042 
00043                 belief_vector& c = *((*iter)->b);
00044                 cval = (*iter)->v;
00045 
00046 
00047                 //cout << "c.size() " << c.size() << " cval " << cval << endl;
00048 
00049                 DEBUG_TRACE( cout << "UpperBound::getValue:1 " << " pr->version: " << (*iter)->version << " cornerPointsVersion " << cornerPointsVersion << endl;);
00050                 if((*iter)->version == cornerPointsVersion) 
00051                 {
00052                         DEBUG_TRACE( cout << "UpperBound::getValue:2 " << endl; );
00053                         inner_cornerPoints_c = (*iter)->innver_cornerPoints;
00054                 }
00055                 else 
00056                 {
00057                         DEBUG_TRACE( cout << "UpperBound::getValue:3 " << endl; );
00058 
00059                         inner_cornerPoints_c = inner_prod( cornerPoints, c );
00060                         (*iter)->innver_cornerPoints = inner_cornerPoints_c;
00061                         (*iter)->version = cornerPointsVersion;
00062                 }
00063 
00064                 //                      if (inner_cornerPoints_c <= cval)
00065                 //                      {
00066                 //                // FIX: should figure out a way to prune c here
00067                 //                //cout << "  bad cval" << endl;
00068                 //                              continue;
00069                 //                      }
00070 
00071                 min_ratio = 99e+20;
00072 
00073                 bi = b->data.begin();
00074                 bend = b->data.end();
00075 
00076                 ci = c.data.begin();
00077                 cend = c.data.end();
00078 
00079                 for (; ci != cend; ci++) {
00080                         if (0.0 == ci->value) continue;
00081 
00082                         // advance until bi->index >= ci->index
00083                         while (1) {
00084                                 if (bi == bend) {
00085                                         bdone = true;
00086                                         goto break_ci_loop;
00087                                 }
00088                                 if (bi->index >= ci->index) break;
00089                                 bi++;
00090                         }
00091 
00092                         if (bi->index > ci->index) {
00093                                 // we found a j such that b(j) = 0 and c(j) != 0, which implies
00094                                 // min_ratio = 0, so this c is useless and should be skipped
00095                                 //cout << "  skipping out" << endl;
00096                                 goto continue_Points_loop;
00097                         }
00098 
00099                         ratio = bi->value / ci->value;
00100                         if (ratio < min_ratio) min_ratio = ratio;
00101                 }
00102 break_ci_loop:
00103                 if (bdone) {
00104                         for (; ci != cend; ci++) {
00105                                 if (0.0 != ci->value) goto continue_Points_loop;
00106                         }
00107                 }
00108 
00109                 val = inner_cornerPoints_b + min_ratio * ( cval - inner_cornerPoints_c );
00110 #if 1
00111                 if (min_ratio > 1) 
00112                 {
00113                         if (min_ratio < 1 + MIN_RATIO_EPS) 
00114                         {
00115                                 // round-off error, correct it down to 1
00116                                 min_ratio = 1;
00117                         } 
00118                         else 
00119                         {
00120                                 cout << "ERROR: min_ratio > 1 in upperBoundInternal!" << endl;
00121                                 cout << "  (min_ratio-1)=" << (min_ratio-1) << endl;
00122                                 cout << "  normb=" << b->norm_1() << endl;
00123                                 cout << "  b=";
00124                                 b->write(cout);
00125                                 cout << endl;
00126 
00127                                 cout << "  normc=" << c.norm_1() << endl;
00128                                 cout << "  c=";
00129                                 c.write(cout);
00130                                 cout << endl;
00131 
00132                                 exit(EXIT_FAILURE);
00133                         }
00134                 }
00135 #endif
00136 
00137                 if (val < min_val) min_val = val;
00138 continue_Points_loop:
00139                 ;
00140         }
00141 
00142         return min_val;
00143 }
00144 
00145 
00146 
00147 REAL_VALUE BeliefValuePairPool::getValue_NoInterpolation(const belief_vector& b) 
00148 {
00149         REAL_VALUE min_val;
00150         REAL_VALUE inner_cornerPoints_b;
00151 
00152         inner_cornerPoints_b = inner_prod( cornerPoints, b );
00153         min_val = inner_cornerPoints_b;
00154 
00155         return min_val;
00156 }
00157 
00158 // update helper functions
00159 SharedPointer<BeliefValuePair> BeliefValuePairPool::addPoint(SharedPointer<belief_vector>&  b, REAL_VALUE val) 
00160 {
00161 
00162         DEBUG_TRACE( cout << "BVPairPool::add" << endl; );
00163         DEBUG_TRACE( cout << "val " << val << endl; );
00164         DEBUG_TRACE( cout << "b   "; );
00165         DEBUG_TRACE( b->write(cout) << endl; );
00166 
00167         int wc = whichCornerPoint(b);
00168         if (-1 == wc) 
00169         {
00170                 SharedPointer<BeliefValuePair>bvp = new BeliefValuePair();
00171                 // new bvp will have "disabled" set to false
00172                 bvp->b = b;
00173                 bvp->v = val;
00174 
00175                 points.push_back(bvp);
00176                 return bvp;
00177         }
00178         else 
00179         {
00180                 cornerPointsVersion++;
00181                 cornerPoints(wc) = val;
00182                 return NULL;
00183         }
00184 }
00185 
00186 // if b is a corner point e_i, return i.  else return -1
00187 int BeliefValuePairPool::whichCornerPoint(const SharedPointer<belief_vector>&  b) const {
00188         int numStates = problem->getBeliefSize();
00189         int non_zero_count = 0;
00190         int non_zero_index = -1;
00191         FOR(i, numStates) {
00192                 if (fabs((*b)(i)) > CORNER_EPS) {
00193                         if (non_zero_count == 0) {
00194                                 non_zero_index = i;
00195                                 non_zero_count++;
00196                         } else {
00197                                 return -1;
00198                         }
00199                 }
00200         }
00201         if (non_zero_count == 0) {
00202                 return -1;
00203         }
00204         else {
00205                 return non_zero_index;
00206         }
00207 }
00208 //private methods
00209 //for debugging purposes
00210 void BeliefValuePairPool::printCorners() const {
00211         printf("[");
00212 
00213         FOREACH(REAL_VALUE, di,  cornerPoints.data) {
00214                 printf("%f, ", *di);
00215         }
00216         printf("]\n");
00217 }//end printAlphal


appl
Author(s): petercai
autogenerated on Tue Jan 7 2014 11:02:28