void print_matrix(const real_2d_array& m, int row1, int row2, int col1, int col2) {
	row1 = min(row1, m.rows());
	row2 = min(row2, m.rows());
	col1 = min(col1, m.cols());
	col2 = min(col2, m.cols());
	for (int r=row1; r<row2; ++r) {
		for (int c=col1; c<col2; ++c) {
			cout<<m[r][c]<<"\t";
		}
		cout<<endl;
	}
}
void store_idata(const char* fpath, const real_2d_array& data) {
	FILE* f = fopen(fpath, "w");
	if (!f) throw "[store_data] Failed opening file!";
	for (int row=0; row<data.rows(); ++row) {
        for (int col=0; col<data.cols()-1; ++col) {
        	fprintf(f, "%i\t",(int)data[row][col]);
        }
        fprintf(f, "%i", (int)data[row][data.cols()-1]);
        //if (row != data.rows()-1)
		fprintf(f, "\n");
	}
	fclose(f);
}
CostCalculator_eigen::CostCalculator_eigen(const real_1d_array expectReturn,
                                           const real_2d_array &varMatrix,
                                           const real_1d_array &tradingCost,
                                           const real_1d_array &currentWeight) {
    assert(expectReturn.length() == varMatrix.rows());
    assert(varMatrix.rows() == varMatrix.cols());
    assert(tradingCost.length() == varMatrix.rows());
    assert(currentWeight.length() == varMatrix.rows());
    variableNumber_ = expectReturn.length();

    xReal_.resize(variableNumber_, 1);
    varMatrix_.resize(variableNumber_, variableNumber_);
    expectReturn_.resize(variableNumber_);
    tradingCost_.resize(variableNumber_);
    currentWeight_.resize(variableNumber_);

    for (int i = 0; i != variableNumber_; ++i) {
        expectReturn_(i) = expectReturn[i];
        tradingCost_(i) = tradingCost[i];
        currentWeight_(i) = currentWeight[i];
        for (int j = 0; j != variableNumber_; ++j)
            varMatrix_(i, j) = varMatrix[i][j];
    }
}
Esempio n. 4
0
// Construction of a kmean Trea for the neighborhoods stored in xy.
void kmeanTree(Node* node, real_2d_array xy, int min_size){
  if(xy.rows()>min_size){ //Stop if we have less than min_size neighborhoods.

    // K_mean (K=4) :
    clusterizerstate s;
    kmeansreport rep;

    clusterizercreate(s);
    clusterizersetpoints(s, xy, 2);
    clusterizersetkmeanslimits(s, 5, 0);
    clusterizerrunkmeans(s, 4, rep);

    if(int(rep.terminationtype)==1){ // Check if the K_mean step finished correctly.

      // Count the number of neighborhoods in each cluster.
      vector<int> count(4);
      for(int c=0;c<4;c++){
        for(int i=0;i<rep.npoints;++i){
          if(rep.cidx[i] == c){
            count[c]++;
          }
        }
      }

      if(count[0] && count[1] && count[2] && count[3]){ // Check that each cluster is non-empty.

        // Create new nodes
        for(int c=0;c<4;c++){
          Node*  nodeSon = new Node;
          node->addSon(nodeSon);
        }

        vector<real_2d_array> clusters(4);

        for(int c=0;c<4;c++){
          // Add the center element of each cluster in its related node.
          for(int f=0;f<rep.nfeatures;++f){
            node->getSon(c)->addCenterCoord((double)rep.c[c][f]);
          }

          // Separate the neighborhoods into the new nodes.
          int id = 0;
          clusters[c].setlength(count[c],rep.nfeatures);
          for(int i=0;i<rep.npoints;++i){
            if(rep.cidx[i] == c){
              for(int f=0;f<rep.nfeatures;++f){
                clusters[c][id][f] = xy[i][f];
              }
              id++;
            }
          }
        }

        // Iterate the construction on the new nodes.
        for(int c=0;c<4;c++){
          kmeanTree(node->getSon(c),clusters[c],min_size);
        }
      }
    }
  }
}