double TwoStageDynamicBayesianNetwork:: GetYProbabilityGeneral( const Scope& Xscope, const vector<Index>& X, const Scope& Ascope, const vector<Index>& A, const Scope& YIIscope, const vector<Index>& YII, const Scope& Yscope, const vector<Index>& Y ) const { double p = 1.0; for(Index Y_index=0; Y_index < Y.size(); Y_index++) { //Y_index is index in vector Y Index y = Yscope.at(Y_index); // the index to the variable we look at Index yVal = Y.at(Y_index); // the value of that variable vector<Index> X_restr(GetXSoI_Y(y).size()); IndexTools::RestrictIndividualIndicesToNarrowerScope(X, Xscope, GetXSoI_Y(y), X_restr ); vector<Index> A_restr(GetASoI_Y(y).size()); IndexTools::RestrictIndividualIndicesToNarrowerScope(A, Ascope, GetASoI_Y(y), A_restr ); vector<Index> YII_restr(GetYSoI_Y(y).size()); IndexTools::RestrictIndividualIndicesToNarrowerScope(YII, YIIscope, GetYSoI_Y(y), YII_restr ); Index iiI = IndividualToJointYiiIndices(y, X_restr, A_restr, YII_restr); double p_y = _m_Y_CPDs[y]->Get(yVal, iiI); p *= p_y; } return(p); }
double FSAOHDist_NECOF::GetOHProb_relT( const Scope& agSc, const std::vector<Index>& ohI) const { double p = 1.0; for(Index i=0; i < agSc.size(); i++) { Index agI = agSc.at(i); Index ohI_agI = ohI.at(i); double p_this_agent = _m_oHistMarginals.at(agI).at(ohI_agI); p *= p_this_agent; } return p; }
string MADPComponentFactoredStates::SoftPrintPartialState( const Scope& sfSc, const std::vector<Index>& sfValues) const { if(sfSc.size() != sfValues.size()) throw E("MADPComponentFactoredStates::SoftPrintPartialState: sfSc.size() != sfValues.size()"); stringstream ss; ss << "<"; for(Index scI=0; scI < sfSc.size(); scI++) { Index sfI = sfSc.at(scI); Index sfValI = sfValues.at(scI); StateFactorDiscrete* sfac = _m_stateFactors.at(sfI); string name = sfac->GetName(); string value = sfac->GetStateFactorValue(sfValI); if(scI > 0) ss << ","; ss << name << "=" << value; } ss << ">"; return(ss.str()); }
double FSAOHDist_NECOF::GetXOHProb_relT_SufficientSFscope( const Scope& sfSc, const std::vector<Index>& sfacIndices, const Scope& agSc, const std::vector<Index>& ohIndices ) const { double p = GetXProb(sfSc, sfacIndices); for(Index i=0; i < agSc.size(); i++) { Index agI=agSc.at(i); Index ohI=ohIndices.at(i); vector<Index> sfacIrestr(_m_sfacSoI.at(agI).size()); try{ IndexTools::RestrictIndividualIndicesToNarrowerScope( sfacIndices, sfSc, _m_sfacSoI.at(agI), sfacIrestr ); }catch(ENoSubScope& e) { stringstream ss; ss <<"Error FSAOHDist_NECOF::GetXOHProb_relT should be called with all state factors on which the observation histories (of the agents in agSc) depend\n"; ss <<"sfSc="<<sfSc<<", agSc="<<agSc<<endl; string s(ss.str()); cerr << s; throw ENoSubScope(s); } Index x = IndexTools::IndividualToJointIndicesStepSize(sfacIrestr, _m_stepsize.at(agI) ); double p_i = _m_oHistConditional.at(agI)->Get(ohI, x); p *= p_i; } return p; }
void BGCG_SolverMaxPlus::Construct_JointType_Factors_CGBG( const boost::shared_ptr<const BayesianGameCollaborativeGraphical> &bgcg, const vector< vector<Index> >& var_indices, const vector< libDAI::Var >& vars, vector<libDAI::Factor>& facs, int verbosity) { size_t nrLRFs = bgcg->GetNrLRFs(); //compute nrFacs: size_t nrFacs = 0; for(Index e=0; e < nrLRFs; e++) { const BayesianGameIdenticalPayoff * bgip_e = bgcg->GetBGIPforLRF (e); size_t nrJT_e = bgip_e->GetNrJointTypes(); if(verbosity >= 2) cout << "Edge "<<e<<" has " << nrJT_e << " local joint types (i.e. factors)" <<endl; nrFacs += nrJT_e; } if(verbosity >= 1) cout << "FactorGraph has "<<nrFacs<<" factors"<<endl; facs.reserve(nrFacs); for(Index e=0; e < nrLRFs; e++) { //construct the factors for LRF component e. const BayesianGameIdenticalPayoff * bgip_e = bgcg->GetBGIPforLRF (e); Scope agentScope = bgcg->GetScope(e); const size_t nrJA_e = bgip_e->GetNrJointActions(); for(Index jtI_e=0; jtI_e < bgip_e->GetNrJointTypes(); jtI_e++) { vector<Index> indTypes_e = bgip_e->JointToIndividualTypeIndices(jtI_e); if(verbosity > 5) cout << "Adding factor for joint type jtI_e="<<jtI_e<< " ( indtypes_e="<<SoftPrintVector(indTypes_e) << " )"<<endl; //compute the agent-type (i.e., factor) indices atI, get the //relevant variables and put them in a VarSet libDAI::VarSet vs; for(Index agI_e=0; agI_e < bgip_e->GetNrAgents(); agI_e++) { //translate agI_e to global agent index: Index agI = agentScope.at(agI_e); Index atI = var_indices[agI][indTypes_e[agI_e]]; vs = vs | vars[atI]; if(verbosity > 5) cout << "agent "<<agI<< " contributes through variable atI="<<atI<<endl; } libDAI::Real factor_vals[nrJA_e]; for(Index jaI_e=0; jaI_e < nrJA_e; jaI_e++) { vector<Index> indAcs = bgip_e->JointToIndividualActionIndices(jaI_e); vector<size_t> indAcsST(indAcs.size()); for(Index i=0; i<indAcs.size(); i++) indAcsST[i] = (size_t) indAcs[i]; if(verbosity > 5) cout << "Computing joint factor index (JFI) for vs="<<vs <<", (action)indices="<< SoftPrintVector(indAcs) <<endl; Index jointFactorIndex = libDAI::Factor::IndividualToJointFactorIndex(vs, indAcsST); if(verbosity > 5) cout << "JFI="<<jointFactorIndex<< " Utility " << bgip_e->GetUtility(jtI_e, jaI_e) << " Prob " << bgip_e->GetProbability(jtI_e) << endl; double perturbation=(1e-6*rand())/RAND_MAX; factor_vals[jointFactorIndex] = (bgip_e->GetUtility(jtI_e, jaI_e) + perturbation) * bgip_e->GetProbability(jtI_e); } libDAI::Factor f(vs, factor_vals); if(verbosity > 4) { cout << "Added factor f:"<< f << endl; } facs.push_back(f); }// <- end for jtI_e }// <- end for e }
double FSAOHDist_NECOF::GetXOHProb_relT( const Scope& sfSc, const std::vector<Index>& sfacIndices, const Scope& agSc, const std::vector<Index>& ohIndices ) const { //we use a running example to illustrate the code: // ->we want to compute P(o1o2x1x2) //However, o1 additionally depends on x3 and o2 on x4 //first check if sfSc contains all state factors that can influence //the observation (and thus the OHs) of the agents in agentSc, if //not, we will need to marginalize over the state factors that are not //in sfSc (but are in the SoI). Scope all_relevant_sfacIs(sfSc); for(Index i=0; i < agSc.size(); i++) all_relevant_sfacIs.Insert( _m_puf->GetFDPOMDPD()-> Get2DBN()->GetYSoI_O( agSc[i] ) ); Scope to_marginalize_over_sfacIs(all_relevant_sfacIs); to_marginalize_over_sfacIs.Remove(sfSc); //p(x1x2) = P(x1)*P(x2) double p1 = GetXProb(sfSc, sfacIndices); vector<Index> marg_vec(to_marginalize_over_sfacIs.size(), 0); vector<size_t> marg_nrSFvals( to_marginalize_over_sfacIs.size()); IndexTools::RestrictIndividualIndicesToScope( _m_puf->GetFDPOMDPD()->GetNrValuesPerFactor(), to_marginalize_over_sfacIs, marg_nrSFvals); //this will hold P(o1o2|x1x2) double p_ohIndices_given_sfacIndices = 0.0; do { //the probability of this instantiation of marg_vec //P(x3x4) double p2 = GetXProb(to_marginalize_over_sfacIs, marg_vec); if(p2>0) // optimalization: if p2 is zero the rest will be zero as well { //construct the 'full' state vector (i.e., containing all_relevant_sfacIs) //the vector x1x2x3x4 vector<Index> all_relevant_vals(sfacIndices); all_relevant_vals.insert(all_relevant_vals.end(), marg_vec.begin(), marg_vec.end()); //P(o1o2|x1x2x3x4) double p_ohIndices_given_all_relevant_vals = 1.0; for(Index i=0; i < agSc.size(); i++) { Index agI=agSc.at(i); Index ohI=ohIndices.at(i); vector<Index> sfacIrestr(_m_sfacSoI.at(agI).size()); try{ IndexTools::RestrictIndividualIndicesToNarrowerScope( all_relevant_vals, all_relevant_sfacIs, _m_sfacSoI.at(agI), sfacIrestr); }catch(ENoSubScope& e) { throw ENoSubScope("bah"); } Index x = IndexTools::IndividualToJointIndicesStepSize( sfacIrestr, _m_stepsize.at(agI) ); double p_OHi_given_all_relevant_vals = _m_oHistConditional.at(agI)->Get(ohI, x); //P(o1o2|x1x2x3x4) = P(o1|x1x2x3)P(o2|x1x2x4) p_ohIndices_given_all_relevant_vals *= p_OHi_given_all_relevant_vals; } //P(o1o2x3x4|x1x2) = P(o1o2|x1x2x3x4)P(x3x4) double p_ohIndices_marg_vec_given_sfacIndices = p2 * p_ohIndices_given_all_relevant_vals; //P(o1o2|x1x2) = sum_{x3x4} P(o1o2x3x4|x1x2) p_ohIndices_given_sfacIndices += p_ohIndices_marg_vec_given_sfacIndices; } }while (! IndexTools::Increment( marg_vec, marg_nrSFvals ) ); //finally P(o1o2x1x2): double result = p1 * p_ohIndices_given_sfacIndices; return(result); }