/*--------------------------------------------------------*/
void AzOptOnTree::_refreshPred_TempFile()
{
  if (v_w.rowNum() == 0 && v_p.rowNum() == 0) return; 

  v_p.zeroOut(); 
  v_p.set(var_const+fixed_const);  

  const double *w = v_w.point(); 

  int tree_num = ens->size(); 
  int tx; 
  for (tx = 0; tx < tree_num; ++tx) {
    ens->tree_u(tx)->restoreDataIndexes(); 
    AzIIarr iia_nx_fx; 
    tree_feat->featIds(tx, &iia_nx_fx); 
    int num = iia_nx_fx.size(); 
    int ix; 
    for (ix = 0; ix < num; ++ix) {
      int nx, fx; 
      iia_nx_fx.get(ix, &nx, &fx); 

      if (tree_feat->featInfo(fx)->isRemoved) continue; 
      int dxs_num; 
      const int *dxs = data_points(fx, &dxs_num); 
      updatePred(dxs, dxs_num, w[fx], &v_p); 
    }
    ens->tree_u(tx)->releaseDataIndexes(); 
  }
}
/*--------------------------------------------------------*/
void AzOptOnTree::_update_with_features_TempFile(
                      double nlam, 
                      double nsig, 
                      double py_avg, 
                      AzRgf_forDelta *for_del) /* updated */
{
  int tree_num = ens->size();
  int tx; 
  for (tx = 0; tx < tree_num; ++tx) {
    ens->tree_u(tx)->restoreDataIndexes(); 
    AzIIarr iia_nx_fx; 
    tree_feat->featIds(tx, &iia_nx_fx); 
    int num = iia_nx_fx.size(); 
    int ix; 
    for (ix = 0; ix < num; ++ix) {
      int nx, fx; 
      iia_nx_fx.get(ix, &nx, &fx); 
      if (tree_feat->featInfo(fx)->isRemoved) continue; /* shouldn't happen though */

      double w = v_w.get(fx); 
      int dxs_num; 
      const int *dxs = data_points(fx, &dxs_num); 
      double my_nlam = reg_depth->apply(nlam, node(fx)->depth); 
      double my_nsig = reg_depth->apply(nsig, node(fx)->depth); 
      double delta = getDelta(dxs, dxs_num, w, my_nlam, my_nsig, py_avg, for_del); 
      v_w.set(fx, w+delta); 
      updatePred(dxs, dxs_num, delta, &v_p); 
    }
    ens->tree_u(tx)->releaseDataIndexes(); 
  }
}
Example #3
0
int main(int argc, char** argv) {
	if (argc > 1) {
		srand(atoi(argv[1]));
	} else {
		srand(time(NULL));
	}

	// gnuplot: plot 'data_points.dat', 'hull_points.dat' with lines
	DATA_POINTS_FILE = fopen("data_points.dat", "w");
	HULL_POINTS_FILE = fopen("hull_points.dat", "w");

	assert(DATA_POINTS_FILE != NULL);
	assert(HULL_POINTS_FILE != NULL);

	std::vector<t_point2i> data_points(NUM_DATA_POINTS);
	std::vector<t_point2i> hull_points;
	std::vector<t_convex_hull_builder*> builders = {
		new t_graham_scan_convex_hull_builder(),
		new t_line_wrap_convex_hull_builder(),
	};

	builders[1]->setup(data_points, hull_points);
	builders[1]->build(data_points, hull_points);

	for (unsigned int n = 0; n < builders.size(); n++) {
		delete builders[n];
	}

	fclose(DATA_POINTS_FILE);
	fclose(HULL_POINTS_FILE);
	return 0;
}
/*--------------------------------------------------------*/
void AzOptOnTree_TreeReg::update_weight(int nx, 
                                   int fx, 
                                   double delta,
                                   AzReg_TreeReg *reg)
{
  double new_w = v_w.get(fx) + delta; 
  v_w.set(fx, new_w); 

  int dxs_num; 
  const int *dxs = data_points(fx, &dxs_num); 
  updatePred(dxs, dxs_num, delta, &v_p); 

  /*---  update the weight in the ensemble  ---*/ 
  const AzTrTreeFeatInfo *fp = tree_feat->featInfo(fx); 
  rgf_ens->tree_u(fp->tx)->setWeight(fp->nx, new_w); 
  reg->changeWeight(nx, delta); 
}
/*--------------------------------------------------------*/
void AzOptOnTree::_refreshPred()
{
  if (v_w.rowNum() == 0 && v_p.rowNum() == 0) return; 

  v_p.zeroOut(); 
  v_p.set(var_const+fixed_const);  

  const double *w = v_w.point(); 
  int f_num = v_w.rowNum(); 
  int fx; 
  for (fx = 0; fx < f_num; ++fx) {
    if (tree_feat->featInfo(fx)->isRemoved) continue; 
    int dxs_num; 
    const int *dxs = data_points(fx, &dxs_num); 
    updatePred(dxs, dxs_num, w[fx], &v_p); 
  }
}
/*--------------------------------------------------------*/
double AzOptOnTree_TreeReg::bestDelta(
                      int nx, 
                      int fx, 
                      AzReg_TreeReg *reg, 
                      double nlam, 
                      double nsig, 
                      double py_avg, 
                      AzRgf_forDelta *for_delta) /* updated */
const
{
  const char *eyec = "AzOptOnTree_TI::bestDelta"; 

  double w = v_w.get(fx); 
  int dxs_num; 
  const int *dxs = data_points(fx, &dxs_num); 
  if (dxs_num <= 0) {
    throw new AzException(eyec, "no data indexes"); 
  }

  const double *fixed_dw = NULL; 
  if (!AzDvect::isNull(&v_fixed_dw)) fixed_dw = v_fixed_dw.point(); 
  const double *p = v_p.point(); 
  const double *y = v_y.point(); 
  double nega_dL = 0, ddL= 0; 
  if (fixed_dw == NULL) {
    AzLoss::sum_deriv(loss_type, dxs, dxs_num, p, y, py_avg, 
                      nega_dL, ddL); 
  }
  else {
    AzLoss::sum_deriv_weighted(loss_type, dxs, dxs_num, p, y, fixed_dw, py_avg, 
                      nega_dL, ddL);
  }

  double dR, ddR; 
  reg->penalty_deriv(nx, &dR, &ddR); 

  double dd = ddL + nlam*ddR; 
  if (dd == 0) dd = 1; 
  double delta = (nega_dL-nlam*dR)*eta/dd; 
  for_delta->check_delta(&delta, max_delta); 

  return delta; 
}
/*--------------------------------------------------------*/
void AzOptOnTree::_update_with_features(
                      double nlam, 
                      double nsig, 
                      double py_avg, 
                      AzRgf_forDelta *for_del) /* updated */
{
  int fx; 
  int f_num = tree_feat->featNum(); 
  for (fx = 0; fx < f_num; ++fx) {
    if (tree_feat->featInfo(fx)->isRemoved) continue; 

    double w = v_w.get(fx); 
    int dxs_num; 
    const int *dxs = data_points(fx, &dxs_num); 
    double my_nlam = reg_depth->apply(nlam, node(fx)->depth); 
    double my_nsig = reg_depth->apply(nsig, node(fx)->depth); 
    double delta = getDelta(dxs, dxs_num, w, my_nlam, my_nsig, py_avg, for_del); 
    v_w.set(fx, w+delta); 
    updatePred(dxs, dxs_num, delta, &v_p); 
  }
}