Exemple #1
0
double
quasisymmetric0(double *x, double *y, int n, int m, double *dtwm, int **constr)
{
  int i, j;
  double d;
  double dtwm_i, dtwm_j, dtwm_ij;


  for (i=0; i<n; i++)
    for (j=0; j<m; j++)
      dtwm[i * m + j] = DBL_MAX;

  dtwm[0] = euclidean(x[0], y[0]); // DP-algorithm (i - 1), (j - 1) 
   
  for (j=constr[0][0] + 1; j<=constr[1][0]; j++)
    {
      d = euclidean(x[0], y[j]);
      dtwm_j  = dtwm[0 * m + (j - 1)] + d; // DP-algorithm (j - 1)
      dtwm[0 * m + j] = dtwm_j;
    }

  for (i=1; i<n; i++)             
    for (j=constr[0][i]; j<=constr[1][i]; j++)
      {
	d = euclidean(x[i], y[j]); 
	
	if (j == 0)
	  {
	    dtwm_i = dtwm[(i - 1) * m + j] + d; // DP-algorithm (i - 1)
	    dtwm[i * m + j] = dtwm_i;
	  }
	else
	  {
	    dtwm_i = dtwm[(i - 1) * m + j];
	    dtwm_ij = dtwm[(i - 1) * m + (j - 1)];
	    dtwm_j  = dtwm[i * m + (j - 1)];
	    
	    if (dtwm_i != DBL_MAX)
	      dtwm_i = dtwm_i + d; // DP-algorithm (i - 1)
	    
	    if (dtwm_ij != DBL_MAX)
	      dtwm_ij = dtwm_ij + d; // DP-algorithm (i - 1), (j - 1) 

	    if (dtwm_j != DBL_MAX)
	      dtwm_j = dtwm_j + d; // DP-algorithm (j - 1)
	       
	    dtwm[i * m + j] = min3(dtwm_i, dtwm_j, dtwm_ij);
	  }
      }
  
  return dtwm[(m * n) - 1] / (double) (n + m);
}
Exemple #2
0
float dtw_windowed(void * seq1v, void * seq2v, int seq1_len, int seq2_len, int window) {
    // THIS WILL SEGFAULT IF seq1 AND seq2 ARE NOT (n,m)x4
    float* seq1 = (float*) seq1v;
    float* seq2 = (float*) seq2v;
    float* distance_mat = make_mat(seq1_len, seq2_len, INFINITY);
    //printf("n: %d, m: %d\n", seq1_len, seq2_len);
    distance_mat[int_pos(0,0, seq1_len)] = 0;
    window = MAX(window, abs(seq1_len - seq2_len));
    //printf("Window: %d\n", window);
    int i, j;
    for (i = 1; i < seq1_len; i++) {
        //for (j = MAX(1, i-window); j < MIN(seq2_len,i+window); j++) {
        for (j = 1; j < seq2_len; j++) {
            // Wish there was a better way to do this...
            float eucl_dist = euclidean(seq1[int_pos(i,0,4)], seq1[int_pos(i,1,4)], seq2[int_pos(j,0,4)], seq2[int_pos(j,1,4)]);
            float cosi_dist = cosine(seq1[int_pos(i,2,4)], seq1[int_pos(i,3,4)], seq2[int_pos(j,2,4)], seq2[int_pos(j,3,4)]);
            distance_mat[int_pos(i,j,seq2_len)] = (eucl_dist + cosi_dist) + MIN(distance_mat[int_pos(i-1,j-1,seq2_len)], 
                                                                                MIN(distance_mat[int_pos(i-1,j,seq2_len)],
                                                                                    distance_mat[int_pos(i,j-1,seq2_len)]));
        }   
    }
    // find minimal value along the last column as our distance
    float retval = distance_mat[int_pos(seq1_len-1, seq2_len-1, seq2_len)];
    free(distance_mat);
    return retval;
}
Exemple #3
0
int main() {
    int a, b, answer;
    scanf("%d", &a);
    scanf("%d", &b);
    answer = euclidean(a, b);
    printf("%d\n", answer);
    return 0;
}
Exemple #4
0
void _identifySuccessors(struct grid *gd, struct node *activeNode, struct open_list *current, struct node *endNode)
{
	int endX = endNode->x;
	int endY = endNode->y;
	int *jumpPoint;
	struct neighbor_xy_list *neighbors_head = _findNeighbors(gd, activeNode);
	struct neighbor_xy_list *neighbors_current = neighbors_head;
	while (neighbors_head != (neighbors_current = neighbors_current->right)) {
		if (DEBUG) {
			if (isWalkableAt(gd, neighbors_current->x, neighbors_current->y))
				printf("Neighbor x:%d/y:%d is walkable!\n", neighbors_current->x, neighbors_current->y);
			else
				printf("Neighbor x:%d/y:%d is NOT walkable!\n", neighbors_current->x, neighbors_current->y);
		}

		jumpPoint = _jump(gd, neighbors_current->x, neighbors_current->y, activeNode->x, activeNode->y, endNode);
		if (DEBUG)
			printf("Jump point not set!\n\n");
		if (jumpPoint != NULL) {
			int jx, jy, d, ng;
			struct node *jumpNode;
			if (DEBUG)
				printf("Jump point set!\n\n");
			jx = jumpPoint[0];
			jy = jumpPoint[1];

			free(jumpPoint);
			malloc_count--; /* [ Malloc Count ] */

			jumpNode = getNodeAt(gd, jx, jy);
			if (jumpNode->closed) {
				continue;
			}

			d = euclidean(abs(jx - activeNode->x), abs(jy - activeNode->y));
			ng = activeNode->g + d;
			if (!jumpNode->opened || ng < jumpNode->g) {
				jumpNode->g = ng;
				if (!jumpNode->h)
					jumpNode->h = manhattan(abs(jx - endX), abs(jy - endY));
				/* jumpNode->h = jumpNode->h || manhattan(abs(jx - endX), abs(jy - endY)); // ASK FIDELIS !! */
				jumpNode->f = jumpNode->g + jumpNode->h;
				if (DEBUG)
					printf("Node g:%d h:%d f:%d\n", jumpNode->g, jumpNode->h, jumpNode->f);
				jumpNode->parent = activeNode;

				if (!jumpNode->opened) {
					current = ol_insert_right(current, jumpNode);
					jumpNode->opened = true;
				} else {
					ol_listsort(current->right);
				}
			}
		}
	}
	neighbor_xy_clean(neighbors_head);
}
Exemple #5
0
void generate_output(node_type *np, int node_num)
{
  #define NODE_LINK
  #define XY_CORD
  #define LINK_WEIGHT
  #define SYMMETRY

  int i, j, dist;
  int link_num = 0;

  for (i=0; i<node_num; ++i)
  {
    link_num += np[i].degree - np[i].free_degree;
    np[i].degree -= np[i].free_degree;
  }

  if ((link_num % 2) != 0)
  {
    fprintf(stderr, "generate_output: error, total outdegree is odd!\n");
    exit(-1);
  }

#ifdef NODE_LINK
  //printf("Num of nodes: %d  Num of links: %d\n", node_num, link_num);
  printf("%d %d\n", node_num,  link_num/2);
#endif /* NODE_LINK */

#ifdef XY_CORD
  for (i=0; i<node_num; ++i)
    printf("%d\t%d\t%d\n", np[i].nid, np[i].x, np[i].y);
#endif /* XY_CORD */

  for (i=0; i<node_num; ++i)
  {
    for(j=0; j<np[i].degree; ++j)
    {
#ifdef SYMMETRY
      if (np[i].nnp[j] && np[i].nnp[j]->nid > np[i].nid) 
      /* output one of the two links assuming symmetry */
#endif /* SYMMETRY */
      {
#ifdef LINK_WEIGHT
        dist = euclidean(&np[i], np[i].nnp[j]); 
        printf("%d\t%d\t%d\n", np[i].nid, np[i].nnp[j]->nid, dist);
#else
        printf("%d\t%d\n", np[i].nid, np[i].nnp[j]->nid);
	// doing this just for clique script, it wants nodeA:nodeB
	//printf("%d:%d\n", np[i].nid, np[i].nnp[j]->nid);
	
#endif /* LINK_WEIGHT */

      } 
    }
  }
}
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{

	if (nrhs == 0 || nrhs != 3) {
		mexPrintf("d = distance(x1,x2,flag\n");
		mexPrintf("flag = 1 : normalized correlation\n");
		mexPrintf("flag = 2 : euclidean distance\n");
		return;
	}

	double *x1 = mxGetPr(prhs[0]); int N1 = mxGetN(prhs[0]); int M1 = mxGetM(prhs[0]);
	double *x2 = mxGetPr(prhs[1]); int N2 = mxGetN(prhs[1]); int M2 = mxGetM(prhs[1]);

	if (M1 != M2) {
		mexPrintf("Wrong input!\n");
		return;
	}

	plhs[0] = mxCreateDoubleMatrix(N1, N2, mxREAL);
	double *resp = mxGetPr(plhs[0]);

	int flag = *mxGetPr(prhs[2]);

	switch (flag)
	{
	case 1 :
		for (int i = 0; i < N2; i++) {
			for (int ii = 0; ii < N1; ii++) {
				*resp++ = ccorr_normed(x1+ii*M1,x2+i*M1,M1);
			}
		}

		return;
	case 2 :

		for (int i = 0; i < N2; i++) {
			for (int ii = 0; ii < N1; ii++) {
				*resp++ = euclidean(x1+ii*M1,x2+i*M1,M1);
			}
		}

		return;
	}
}
Exemple #7
0
inline
void
VectorDBase<T>::normalise()
{
    T* i = start;
    while(i != end && *i == 0) { ++i; }
    if (i == end) return;
    T gcd = *i;
    if (gcd < 0) { gcd *= -1; }
    if (gcd == 1) return;
    ++i;
    while (i != end && *i == 0) { ++i; }
    while (i != end) {
        euclidean(gcd, *i, gcd);
        if (gcd == 1) return;
        ++i;
        while (i != end && *i == 0) { ++i; }
    }
    if (gcd != 1) { diveq(gcd); }
}
void calc_depth(unsigned char *depth_map, unsigned char *left,
        unsigned char *right, int image_width, int image_height,
        int feature_width, int feature_height, int maximum_displacement) {

    int mapsize = image_width * image_height;
    int ftrboxw = (feature_width * 2) + 1;
    int ftrboxh = (feature_height * 2) + 1;
    int ftrboxsize = ftrboxw * ftrboxh;
    int srchboxwidth = feature_width + maximum_displacement;
    int srchboxheight = feature_height + maximum_displacement;
    int j = 0;

    if (maximum_displacement == 0){
        for (int z = 0; z < mapsize; z++){
            depth_map[z] = 0;
        }
    }else{   
        while (j < mapsize) {
        int x = j % image_width;
        int y = (j - (j % image_width))/image_width;

            if (feature_width > x || feature_width >= image_width - x ||
                feature_height > y || feature_height >= image_height - y) {
                depth_map[j] = 0;
            }else{
                int startx, starty, endx, endy;
                /* Establish search box boundaries */
                startx = x - srchboxwidth;
                starty = y - srchboxheight;
                endx = x + srchboxwidth;
                endy = y + srchboxheight; 
                if (startx < 0){
                    startx = 0;
                }
                if (starty < 0){
                    starty = 0;
                }
                if (endx >= image_width){
                    endx = image_width - 1;
                }
                if (endy >= image_height){
                    endy = image_height - 1;
                }

                /* Creates an array for left-image feature box centered around j */
                unsigned char leftftrbox[ftrboxsize];
                int lstart = ((y-feature_height)*image_width) + (x-feature_width);
                int count = 0;

                for (int s = lstart; s <= lstart + ((ftrboxh-1)*image_width); s=s+image_width){
                    for (int t = 0; t < ftrboxw; t++){
                        leftftrbox[t+count] = left[s+t];
                    }
                    count = count+ftrboxw;
                }
            
                int start = (starty * image_width) + startx;
                int iterendx = endx - (startx + (ftrboxw-1));
                int iterendy = endy - (starty + (ftrboxh-1));            
                int abs(int x);

                unsigned char normdis;
                unsigned char normdistemp;
                unsigned int tempedistnc;
                unsigned int edistance;
                int temp = 0;
                /* Iterate through all right-image feature box in search box */
                for (int k = start; k <= start + iterendx; k++){
                    for (int m = k; m <= k+(iterendy*image_width); m=m+image_width){
                        /* Creates an array for each right-image feature box centered around m */
                        unsigned char currftrbox[ftrboxsize];
                        int counter = 0;
                        for (int p = m; p <= m + ((ftrboxh-1)*image_width); p=p+image_width){
                            for (int n = 0; n < ftrboxw; n++) {
                                currftrbox[n+counter] = right[n+p];
                            }
                            counter = counter+ftrboxw;
                        }
                        int centerftrx = (m+feature_width)%image_width;
                        int centerftry = ((m+ (feature_height*image_width)) -((m+ (feature_height*image_width))%image_width)) / image_width;
                        int xloc = abs(centerftrx - x);
                        int yloc = abs(centerftry - y);
                        normdistemp = normalized_displacement(xloc, yloc, maximum_displacement);                    
                        if (temp == 0) {
                            normdis = normalized_displacement(xloc, yloc, maximum_displacement);
                            edistance = euclidean(leftftrbox, currftrbox, ftrboxsize);
                        }else{
                        
                            tempedistnc = euclidean(leftftrbox, currftrbox, ftrboxsize);
                            if (tempedistnc < edistance) {
                                edistance = tempedistnc;
                                normdis = normalized_displacement(xloc, yloc, maximum_displacement);                                        }
                            if (tempedistnc == edistance){
                                if (normdistemp < normdis){
                                    normdis = normdistemp;
                                }
                            }
                        }   
                        temp++;
                    }
                }   
                depth_map[j] = normdis;
            }
            j++;
        }
    }
}
Exemple #9
0
double
asymmetric0_od(double *x, double *y, int n, int m, int **constr)
{
  int i, j;
  double d, dist = 0.0;
  double dtwm_i, dtwm_j, dtwm_ij;
  
  double *prev_col = (double *) malloc (m * sizeof(double));
  double *next_col = (double *) malloc (m * sizeof(double));
  double *tmp;


  for (j=0; j<m; j++)
    {
      prev_col[j] = DBL_MAX;
      next_col[j] = DBL_MAX;      
    }

  prev_col[0] = euclidean(x[0], y[0]); // DP-algorithm (i - 1), (j - 1) 
   
  for (j=constr[0][0] + 1; j<=constr[1][0]; j++)
    {
      d = euclidean(x[0], y[j]);
      dtwm_j  = prev_col[j - 1]; // DP-algorithm (j - 1)
      prev_col[j] = dtwm_j;
    }

  for (i=1; i<n; i++)
    {
      for (j=constr[0][i]; j<=constr[1][i]; j++)
	{
	  d = euclidean(x[i], y[j]); 
	  
	  if (j == 0)
	    {
	      dtwm_i = prev_col[j] + d; // DP-algorithm (i - 1)
	      next_col[j] = dtwm_i;
	    }
	  else
	    {
	      dtwm_i  = prev_col[j];
	      dtwm_ij = prev_col[j - 1];
	      dtwm_j  = next_col[j - 1];
	      
	      if (dtwm_i != DBL_MAX)
		dtwm_i = dtwm_i + d; // DP-algorithm (i - 1)
	      
	      if (dtwm_ij != DBL_MAX)
		dtwm_ij = dtwm_ij + d; // DP-algorithm (i - 1), (j - 1) 
	      
	      // DP-algorithm (j - 1) (no sum)
	      
	      next_col[j] = min3(dtwm_i, dtwm_j, dtwm_ij);
	    }
	}
      
      dist = next_col[m - 1];
      
      tmp = prev_col;
      prev_col = next_col;
      next_col = tmp;
      
      for (j=0; j<m; j++)
	next_col[j] = DBL_MAX;      
            
    }
  
  free(prev_col);
  free(next_col);

  return dist / (double) n;
}
Exemple #10
0
int euclidean(int m, int n) {
    if (n == 0) {
        return m;
    }
    return euclidean(n, m % n);
}
void
run_test(Cost_Map &map, Point &start, Point &goal, vector<int> &times) {
	struct timeval pre;
	struct timeval post;

///	printf("UCS\n");
	UCS ucs(&map, start, goal);
	gettimeofday(&pre, NULL); 
	Path path = ucs.search();
	gettimeofday(&post, NULL); 
	double reference_cost = path.length;
	check_and_update(start, goal, pre, post, reference_cost, path.length, times);
	check_and_update(start, goal, pre, post, reference_cost, path.length, times);

///	printf("A*\n");
	Manhattan Manhattan(&map, start, goal);
	gettimeofday(&pre, NULL); 
	path = Manhattan.search();
	gettimeofday(&post, NULL); 
	check_and_update(start, goal, pre, post, reference_cost, path.length, times);
	check_and_update(start, goal, pre, post, reference_cost, path.length, times);

///	printf("A*\n");
	Euclidean euclidean(&map, start, goal);
	gettimeofday(&pre, NULL); 
	path = euclidean.search();
	gettimeofday(&post, NULL); 
	check_and_update(start, goal, pre, post, reference_cost, path.length, times);
	check_and_update(start, goal, pre, post, reference_cost, path.length, times);

///	printf("A*\n");
	Octile octile(&map, start, goal);
	gettimeofday(&pre, NULL); 
	path = octile.search();
	gettimeofday(&post, NULL); 
	check_and_update(start, goal, pre, post, reference_cost, path.length, times);
	check_and_update(start, goal, pre, post, reference_cost, path.length, times);

///	printf("Coarse single\n");
	CUCS_Heuristic cucs(&map, start, goal);
	gettimeofday(&pre, NULL); 
	path = cucs.search();
	gettimeofday(&post, NULL); 
	check_and_update(start, goal, pre, post, reference_cost, path.length, times);
	check_and_update(start, goal, pre, post, reference_cost, path.length, times);

///	printf("BB\n");
	Boundaries_Blocking bb2(&map, start, goal, 2, xscale, yscale);
	gettimeofday(&pre, NULL); 
	path = bb2.search();
	gettimeofday(&post, NULL); 
	check_and_update(start, goal, pre, post, reference_cost, path.length, times);

	Boundaries_Blocking bb(&map, start, goal, levels, xscale, yscale);
	gettimeofday(&pre, NULL); 
	path = bb.search();
	gettimeofday(&post, NULL); 
	check_and_update(start, goal, pre, post, reference_cost, path.length, times);

///	printf("BN\n");
	Boundaries_NonBlocking bnb2(&map, start, goal, 2, xscale, yscale);
	gettimeofday(&pre, NULL); 
	path = bnb2.search();
	gettimeofday(&post, NULL); 
	check_and_update(start, goal, pre, post, reference_cost, path.length, times);

	Boundaries_NonBlocking bnb(&map, start, goal, levels, xscale, yscale);
	gettimeofday(&pre, NULL); 
	path = bnb.search();
	gettimeofday(&post, NULL); 
	check_and_update(start, goal, pre, post, reference_cost, path.length, times);

///	printf("CB\n");
	Corners_Blocking cb2(&map, start, goal, 2, xscale, yscale);
	gettimeofday(&pre, NULL); 
	path = cb2.search();
	gettimeofday(&post, NULL); 
	check_and_update(start, goal, pre, post, reference_cost, path.length, times);

	Corners_Blocking cb(&map, start, goal, levels, xscale, yscale);
	gettimeofday(&pre, NULL); 
	path = cb.search();
	gettimeofday(&post, NULL); 
	check_and_update(start, goal, pre, post, reference_cost, path.length, times);

///	printf("CN\n");
	Corners_NonBlocking cnb2(&map, start, goal, 2, xscale, yscale);
	gettimeofday(&pre, NULL); 
	path = cnb2.search();
	gettimeofday(&post, NULL); 
	check_and_update(start, goal, pre, post, reference_cost, path.length, times);

	Corners_NonBlocking cnb(&map, start, goal, levels, xscale, yscale);
	gettimeofday(&pre, NULL); 
	path = cnb.search();
	gettimeofday(&post, NULL); 
	check_and_update(start, goal, pre, post, reference_cost, path.length, times);
}
Exemple #12
0
 bool AStarPlanner::computePath(std::vector<Util::Point>& agent_path,  Util::Point start, Util::Point goal, SteerLib::GridDatabase2D * _gSpatialDatabase, bool append_to_path)
 {
     gSpatialDatabase = _gSpatialDatabase;
     
     //TODO
     std::cout<<"\nIn A*\n";
     //coordinate for start & goal in the cell
     int startIndex = gSpatialDatabase->getCellIndexFromLocation(start);
     gSpatialDatabase->getLocationFromIndex(startIndex, start);
     int goalIndex = gSpatialDatabase->getCellIndexFromLocation(goal);
     gSpatialDatabase->getLocationFromIndex(goalIndex, goal);
     
     std::vector<AStarPlannerNode> closedset;
     std::vector<AStarPlannerNode> openset;
     std::vector<AStarPlannerNode> neighbors;
     std::map<AStarPlannerNode, AStarPlannerNode> came;
     
     AStarPlannerNode startNode = AStarPlannerNode(start, 0, 0, NULL);
     startNode.startNode = 1;
     startNode.g = 0;
     startNode.f = startNode.g + W * heuristic(start, goal);
     
     openset.push_back(startNode);
     
     AStarPlannerNode curr;
     int count = 0;
     
     while(!openset.empty()){
         count++;
         //get Lowest F
         int index = 0;
         curr = openset[0];
         for(int i = 0; i < openset.size(); i++){
             if( openset[i] < curr){
                 curr = openset[i];
                 index = i;
             }
             
             if( openset[i].f == curr.f){
                 if(openset[i].g > curr.g){
                     curr = openset[i];
                     index = i;
                 }
             }
             
         }
         if(curr.point == goal){
             agent_path.push_back(curr.point);
             while(curr.startNode != 1){
                 for(AStarPlannerNode node: closedset){
                     if(node.point.x == curr.parentx && node.point.z == curr.parenty){
                         curr = node;
                         agent_path.push_back(curr.point);
                         break;
                     }
                 }
             }
             std::cout << "path:" << agent_path.size() <<std::endl;
             std::cout << "expanded node:" << closedset.size()+1 <<std::endl;
             std::reverse(agent_path.begin(), agent_path.end());
             return true;
         }
         
         openset.erase(openset.begin() + index);
         closedset.push_back(curr);
         
         neighbors = findNeighbor(curr.point, gSpatialDatabase);
         for(AStarPlannerNode neighbor: neighbors){
             if(closed(neighbor, closedset)){
                 continue;
             }
             
             double temp_g = curr.g + euclidean(curr.point, neighbor.point);
             
             
             
             if(temp_g < neighbor.g){
                 neighbor.g = temp_g;
                 neighbor.f = neighbor.g + W * heuristic(neighbor.point, goal);
                 neighbor.parentx = curr.point.x;
                 neighbor.parenty = curr.point.z;
                 if(!opened(neighbor, openset)){
                     openset.push_back(neighbor);
                 }
             }
         }
     }
     return false;
 }
Exemple #13
0
 double heuristic(Util::Point s, Util::Point g){
     if(EUCLIDEAN)
         return euclidean(s, g);
     else
         return manhattan(s, g);
 }
Exemple #14
0
/*MAIN FUNCTION*/
int main(int argc, char * * argv){
    /******************FILE READ******************/
    FILE * fp = fopen(argv[1],"r");
    int id,age,sex,marry,race,place,lang,job,money;
    int population,given_id;
    float delta1,delta2,alpha;
    fscanf(fp,"%d,%f,%f,%d,%f", &population,&delta1,&delta2,&given_id,&alpha);
    
    int index = 0;
    profile users[population];
    while(!feof(fp)){
        fscanf(fp, "%d,%d,%d,%d,%d,%d,%d,%d,%d", &id,&age,&sex,&marry,&race,&place,&lang,&job,&money);
        users[index] = create_profile(id,age,sex,marry,race,place,lang,job,money);
        index++;
    }
    /******************GRAPH MATRIX******************/
    double matrix[population][population];
    double matrix_weight[population][population];
    int dense[population][population];
    int sparse[population][population];
    int k = 0;
    //MATRIX
    int i = 0, j = 0;
    for(i = 0; i < population; i++){
        for(j = 0; j < population; j++){
            if(i == j){
                matrix[i][j] = 0;
            }else{
                matrix[i][j] = euclidean(users[i], users[j]);
            }
        }
    }
    
    //MATRIX_WEIGHT
    double max = findmax(population, matrix);
    for(i = 0; i < population; i++){
        for(j = 0; j < population; j++){
            matrix_weight[i][j] = 1 - matrix[i][j]/max;
        }
    }
    
    //DENSE
    for(i = 0; i < population; i++){
        for(j = 0; j < population; j++){
            if(i == j){
                dense[i][j] = -1;
            }else if((int)(matrix_weight[i][j]*100) > delta1*100){
                dense[i][j] = (int)(matrix_weight[i][j]*100);
            }else{
                dense[i][j] = -1;
            }
        }
    }
    
    //SPARSE
    for(i = 0; i < population; i++){
        for(j = 0; j < population; j++){
            if(i == j){
                sparse[i][j] = -1;
            }else if((int)(matrix_weight[i][j]*100) > delta2*100){
                sparse[i][j] = (int)(matrix_weight[i][j]*100);
            }else{
                sparse[i][j] = -1;
            }
        }
    }
    /******************DENSE PART******************/
    /*QUERY1*/
    int id_index = given_id - 1;
    int shortest = 100;
    int buffer[2048];
    for(i = 0; i < population; i++){
        buffer[i] = 0;
    }
    for(j = 0; j < population; j++){
        if(j != id_index && dense[id_index][j] > 0 && dense[id_index][j] < shortest){
            shortest = dense[id_index][j];
        }
    }
    for(j = 0; j < population; j++){
        if(j != id_index && dense[id_index][j] > 0 && dense[id_index][j] == shortest){
            buffer[j] = 1;
        }
    }
    printf("%.2f", shortest/100.00);
    for(j = 0; j < population; j++){
        if(buffer[j]){
            printf(",%d", j+1);
        }
    }
    printf("\n");
    /*QUERY2*/
    dijkstra(population, id_index, dense, (int)(alpha*100));
    /*QUERY3*/
    int immediate = find_im(population, id_index, dense, 3);
    /*QUERY4*/
    int twohop = find_2h(population, id_index, dense, 4);
    /*QUERY5*/
    immediate = 0;
    for(i = 0; i < population; i++){
        immediate += find_im(population, i, dense, 5);
    }
    printf("%.2f\n", immediate/(float)population);
    /*QUERY6*/
    twohop = 0;
    for(i = 0; i < population; i++){
        twohop += find_2h(population, i, dense, 5);
    }
    printf("%.2f\n", twohop/(float)population);
    /******************SPARSE PART******************/
    /*QUERY1*/
    printf("\n");
    shortest = 100;
    for(i = 0; i < population; i++){
        buffer[i] = 0;
    }
    for(j = 0; j < population; j++){
        if(j != id_index && sparse[id_index][j] > 0 && sparse[id_index][j] < shortest){
            shortest = sparse[id_index][j];
        }
    }
    for(j = 0; j < population; j++){
        if(j != id_index && sparse[id_index][j] > 0 && sparse[id_index][j] == shortest){
            buffer[j] = 1;
        }
    }
    printf("%.2f", shortest/100.00);
    for(j = 0; j < population; j++){
        if(buffer[j]){
            printf(",%d", j+1);
        }
    }
    printf("\n");
    /*QUERY2*/
    dijkstra(population, id_index, sparse, (int)(alpha*100));
    /*QUERY3*/
    immediate = find_im(population, id_index, sparse, 3);
    /*QUERY4*/
    twohop = find_2h(population, id_index, sparse, 4);
    /*QUERY5*/
    immediate = 0;
    for(i = 0; i < population; i++){
        immediate += find_im(population, i, sparse, 5);
    }
    printf("%.2f\n", immediate/(float)population);
    /*QUERY6*/
    twohop = 0;
    for(i = 0; i < population; i++){
        twohop += find_2h(population, i, sparse, 6);
    }
    printf("%.2f\n", twohop/(float)population);
    
    fclose(fp);
    return EXIT_SUCCESS;
}
Exemple #15
0
void KMplex::resample()
{
    std::cout << "KMplex::resample" << std::endl;

    // Build dissimilarity matrix for clustered data X
    const MATRIX& X(*(kmeans_->X_));
    D_.resize(X.rows(), X.rows());

    std::cout << "\nKMplex: (Allocation rule: "
              << (unsigned)alloc_ << ")" << std::endl;
    for(unsigned i = 0; i < kmeans_->K(); ++i)
    {
        const KMeans::Cluster& cluster(kmeans_->cluster(i));

        if(cluster.size() > 0)
        {
            unsigned NTr = trainingQuota(cluster);
            unsigned NTe = testingQuota(cluster);
            unsigned NVa = validatingQuota(cluster);

            std::cout << "Cluster (" << i << ") N: " << cluster.size() <<
                      " NTr: " << NTr <<
                      " NTe: " << NTe <<
                      " NVa: " << NVa << std::endl;

            // Calculate intra-cluster distances
            KEY indices = kmeans_->cluster(i).indices();
            for(KEY::iterator a = indices.begin(); a != indices.end(); ++a)
                for(KEY::iterator b = a + 1; b != indices.end(); ++b)
                {
                    D_[*a][*b] = euclidean(X[*a], X[*b]);
                    D_[*b][*a] = D_[*a][*b];
                }

            // Initialise samples
            KEY training, testing, validating;
            if(NTr > 0)
                initialise(indices, training);

            if(NTe > 0)
                initialise(indices, testing);

            if(NVa > 0)
                initialise(indices, validating);

            // Sample remaining points using Duplex sampling
            while(indices.size() > 0)
            {
                if(training.size() < NTr)
                    sample(indices, training);

                if(testing.size() < NTe)
                    sample(indices, testing);

                if(validating.size() < NVa)
                    sample(indices, validating);
            }

            // Add intra-cluster samples to respective global samples
            if(training.size() > 0)
                merge(training, trainingKey_);

            if(testing.size() > 0)
                merge(testing, testingKey_);

            if(validating.size() > 0)
                merge(validating, validatingKey_);
        }
    }

    allocate(trainingKey_, training_);
    allocate(testingKey_, testing_);
    allocate(validatingKey_, validating_);
}
Exemple #16
0
void distribute_neighbors(node_type *node, int node_num, node_type *rnp)
{
  int i, j;
  int *nn;
  int *nd_counter;
  int dist, min_dist, min_index, degree;
  node_type **tnp;

  nn = (int *)malloc(sizeof(int)*rnp->degree);
  if (!nn)
  {
    fprintf(stderr, "distribute_neighbors: no memory for nn (%d bytes)\n", (int)sizeof(int)*rnp->degree);
    exit(-1);
  }

  nd_counter = (int *)malloc(sizeof(int)*(node_num+1));
  if (!nd_counter)
  {
    fprintf(stderr, "distribute_neighbors: no memory for nd_counter (%d bytes)\n", (int)sizeof(int)*(node_num+1));
    exit(-1);
  }

  
  for (i=0; i<node_num; ++i) nd_counter[i] = 0;

  /*********************************************************/
  /* determine which neighbor goes to which expansion node */
  /*********************************************************/
  for (i=0; i<rnp->degree; ++i)
  {
    min_dist = euclidean(rnp->nnp[i], rnp);
    min_index = node_num;

    for (j=0; j<node_num; ++j)
    {
      dist = euclidean(rnp->nnp[i], &node[j]);
      if (dist < min_dist)
      {
        min_dist = dist;
        min_index = j;
      }
    }

    nn[i] = min_index;
    nd_counter[min_index]++;
  }

  /******************************/
  /* update the expansion nodes */
  /******************************/
  for (i=0; i<node_num; ++i)
  {
    tnp = (node_type **)malloc(sizeof(node_type *)*(node[i].degree+nd_counter[i]));
    if (!tnp)
    {
      fprintf(stderr, "distribute_neighbors: no memory for tnp (%d bytes)!\n", (int)sizeof(node_type *)*(node[i].degree+nd_counter[i]));
      exit(-1);
    }

    for (j=0; j<node[i].degree; ++j)
      tnp[j] = node[i].nnp[j];
    
    for (; j<node[i].degree+nd_counter[i]; ++j)
      tnp[j] = 0;

    free(node[i].nnp);
    node[i].nnp = tnp;
  }
  /*****************/
  /* the root node */
  /*****************/
  tnp = (node_type **)malloc(sizeof(node_type *)*nd_counter[node_num]);
  if (!tnp)
  { 
    fprintf(stderr, "distribute_neighbors: no memory for tnp (%d bytes)!\n", (int)sizeof(node_type *)*nd_counter[i]);
    exit(-1);
  } 


  min_index = 0;
  for (i=0; i<rnp->degree; ++i)
  {
    if (nn[i]<node_num)
    {
      degree = node[nn[i]].degree;
      node[nn[i]].nnp[degree] = rnp->nnp[i];
      ++node[nn[i]].degree;
    }
    else
      tnp[min_index++] = rnp->nnp[i];
  }

  if (min_index != nd_counter[node_num])
  {
    fprintf(stderr, "distribute_neighbors: fatal error! min_index=%d, nd_counter[%d]=%d!\n", min_index, node_num, nd_counter[node_num]);
    exit(-1);
  }

  free(rnp->nnp);
  rnp->nnp = tnp;
  rnp->degree = min_index;

  free(nn);
  free(nd_counter);
}