Exemple #1
0
int main(int argc, char* argv[]){
   int retval, i, pass, size, kid;
   
   size = 10;
   
   ancestor_info_t* anc = calloc(sizeof(ancestor_info_t), size);
   
   /* Makes sure a NULL table returns -1 and sets errno to EFAULT */
   retval = ancestors(0, size, NULL);
   if(retval == -1){
      if(errno == EFAULT)
         fprintf(stdout, "PASS: a NULL table threw an EFAULT error\n");
      else
         fprintf(stdout, "FAIL: a NULL table should have thrown an EFAULT error.\n");
   }
   else
      fprintf(stdout, "FAIL: a NULL table should have thrown an EFAULT error.\n");
   
   /* Makes sure a table address in kernel space returns -1 and sets errno to EFAULT */
   
   retval = ancestors(0, size, (ancestor_info_t*)(0xFFFFFFFE));
   if(retval == -1){
      if(errno == EFAULT)
         fprintf(stdout, "PASS: a table address in kernel space threw an EFAULT error\n");
      else
         fprintf(stdout, "FAIL: a table address in kernel space should have thrown an EFAULT error.\n");
   }
   else
      fprintf(stdout, "FAIL: a table address in kernel space should have thrown an EFAULT error.\n");
   
   return 0;
}
Exemple #2
0
void RegionGraph::calcCVMCountingNumbers() {
    // Calculates counting numbers of inner regions based upon counting numbers of outer regions

    vector<vector<size_t> > ancestors(nrIRs());
    boost::dynamic_bitset<> assigned(nrIRs());
    for( size_t beta = 0; beta < nrIRs(); beta++ ) {
        IR(beta).c() = 0.0;
        for( size_t beta2 = 0; beta2 < nrIRs(); beta2++ )
            if( (beta2 != beta) && IR(beta2) >> IR(beta) )
                ancestors[beta].push_back(beta2);
    }

    bool new_counting;
    do {
        new_counting = false;
        for( size_t beta = 0; beta < nrIRs(); beta++ ) {
            if( !assigned[beta] ) {
                bool has_unassigned_ancestor = false;
                for( vector<size_t>::const_iterator beta2 = ancestors[beta].begin(); (beta2 != ancestors[beta].end()) && !has_unassigned_ancestor; beta2++ )
                    if( !assigned[*beta2] )
                        has_unassigned_ancestor = true;
                if( !has_unassigned_ancestor ) {
                    Real c = 1.0;
                    foreach( const Neighbor &alpha, nbIR(beta) )
                        c -= OR(alpha).c();
                    for( vector<size_t>::const_iterator beta2 = ancestors[beta].begin(); beta2 != ancestors[beta].end(); beta2++ )
                        c -= IR(*beta2).c();
                    IR(beta).c() = c;
                    assigned.set(beta, true);
                    new_counting = true;
                }
            }
        }
    const Object* Object::getCommonAncestor(const Object* object) const
    {
      if (m_model == object->m_model)
      {
        std::list<Object*> ancestors(getAncestorPath()) ;
        std::list<Object*> object_ancestors(object->getAncestorPath()) ;
        
        const Object* result = NULL ;
        
        std::list<Object*>::const_iterator i = ancestors.begin() ;
        std::list<Object*>::const_iterator j = object_ancestors.begin() ;
        
        while (i != ancestors.end() && j != object_ancestors.end() &&
               *i == *j)
        {
          result = *i ;
          ++i ;
          ++j ;
        } 
        
        return result ;
      }

      return NULL ;
    }
Exemple #4
0
RcppExport SEXP LocalRecProg_cpp(SEXP data, SEXP K_Level_R, SEXP FindLowestK_R, SEXP ancestor_R, SEXP weight_R, SEXP range_R, SEXP categoryCount_R, SEXP LowMemory_R, SEXP g_MissingValue_R)
{
BEGIN_RCPP
  int i, j;
  int c2;
  int K_Level;
  //uint TimeStart = TimeGetMilliSecond(), TimeMid;
  EMatch Match = e_Match_Complete;
  BOOL FindLowestK = FALSE, LowMemory = FALSE;

    //============= Init
  Uninit_LocalRec();

  g_Debug_LocalRec = FALSE;
  g_Output = e_Output_Average;
  g_Epsilon = es_Epsilon;

  Rcpp::NumericMatrix Mat_LocalRec(data);  // creates Rcpp matrix from SEXP
  g_NbRow_LocRec = Mat_LocalRec.rows();
  int NbVar_LocRec = Mat_LocalRec.cols();
  // NbVar_LocRec = NbTotalVar / 2;
  //=============init Parameters
  K_Level = Rcpp::as<int>(K_Level_R);
  //  printf("klevel %d \n",K_Level);
  if(K_Level >= 1) Match = e_Match_Kneib;
  if(Rcpp::as<bool>(range_R)) g_Output = e_Output_Range;
  FindLowestK = Rcpp::as<bool>(FindLowestK_R);
  LowMemory = Rcpp::as<bool>(LowMemory_R);
  double g_MissingValue_LocalRec = Rcpp::as<double>(g_MissingValue_R);
  if(g_MissingValue_LocalRec == 0) g_MissingValue_LocalRec = -1.0f;
  // Ancestors
  // 1. Column: Position of the variable with ancestor variables (0-based)
  // 2. Column: number of ancestor variables
  Rcpp::NumericMatrix ancestors(ancestor_R);
  int ancestorLen = ancestors.rows();
  int ancestorCol = ancestors.cols();
  if(ancestorLen > 1 && ancestorCol>1)
  {
    for(j=0; j < ancestorLen; j++)
    {
      NbVar_LocRec -= ancestors(j,1);
    }
    // NbVar_LocRec = NbTotalVar / 2;
    // NbVar_LocRec: position of the first ancestor variable
    // ancestors: ancestor array
    CData::InitAncestors(NbVar_LocRec, ancestors);
  }
  Rcpp::NumericMatrix res(g_NbRow_LocRec, NbVar_LocRec); // result matrix that is returned

  // weight-vector:
  // 1. Column: Value of the weight for the variable
  // 2. Column: TRUE/FALSE if variable is of type categorial
  Rcpp::NumericMatrix weight(weight_R);
  if(NbVar_LocRec == weight.rows()) {
    CData::Init(NbVar_LocRec, weight);
  } else {
    Uninit_LocalRec();
    return Rcpp::wrap("Error: Not enough weights specified");
  }

  bool categoryCount = Rcpp::as<bool>(categoryCount_R);
  if(categoryCount) {
    CData::m_CategoryCountVar = TRUE;
    --NbVar_LocRec;
  }
  //printf("NbVar_LocRec %d \n",NbVar_LocRec);
  if (g_NbRow_LocRec <= 0 || CData::m_NbVariable <= 0)
  {
    Uninit_LocalRec();
    return Rcpp::wrap(-3);
  }

  if (Match == e_Match_Kneib
    && (K_Level >= g_NbRow_LocRec || K_Level < 1))
  {
    Uninit_LocalRec();
    return Rcpp::wrap(-4);
  }
    //============= Data Allocation & Loading
  g_Data = new CData[g_NbRow_LocRec];

  if (LoadData(g_NbRow_LocRec, g_MissingValue_LocalRec, g_Data, Mat_LocalRec) != g_NbRow_LocRec)
  {
    Uninit_LocalRec();
    return Rcpp::wrap(-5);
  }

  if (!LowMemory && g_NbRow_LocRec > 1 && CData::m_NbVariable > 2)
  {
    TDist *pAllDist = new TDist[g_NbRow_LocRec * (g_NbRow_LocRec - 1) / 2];
    int NbEntry = 0;

    ForLoop (i, g_NbRow_LocRec)
    {
      g_Data[i].m_pDist = pAllDist + NbEntry;

      int NbDist = g_NbRow_LocRec - i - 1;

      ForLoop (j, NbDist)
        g_Data[i].m_pDist[j] = dist(g_Data + i, g_Data + i + j + 1);

      --g_Data[i].m_pDist;    //Optim: avoid to have to do it each time dist() is called
      NbEntry += NbDist;
    }
Exemple #5
0
// [[Rcpp::export]]
Rcpp::NumericMatrix infoContentMethod_cpp(
    Rcpp::StringVector&  id1_,
    Rcpp::StringVector&  id2_,
    Rcpp::List&          anc_,
    Rcpp::NumericVector& ic_,
    const std::string&   method_,
    const std::string&   ont_
) {
    go_dist_func_t* go_dist;
    // Resnik does not consider how distant the terms are from their common ancestor.
    //  Lin and Jiang take that distance into account.
    if (method_ == "Resnik") {
        go_dist = &go_dist_Resnik;
    }
    else if (method_ == "Lin") {
        go_dist = &go_dist_Lin;
    }
    else if (method_ == "Jiang") {
        go_dist = &go_dist_Jiang;
    }
    else if (method_ == "Rel") {
        go_dist = &go_dist_Rel;
    }
    else {
        throw std::runtime_error( "Unknown GO distance method" );
    }

    typedef std::string term_id_t;
    typedef std::set<term_id_t> term_set_t;

    // calculate the maximum IC and build the map of normalized IC
    typedef std::map<term_id_t, double> ic_map_t;
    ic_map_t normIcMap;
    // more specific term, larger IC value.
    // Normalized, all divide the most informative IC.
    // all IC values range from 0(root node) to 1(most specific node)
    double mic = NA_REAL;
    {
        Rcpp::StringVector icNames( ic_.names() );
        for (std::size_t i=0; i < ic_.size(); i++ ) {
            const double cic = ic_[i];
            if ( Rcpp::NumericVector::is_na( cic ) || cic == R_PosInf ) continue;
            if ( Rcpp::NumericVector::is_na( mic ) || mic < cic ) mic = cic;
        }
        LOG_DEBUG( "mic=" << mic );
        for (std::size_t i=0; i < ic_.size(); i++ ) {
            const double cic = ic_[i];
            if ( Rcpp::NumericVector::is_na( cic ) || cic == R_PosInf ) continue;
            normIcMap.insert( std::make_pair( (std::string) icNames[i], cic / mic ) );
        }
    }

    // set root node IC to 0
    if(ont_ == "DO") {
        normIcMap["DOID:4"] = 0;
    } else {
        normIcMap["all"] = 0;
    }

    // convert anc_ into map of sets
    typedef std::map<term_id_t, term_set_t> anc_map_t;
    anc_map_t ancMap;
    {
        Rcpp::StringVector goTerms( anc_.names() );
        for (std::size_t i=0; i < anc_.size(); i++ ) {
            const std::vector<std::string> ancVec = Rcpp::as<std::vector<std::string> >( anc_[i] );
            term_set_t ancestors( ancVec.begin(), ancVec.end() );
            // term itself is also considered an ancestor
            ancestors.insert( (std::string)goTerms[i] );
            ancMap.insert( std::make_pair( (std::string) goTerms[i], ancestors ) );
        }
    }

    Rcpp::NumericMatrix res( id1_.size(), id2_.size() );
    res.attr("dimnames") = Rcpp::Rcpp_list2( id1_, id2_ );
    for ( std::size_t i = 0; i < id1_.size(); i++ ) {
        const std::string id1_term = (std::string)id1_[i];
        const ic_map_t::const_iterator iIcIt = normIcMap.find( id1_term );
        if ( iIcIt != normIcMap.end() && iIcIt->second != 0 ) {
            const double iIc = iIcIt->second;
            LOG_DEBUG( "ic[" << id1_term << "]=" << iIc );
            const anc_map_t::const_iterator iAncsIt = ancMap.find( id1_term );
            for ( std::size_t j = 0; j < id2_.size(); j++ ) {
                const std::string id2_term = (std::string)id2_[j];
                const ic_map_t::const_iterator jIcIt = normIcMap.find( id2_term );
                if ( jIcIt != normIcMap.end() && jIcIt->second != 0 ) {
                    const anc_map_t::const_iterator jAncsIt = ancMap.find( id2_term );
                    // find common ancestors
                    term_set_t commonAncs;
                    if ( iAncsIt != ancMap.end() && jAncsIt != ancMap.end() ) {
                        std::set_intersection( iAncsIt->second.begin(), iAncsIt->second.end(),
                                               jAncsIt->second.begin(), jAncsIt->second.end(),
                                               std::inserter( commonAncs, commonAncs.end() ) );
                    }
                    LOG_DEBUG( "n(commonAncs(" << id1_term << "," << id2_term << "))=" << commonAncs.size() );

                    // Information Content of the most informative common ancestor (MICA)
                    double mica = 0;
                    for ( term_set_t::const_iterator termIt = commonAncs.begin(); termIt != commonAncs.end(); ++termIt ) {
                        ic_map_t::const_iterator ancIcIt = normIcMap.find( *termIt );
                        if ( ancIcIt != normIcMap.end() && mica < ancIcIt->second ) mica = ancIcIt->second;
                    }
                    LOG_DEBUG( "mica(" << id1_term << "," << id2_term << ")=" << mica );
                    res(i,j) = go_dist( mica, iIc, jIcIt->second, mic );
                } else {
                    res(i,j) = NA_REAL;
                }
            }
        } else {
            for ( std::size_t j = 0; j < id2_.size(); j++ ) {
                res(i,j) = NA_REAL;
            }
        }
    }
    return ( res );
}
Exemple #6
0
void ilp_problem_t::add_constraints_to_forbid_looping_unification(
    pg::edge_idx_t idx_uni_1, pg::node_idx_t idx_explained,
    std::list<constraint_idx_t> *out)
{
    const pg::edge_t &e_uni_1 = m_graph->edge(idx_uni_1);
    assert(e_uni_1.is_unify_edge());

    // IF A LITERAL IS UNIFIED AND EXPLAINED BY ANOTHER ONE,
    // THEN CHAINING FROM THE LITERAL IS FORBIDDEN.

    variable_idx_t v_uni_1 = find_variable_with_edge(idx_uni_1);
    if (v_uni_1 < 0) return;

    auto from = m_graph->hypernode(e_uni_1.tail());
    if (from[0] != idx_explained and from[1] != idx_explained) return;

    pg::node_idx_t idx_explains = (from[0] == idx_explained) ? from[1] : from[0];
    hash_set<pg::node_idx_t> descendants;
    hash_set<pg::node_idx_t> ancestors(m_graph->node(idx_explained).ancestors());

    m_graph->enumerate_descendant_nodes(idx_explains, &descendants);
    descendants.insert(idx_explains);
    ancestors.insert(idx_explained);

    hash_map<std::string, hash_set<pg::node_idx_t> > a2n_1, a2n_2;
    for (auto it = descendants.begin(); it != descendants.end(); ++it)
        a2n_1[m_graph->node(*it).literal().get_arity()].insert(*it);
    for (auto it = ancestors.begin(); it != ancestors.end(); ++it)
        a2n_2[m_graph->node(*it).literal().get_arity()].insert(*it);

    for (auto it1 = a2n_1.begin(); it1 != a2n_1.end(); ++it1)
    {
        auto it2 = a2n_2.find(it1->first);
        if (it2 == a2n_2.end()) continue;

        for (auto n1 = it1->second.begin(); n1 != it1->second.end(); ++n1)
        for (auto n2 = it2->second.begin(); n2 != it2->second.end(); ++n2)
        {
            pg::edge_idx_t idx_uni_2 = m_graph->find_unifying_edge(*n1, *n2);
            if (idx_uni_2 < 0 or idx_uni_2 == idx_uni_1) continue;

            const pg::edge_t &e_uni_2 = m_graph->edge(idx_uni_2);
            ilp::variable_idx_t v_uni_2 = find_variable_with_edge(idx_uni_2);

            if (v_uni_2 >= 0)
            {
                ilp::constraint_t con(
                    util::format("muex_unify:e(%d,%d)", idx_uni_1, idx_uni_2),
                    ilp::OPR_GREATER_EQ, -1.0);

                con.add_term(v_uni_1, -1.0);
                con.add_term(v_uni_2, -1.0);

                constraint_idx_t con_idx = add_constraint(con);

                if (con_idx >= 0 and out != NULL)
                    out->push_back(con_idx);
            }
        }
    }
}
void bi::MetropolisResamplerHost::ancestorsPermute(Random& rng, const V1 lws,
    V2 as, int B) {
  ancestors(rng, lws, as, B);
  permute(as);
}