コード例 #1
0
ファイル: randomforest.cpp プロジェクト: 20twenty/kglSN2
double infoGain(vector<vector<int> >& al) {
   int total_l0=0, total_l1=0;
   vector<double> Es(al.size(),0);
   for(int a = 0; a < al.size(); a++) {
      total_l0 += al[a][0];
      total_l1 += al[a][1];
      Es[a] = Entropy(al[a][0],al[a][1]);
   }
   int total = total_l0 + total_l1;
   double E = Entropy(total_l0,total_l1);
   double IG = E;
   for(int a = 0; a < al.size(); a++) {
      IG -= (Es[a] * (double)(al[a][0]+al[a][1])/(double)total);
   }
   if(IG < 1e-5 || IG != IG) IG = 0;
   //cerr << IG << "," << E << "," << Es[0] << "," << Es[1] << endl;
   return IG;
}
コード例 #2
0
ファイル: base.cpp プロジェクト: kryv/jmbgsddb
Machine::Machine(const Config& c)
    :p_elements()
    ,p_trace(NULL)
    ,p_info()
{
    std::string type(c.get<std::string>("sim_type"));

    info_mutex_t::scoped_lock G(info_mutex);

    p_state_infos_t::iterator it = p_state_infos.find(type);
    if(it==p_state_infos.end()) {
        std::ostringstream msg;
        msg<<"Unsupport sim_type '"<<type<<"'";
        throw key_error(msg.str());
    }

    p_info = it->second;

    typedef Config::vector_t elements_t;
    elements_t Es(c.get<elements_t>("elements"));

    p_elements_t result;
    result.reserve(Es.size());

    size_t idx=0;
    for(elements_t::iterator it=Es.begin(), end=Es.end(); it!=end; ++it)
    {
        const Config& EC = *it;

        const std::string& etype(EC.get<std::string>("type"));

        state_info::elements_t::iterator eit = p_info.elements.find(etype);
        if(eit==p_info.elements.end())
            throw key_error(etype);

        element_builder_t* builder = eit->second;

        ElementVoid *E;
        try{
            E = builder->build(EC);
        }catch(key_error& e){
            std::ostringstream strm;
            strm<<"Error while initializing element "<<idx<<" '"<<EC.get<std::string>("name", "<invalid>")
               <<"' : missing required parameter '"<<e.what()<<"'";
            throw key_error(strm.str());

        }catch(std::exception& e){
            std::ostringstream strm;
            strm<<"Error while constructing element "<<idx<<" '"<<EC.get<std::string>("name", "<invalid>")
               <<"' : "<<e.what();
            throw std::runtime_error(strm.str());
        }

        *const_cast<size_t*>(&E->index) = idx++; // ugly

        result.push_back(E);
    }

    G.unlock();

    p_elements.swap(result);
}