Esempio n. 1
0
int main() {

  Star s;

  union u {
              float f[4];
              vector float v;
  } data;

  data.v = fTest(s);
  for (int i=0 ; i < 4; i++)
     if (data.f[i] != (float)(i+1))
       abort();
  return 0;
}
Esempio n. 2
0
void SPxSolver::updateFtest()
{
   METHOD( "SPxSolver::updateFtest()" );
   const IdxSet& idx = theFvec->idx();
   Vector& ftest = theCoTest;      // |== fTest()|
   assert(&ftest == &fTest());

   assert(type() == LEAVE);
   for (int j = idx.size() - 1; j >= 0; --j)
   {
      int i = idx.index(j);

      ftest[i] = ((*theFvec)[i] > theUBbound[i])
         ? theUBbound[i] - (*theFvec)[i]
         : (*theFvec)[i] - theLBbound[i];
   }
}
Esempio n. 3
0
void SPxSolver::qualRedCostViolation(Real& maxviol, Real& sumviol) const
{   
   maxviol = 0.0;
   sumviol = 0.0;

   int i;
   // TODO:   y = c_B * B^-1  => coSolve(y, c_B)
   //         redcost = c_N - yA_N 
   // solve system "x = e_i^T * B^-1" to get i'th row of B^-1
   // DVector y( nRows() );
   // basis().coSolve( x, spx->unitVector( i ) );
   // DVector rdcost( nCols() );
#if 0 // un-const
   if (lastUpdate() > 0)
      factorize();

   computePvec();

   if (type() == ENTER)
      computeTest();
#endif
   if (type() == ENTER)
   {
      for(i = 0; i < dim(); ++i)
      {
         Real x = coTest()[i];
         
         if (x < 0.0)
         {
            sumviol -= x;
            
            if (x < maxviol)
               maxviol = x;
         }
      }
      for(i = 0; i < coDim(); ++i)
      {
         Real x = test()[i];
         
         if (x < 0.0)
         {
            sumviol -= x;
            
            if (x < maxviol)
               maxviol = x;
         }
      } 
   }
   else
   {
      assert(type() == LEAVE);

      for(i = 0; i < dim(); ++i)
      {
         Real x = fTest()[i];
         
         if (x < 0.0)
         {
            sumviol -= x;
            
            if (x < maxviol)
               maxviol = x;
         }
      }
   }
   maxviol *= -1;
}
Esempio n. 4
0
void RandomForest::build() {
   srand(9167543);
   vector<pair<double,double> > oob_predict_acum(pXD_->trn_rows,pair<double,double>(0,0));
   vector<pair<double,double> > validate_predict_acum(pXD_->trn_rows,pair<double,double>(0,0));
   vector<pair<double,double> > tst_predict_acum(pXD_->tst_rows,pair<double,double>(0,0));
   vector<double> tst_predict(pXD_->tst_rows,0);
   vector<pair<int,int> > tst;
   for(int tst_idx=0; tst_idx < pXD_->tst_rows; tst_idx++) {
      tst.push_back(pair<int,int>(tst_idx,1));
   }
   int good_tree_cnt = 0;

   // the below is setup for the key press checking
   char c;
   int n, tem;
   int cnt = 0;
   tem = fcntl(0, F_GETFL, 0);
   fcntl (0, F_SETFL, (tem | O_NDELAY));
   // end key press setup

   vector<int> train_idx;
   vector<pair<int,int> > validate;
   for(int i=0; i<pXD_->trn_labl.size(); i++) {
      if((rand() % folds_) == fold_) validate.push_back(pair<int,int>(i,1));
      else train_idx.push_back(i);
   }

   int tree = 0;
   while(1) {
      //base_feature_sample_cnt_ = (int)(pow((double)(rand() % 1000)/1000.0,2) * 400); //(int)((double)train_idx.size()/3.0);
      vector<pair<int,int> > bag, oob;
      vector<int> w(train_idx.size(),0);
      for(int idx=0; idx<train_idx.size(); idx++) {
         //w[idx]=(int)(338 / pXD_->trn_attr[train_idx[idx]][(pXD_->trn_cols)-1]);
         w[idx]=1;
         //cerr << pXD_->trn_attr[train_idx[idx]][(pXD_->trn_cols)-1] << "," << w[idx] << endl;
      }
      create_bag(bag,oob,train_idx,w);
      //Node* root = new Node;
      Node root;
      int node_cnt=0; 
      double tree_gini=0;
      recAssignNode(&root, bag, "", 0, node_cnt, tree_gini);

      /*
      vector<double> v_predict(pXD_->trn_rows,0);
      recPredict(&root, validate, &(pXD_->trn_attr), v_predict, 0, 0);
      double d2 = 0;
      for(int i=0; i < validate.size(); i++) {
         d2+=pow(pXD_->trn_labl[validate[i].first] - v_predict[validate[i].first],2);
      }
      d2 /= (double)validate.size();
      */

      cerr << tree+1 << "/" << trees_ << "; M: " << base_feature_sample_cnt_ << "; bag_size: " << bag.size() \
           << "; oob_size: " << oob.size() /*<< "; d2: " << d2  */<< "; leaf_node_cnt: " << node_cnt \
           << "; tree_gini: " << tree_gini << endl;

      if(/*d2 < 0.3 || */1) {
         //double weight = 1 / pow((double)node_cnt,20);
         double weight = 1;
         predict(&root, &(pXD_->trn_attr), oob, oob_predict_acum, weight);
         predict(&root, &(pXD_->trn_attr), validate, validate_predict_acum, weight);
         predict(&root, &(pXD_->tst_attr), tst, tst_predict_acum, weight);
         good_tree_cnt++;
      }
      cerr << "good_tree_cnt: " << good_tree_cnt << endl;
      eval(oob_predict_acum,good_tree_cnt,"oob");
      eval(validate_predict_acum,good_tree_cnt,"validate");
      tree++;
      n=0;
      //n = read(0, &c, 1);
      if (n > 0 || good_tree_cnt == trees_) {
         cerr << "Key pressed: " << c << endl;
         break;
      }
   }
   cerr << endl;
   eval(oob_predict_acum,good_tree_cnt,"oob");
   eval(validate_predict_acum,good_tree_cnt,"validate");
   cerr << "writing tst set predictions..." << endl;

   char tstFile[100];
   sprintf(tstFile, "workspace/tst_predict_%d_%d_%d_%d.csv", fold_, folds_, base_feature_sample_cnt_, trees_);
   ofstream fTest(tstFile);
   if(!fTest.is_open()) {
      cerr << "Cannot open " << tstFile << " for writing" << endl;
      return;
   }
   for(int tst_idx=0; tst_idx < pXD_->tst_rows; tst_idx++) {
      double p = tst_predict_acum[tst_idx].first/(double)tst_predict_acum[tst_idx].second;
      p = (p*(double)good_tree_cnt + (double)ls1_)/(double)(good_tree_cnt+ls2_);
      fTest << p << endl;
   }
   fTest.close();

   char oobFile[100];
   sprintf(oobFile, "workspace/oob_predict_%d_%d_%d_%d.csv", fold_, folds_, base_feature_sample_cnt_, trees_);
   ofstream fOob(oobFile);
   if(!fOob.is_open()) {
      cerr << "Cannot open " << oobFile << " for writing" << endl;
      return;
   }
   for(int trn_idx=0;trn_idx < pXD_->trn_rows; trn_idx++) {
      double p = oob_predict_acum[trn_idx].first/(double)oob_predict_acum[trn_idx].second;
      fOob << pXD_->trn_labl[trn_idx] << "," << p << endl;
   }
   fOob.close();
}