00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include <iostream>
00031 using namespace std;
00032
00033 #include "../sbpl/headers.h"
00034
00035
00036
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
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
00136 PredsID.clear();
00137
00138
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
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
00201
00202
00203
00204 return false;
00205 }
00206
00207
00208
00209 bool CMDPSTATE::RemoveAllActions()
00210 {
00211 CMDPACTION* action;
00212
00213
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
00259 bool CMDP::empty()
00260 { return ((int)StateArray.size() == 0);};
00261
00262 bool CMDP::full()
00263 { return ((int)StateArray.size() >= MAXSTATESPACESIZE);};
00264
00265
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
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