void TwoStageDynamicBayesianNetwork::
ScopeBackup(    const Scope & stateScope, 
                const Scope & agentScope,
                Scope & X,
                Scope & A) const
{
    //first we compute the 'closure' of the NS state factors Y and
    //observations O. I.e., within stage connections can grow the set
    //of Y and O that need to be considered:
    Scope Y = stateScope;
    Scope O = agentScope;
    ComputeWithinNextStageClosure(Y,O);    

    X.clear();
    A.clear();
    //Next we do the backup of the Ys and Os
    for( Scope::iterator y_it = Y.begin(); y_it != Y.end(); y_it++)
    {
        Index yI = *y_it;
        X.Insert( _m_XSoI_Y.at(yI) );
        A.Insert( _m_ASoI_Y.at(yI) );
    }
    for( Scope::iterator o_it = O.begin(); o_it != O.end(); o_it++)
    {
        X.Insert( _m_XSoI_O.at(*o_it) );
        A.Insert( _m_ASoI_O.at(*o_it) );
    }
    X.Sort();
    A.Sort();
    return;
}
void TwoStageDynamicBayesianNetwork::
ComputeWithinNextStageClosure(Scope& Y, Scope& O) const
{    
    bool converged = true;
    do{
        converged = true;
        //check all Y for non-included Y dependencies
        Scope::const_iterator s_it = Y.begin();
        Scope::const_iterator s_last = Y.end();
        while(s_it != s_last)
        {
            Index yI = *s_it;
            const Scope& y_YSoI = _m_YSoI_Y.at(yI);
            for(Scope::const_iterator oy_it = y_YSoI.begin(); 
                    oy_it != y_YSoI.end(); oy_it++)
                //yI has other Y (oyI = *oy_it) that point to it...
                //let's see if they are in Y already.
                if(! Y.Contains( *oy_it ) )
                {
                    converged = false;
                    Y.Insert(*oy_it);
                }
            s_it++;
        }
        
        //check all O for non-included O and Y dependencies
        Scope::const_iterator o_it = O.begin();
        Scope::const_iterator o_last = O.end();
        while(o_it != o_last)
        {
            Index oI = *o_it;
            const Scope& o_YSoI = _m_YSoI_O.at(oI);
            for(Scope::const_iterator oy_it = o_YSoI.begin(); 
                oy_it != o_YSoI.end(); oy_it++)
                //oI has other Y (oyI = *oy_it) that point to it...
                //let's see if they are in Y already.
                if(! Y.Contains( *oy_it ) )
                {
                    converged = false;
                    Y.Insert(*oy_it);
                }

            const Scope& o_OSoI = _m_OSoI_O.at(oI);
            for(Scope::const_iterator oo_it = o_OSoI.begin(); 
                oo_it != o_OSoI.end(); oo_it++)
                //oI has other O (ooI = *oo_it) that point to it...
                //let's see if they are in O already.
                if(! O.Contains( *oo_it ) )
                {
                    converged = false;
                    O.Insert(*oo_it);
                }


            o_it++;
        }

    }while (! converged );
}
Scope ProblemFireFightingGraph::GetHousesAgentInfluences(Index agI) const
{
    Scope houses;
    // agent's actions only influence two houses
    houses.Insert(agI);
    houses.Insert(agI+1);
    return(houses);
}
void FactoredDecPOMDPDiscrete::SetScopeForLRF(Index LRF, 
            const Scope& X, //the X scope
            const Scope& A, //the A scope
            const Scope& Y,
            const Scope& O
        )
{
    Scope Xbackedup = StateScopeBackup( Y, O);
    Scope Abackedup = AgentScopeBackup( Y, O);
    // X'' = X' + X
    Xbackedup.Insert(X);
    Xbackedup.Sort();
    // A'' = A' + A
    Abackedup.Insert(A);
    Abackedup.Sort();
    SetScopeForLRF(LRF, Xbackedup, Abackedup);
}
void ProblemFireFightingGraph::SetYScopes()
{    
//specify connections for the 2DBN
#if DEBUG_PFFF
    cout << "About to set the SoIs for all next-stage(NS) SFs Y..."<<endl;
#endif
    Scope allAgents;
    for(Index agI=0; agI < GetNrAgents(); agI++)
        allAgents.Insert(agI);

    for(Index yI=0; yI < _m_nrHouses; yI++)
    {
        //determine the X scope of influence (i.e., state factors at prev.stage)
        Scope x;
        Scope a;
        if(yI > 0)
        {
            x.Insert(yI-1);
            a.Insert(yI-1);
        }
        x.Insert(yI); // firelevel of house yI influenced by its PS firelevel
        if(yI < (_m_nrHouses-1) )
        {
            x.Insert(yI+1);
            a.Insert(yI);
        }

        SetSoI_Y( yI, //sfacI = 0 -> house 0
                  x,
                  a,
                  //allAgents,
                  Scope("<>") // no interdependencies between next-stage FLs
                );
    }
}
void ProblemFireFightingGraph::SetOScopes()
{
#if DEBUG_PFFF
    cout << "About to set the SoIs for all observations O..."<<endl;
#endif
    for(Index oI=0; oI < _m_nrAgents; oI++)
    {
        Scope agSC;
        agSC.Insert(oI); //only agents' own action influences its obseration
        SetSoI_O( oI, //sfacI = 0 -> house 0
                  agSC,
                  GetHousesAgentInfluences(oI),
                  Scope("<>") // no interdependencies between next-stage Os
            );
    }
}
Beispiel #7
0
FSAOHDist_NECOF::FSAOHDist_NECOF(const PlanningUnitFactoredDecPOMDPDiscrete* p)
    :
        _m_puf(p)
        ,_m_stage(0)
        ,_m_sfacMarginals( new FSDist_COF() )
        ,_m_oHistConditional( p->GetNrAgents(), 0 )
        ,_m_oHistMarginals( vector< vector<double> >( 
                    p->GetNrAgents(),
                    vector<double>(1, 1.0) ) 
                )
        ,_m_sfacSoI( p->GetFDPOMDPD()->GetNrAgents() )
        ,_m_sfacSoI_ii_size( p->GetFDPOMDPD()->GetNrAgents() )
        ,_m_stepsize( p->GetFDPOMDPD()->GetNrAgents(), 0)
{
    const FactoredDecPOMDPDiscreteInterface* fd =  _m_puf->GetFDPOMDPD();
    const vector<size_t>& nrValsPerSF = fd->GetNrValuesPerFactor();
    //first fill the scope of influence for all agent's (observations)
    for(Index agI=0; agI < _m_puf->GetNrAgents(); agI++)
    {
        Scope sfSc_dummy;
        Scope agSc;
        agSc.Insert(agI);
        _m_sfacSoI.at(agI) = fd->Get2DBN()->GetYSoI_O(agI);
#if DEBUG_CONSTRUCTOR        
        cout << "_m_sfacSoI.at("<<agI<<") is "<< _m_sfacSoI.at(agI)<<endl;
#endif                 
        //compute the stepsize
        vector<size_t> nrValsForScope(_m_sfacSoI.at(agI).size());
        IndexTools::RestrictIndividualIndicesToScope(nrValsPerSF,_m_sfacSoI.at(agI),
                                                     nrValsForScope);
        _m_sfacSoI_ii_size.at(agI) = VectorTools::VectorProduct(nrValsForScope);
        _m_stepsize.at(agI) = IndexTools::CalculateStepSize(nrValsForScope);
    }

    //now fill the probabilities of the (initial=empty) observation
    //history for each agent
    for(Index agI=0; agI < _m_puf->GetNrAgents(); agI++)
    {
        size_t nrXs = _m_sfacSoI_ii_size.at(agI);
        size_t nrOHs = 1;
        CPT* cpt =  new CPT(nrOHs, nrXs );
        for(Index xI=0; xI < nrXs; xI++)
            cpt->Set(0, xI, 1.0);

        _m_oHistConditional.at(agI) =  cpt;
    }
}