mdp.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2008, Maxim Likhachev
00003  * All rights reserved.
00004  * 
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  * 
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Neither the name of the University of Pennsylvania nor the names of its
00014  *       contributors may be used to endorse or promote products derived from
00015  *       this software without specific prior written permission.
00016  * 
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
00028  */
00029 //MDP.cpp - contains all the functions for MDP classes
00030 #include <iostream>
00031 using namespace std;
00032 
00033 #include "../sbpl/headers.h"
00034 
00035 
00036 //-------------------------MDPACTION class functions----------
00037 bool CMDPACTION::Delete()
00038 {
00039         SuccsID.clear();
00040         Costs.clear();
00041         SuccsProb.clear();
00042         return true;
00043 };
00044 
00045 
00046 bool CMDPACTION::DeleteAllOutcomes()
00047 {
00048         SuccsID.clear();
00049         Costs.clear();
00050         SuccsProb.clear();
00051         return true;
00052 };
00053 
00054 bool CMDPACTION::IsValid()
00055 {
00056         float Prob = 0;
00057         for(int i = 0; i < (int)SuccsProb.size(); i++)
00058         {
00059                 Prob += SuccsProb[i];
00060         }
00061         
00062         return (fabs(Prob-1.0) < EPS_ERROR); 
00063 };
00064 
00065 void CMDPACTION::AddOutcome(int OutcomeStateID, int OutcomeCost, float OutcomeProb)
00066 {
00067 
00068 #if MEM_CHECK
00069         DisableMemCheck();
00070 #endif
00071 
00072         SuccsID.push_back(OutcomeStateID);
00073         Costs.push_back(OutcomeCost);
00074         SuccsProb.push_back(OutcomeProb);
00075 
00076 #if MEM_CHECK
00077         EnableMemCheck();
00078 #endif
00079 
00080 };
00081 
00082 void CMDPACTION::operator = (const CMDPACTION& rhsaction)
00083 {
00084         this->ActionID = rhsaction.ActionID;
00085 }
00086 
00087 int CMDPACTION::GetIndofMostLikelyOutcome()
00088 {
00089         double HighestProb = 0;
00090         int mlind = -1;
00091 
00092         for(int oind = 0; oind < (int)this->SuccsID.size(); oind++)
00093         {
00094                 if(this->SuccsProb[oind] >= HighestProb)
00095                 {
00096                         mlind = oind;
00097                         HighestProb = this->SuccsProb[oind];
00098                 }
00099         }
00100 
00101         return mlind;
00102 
00103 
00104 }
00105 
00106 int CMDPACTION::GetIndofOutcome(int OutcomeID)
00107 {
00108 
00109         for(int oind = 0; oind < (int)this->SuccsID.size(); oind++)
00110         {
00111                 if(this->SuccsID[oind] == OutcomeID)
00112                 {
00113                         return oind;
00114                 }
00115         }
00116 
00117         return -1;
00118 
00119 
00120 }
00121 
00122 //----------------------------------------------------------------------
00123 
00124 //----------------------------MDPSTATE class functions---------------
00125 bool CMDPSTATE::Delete()
00126 {
00127         CMDPACTION* action;
00128 
00129         if(this->PlannerSpecificData != NULL)
00130         {
00131                 SBPL_ERROR("ERROR deleting state: planner specific data is not deleted\n");
00132                 throw new SBPL_Exception();
00133         }
00134         
00135         //delete predecessors array
00136         PredsID.clear();
00137 
00138         //delete actions array
00139         while((int)Actions.size() > 0)
00140         {
00141                 action = Actions[Actions.size()-1];
00142                 Actions.pop_back();
00143 
00144                 action->Delete();
00145                 delete action;
00146         }
00147         
00148         return true;
00149 };
00150 
00151 CMDPACTION* CMDPSTATE::AddAction(int ID)
00152 {
00153         CMDPACTION* action = new CMDPACTION(ID, this->StateID);
00154 
00155 #if MEM_CHECK
00156         DisableMemCheck();
00157 #endif
00158 
00159         Actions.push_back(action);
00160 
00161 #if MEM_CHECK
00162         EnableMemCheck();
00163 #endif
00164 
00165 
00166         return action;
00167 };
00168 
00169 
00170 bool CMDPSTATE::AddPred(int stateID)
00171 {
00172         //add the predecessor
00173         if(!ContainsPred(stateID))
00174         {
00175 #if MEM_CHECK
00176         DisableMemCheck();
00177 #endif
00178 
00179     PredsID.push_back(stateID);
00180 #if MEM_CHECK
00181         EnableMemCheck();
00182 #endif
00183         }
00184 
00185         return true;
00186 }
00187 
00188 bool CMDPSTATE::RemovePred(int stateID)
00189 {
00190         for(int i = 0; i < (int)this->PredsID.size(); i++)
00191         {
00192                 if(this->PredsID.at(i) == stateID)
00193                 {
00194                         this->PredsID.at(i) = this->PredsID.at(this->PredsID.size()-1);
00195                         this->PredsID.pop_back();
00196                         return true;
00197                 }
00198         }
00199 
00200         //can happen when a state is twice a successor
00201         //SBPL_ERROR("ERROR in RemovePred: no Pred is found\n");
00202         //throw new SBPL_Exception();
00203 
00204         return false;
00205 }
00206 
00207 
00208 //requires the deletion of Preds elsewhere
00209 bool CMDPSTATE::RemoveAllActions()
00210 {
00211         CMDPACTION* action;
00212 
00213         //delete actions array
00214         while((int)Actions.size() > 0)
00215         {
00216                 action = Actions[Actions.size()-1];
00217                 Actions.pop_back();
00218 
00219                 action->Delete();
00220                 delete action;
00221         }
00222         
00223         return true;
00224 }
00225 
00226 bool CMDPSTATE::ContainsPred(int stateID)
00227 {
00228         for(int i = 0; i < (int)PredsID.size(); i++)
00229         {
00230                 if(PredsID[i] == stateID)
00231                         return true;
00232         }
00233         return false;
00234 };
00235 
00236 void CMDPSTATE::operator = (const CMDPSTATE& rhsstate)
00237 {
00238         this->StateID = rhsstate.StateID;
00239 
00240 }
00241 
00242 CMDPACTION* CMDPSTATE::GetAction(int actionID)
00243 {
00244 
00245         for(int i = 0; i < (int)Actions.size(); i++)
00246         {
00247                 if(Actions[i]->ActionID == actionID)
00248                         return Actions[i];
00249         }
00250         
00251         return NULL;
00252 }
00253 
00254 
00255 //-------------------------------------------------------------------------
00256 
00257 
00258 //-------------MDP class functions--------------------------------
00259 bool CMDP::empty()
00260 {       return ((int)StateArray.size() == 0);};
00261 
00262 bool CMDP::full()
00263 {       return ((int)StateArray.size() >= MAXSTATESPACESIZE);};
00264 
00265 //creates numofstates states. Their ids must be initialized elsewhere
00266 bool CMDP::Create(int numofstates)
00267 {
00268         CMDPSTATE* state;
00269 
00270         if(numofstates > MAXSTATESPACESIZE)
00271         {
00272                 SBPL_ERROR("ERROR in Create: maximum MDP size is reached\n");
00273                 throw new SBPL_Exception();
00274         }
00275 
00276         for(int i = 0; i < numofstates; i++)
00277         {
00278                 state = new CMDPSTATE(-1);
00279 
00280 #if MEM_CHECK
00281         DisableMemCheck();
00282 #endif
00283                 StateArray.push_back(state);
00284 #if MEM_CHECK
00285         EnableMemCheck();
00286 #endif
00287 
00288         }
00289 
00290         return true;
00291 };
00292 
00293 //Adds a new state The id must be initialized elsewhere
00294 CMDPSTATE* CMDP::AddState(int StateID)
00295 {
00296         CMDPSTATE* state;
00297 
00298         if((int)StateArray.size()+1 > MAXSTATESPACESIZE)
00299         {
00300                 SBPL_ERROR("ERROR: maximum of states is reached in MDP\n");
00301                 throw new SBPL_Exception();
00302         }
00303 
00304         state = new CMDPSTATE(StateID);
00305 
00306 #if MEM_CHECK
00307         DisableMemCheck();
00308 #endif
00309         StateArray.push_back(state);
00310 #if MEM_CHECK
00311         EnableMemCheck();
00312 #endif
00313 
00314 
00315         return state;
00316 };
00317 
00318 
00319 bool CMDP::Delete()
00320 {
00321         CMDPSTATE* state;
00322 
00323         while((int)StateArray.size() > 0)
00324         {
00325                 state = StateArray[StateArray.size()-1];
00326                 StateArray.pop_back();
00327 
00328                 state->Delete();
00329                 delete state;
00330         }
00331 
00332         return true;
00333 };
00334 
00335 void CMDP::Print(FILE* fOut)
00336 {
00337         SBPL_FPRINTF(fOut, "MDP statespace size=%d\n", (unsigned int)StateArray.size());
00338         for(int i = 0; i < (int)StateArray.size(); i++)
00339         {
00340                 SBPL_FPRINTF(fOut, "%d: ", StateArray[i]->StateID);
00341                 for( int j = 0; j < (int)StateArray[i]->Actions.size(); j++)
00342                 {
00343                         CMDPACTION* action = StateArray[i]->Actions[j];
00344                         SBPL_FPRINTF(fOut, "[%d", action->ActionID);
00345                         for( int outind = 0; outind < (int)action->SuccsID.size(); outind++)
00346                         {
00347                                 SBPL_FPRINTF(fOut, " %d %d %f", action->SuccsID[outind], action->Costs[outind],
00348                                         action->SuccsProb[outind]);
00349                         }
00350                         SBPL_FPRINTF(fOut, "] ");
00351                 }
00352                 SBPL_FPRINTF(fOut, "\n");
00353         }
00354 }
00355 
00356 //--------------------------------------------------------
00357 
00358 //----------------other functions-------------------------
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines


sbpl
Author(s): Maxim Likhachev/maximl@seas.upenn.edu
autogenerated on Fri Jan 18 2013 13:41:53