bool MCTS::Update(int action, int observation, double reward) { History.Add(action, observation); BELIEF_STATE beliefs; // Find matching vnode from the rest of the tree QNODE &qnode = Root->Child(action); VNODE *vnode = qnode.Child(observation); if( vnode ) { if( Params.Verbose >= 1 ) cout << "Matched " << vnode->Beliefs().GetNumSamples() << " states" << endl; beliefs.Copy(vnode->Beliefs(), Simulator); } else { if( Params.Verbose >= 1 ) cout << "No matching node found" << endl; } // Generate transformed states to avoid particle deprivation if( Params.UseTransforms ) AddTransforms(Root, beliefs); // If we still have no particles, fail if( beliefs.Empty() && (!vnode || vnode->Beliefs().Empty())) return false; if( Params.Verbose >= 1 ) Simulator.DisplayBeliefs(beliefs, cout); // Find a state to initialise prior (only requires fully observed state) const STATE *state = 0; if( vnode && !vnode->Beliefs().Empty()) state = vnode->Beliefs().GetSample(0); else state = beliefs.GetSample(0); // Delete old tree and create new root VNODE::Free(Root, Simulator); VNODE *newRoot = ExpandNode(state); newRoot->Beliefs() = beliefs; Root = newRoot; return true; }
void ROOMS::DisplayBeliefs(const BELIEF_STATE &belief, std::ostream &ostr) const { unordered_map<COORD, int> m; for (int i = 0; i < belief.GetNumSamples(); ++i) { const ROOMS_STATE &state = safe_cast<const ROOMS_STATE &>(*belief.GetSample(i)); m[state.AgentPos] += 1; } vector<pair<double, const COORD *>> sorted; for (unordered_map<COORD, int>::iterator it = m.begin(); it != m.end(); ++it) { double p = double(it->second) / double(belief.GetNumSamples()); sorted.push_back(make_pair(p, &(it->first))); } sort(sorted.begin(), sorted.end(), greater<pair<double, const COORD *>>()); ostr << "#Belief: "; for (uint i = 0; i < sorted.size(); ++i) { ostr << "#" << *(sorted[i].second) << " (" << sorted[i].first << ") "; } ostr << std::endl; }
bool FlatMCTS::Update(int action, int observation, STATE &state) { History.Add(action, observation); if (Simulator.mFullyObservable) { // running an MDP in fact in cases of hplanning // Delete old tree and create new root VNODE::Free(Root, Simulator); Root = ExpandNode(&state, History); STATE *sample = Simulator.Copy(state); Root->Beliefs().AddSample(sample); if (Params.Verbose >= 1) Simulator.DisplayBeliefs(Root->Beliefs(), cout); return true; } else { BELIEF_STATE beliefs; // Find matching vnode from the rest of the tree QNODE &qnode = Root->Child(action); VNODE *vnode = qnode.Child(observation); if (vnode) { if (Params.Verbose >= 1) cout << "Matched " << vnode->Beliefs().GetNumSamples() << " states" << endl; beliefs.Copy(vnode->Beliefs(), Simulator); } else { if (Params.Verbose >= 1) cout << "No matching node found" << endl; } if (Params.Verbose >= 1) Simulator.DisplayBeliefs(beliefs, cout); if (Params.UseParticleFilter) { ParticleFilter(beliefs); if (Params.Verbose >= 1) Simulator.DisplayBeliefs(beliefs, cout); } // Generate transformed states to avoid particle deprivation if (Params.UseTransforms) { AddTransforms(beliefs); if (Params.Verbose >= 1) Simulator.DisplayBeliefs(beliefs, cout); } // If we still have no particles, fail if (beliefs.Empty() && (!vnode || vnode->Beliefs().Empty())) return false; // Find a state to initialise prior (only requires fully observed state) const STATE *sample = 0; if (vnode && !vnode->Beliefs().Empty()) sample = vnode->Beliefs().GetSample(); else sample = beliefs.GetSample(); if (vnode && Params.ReuseTree) { int size1 = VNODE::GetNumAllocated(); VNODE::Free(Root, Simulator, vnode); int size2 = VNODE::GetNumAllocated(); assert(size2 < size1); Root = vnode; Root->Beliefs().Free(Simulator); } else { // Delete old tree and create new root VNODE::Free(Root, Simulator); Root = ExpandNode(sample, History); } Root->Beliefs() = beliefs; return true; } }