POMDP.cpp
Go to the documentation of this file.
00001 /********** tell emacs we use -*- c++ -*- style comments *******************
00002 $Revision: 2.28 $  $Author: duyanzhu $  $Date: 2009/01/07 05:10:04 $
00003 
00004 @file    POMDP.cc
00005 @brief   No brief
00006 
00007 Copyright (c) 2002-2005, Trey Smith. All rights reserved.
00008 
00009 Licensed under the Apache License, Version 2.0 (the "License"); you may
00010 not use this file except in compliance with the License.  You may
00011 obtain a copy of the License at
00012 
00013 http://www.apache.org/licenses/LICENSE-2.0
00014 
00015 Unless required by applicable law or agreed to in writing, software
00016 distributed under the License is distributed on an "AS IS" BASIS,
00017 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
00018 implied.  See the License for the specific language governing
00019 permissions and limitations under the License.
00020 
00021 ***************************************************************************/
00022 
00023 /***************************************************************************
00024 * INCLUDES
00025 ***************************************************************************/
00026 
00027 #include <assert.h>
00028 #include <stdlib.h>
00029 #ifdef _MSC_VER
00030 #else
00031 #include <unistd.h>
00032 #include <sys/time.h>
00033 #endif
00034 
00035 #include <stdio.h>
00036 #include <string.h>
00037 
00038 
00039 #include <iostream>
00040 #include <cerrno>
00041 #include <fstream>
00042 
00043 #include "pomdpCassandraWrapper.h"
00044 #include "POMDP.h"
00045 #include "MathLib.h"
00046 
00047 using namespace std;
00048 using namespace momdp;
00049 
00050 namespace momdp {
00051 
00052         /***************************************************************************
00053         * STATIC HELPER FUNCTIONS
00054         ***************************************************************************/
00055 
00056         static void reaDenseVector(char *data, DenseVector& b, int numValues)
00057         {
00058                 int i;
00059                 char *inp = data;
00060                 char *tok;
00061 
00062                 for (i=0; i < numValues; i++) {
00063                         tok = strtok(inp," ");
00064                         if (0 == tok) {
00065                                 cout << "ERROR: not enough entries in initial belief distribution"
00066                                         << endl;
00067                                 exit(EXIT_FAILURE);
00068                         }
00069                         inp = 0;
00070 
00071                         b(i) = atof(tok);
00072                 }
00073         }
00074 
00075         static void trimTrailingWhiteSpace(char *s)
00076         {
00077                 int n = strlen(s);
00078                 int i;
00079                 for (i = n-1; i >= 0; i--) {
00080                         if (!isspace(s[i])) break;
00081                 }
00082                 s[i+1] = '\0';
00083         }
00084 
00085         /***************************************************************************
00086         * POMDP FUNCTIONS
00087         ***************************************************************************/
00088 
00089         POMDP::POMDP( std::string& fileName, bool useFastParser)
00090         {
00091                 this->fileName = fileName;//added Xan 12082007
00092                 size_t backslash = this->fileName.rfind( "/", this->fileName.length() );
00093                 if ( backslash != std::string::npos ) this->fileName = this->fileName.substr(backslash+1);
00094 
00095                 readFromFile(fileName, useFastParser);
00096         }
00097 
00098         void POMDP::readFromFile( std::string& fileName,
00099                 bool useFastParser)
00100         {
00101                 if (useFastParser) {
00102                         readFromFileFast2(fileName);
00103                 } else {
00104                         readFromFileCassandra(fileName);
00105                 }
00106 
00107                 // post-process: calculate isPOMDPTerminalState
00108 #if USE_DEBUG_PRINT
00109                 printf("POMDP::readFromFile: marking zero-reward absorbing states as terminal\n");
00110 #endif
00111                 isPOMDPTerminalState.resize(numStates, /* initialValue = */ true);
00112                 FOR (s, numStates) {
00113                         FOR (a, numActions) {
00114                                 if ((fabs(1.0 - T[a](s,s)) > OBS_IS_ZERO_EPS) || R(s,a) != 0.0) {
00115                                         isPOMDPTerminalState[s] = false;
00116                                         break;
00117                                 }
00118                         }
00119                 }
00120         }
00121 
00122         //********added on 30/03/2007 rn
00123         //      for implementation of the access functions
00124          SparseMatrix& POMDP::getTransitionMatrix(int a) {
00125                 return T[a];
00126         }//end getTransitionMatrix
00127 
00128          SparseMatrix& POMDP::getTransposedTransitionMatrix(int a) {
00129                 return Ttr[a];
00130         }//end getTransposedTransitionMatrix
00131 
00132          SparseMatrix& POMDP::getObservationMatrix(int a) {
00133                 return O[a];
00134         }//end getObservationMatrix
00135 
00136          SparseMatrix& POMDP::getTransposedObservationMatrix(int a) {
00137                 return Otr[a];
00138         }//end getTransposedObservationMatrix
00139 
00140          SparseMatrix& POMDP::getRewardMatrix() {
00141                 return R;
00142         }//end getRewardMatrix
00143 
00144         REAL_VALUE POMDP::getMaxReward() {
00145                 return R.getMaxValue();
00146         }
00147 
00148         //********end added
00149 
00150 
00151          belief_vector& POMDP::getInitialBelief(void) 
00152         {
00153                 
00154                 return initialBelief;
00155         }
00156 
00157         obs_prob_vector& POMDP::getObsProbVector(obs_prob_vector& result,
00158                  belief_vector& b,
00159                 int a) 
00160         {
00161                 DenseVector tmp, tmp2;
00162                 // --- overall: result = O_a' * T_a' * b
00163                 // tmp = T_a' * b
00164                 mult( tmp, Ttr[a], b );
00165                 // result = O_a' * tmp
00166                 mult( tmp2, tmp, O[a] );
00167 
00168                 copy(result, tmp2);
00169                 return result;
00170         }
00171 
00172         // T[a](s,s'), Ttr[a](s',s), O[a](s',o)
00173 
00174         belief_vector& POMDP::getNextBelief(belief_vector& result,
00175                  belief_vector& b,
00176                 int a, int o) 
00177         {
00178                 belief_vector tmp;
00179 
00180                 // result = O_a(:,o) .* (T_a * b)
00181                 mult( tmp, Ttr[a], b );
00182                 emult_column( result, O[a], o, tmp );
00183 
00184                 // renormalize
00185                 result *= (1.0/(result.norm_1()));
00186 
00187                 return result;
00188         }
00189 
00190         REAL_VALUE POMDP::getReward( belief_vector& b, int a) 
00191         {
00192                 return inner_prod_column( R, a, b );
00193         }
00194 
00195         bool POMDP::getIsTerminalState( belief_vector& s) 
00196         {
00197                 REAL_VALUE nonTerminalSum =     s.maskedSum(isPOMDPTerminalState);
00198                 return (nonTerminalSum < 1e-10);
00199         }
00200 
00201         void POMDP::readFromFileCassandra( string& fileName) {
00202 #if USE_DEBUG_PRINT
00203                 timeval startTime, endTime;
00204                 cout << "reading problem from " << fileName << endl;
00205                 gettimeofday(&startTime,0);
00206 #endif
00207 
00208                 PomdpCassandraWrapper p;
00209                 p.readFromFile(fileName);
00210 
00211                 numStates = p.getNumStates();
00212                 setBeliefSize(numStates);
00213                 numActions = p.getNumActions();
00214                 numObservations = p.getNumObservations();
00215                 discount = p.getDiscount();
00216 
00217 //              cout << "Number of States :: " << numStates << endl;
00218                 cout << "input file   : " << fileName << endl;
00219                 
00220                 // convert R to sla format
00221                 kmatrix Rk;
00222                 copy(Rk, p.getRTranspose(), numStates);
00223                 kmatrix_transpose_in_place(Rk);
00224                 copy(R, Rk);
00225 
00226                 // convert T, Tr, and O to sla format
00227                 kmatrix Tk, Ok;
00228                 T.resize(numActions);
00229                 Ttr.resize(numActions);
00230                 O.resize(numActions);
00231                 Otr.resize(numActions);  
00232                 FOR (a, numActions) {
00233                         copy(Tk, p.getT(a), numStates);
00234                         copy(T[a], Tk);
00235                         kmatrix_transpose_in_place(Tk);
00236                         copy(Ttr[a], Tk);
00237                         //   copy(O[a], p.getO(a), numObservations);
00238                         copy(Ok, p.getO(a), numObservations);
00239                         copy(O[a], Ok);
00240                         kmatrix_transpose_in_place(Ok);
00241                         copy(Otr[a], Ok);
00242                 }
00243 
00244                 // convert initialBelief to sla format
00245                 DenseVector initialBeliefD;
00246                 initialBeliefD.resize(numStates);
00247                 FOR (s, numStates) {
00248                         initialBeliefD(s) = p.getInitialBelief(s);
00249                 }
00250                 copy(initialBelief, initialBeliefD);
00251                 initialBelief.finalize();
00252 
00253                 //cout << "Initial Belief :: " << initialBelief.toString() << endl;
00254 
00255 #if 0
00256                 DenseVector initialBeliefx;
00257                 std::vector<bool> isPOMDPTerminalStatex;
00258                 kmatrix Rx;
00259                 std::vector<kmatrix> Tx, Ox;
00260 
00261                 // pre-process
00262                 initialBeliefx.resize(numStates);
00263                 set_to_zero(initialBeliefx);
00264                 isPOMDPTerminalStatex.resize(numStates, /* initialValue = */ false);
00265                 Rx.resize(numStates, numActions);
00266                 Tx.resize(numActions);
00267                 Ox.resize(numActions);
00268                 FOR (a, numActions) {
00269                         Tx[a].resize(numStates, numStates);
00270                         Ox[a].resize(numStates, numObservations);
00271                 }
00272 
00273                 // copy
00274                 FOR (s, numStates) {
00275                         initialBeliefx(s) = p.getInitialBelief(s);
00276                         isPOMDPTerminalStatex[s] = p.isTerminalState(s);
00277                         FOR (a, numActions) {
00278                                 kmatrix_set_entry( Rx, s, a, p.R(s,a) );
00279                                 FOR (sp, numStates) {
00280                                         kmatrix_set_entry( Tx[a], s, sp, p.T(s,a,sp) );
00281                                 }
00282                                 FOR (o, numObservations) {
00283                                         kmatrix_set_entry( Ox[a], s, o, p.O(s,a,o) );
00284                                 }
00285                         }
00286                 }
00287 
00288                 // post-process
00289                 copy( initialBelief, initialBeliefx );
00290                 isPOMDPTerminalState = isPOMDPTerminalStatex;
00291                 copy( R, Rx );
00292                 Ttr.resize(numActions);
00293                 O.resize(numActions);
00294                 T.resize(numActions);
00295                 FOR (a, numActions) {
00296                         copy( T[a], Tx[a] );
00297                         kmatrix_transpose_in_place( Tx[a] );
00298                         copy( Ttr[a], Tx[a] );
00299                         copy( O[a], Ox[a] );
00300                 }
00301 #endif // if 0
00302 
00303 #if USE_DEBUG_PRINT
00304                 gettimeofday(&endTime,0);
00305                 REAL_VALUE numSeconds = (endTime.tv_sec - startTime.tv_sec)
00306                         + 1e-6 * (endTime.tv_usec - startTime.tv_usec);
00307                 cout << "[file reading took " << numSeconds << " seconds]" << endl;
00308 
00309                 debugDensity();
00310 #endif
00311         }
00312 
00313 
00314         string TrimStr( string& Src)
00315         {
00316                 string c = " \r\n";
00317                 int p2 = Src.find_last_not_of(c);
00318                 if (p2 == std::string::npos) 
00319                         return std::string();
00320                 int p1 = Src.find_first_not_of(c);
00321                 if (p1 == std::string::npos) p1 = 0;
00322                 return Src.substr(p1, (p2-p1)+1);
00323         }
00324 
00325         // yanzhu's version of fast parser
00326         void POMDP::readFromFileFast2( std::string& fileName)
00327         { 
00328                 int lineNumber;
00329                 string curline;
00330                 ifstream in;
00331                 char sbuf[512];
00332                 int numSizesSet = 0;
00333                 bool inPreamble = true;
00334                 //char *dataBuf = (char *)malloc(1>>20);
00335                 //if(dataBuf == NULL)
00336                 //{
00337                 //      cout << "Failed to allocate memory for Fast POMDP parser" << endl;
00338                 //      exit(-1);
00339                 //}
00340 
00341 #if USE_DEBUG_PRINT
00342                 timeval startTime, endTime;
00343                 cout << "reading problem (in fast mode) from " << fileName << endl;
00344                 gettimeofday(&startTime,0);
00345 #endif
00346 
00347                 in.open(fileName.c_str());
00348                 if (!in) 
00349                 {
00350                         cerr << "ERROR: couldn't open " << fileName << " for reading: " << endl;
00351                         exit(EXIT_FAILURE);
00352                 }
00353 
00354                 DenseVector initialBeliefx;
00355                 kmatrix Rx;
00356                 std::vector<kmatrix> Tx, Ox;
00357 
00358 #define PM_PREFIX_MATCHES_STL(X) ( string::npos != curline.find((X)))
00359 
00360                 lineNumber = 1;
00361                 while (getline(in, curline)) 
00362                 {
00363                         if (in.fail() && !in.eof()) {
00364                                 cerr << "ERROR: readFromFileFast: line too long for buffer"
00365                                         << " (max length " << curline.size() << ")" << endl;
00366                                 exit(EXIT_FAILURE);
00367                         }
00368 
00369                         string buf = TrimStr(curline);
00370                         if ('#' == buf.c_str()[0]) continue;
00371                         if ('\0' == buf.c_str()[0]) continue;
00372 
00373                         if (inPreamble) 
00374                         {
00375                                 if (PM_PREFIX_MATCHES_STL("discount:")) 
00376                                 {
00377                                         if (1 != sscanf(buf.c_str(),"discount: %lf", &discount)) 
00378                                         {
00379                                                 cerr << "ERROR: line " << lineNumber
00380                                                         << ": syntax error in discount statement"
00381                                                         << endl;
00382                                                 exit(EXIT_FAILURE);
00383                                         }
00384                                 } 
00385                                 else if (PM_PREFIX_MATCHES_STL("values:")) 
00386                                 {
00387                                         if (1 != sscanf(buf.c_str(),"values: %s", sbuf)) 
00388                                         {
00389                                                 cerr << "ERROR: line " << lineNumber
00390                                                         << ": syntax error in values statement"
00391                                                         << endl;
00392                                                 exit(EXIT_FAILURE);
00393                                         }
00394                                         if (0 != strcmp(sbuf,"reward")) 
00395                                         {
00396                                                 cerr << "ERROR: line " << lineNumber
00397                                                         << ": can only handle values of type reward"
00398                                                         << endl;
00399                                                 exit(EXIT_FAILURE);
00400                                         }
00401                                 } 
00402                                 else if (PM_PREFIX_MATCHES_STL("actions:")) 
00403                                 {
00404                                         if (1 != sscanf(buf.c_str(),"actions: %d", &numActions)) 
00405                                         {
00406                                                 cerr << "ERROR: line " << lineNumber
00407                                                         << ": syntax error in actions statement"
00408                                                         << endl;
00409                                                 exit(EXIT_FAILURE);
00410                                         }
00411                                         numSizesSet++;
00412                                 }
00413                                 else if (PM_PREFIX_MATCHES_STL("observations:")) 
00414                                 {
00415                                         if (1 != sscanf(buf.c_str(),"observations: %d", &numObservations)) 
00416                                         {
00417                                                 cerr << "ERROR: line " << lineNumber
00418                                                         << ": syntax error in observations statement"
00419                                                         << endl;
00420                                                 exit(EXIT_FAILURE);
00421                                         }
00422                                         numSizesSet++;
00423                                 } 
00424                                 else if (PM_PREFIX_MATCHES_STL("states:")) 
00425                                 {
00426                                         if (1 != sscanf(buf.c_str(),"states: %d", &numStates)) 
00427                                         {
00428                                                 cerr << "ERROR: line " << lineNumber
00429                                                         << ": syntax error in states statement"
00430                                                         << endl;
00431                                                 exit(EXIT_FAILURE);
00432                                         }
00433                                         numSizesSet++;
00434                                 } 
00435                                 else 
00436                                 {
00437                                         cerr << "ERROR: line " << lineNumber
00438                                                 << ": got unexpected statement type while parsing preamble"
00439                                                 << endl;
00440                                         exit(EXIT_FAILURE);
00441                                 }
00442 
00443                                 if (3 == numSizesSet) {
00444                                         // pre-process
00445                                         setBeliefSize(numStates);
00446                                         initialBeliefx.resize(numStates);
00447                                         set_to_zero(initialBeliefx);
00448                                         Rx.resize(numStates, numActions);
00449                                         Tx.resize(numActions);
00450                                         Ox.resize(numActions);
00451                                         FOR (a, numActions) {
00452                                                 Tx[a].resize(numStates, numStates);
00453                                                 Ox[a].resize(numStates, numObservations);
00454                                         }
00455 
00456                                         inPreamble = false;
00457                                 }
00458 
00459                         } 
00460                         else 
00461                         {
00462 
00463                                 if (PM_PREFIX_MATCHES_STL("start:")) 
00464                                 {
00465                                         char *cstr = strdup(buf.c_str());
00466                                         char *data = cstr + strlen("start: ");
00467                                         reaDenseVector(data,initialBeliefx,numStates);
00468                                         free(cstr);
00469 
00470                                         //reaDenseVectorSTL(buf,initialBeliefx,numStates);
00471                                 } 
00472                                 else 
00473                                         if (PM_PREFIX_MATCHES_STL("R:")) 
00474                                         {
00475                                                 int s, a;
00476                                                 REAL_VALUE reward;
00477                                                 if (3 != sscanf(buf.c_str(),"R: %d : %d : * : * %lf", &a, &s, &reward)) {
00478                                                         cerr << "ERROR: line " << lineNumber
00479                                                                 << ": syntax error in R statement"
00480                                                                 << endl;
00481                                                         exit(EXIT_FAILURE);
00482                                                 }
00483                                                 kmatrix_set_entry( Rx, s, a, reward );
00484                                         } 
00485                                         else if (PM_PREFIX_MATCHES_STL("T:")) 
00486                                         {
00487                                                 int s, a, sp;
00488                                                 REAL_VALUE prob;
00489                                                 if (4 != sscanf(buf.c_str(),"T: %d : %d : %d %lf", &a, &s, &sp, &prob)) 
00490                                                 {
00491                                                         cerr << "ERROR: line " << lineNumber
00492                                                                 << ": syntax error in T statement"
00493                                                                 << endl;
00494                                                         exit(EXIT_FAILURE);
00495                                                 }
00496                                                 kmatrix_set_entry( Tx[a], s, sp, prob );
00497                                         } 
00498                                         else if (PM_PREFIX_MATCHES_STL("O:")) 
00499                                         {
00500                                                 int s, a, o;
00501                                                 REAL_VALUE prob;
00502                                                 if (4 != sscanf(buf.c_str(),"O: %d : %d : %d %lf", &a, &s, &o, &prob)) 
00503                                                 {
00504                                                         cerr << "ERROR: line " << lineNumber
00505                                                                 << ": syntax error in O statement"
00506                                                                 << endl;
00507                                                         exit(EXIT_FAILURE);
00508                                                 }
00509                                                 kmatrix_set_entry( Ox[a], s, o, prob );
00510                                         } 
00511                                         else 
00512                                         {
00513                                                 cerr << "ERROR: line " << lineNumber
00514                                                         << ": got unexpected statement type while parsing body"
00515                                                         << endl;
00516                                                 exit(EXIT_FAILURE);
00517                                         }
00518                         }
00519 
00520                         lineNumber++;
00521                 }
00522 
00523                 in.close();
00524 
00525                 // post-process
00526                 copy( initialBelief, initialBeliefx );
00527                 initialBelief.finalize();
00528                 copy( R, Rx );
00529                 Ttr.resize(numActions);
00530                 O.resize(numActions);
00531                 Otr.resize(numActions);
00532                 T.resize(numActions);
00533                 FOR (a, numActions) {
00534                         copy( T[a], Tx[a] );
00535                         kmatrix_transpose_in_place( Tx[a] );
00536                         copy( Ttr[a], Tx[a] );
00537                         copy( O[a], Ox[a] );
00538                         kmatrix_transpose_in_place(Ox[a] );
00539                         copy(Otr[a], Ox[a]);
00540                 }
00541 
00542 #if USE_DEBUG_PRINT
00543                 gettimeofday(&endTime,0);
00544                 REAL_VALUE numSeconds = (endTime.tv_sec - startTime.tv_sec)
00545                         + 1e-6 * (endTime.tv_usec - startTime.tv_usec);
00546                 cout << "[file reading took " << numSeconds << " seconds]" << endl;
00547 #endif
00548 
00549                 debugDensity();
00550                 //free(dataBuf);
00551         }
00552 
00553 
00554         // this is functionally similar to readFromFile() but much faster.
00555         // the POMDP file must obey a restricted syntax.
00556         void POMDP::readFromFileFast( std::string& fileName)
00557         {
00558                 char buf[1<<20];
00559                 int lineNumber;
00560                 ifstream in;
00561                 char sbuf[512];
00562                 int numSizesSet = 0;
00563                 bool inPreamble = true;
00564                 char *data;
00565 
00566 #if USE_DEBUG_PRINT
00567                 timeval startTime, endTime;
00568                 cout << "reading problem (in fast mode) from " << fileName << endl;
00569                 gettimeofday(&startTime,0);
00570 #endif
00571 
00572                 in.open(fileName.c_str());
00573                 if (!in) {
00574                         cerr << "ERROR: couldn't open " << fileName << " for reading: " << endl;
00575                         exit(EXIT_FAILURE);
00576                 }
00577 
00578                 DenseVector initialBeliefx;
00579                 kmatrix Rx;
00580                 std::vector<kmatrix> Tx, Ox;
00581 
00582 #define PM_PREFIX_MATCHES(X) \
00583         (0 == strncmp(buf,(X),strlen(X)))
00584 
00585                 lineNumber = 1;
00586                 while (!in.eof()) {
00587                         in.getline(buf,sizeof(buf));
00588                         if (in.fail() && !in.eof()) {
00589                                 cerr << "ERROR: readFromFileFast: line too long for buffer"
00590                                         << " (max length " << sizeof(buf) << ")" << endl;
00591                                 exit(EXIT_FAILURE);
00592                         }
00593 
00594                         if ('#' == buf[0]) continue;
00595                         trimTrailingWhiteSpace(buf);
00596                         if ('\0' == buf[0]) continue;
00597 
00598                         if (inPreamble) {
00599                                 if (PM_PREFIX_MATCHES("discount:")) {
00600                                         if (1 != sscanf(buf,"discount: %lf", &discount)) {
00601                                                 cerr << "ERROR: line " << lineNumber
00602                                                         << ": syntax error in discount statement"
00603                                                         << endl;
00604                                                 exit(EXIT_FAILURE);
00605                                         }
00606                                 } else if (PM_PREFIX_MATCHES("values:")) {
00607                                         if (1 != sscanf(buf,"values: %s", sbuf)) {
00608                                                 cerr << "ERROR: line " << lineNumber
00609                                                         << ": syntax error in values statement"
00610                                                         << endl;
00611                                                 exit(EXIT_FAILURE);
00612                                         }
00613                                         if (0 != strcmp(sbuf,"reward")) {
00614                                                 cerr << "ERROR: line " << lineNumber
00615                                                         << ": can only handle values of type reward"
00616                                                         << endl;
00617                                                 exit(EXIT_FAILURE);
00618                                         }
00619                                 } else if (PM_PREFIX_MATCHES("actions:")) {
00620                                         if (1 != sscanf(buf,"actions: %d", &numActions)) {
00621                                                 cerr << "ERROR: line " << lineNumber
00622                                                         << ": syntax error in actions statement"
00623                                                         << endl;
00624                                                 exit(EXIT_FAILURE);
00625                                         }
00626                                         numSizesSet++;
00627                                 } else if (PM_PREFIX_MATCHES("observations:")) {
00628                                         if (1 != sscanf(buf,"observations: %d", &numObservations)) {
00629                                                 cerr << "ERROR: line " << lineNumber
00630                                                         << ": syntax error in observations statement"
00631                                                         << endl;
00632                                                 exit(EXIT_FAILURE);
00633                                         }
00634                                         numSizesSet++;
00635                                 } else if (PM_PREFIX_MATCHES("states:")) {
00636                                         if (1 != sscanf(buf,"states: %d", &numStates)) {
00637                                                 cerr << "ERROR: line " << lineNumber
00638                                                         << ": syntax error in states statement"
00639                                                         << endl;
00640                                                 exit(EXIT_FAILURE);
00641                                         }
00642                                         numSizesSet++;
00643                                 } else {
00644                                         cerr << "ERROR: line " << lineNumber
00645                                                 << ": got unexpected statement type while parsing preamble"
00646                                                 << endl;
00647                                         exit(EXIT_FAILURE);
00648                                 }
00649 
00650                                 if (3 == numSizesSet) {
00651                                         // pre-process
00652                                         setBeliefSize(numStates);
00653                                         initialBeliefx.resize(numStates);
00654                                         set_to_zero(initialBeliefx);
00655                                         Rx.resize(numStates, numActions);
00656                                         Tx.resize(numActions);
00657                                         Ox.resize(numActions);
00658                                         FOR (a, numActions) {
00659                                                 Tx[a].resize(numStates, numStates);
00660                                                 Ox[a].resize(numStates, numObservations);
00661                                         }
00662 
00663                                         inPreamble = false;
00664                                 }
00665 
00666                         } else {
00667 
00668                                 if (PM_PREFIX_MATCHES("start:")) {
00669                                         data = buf + strlen("start: ");
00670                                         reaDenseVector(data,initialBeliefx,numStates);
00671                                 } else if (PM_PREFIX_MATCHES("R:")) {
00672                                         int s, a;
00673                                         REAL_VALUE reward;
00674                                         if (3 != sscanf(buf,"R: %d : %d : * : * %lf", &a, &s, &reward)) {
00675                                                 cerr << "ERROR: line " << lineNumber
00676                                                         << ": syntax error in R statement"
00677                                                         << endl;
00678                                                 exit(EXIT_FAILURE);
00679                                         }
00680                                         kmatrix_set_entry( Rx, s, a, reward );
00681                                 } else if (PM_PREFIX_MATCHES("T:")) {
00682                                         int s, a, sp;
00683                                         REAL_VALUE prob;
00684                                         if (4 != sscanf(buf,"T: %d : %d : %d %lf", &a, &s, &sp, &prob)) {
00685                                                 cerr << "ERROR: line " << lineNumber
00686                                                         << ": syntax error in T statement"
00687                                                         << endl;
00688                                                 exit(EXIT_FAILURE);
00689                                         }
00690                                         kmatrix_set_entry( Tx[a], s, sp, prob );
00691                                 } else if (PM_PREFIX_MATCHES("O:")) {
00692                                         int s, a, o;
00693                                         REAL_VALUE prob;
00694                                         if (4 != sscanf(buf,"O: %d : %d : %d %lf", &a, &s, &o, &prob)) {
00695                                                 cerr << "ERROR: line " << lineNumber
00696                                                         << ": syntax error in O statement"
00697                                                         << endl;
00698                                                 exit(EXIT_FAILURE);
00699                                         }
00700                                         kmatrix_set_entry( Ox[a], s, o, prob );
00701                                 } else {
00702                                         cerr << "ERROR: line " << lineNumber
00703                                                 << ": got unexpected statement type while parsing body"
00704                                                 << endl;
00705                                         exit(EXIT_FAILURE);
00706                                 }
00707                         }
00708 
00709                         lineNumber++;
00710                 }
00711 
00712                 in.close();
00713 
00714                 // post-process
00715                 copy( initialBelief, initialBeliefx );
00716                 copy( R, Rx );
00717                 Ttr.resize(numActions);
00718                 O.resize(numActions);
00719                 T.resize(numActions);
00720                 FOR (a, numActions) {
00721                         copy( T[a], Tx[a] );
00722                         kmatrix_transpose_in_place( Tx[a] );
00723                         copy( Ttr[a], Tx[a] );
00724                         copy( O[a], Ox[a] );
00725                 }
00726 
00727 #if USE_DEBUG_PRINT
00728                 gettimeofday(&endTime,0);
00729                 REAL_VALUE numSeconds = (endTime.tv_sec - startTime.tv_sec)
00730                         + 1e-6 * (endTime.tv_usec - startTime.tv_usec);
00731                 cout << "[file reading took " << numSeconds << " seconds]" << endl;
00732 #endif
00733 
00734                 debugDensity();
00735         }
00736 
00737         void POMDP::debugDensity(void) {
00738                 int Ttr_size = 0;
00739                 int Ttr_filled = 0;
00740                 int O_size = 0;
00741                 int O_filled = 0;
00742                 FOR (a, numActions) {
00743                         Ttr_size += Ttr[a].size1() * Ttr[a].size2();
00744                         O_size += O[a].size1() * O[a].size2();
00745                         Ttr_filled += Ttr[a].filled();
00746                         O_filled += O[a].filled();
00747                 }
00748                 cout << "T density = " << (((REAL_VALUE) Ttr_filled) / Ttr_size)
00749                         << ", O density = " << (((REAL_VALUE) O_filled) / O_size)
00750                         << endl;
00751         }
00752 
00753         void POMDP::getPossibleObservations(vector<int>& result, vector<REAL_VALUE>&
00754                 resultProbs,  belief_vector& bel, int act) 
00755         {
00756                         belief_vector obsBel;
00757                          SparseMatrix obsMat = Otr[act];//problem.getTransposedObservationMatrix(act);
00758                         mult(obsBel, obsMat, bel); 
00759 
00760                         //now to put this info into the vectors
00761                         obsBel.copyIndex(result);
00762                         obsBel.copyValue(resultProbs);
00763         }
00764 
00765         void POMDP::getPossibleActions(vector<int>& result,  belief_vector bel) 
00766         {
00767                 int numActs = getNumActions(); 
00768                 result.reserve(numActs);
00769                 
00770                 vector<int> activeIndices;
00771                 bel.copyIndex(activeIndices);
00772 
00773                 vector<int>::const_iterator iter;
00774                 for(int i = 0; i< numActs; i++)
00775                 {
00776                         SparseMatrix transMat = Ttr[i];//problem.getTransposedTransitionMatrix(i);
00777                         for(iter = activeIndices.begin(); iter != activeIndices.end(); iter++)
00778                         {
00779                                 if(!transMat.isColumnEmpty((*iter)))
00780                                 {
00781                                         result.push_back(i);
00782                                         break;
00783                                 }
00784                         }
00785                 }
00786         }
00787 
00788 }; // namespace momdp
00789 
00790 /***************************************************************************
00791 * REVISION HISTORY:
00792 * $Log: POMDP.cc,v $
00793 * Revision 2.28  2009/01/07 05:10:04  duyanzhu
00794 * added GES.h
00795 *
00796 * Revision 2.26  2009/01/07 05:05:59  duyanzhu
00797 * added evaluator_win
00798 *
00799 * Revision 2.24  2009/01/07 05:01:32  duyanzhu
00800 * add GES
00801 *
00802 * Revision 2.22  2009/01/07 04:59:00  duyanzhu
00803 * adding in UniqueBeliefHeap
00804 *
00805 * Revision 2.20  2009/01/07 04:33:54  duyanzhu
00806 * APPL 0.3, added Policy Evaluator, added Memory Limit option
00807 *
00808 * Revision 2.18  2008/09/17 14:08:35  duyanzhu
00809 * Fix: Prune now only starts 5 seconds after initialization
00810 *
00811 * Revision 2.16  2008/07/16 13:27:45  duyanzhu
00812 * Bring everything to version 2.16
00813 *
00814 * Revision 2.12  2008/07/16 08:38:15  duyanzhu
00815 * Add CPMemUtils class
00816 *
00817 * Revision 2.11  2008/07/16 08:34:48  duyanzhu
00818 * Added parser memory allocation check. Added release script
00819 *
00820 * Revision 2.10  2008/06/14 01:41:16  duyanzhu
00821 * 14 Jun 08 by Du Yanzhu. Added command line option percentageThreshold
00822 *
00823 * Revision 1.5  2007/08/17 01:44:35  duyanzhu
00824 * Change Alpha vector to DenseVector. Xan adds dump functions. Xan's bound propagation code is also included, but not added to Makefile
00825 *
00826 * Revision 1.4  2007/08/16 14:06:41  duyanzhu
00827 * Undo last commit
00828 *
00829 * Revision 1.2  2007/08/10 06:03:05  duyanzhu
00830 * *** empty log message ***
00831 *
00832 * Revision 1.1.1.1  2007/07/24 13:17:50  dyhsu
00833 * Approximate POMDP Planning Library (APPL)
00834 *
00835 * Revision 1.6  2007/06/04 07:58:50  jman
00836 * compiling error fixed and the runtime error handled...
00837 * now to get the tests to run ...
00838 *
00839 * Revision 1.5  2007/04/24 14:37:37  elern
00840 * added 'getMaxReward' method for CA maxDepth and delta computations
00841 *
00842 * Revision 1.4  2007/03/30 14:23:13  elern
00843 * Added 'getTransposedObservationMatrix' for POMDP
00844 *
00845 * Revision 1.3  2007/03/30 05:44:34  elern
00846 * the access methods changed to  type
00847 *
00848 * Revision 1.2  2007/03/30 03:25:16  elern
00849 * added access methods for POMDP
00850 *
00851 * Revision 1.1  2007/03/25 18:13:19  elern
00852 * revised
00853 *
00854 * Revision 1.13  2006/06/05 20:10:46  trey
00855 * filled in reasonable defaults for pomdp bounds
00856 *
00857 * Revision 1.12  2006/05/29 06:05:43  trey
00858 * now mark zero-reward absorbing states as terminal, without an explicit list in the pomdp model file
00859 *
00860 * Revision 1.11  2006/05/29 05:05:19  trey
00861 * updated handling of isTerminalState, no longer explicitly specified in the model file
00862 *
00863 * Revision 1.10  2006/05/27 19:02:18  trey
00864 * cleaned up CassandraMatrix -> SparseMatrix conversion
00865 *
00866 * Revision 1.9  2006/04/28 18:53:57  trey
00867 * removed obsolete #if for NO_COMPRESSED_MATRICES
00868 *
00869 * Revision 1.8  2006/04/28 17:57:41  trey
00870 * changed to use apache license
00871 *
00872 * Revision 1.7  2006/04/27 23:10:48  trey
00873 * put some output in USE_DEBUG_PRINT
00874 *
00875 * Revision 1.6  2006/04/06 04:12:54  trey
00876 * removed default bounds (newLowerBound() and newUpperBound())
00877 *
00878 * Revision 1.5  2006/02/17 18:36:35  trey
00879 * fixed getIsTerminalState() function so RTDP can be used
00880 *
00881 * Revision 1.4  2006/02/06 19:26:09  trey
00882 * removed numOutcomes from MDP class because some MDPs have a varying number of outcomes depending on state; replaced with numObservations in POMDP class
00883 *
00884 * Revision 1.3  2006/02/01 01:09:38  trey
00885 * renamed pomdp namespace -> zmdp
00886 *
00887 * Revision 1.2  2006/01/31 20:12:44  trey
00888 * added newXXXBound() functions
00889 *
00890 * Revision 1.1  2006/01/31 18:31:50  trey
00891 * moved many files from common to pomdpCore
00892 *
00893 * Revision 1.6  2006/01/29 00:18:36  trey
00894 * added POMDP() ructor that calls readFromFile()
00895 *
00896 * Revision 1.5  2006/01/28 03:03:23  trey
00897 * replaced BeliefMDP -> MDP, corresponding changes in API
00898 *
00899 * Revision 1.4  2005/11/03 17:45:30  trey
00900 * moved transition dynamics from HSVI implementation to POMDP
00901 *
00902 * Revision 1.3  2005/10/28 03:50:32  trey
00903 * simplified license
00904 *
00905 * Revision 1.2  2005/10/28 02:51:40  trey
00906 * added copyright headers
00907 *
00908 * Revision 1.1  2005/10/27 21:38:16  trey
00909 * renamed POMDPM to POMDP
00910 *
00911 * Revision 1.7  2005/10/21 20:08:28  trey
00912 * added namespace momdp
00913 *
00914 * Revision 1.6  2005/03/10 22:53:32  trey
00915 * now initialize T matrix even when using sla
00916 *
00917 * Revision 1.5  2005/02/08 23:54:25  trey
00918 * updated to use less type-specific function names
00919 *
00920 * Revision 1.4  2005/01/27 05:31:55  trey
00921 * switched to use Ttr instead of T under sla
00922 *
00923 * Revision 1.3  2005/01/26 04:10:41  trey
00924 * modified problem reading to work with sla
00925 *
00926 * Revision 1.2  2005/01/21 15:21:19  trey
00927 * added readFromFileFast
00928 *
00929 * Revision 1.1  2004/11/13 23:29:44  trey
00930 * moved many files from hsvi to common
00931 *
00932 * Revision 1.2  2004/11/09 21:31:59  trey
00933 * got pomdp source tree into a building state again
00934 *
00935 * Revision 1.1.1.1  2004/11/09 16:18:56  trey
00936 * imported hsvi into new repository
00937 *
00938 * Revision 1.7  2003/09/22 21:42:28  trey
00939 * made some minor fixes so that algorithm variants to compile
00940 *
00941 * Revision 1.6  2003/09/20 02:26:10  trey
00942 * found a major problem in F_a_o_transpose, now fixed; added command-line options for experiments
00943 *
00944 * Revision 1.5  2003/09/16 00:57:02  trey
00945 * lots of performance tuning, mostly fixed rising upper bound problem
00946 *
00947 * Revision 1.4  2003/09/11 01:46:42  trey
00948 * completed conversion to compressed matrices
00949 *
00950 * Revision 1.3  2003/09/07 02:28:07  trey
00951 * started to adapt for boost matrix library
00952 *
00953 * Revision 1.2  2003/07/16 16:07:37  trey
00954 * added isTerminalState
00955 *
00956 * Revision 1.1  2003/06/26 15:41:22  trey
00957 * C++ version of pomdp solver functional
00958 *
00959 *
00960 ***************************************************************************/


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