コード例 #1
0
ファイル: mcts.cpp プロジェクト: EHadoux/APSWithPOMCP
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;
}
コード例 #2
0
ファイル: flatmcts.cpp プロジェクト: aijunbai/hplanning
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;
  }
}