コード例 #1
0
double invPowerShift(MAT &A, double omega, VEC q, int maxIter){
    int k = 1;
    //I: identity matrix; LU: LU in-place; 
    MAT I(A.dim()), LU(A.dim()), shiftedA(A.dim());
    //q: 
    VEC z(A.dim()), temp(A.dim()),r(A.dim()), u(A.dim()), w(A.dim());
	
    
    double lambdaN= 100.0, newlambdaN=0.0, tol = 1.0;
    //Form identity matrix
    for (int i =0; i < A.dim(); i++){
	I[i][i] = 1;
    }
    shiftedA = A-(omega*I);
    LU = luFact(shiftedA);       //LU in-place decomposition
    
    while( (k < maxIter) && tol >= 0.000000001 ){
			
		temp = fwdSubs(LU,q);   //forward substitution
		z = bckSubs(LU,temp);//backward substitution
		q = z/l2norm(z);
		lambdaN = q * A * q;
		r = A * q - lambdaN * q;
		u = q * A;
		w = u/l2norm(u);
		tol = l2norm(r)/std::abs(w*q);

    }
    //printf("Iteration: %d\n",k);
    return lambdaN;
}
コード例 #2
0
ファイル: fwi_commons.c プロジェクト: yunzhishi/src
void lbfgs_direction(int n, float *grad, float *r, float **sk, float **yk, sf_optim opt)
/*< calculate search direction >*/
{
	int i, j;
	float *rho, *q, *alpha, tmp, tmp1, gamma, beta;

	// safeguard
	l2norm(n, sk[opt->ipair-1], &tmp);
	l2norm(n, yk[opt->ipair-1], &tmp1);
	if(tmp==0. || tmp1==0.){
		reverse(n, grad, r);
		return;
	}
	
	q=sf_floatalloc(n);
	rho=sf_floatalloc(opt->ipair);
	alpha=sf_floatalloc(opt->ipair);

	copy(n, grad, q);
	
	// first loop
	for(i=opt->ipair-1; i>=0; i--){
		
		// calculate rho
		dot_product(n, yk[i], sk[i], &tmp);  
		rho[i]=1./tmp;

		dot_product(n, sk[i], q, &tmp);
		alpha[i]=rho[i]*tmp;
		for(j=0; j<n; j++)
			q[j] -= alpha[i]*yk[i][j];
	}

	// initial Hessian
	dot_product(n, yk[opt->ipair-1], yk[opt->ipair-1], &tmp);
	gamma=1./tmp/rho[opt->ipair-1];
	for (j=0; j<n; j++){
		r[j]=gamma*q[j];
	}

	// second loop
	for(i=0; i<opt->ipair; i++){
		dot_product(n, yk[i], r, &tmp);
		beta=tmp*rho[i];
		tmp=alpha[i]-beta;
		for(j=0; j<n; j++)
			r[j] += tmp*sk[i][j];
	}

	// opposite direction of H^(-1)*grad(f)
	for(j=0; j<n; j++)
		r[j]=-r[j];

	// deallocate variables
	free(q);
	free(alpha);
	free(rho);
}
コード例 #3
0
ファイル: question33.c プロジェクト: Athas/statml
int main(int argv, char** argc){
	printf("\n\nRunning code for question 3.3:\n\n");
  double* mean_l2 = (double*) malloc(sizeof(double)*MAX_D*2);
  double* mean_dist = mean_l2 + MAX_D;
    
  mtrx* big_m = gen_n_dim(MAX_D, NUM_SAMP);
  
  for (int i = 1; i <= MAX_D; i++){
    *(mean_l2+i-1) = i+1;
    *(mean_l2+i-1) = l2norm(big_m, i);
    *(mean_dist+i-1) = mean_distance(big_m, i);
  }
  
  gplot_one2infty(mean_l2, MAX_D, FIG_OUT"q33.dat");
  free(mean_l2);
  gsl_matrix_free(big_m);
  
/*  figure_ctrl plot;
  init_figure(&plot,"Many Dimmensional Vectors","pdf color");
	
  plot_x_y(&plot,n_val,mean_l2,MAX_D,"Mean vector length", plot_style2str(ps_points));
	plot_x_y(&plot,n_val,mean_dist,MAX_D,"Mean vector distance",plot_style2str(ps_points));
	
//	plot_viewbox(&plot,-1,3,-1,4);
	
	figure2file(&plot,"~/question33.pdf");*/

}
コード例 #4
0
ファイル: lnsrchmp.c プロジェクト: binaryWorld/ctsa
int stopcheck2_mt(double fx, int N, double fo, double *jac, double *dx, double eps,double stoptol, double functol, int retval) {
	int rcode;
	double nrm,nrmnx,relfit;
	rcode = 0;


	if (retval == 3) {
		rcode = 4;
		return rcode;
	}
	if (retval == 15) {
		rcode = 15;
		return rcode;
	}

	nrm = l2norm(jac, N);
	nrmnx = nrm / (double) N;

	if (fabs(fo) < eps) {
		relfit = fabs(fx - fo);
	}
	else {
		relfit = fabs((fx - fo)/fo);
	}

	if (nrmnx < stoptol) {
		return 1; // Successful Convergence
	} else if (relfit < functol) {
		return 6; // Relative fit less than function tolerance
	}

	return rcode;
}
コード例 #5
0
double invPowerShift(MAT &A, double omega, VEC q, int maxIter){
    int k = 1;
    //I: identity matrix; LU: LU in-place; iLU: LU in-place for invA; invA: inverse of A
    MAT I(A.dim()), LU(A.dim()),iLU(A.dim()), invA = inverse(A), shiftedA(A.dim());
    //q: 
    VEC z(A.dim()), temp(A.dim());// q(A.dim()),iq(A.dim()), iz(A.dim()), 
    
    double lambdaN= 100.0, newlambdaN=0.0;
    //Form identity matrix
    for (int i =0; i < A.dim(); i++){
	I[i][i] = 1;
    }
    shiftedA = A-(omega*I);
    LU = luFact(shiftedA);       //LU in-place decomposition
    //iLU = luFact(invA-(w*I));       //LU in-place decomposition
    while( (k < maxIter) && (std::abs(newlambdaN - lambdaN) >= 10E-9) ){
		lambdaN = newlambdaN;	
		temp = fwdSubs(LU,q);   //forward substitution
		z = bckSubs(LU,temp);//backward substitution
		q = z/l2norm(z);
		newlambdaN = q * A * q;
/*
	temp = fwdSubs(iLU,iq);   //forward substitution
	iz = bckSubs(iLU,temp);//backward substitution
	iq = iz / l2norm(iz);
	imu = iq * invA * iq;

	//newCond = mu/imu;	
	*/
    }
    printf("Iteration: %d\n",k);
    return newlambdaN;
}
コード例 #6
0
double l2norm(int mpi_rank, std::vector<double>& v1, int n1, int n2) {
    double global_val = 0;
    double local_val = l2norm(v1, n1, n2);
    local_val *= local_val;
    NORM_REDUCE(&local_val, &global_val, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
    return sqrt(global_val);
}
コード例 #7
0
double invPowerShift(MAT &A, double omega, VEC q, int maxIter){
    int k = 1;
    //I: identity matrix; LU: LU in-place; 
    MAT I(A.dim()), LU(A.dim()), shiftedA(A.dim());
    //q: 
    VEC z(A.dim()), temp(A.dim());// q(A.dim())
    
    double lambdaN = 100.0, newlambdaN=0.0;
    //Form identity matrix
    for (int i =0; i < A.dim(); i++){
	I[i][i] = 1;
    }
    shiftedA = A-(omega*I);
    LU = luFact(shiftedA);       //LU in-place decomposition
    
    while( (k < maxIter) && (std::abs(newlambdaN - lambdaN) >= 0.000000001) ){
		lambdaN = newlambdaN;	
		temp = fwdSubs(LU,q);   //forward substitution
		z = bckSubs(LU,temp);//backward substitution
		q = z/l2norm(z);
		newlambdaN = q * A * q;

    }
    printf("Iteration: %d\n",k);
    return newlambdaN;
}
コード例 #8
0
double l2norm(int mpi_rank, std::vector<double>& v1, std::vector<double>& v2) {
    if (v1.size() != v2.size()) {
        std::cout << "Error! in l2norm(...): vectors are not same length. assuming 0's for missing elements" << std::endl;
        //exit(EXIT_FAILURE);
    }

    return l2norm(mpi_rank, v1, v2, 0, v1.size());
}
コード例 #9
0
ファイル: euclid_lsh.cpp プロジェクト: Ina299/jubatus
void euclid_lsh::neighbor_row(
    const common::sfv_t& query,
    vector<pair<string, float> >& ids,
    uint64_t ret_num) const {
  neighbor_row_from_hash(
      cosine_lsh(query, hash_num_),
      l2norm(query),
      ids,
      ret_num);
}
コード例 #10
0
double powerMethod(MAT &A, VEC q, int maxIter){
	VEC z(A.dim()), r(A.dim()), u(A.dim()), w(A.dim());
	MAT A_k(A.dim());
	double lambda1, tol= 1.0;
	//double new_lambda
	int iter = 0;
	
	while( (iter < maxIter) && (tol >= 0.000000001) ){
		z = A * q;
		iter++;
		q = z/l2norm(z);
		lambda1 = q * A * q;
		r = A * q - lambda1 * q;
		u = q * A;
		w = u/l2norm(u);
		tol = l2norm(r)/std::abs(w*q);	
	}
	return lambda1;
}
コード例 #11
0
ファイル: euclid_lsh.cpp プロジェクト: yukimori/jubatus_core
void euclid_lsh::neighbor_row(
    const common::sfv_t& query,
    vector<pair<string, float> >& ids,
    uint64_t ret_num) const {
  util::concurrent::scoped_rlock lk(get_const_table()->get_mutex());

  /* table lock acquired; all subsequent table operations must be nolock */

  neighbor_row_from_hash(
      cosine_lsh(query, hash_num_, threads_, cache_),
      l2norm(query),
      ids,
      ret_num);
}
コード例 #12
0
ファイル: lnsrchmp.c プロジェクト: binaryWorld/ctsa
int stopcheck3_mt(double *xi,double *xf,double fx, int N, double fo, double *jac, double *dx, double eps,
		double stoptol, double functol, int retval) {
	int rcode,i;
	double nrm,nrmnx,relfit,num,den,stop0;
	double *scheck;
	rcode = 0;

	scheck = (double*)malloc(sizeof(double)*N);

	if (retval == 3) {
		rcode = 4;
		return rcode;
	}
	if (retval == 15) {
		rcode = 15;
		return rcode;
	}

	nrm = l2norm(jac, N);
	nrmnx = nrm / (double) N;

	if (fabs(fo) < eps) {
		relfit = fabs(fx - fo);
	}
	else {
		relfit = fabs((fx - fo)/fo);
	}

	if (nrmnx < stoptol) {
		rcode = 1; // Successful Convergence
	} else if (relfit < functol) {
		rcode = 6; // Relative fit less than function tolerance
	} else {
		for (i = 0; i < N; ++i) {
			den = 1.0+fabs(xf[i]);
			num = fabs(xf[i] - xi[i]);
			scheck[i] = num / den;
		}
		stop0 = array_max_abs(scheck, N);
		if (stop0 <= stoptol) {
			rcode = 2;
		}
	}

	free(scheck);
	return rcode;
}
コード例 #13
0
vector<float> FastShift::maxDirectionTwoSide(vector<float>  shiftPoint, vector<float>  shiftValue)
{
	vector<float>  external_force(n, 0);
	vector<float>  force(n, 0);
	vector<float>  force1(n, 0);
	bool unset = true;
	float max;
	int shift;
	
	vector<int> grid_pos(n,  0);
	whichGrid(shiftPoint, grid_pos);
	int pos = findposition(grid_pos);
	
	for(int i = 0; i < selectD; i ++)
	{
		for(int j = -1; j <= 1; j += 2)
		{
			calForce(shiftPoint, pos + j * factor[selected[i]], force1);
			force += force1;
		}
		float l = l2norm(force);
		if(unset)
		{
			max = l;
			external_force = force;
			shift = pos - factor[selected[i]];
			unset = false;
			continue;
		}
		if(l > max)
		{
			max = l;
			external_force = force;
		}
	}
	//copy(C[shift], shiftValue, n);
	return external_force;
}
コード例 #14
0
ファイル: euclid_lsh.cpp プロジェクト: yukimori/jubatus_core
void euclid_lsh::set_row(const string& id, const common::sfv_t& sfv) {
  // TODO(beam2d): support nested algorithm, e.g. when used by lof and then we
  // cannot suppose that the first two columns are assigned to euclid_lsh.
  get_table()->add(id, owner(my_id_),
                   cosine_lsh(sfv, hash_num_, threads_, cache_), l2norm(sfv));
}
コード例 #15
0
double l2norm(int mpi_rank, std::vector<double>& v1)
{
    return l2norm(mpi_rank, v1, 0, v1.size());
}
コード例 #16
0
ファイル: multitest.c プロジェクト: isohatalaj/psede
int
main()
{
  int i;
  int status = 0;
  const int ns[DIM] = {24, 25, 31};
  const int m = ns[0]*ns[1]*ns[2];
  const psede_colloc_t *collocs[DIM] = {&psede_Tx, &psede_Tx, &psede_Tx};

  double a[DIM] = {1.0, 2.0, 3.0};

  psede_multicolloc_t multicolloc;
  psede_multitransf_t nodes[DIM], diffs[DIM], func;
  double *xs[DIM], *fs, *ds[DIM];

  /* ******************************************************************************** */
  /* Pre-initialization */
  
  /* Recommended practice is to set all objects to their corresponding
   * nil values before initialization. */
  multicolloc = psede_multicolloc_nil;  
  for (i = 0; i < DIM; ++i)
    {
      xs[i] = ds[i] = NULL;
      nodes[i] = psede_multitransf_nil;
      diffs[i] = psede_multitransf_nil;
    }
  fs = NULL;
  func = psede_multitransf_nil;

  /* ******************************************************************************** */
  /* Initialization */ 

  for (i = 0; i < DIM; ++i)
    {
      xs[i] = psede_fct_alloc_array(m);
      ds[i] = psede_fct_alloc_array(m);
      
      if (xs[i] == NULL || ds[i] == NULL) EXIT_WITH("Out of memory.\n");
    }

  fs = psede_fct_alloc_array(m);
  if (fs == NULL) EXIT_WITH("Out of memory.\n");

  status = psede_init_multicolloc(&multicolloc, DIM, collocs,
				  PSEDE_RETAIN_OWNERSHIP);
  if (status) EXIT_WITH("Multicollocation object init failed.\n");


  status = psede_init_multifunc(&multicolloc, &func,
				(psede_multifunc_t*) myfunc_wrap, a);
  if (status) EXIT_WITH("Function tranform init failed.\n");


  for (i = 0; i < DIM; ++i)
    {
      status = psede_init_multinodes(&multicolloc, &nodes[i], i);
      if (status) EXIT_WITH("Failed initializing nodes transform.\n");
    }

  for (i = 0; i < DIM; ++i)
    {
      int orders[DIM] = {0};
      orders[i] = 1;
      
      status = psede_init_multidiff(&multicolloc, &diffs[i], orders);
      if (status) EXIT_WITH("Failed initializing diffs transform.\n");
    }

  /* ******************************************************************************** */
  /* Run transforms */

  /* Set input arrays to ones. Function application and nodes
   * operators multiply the inputs by their own output values to
   * produce the final result. */
  psede_multitransf_apply(&psede_multitransf_ones, xs[0], DIM, ns, 1, 1, 0);
  psede_multitransf_apply(&psede_multitransf_ones, xs[1], DIM, ns, 1, 1, 0);
  psede_multitransf_apply(&psede_multitransf_ones, xs[2], DIM, ns, 1, 1, 0);
  psede_multitransf_apply(&psede_multitransf_ones, fs, DIM, ns, 1, 1, 0);
  
  for (i = 0; i < DIM; ++i)
    {
      status = psede_multitransf_apply(&nodes[i], xs[i], DIM, ns, 1, 1, 0);
      if (status) EXIT_WITH("Failed applying nodes transform.\n");
    }

  status = psede_multitransf_apply(&func, fs, DIM, ns, 1, 1, 0);
  if (status) EXIT_WITH("Failed applying nodes transform.\n");

  for (i = 0; i < DIM; ++i) psede_copy(ds[i], fs, m, 1, 1, 0);

  for (i = 0; i < DIM; ++i)
    {
      status = psede_multitransf_apply(&diffs[i], ds[i], DIM, ns, 1, 1, 0);
      if (status) EXIT_WITH("Failed applying diff transform.\n");
    }

  /* ******************************************************************************** */
  /* Output */

  double max_err = 0.0;
  for (i = 0; i < m; ++i)
    {
      double y_x, dy_x[3];
      double x_x[3];
      double err;

      x_x[0] = xs[0][i];
      x_x[1] = xs[1][i];
      x_x[2] = xs[2][i];

      myfunc(x_x, &y_x, dy_x, a);

      if (y_x != fs[i]) EXIT_WITH("Function in transform array does not match "
				  "the function value\n");

      double dy_n[3];
      dy_n[0] = ds[0][i];
      dy_n[1] = ds[1][i];
      dy_n[2] = ds[2][i];
      
      err = l2norm(DIM, dy_x, dy_n);
      if (err > max_err) max_err = err;

      printf(" %25.15lf %25.15lf %25.15lf %25.15lf %25.15lf"
	     " %25.15lf %25.15lf %25.15lf %25.15lf %25.15lf\n",
	     xs[0][i], xs[1][i], xs[2][i], fs[i],
	     ds[0][i], ds[1][i], ds[2][i],
	     dy_x[0], dy_x[1], dy_x[2]);
    }

  fprintf(stderr, "# log10 ||err|| = %lf\n", log10(max_err));

  
  /* ******************************************************************************** */
  /* Cleanup and exit */
  
 exit:

  if (fs) psede_fct_free_array(fs);  
  for (i = 1; i < DIM; ++i) if (xs[i]) psede_fct_free_array(xs[i]);
  for (i = 1; i < DIM; ++i) if (ds[i]) psede_fct_free_array(ds[i]);
  
  for (i = 1; i < DIM; ++i) psede_multitransf_destroy(&nodes[i]);
  for (i = 1; i < DIM; ++i) psede_multitransf_destroy(&diffs[i]);

  psede_multitransf_destroy(&func);
  psede_multicolloc_destroy(&multicolloc);

  return status;
}
コード例 #17
0
ファイル: euclid_lsh.cpp プロジェクト: Ina299/jubatus
void euclid_lsh::set_row(const string& id, const common::sfv_t& sfv) {
  get_table()->add(id, owner(my_id_), cosine_lsh(sfv, hash_num_), l2norm(sfv));
}
コード例 #18
0
ファイル: Psfwi.c プロジェクト: yunzhishi/src
void fwi(sf_file Fdat, sf_file Finv, sf_file Ferr, sf_file Fgrad, sf_mpi *mpipar, sf_sou soupar, sf_acqui acpar, sf_vec_s array, sf_fwi_s fwipar, sf_optim optpar, bool verb1, int seislet)
/*< fwi >*/
{
	int iter=0, flag;
	float fcost;
	float *x, *direction, *grad;
	sf_gradient gradient;
	FILE *fp;

	/* initialize */
	gradient_init(Fdat, mpipar, soupar, acpar, array, fwipar, verb1);

	/* gradient type */
	gradient=gradient_standard;
	x=array->vv;

	/* calculate first gradient */
	grad=sf_floatalloc(nzx);
	gradient(x, &fcost, grad);

	/* output first gradient */
	if(mpipar->cpuid==0) sf_floatwrite(grad, nzx, Fgrad);

	/* if onlygrad=y, program terminates */
	if(fwipar->onlygrad) return; 

	if(mpipar->cpuid==0) fp=fopen("iterate.txt","a");

	direction=sf_floatalloc(nzx);
	optpar->sk=sf_floatalloc2(nzx, optpar->npair);
	optpar->yk=sf_floatalloc2(nzx, optpar->npair);

	optpar->igrad=1;
	optpar->ipair=0;
	optpar->ils=0;
	optpar->fk=fcost;
	optpar->f0=fcost;
	optpar->alpha=1.;
	/* initialize data error vector */
	for(iter=0; iter<optpar->nerr; iter++){
		optpar->err[iter]=0.;
	}
	optpar->err[0]=optpar->fk;
	if (optpar->err_type==1) optpar->err[optpar->nerr/2]=swap;

	iter=0;
	if(mpipar->cpuid==0){
		l2norm(nzx, grad, &optpar->gk_norm);
		print_iteration(fp, iter, optpar);
	}

	/* optimization loop */
	for(iter=0; iter<optpar->niter; iter++){
		if(mpipar->cpuid==0) sf_warning("-------------------iter=%d----------------------", iter+1);
		
		optpar->ils=0;
		if(iter%optpar->repeat==0) optpar->alpha=1.;

		/* search direction */
		if(iter==0){
			reverse(nzx, grad, direction);
		}else{
			lbfgs_update(nzx, x, grad, optpar->sk, optpar->yk, optpar);
			lbfgs_direction(nzx, grad, direction, optpar->sk, optpar->yk, optpar);
		}

		/* line search */
		lbfgs_save(nzx, x, grad, optpar->sk, optpar->yk, optpar);
		line_search(nzx, x, grad, direction, gradient, optpar, threshold, &flag, mpipar->cpuid, 1);
		optpar->err[iter+1]=optpar->fk;
		if (optpar->err_type==1) optpar->err[optpar->nerr/2+iter+1]=swap;
		
		if(mpipar->cpuid==0){
			l2norm(nzx, grad, &optpar->gk_norm);
			print_iteration(fp, iter+1, optpar);
			fclose(fp); /* get written to disk right away */
			fp=fopen("iterate.txt","a");
		}

		if(mpipar->cpuid==0 && flag==2){
			fprintf(fp, "Line Search Failed\n");
			break;
		}

		if(mpipar->cpuid==0 && optpar->fk/optpar->f0 < optpar->conv_error){
			fprintf(fp, "Convergence Criterion Reached\n");
			break;
		}
	} // end of iter

	if(mpipar->cpuid==0 && iter==optpar->niter){
		fprintf(fp, "Maximum Iteration Number Reached\n");
	}

	/* output vel & misfit */
	if(mpipar->cpuid==0) sf_floatwrite(x, nzx, Finv);
	if(mpipar->cpuid==0) sf_floatwrite(optpar->err, optpar->nerr, Ferr);
	if(mpipar->cpuid==0) fclose(fp);

	return;
}
コード例 #19
0
//proj:each row represent which dimensions should projection in this hash,different row represent different bucket
void meanshiftCluster(const Mat& feature, Mat& convexPoints)
{
	printf("start to meanshift point...\n");
	
	time_t startTime = time(0);

	printf("start to compute the convex point for every point ...\n");
	int nl  =  feature.rows;
	int nc  =  feature.cols;
	printf("nl: %d, nc: %d.\n", nl, nc);
	convexPoints.create(nl, nc, CV_32FC1);
	
	//set params;
	MeanShiftParam mp;
	setMeanShiftParams(mp);
	
	#ifdef LSH_NEIGHBOR
		HashParam hp;
		setHashParam(hp, nl, nc);
		Mat proj;

		#ifdef LOAD_HASHINFO_FROM_FILE
			const char* hashFile = "./output/hashInfo.dat";
			readHashInfo(hashFile, proj, hp.bucketInfo);
			for(int i = 0; i < hp.bucketNumber; i ++)
			{
				for(int j = 0; j < hp.projectionSize; j ++)
				{
					int t = proj.at<int>(i, j);
					if(t < 0 || t >= nc)
						printf("i: %d, j: %d, t: %d\n", i, j, t);
				}
			}
		#else
			proj.create(hp.bucketNumber, hp.projectionSize, CV_32SC1);
			boostSample(hp.bucketNumber, hp.projectionSize, proj, nc);
			HashAllItems(feature, proj, hp);
			const char* hashFile = "./output/hashInfo.dat";
			saveHashInfo(hashFile, proj, hp.bucketInfo);
		#endif
		
		printf("bucketNumber: %d, projectionSize: %d.\n", hp.bucketNumber, hp.projectionSize);
		printf("bucket length: %d\n", hp.bucketLength);
		printHashParamInfo(hp);
		//mp.windowRadius = setWindow(hp, feature, proj);
		mp.windowRadius = 50.001144;
		mp.tinyNearestD = mp.windowRadius * 0.1;
		hp.votes = 2;
		
	#else
		boostSample(hp.bucketNumber, hp.projectionSize, proj, nc);
	#endif
	//end of set params;
	
	/********** set window **************/

	
	int initialPoint;
	float demoninator;
	vector<float> molecular(nc, 0);
	vector<float> shiftVector(nc, 0);
	int inNeighbor;

	int iterations;
	float oldChange;
	float newChange;
	bool advanceConvexed;
	int advancedConvexedPoint;
	
	vector<int> candidates;
	vector<int> frequency;
	vector<int> hashLocation(hp.bucketNumber, -1);
	vector<int> cursors(hp.bucketNumber, 0);
	vector<int> itemNumber(hp.bucketNumber, 0);
	
	vector<float> closeness;
	
	printf("start to enter circles for every point ...\n");
	for(int counts = 0; counts < nl; counts ++)
	{
		printf("the %d-th iterations ...\n", counts);
		initialPoint = counts;
		iterations = mp.maxIterations;
		advanceConvexed = false;
		for(int j = 0; j < nc; j ++)
		{
			shiftVector[j] =  feature.at<float>(initialPoint, j);
		}
		
		while(iterations > 0)
		{
			iterations --;
			inNeighbor = 0;
			demoninator = 0;
			for(int j = 0; j < nc; j ++)
			{
				molecular[j] =  0;
			}
			
			#ifdef LSH_NEIGHBOR
			//hash the shift vector and get candidates
			for(int bucket = 0; bucket < hp.bucketNumber; bucket ++)
			{
				//////////////////////////////////////////////////////////
				hashLocation[bucket] = hashFunction(hp, shiftVector, proj.row(bucket));
			}	
				
			candidates.clear();
			frequency.clear();
			for(int bucket = 0; bucket < hp.bucketNumber; bucket ++)
			{
				cursors[bucket] = 0;
				itemNumber[bucket] =  hp.bucketInfo[bucket][hashLocation[bucket]].size();
			}
			int candidateSize = 0;
			int lastElement = -1;
			while(1)
			{
				bool changeFlag = false;
				int min;
				int minBucket;
				for(int i = 0; i < hp.bucketNumber; i ++)
				{
					if(cursors[i] < itemNumber[i])
					{
						changeFlag = true;
						min = hp.bucketInfo[i][hashLocation[i]][cursors[i]];
						minBucket = i;
						break;
					}
				}
				if(!changeFlag)
					break;
			
				for(int i = minBucket + 1; i < hp.bucketNumber; i ++)
				{
					if(cursors[i] < itemNumber[i] && hp.bucketInfo[i][hashLocation[i]][cursors[i]] < min)
					{
						min = hp.bucketInfo[i][hashLocation[i]][cursors[i]];
						minBucket = i;
					}
				}
				if(min == lastElement)
				{
					frequency[candidateSize-1] ++;
				}
				else
				{
					candidates.push_back(min);
					frequency.push_back(1);
					candidateSize ++;
					lastElement = min;
				}
				cursors[minBucket] ++;
			}
			
			candidatesNumber.push_back(candidateSize);
			//printf("tag1\n");
			
			//filter the candidates;
			int statisticalFilted = 0;
			int size = candidateSize;
			
			/*closeness.clear();
			closeness.resize(size, 0.0);
			for(int cc = 0; cc < size; cc ++)
			{
				int near = candidates[cc];
				for(int bucket = 0; bucket < hp.bucketNumber; bucket ++)
					closeness[cc] += weight[bucket] * (hashLocation[bucket] - hp.hashInfo[near][bucket]) ** 2;
				if(closeness[cc] > mp.windowRadius)
					continue;
				
				if(counts > cc && closeness[cc] < mp.tinyNearestD)
				{
					advancedConvexedPoint = cc;
					advanceConvexed = true;	
					float* curent_data = convexPoints.ptr<float>(counts);
					const float* convex_data = convexPoints.ptr<float>(cc);
					for(int j = 0; j < nc; j ++)
						curent_data[j] = convex_data[j];
					break;
				}
				double temp = exp(0 - closeness[cc]);
				demoninator +=  temp;
				for(int j = 0; j < nc; j ++)
				{
					molecular[j] +=  temp * feature.at<float>(candidates[cc], j);
				}
				inNeighbor ++;
			}*/
			
			for(int cc = 0; cc < size; cc ++)
			{
				if(frequency[cc] >= hp.votes)  
				{
				 	statisticalFilted ++;
					float t = l2norm(shiftVector, feature.row(candidates[cc]));
					//indicate the cc point have convexed and the hash value vevy same
					//and their distance is very near, it's convex
					if(counts > cc && t < mp.tinyNearestD)
					{
						advancedConvexedPoint = cc;
						advanceConvexed = true;	
						float* curent_data = convexPoints.ptr<float>(counts);
						const float* convex_data = convexPoints.ptr<float>(cc);
						for(int j = 0; j < nc; j ++)
							curent_data[j] = convex_data[j];
						break;
					}
					if(t < mp.windowRadius)
					{
						double temp = exp(0 - t);
						demoninator +=  temp;
						for(int j = 0; j < nc; j ++)
						{
							molecular[j] +=  temp * feature.at<float>(candidates[cc], j);
						}
						inNeighbor ++;
					}
				}
			}
			printf("candidateSize: %d, statisticalFilted: %d, inNeighbor: %d\n", candidateSize, statisticalFilted, inNeighbor);
			neighborFileterNumber.push_back(statisticalFilted);
			//printf("tag2\n");
			
			#else
			for(int i = 0; i < nl; i ++)
			{
				float t = l2norm(shiftVector, feature.row(i));
				if(t < mp.tinyNearestD && counts > i)
				{
					advancedConvexedPoint = i;
					advanceConvexed = true;	
					float* curent_data = convexPoints.ptr<float>(counts);
					const float* convex_data = convexPoints.ptr<float>(i);
					for(int j = 0; j < nc; j ++)
						curent_data[j] = convex_data[j];
					break;
				}
				if(t < windowRadius)
				{
					double temp = exp(0 - t);
					demoninator +=  temp;
					for(int j = 0; j < nc; j ++)
					{
						molecular[j] +=  temp * feature.at<float>(i, j);
					}
					inNeighbor ++;
				}
			}
			#endif

			if(inNeighbor == 0)
			{
				printf("neighbor: 0\n");
				advanceConvexed = true;
				advancedConvexedPoint = counts;
				float* curent_data = convexPoints.ptr<float>(counts);
				const float* convex_data = feature.ptr<float>(advancedConvexedPoint);
				for(int j = 0; j < nc; j ++)
					curent_data[j] = convex_data[j];
				break;
			}
			if(advanceConvexed)
				break;
				
			demoninator = 1.0/demoninator;
			for(int j = 0; j < nc; j ++)
			{
				molecular[j] = 	demoninator * molecular[j];
			}
			//printf("tag3\n ");
			
			
			newChange = l2norm(shiftVector, molecular);
			convexTrend.push_back(newChange);
			if(newChange / oldChange < mp.epsilon)
				break;
			oldChange = newChange;
			shiftVector = molecular;
			//printf("tag4\n ");
		}
		
		convexTrend.push_back(-1);//seprate by -1
		iters.push_back(mp.maxIterations - iterations);
		
		if(!advanceConvexed)
		{
			float* data = convexPoints.ptr<float>(counts);
			for(int j = 0; j < nc; j ++)
				data[j] = shiftVector[j];
		}   
	}

	string writer = "./output/convex.dat";
	MatrixDataIo mdi(writer.c_str(), true, convexPoints);
	
	#ifdef LSH_NEIGHBOR
	printf("destroy the bucket info ...\n");
	destroyBucket(hp);
	#endif
	
	//compute used time
	{
		time_t endTime = time(0);
		int totalUsed = endTime - startTime;
		//comparedTime[1] = totalUsed;
		shiftTime = totalUsed;
		printf("meanshift LSH: %d\n", totalUsed);
	}	
	
	programPause();
	printf("exit meanshift clustering...\n");
}
コード例 #20
0
int setWindow(HashParam hp, const Mat& feature, const Mat& proj)
{
	printf("start to auto-set window size ...\n");
	//sample
	int n = feature.rows;
	int nc = feature.cols;
	
	float trySize;//尝试窗口大小
	int omit;//ignorge the head and tail sample element
	int sum_neighbors = 0;
	float average_neighbors;
	int retained_neighbors = 20;
	trySize = 50;
	
	vector<float> sp(nc, 0);
	int candidateSize = 0;
	int neighbors;
	srand(time(0));
	
	omit = 1;
	int throw_stone = 10;
	for(int i =0; i < throw_stone; i ++)
	{
		int t = rand() % n;
		for(int j = 0; j < nc; j ++)
		{
			sp[j] =  feature.at<float>(t, j);
		}
		neighbors = 0;
		vector<int> candidates = getCandidates(hp, sp, proj);
		candidateSize = candidates.size();
		for(int i = 0; i < candidateSize; i ++)
		{
			float distance = l2norm(sp, feature.row(candidates[i]));
			if(distance < trySize)
				neighbors ++;
		}
		sum_neighbors += neighbors;
	}
	average_neighbors = sum_neighbors * 1.0/throw_stone;
	printf("average_neighbors: %f\n", average_neighbors);
	trySize = trySize * pow(retained_neighbors/average_neighbors, 1.0/nc);
	printf("trySize: %f\n", trySize);
	
	
	int samplePoints = 50;
	sum_neighbors = 0;
	omit = 5;
	printf("samplePoints: %d\n", samplePoints);
	for(int i =0; i < samplePoints; i ++)
	{
		int t = rand() % n;
		for(int j = 0; j < nc; j ++)
		{
			sp[j] =  feature.at<float>(t, j);
		}
		neighbors = 0;
		vector<int> candidates = getCandidates(hp, sp, proj);
		candidateSize = candidates.size();
		for(int i = 0; i < candidateSize; i ++)
		{
			float distance = l2norm(sp, feature.row(candidates[i]));
			if(distance < trySize)
				neighbors ++;
		}
		sum_neighbors += neighbors;
		printf("%d\n", neighbors);
	}
	
	printf("\nsum_neighbors: %d\n", sum_neighbors);
	average_neighbors = sum_neighbors * 1.0/samplePoints;
	printf("average_neighbors: %f\n", average_neighbors);
	
	float r = trySize * pow(average_neighbors / retained_neighbors, 1.0/nc);
	printf("window size: %f\n", r);
	return r;
}
コード例 #21
0
FastShift::FastShift(const Mat& feature, Mat& convexPoints)
{
	printf("start to meanshift point...\n");
	
	time_t startTime = time(0);
	data = feature;
	initial();
	printf("start to compute the convex point for every point ...\n");
	convexPoints.create(r, n, CV_32FC1);

	srand(time(0));
	vector<float> shiftPoint(n, 0);
	vector<float> shiftValue(n, 0);
	double w1, w2;
	vector<float> total_force, current_force, external_force;
	vector<int> grid_pos;
	int maxIterations = 50;

	
	for(int counts = 0; counts < r; counts ++)
	{
		printf("the %d-th iterations ...\n", counts);
		int iterations = maxIterations;
		copy((float*)data.row(counts).data, shiftPoint, n);
		while(iterations > 0)
		{
			iterations --;
			//choose the best direction
			radomSelect();
			whichGrid(shiftPoint, grid_pos);
			int pos = findposition(grid_pos);
			//so many cases to get the external forces
			#ifdef one_direction
				external_force = maxDirectionOneSide(shiftPoint, shiftValue);
			#else 
				#ifdef composite_line
					external_force = maxDirectionTwoSide(shiftPoint, shiftValue);
				#else 
					external_force = allDirections(shiftPoint, shiftValue);
				#endif
			#endif
			
			//shift the feature vector
			calForce(shiftPoint, pos, current_force);
			total_force = current_force + external_force;
			
			if(l2norm(external_force) < acceptance)
				continue;
			if(l2norm(total_force) < stopCond)
				break;
			shiftPoint = shiftPoint + step * total_force;
			/*w1 = l2norm(current_force)/l2norm(total_force);
			w2 = external_force/total_force;
			shiftPoint = w1 * shiftPoint + w2 * shiftValue;
			if(w2 < epsilon)
				break;*/
			
		}
		copy(shiftPoint, (float*)convexPoints.row(counts).data, n);
		//iters.push_back(counts);
		
	}
	string writer = "./output/convex.dat";
	MatrixDataIo mdi(writer.c_str(), true, convexPoints);
	
	printUsedTime(startTime);
	programPause();
	printf("exit meanshift clustering...\n");
}