Exemple #1
0
VNODE* BAMCP::ExpandNode()
{
    VNODE* vnode = VNODE::Create();
    vnode->Value.Set(0, 0);
    vnode->SetChildren(0,0);

    if (Params.Verbose >= 2)
    {
        cout << "Expanding node: ";
        History.Display(cout);
        cout << endl;
    }

    return vnode;
}
Exemple #2
0
void FlatMCTS::UnitTestGreedy() {
  TEST_SIMULATOR testSimulator(5, 5, 0);
  PARAMS params;
  FlatMCTS mcts(testSimulator, params, 0);
  int numAct = testSimulator.GetNumActions();

  HISTORY History(0);
  VNODE *vnode = mcts.ExpandNode(testSimulator.CreateStartState(), History);
  vnode->UCB.Value.Set(1, 0);
  vnode->Child(0).UCB.Value.Set(1, 1/*, 2, 1*/);
  for (int action = 1; action < numAct; action++)
    vnode->Child(action).UCB.Value.Set(0, 0/*, 1, 1*/);

  int besta = mcts.GreedyUCB(vnode, false);
  assert(besta == 0);
}
Exemple #3
0
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;
}
Exemple #4
0
VNODE* VNODE::Create()
{
    VNODE* vnode = VNodePool.Allocate();
    vnode->Initialise();
    return vnode;
}
Exemple #5
0
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;
  }
}