예제 #1
0
float CEmdWrapper::emd(signature_t *Signature1, signature_t *Signature2, 
					   flow_t *Flow, int *FlowSize){
	int itr=0;
	float totalCost;
	float w;
	node2_t *XP;
	flow_t *FlowP;
	node1_t U[MAX_SIG_SIZE1], V[MAX_SIG_SIZE1];

	w = init(Signature1, Signature2);

	if (_n1 > 1 && _n2 > 1){/* IF _n1 = 1 OR _n2 = 1 THEN WE ARE DONE */
		for (itr = 1; itr < MAX_ITERATIONS; itr++){
			/* FIND BASIC VARIABLES */
			findBasicVariables(U, V);
			
			/* CHECK FOR OPTIMALITY */
			if (isOptimal(U, V)){
				//printf("itr: %d\n",itr);
				break;
			}
			
			/* IMPROVE SOLUTION */
			newSol();

		}
		
		if (itr == MAX_ITERATIONS)
			fprintf(stderr, "emd: Maximum number of iterations has been reached (%d)\n",
			MAX_ITERATIONS);
    }
	
	/* COMPUTE THE TOTAL FLOW */
	totalCost = 0;
	if (Flow != NULL)
		FlowP = Flow;
	for(XP=_X; XP < _EndX; XP++){
		if (XP == _EnterX)  /* _EnterX IS THE EMPTY SLOT */
			continue;
		if (XP->i == Signature1->n || XP->j == Signature2->n)  /* DUMMY FEATURE */
			continue;
		
		if (XP->val == 0)  /* ZERO FLOW */
			continue;
		
		totalCost += XP->val * _C[XP->i][XP->j];
		if (Flow != NULL){
			FlowP->from = XP->i;
			FlowP->to = XP->j;
			FlowP->amount = XP->val;
			FlowP++;
		}
    }
	
	if (Flow != NULL)
		*FlowSize = FlowP-Flow;
	
	/* RETURN THE NORMALIZED COST == EMD */
	return (float)(totalCost / w);
}
예제 #2
0
파일: emd.cpp 프로젝트: kjarosz/cuda_udat
float emd(signature_t *Signature1, signature_t *Signature2,
	  float (*Dist)(feature_t *, feature_t *),
	  flow_t *Flow, int *FlowSize)
{
  int itr;
  double totalCost;
  float w;
  node2_t *XP;
  flow_t *FlowP;
  node1_t U[MAX_SIG_SIZE1], V[MAX_SIG_SIZE1];

  w = init(Signature1, Signature2, Dist);

#if DEBUG_LEVEL > 1
  printf("\nINITIAL SOLUTION:\n");
  printSolution();
#endif
 
  if (_n1 > 1 && _n2 > 1)  /* IF _n1 = 1 OR _n2 = 1 THEN WE ARE DONE */
    {
      for (itr = 1; itr < MAX_ITERATIONS; itr++)
	{
	  /* FIND BASIC VARIABLES */
	  findBasicVariables(U, V);
	  
	  /* CHECK FOR OPTIMALITY */
	  if (isOptimal(U, V))
	    break;
	  
	  /* IMPROVE SOLUTION */
	  newSol();
	  
#if DEBUG_LEVEL > 1
	  printf("\nITERATION # %d \n", itr);
	  printSolution();
#endif
	}

      if (itr == MAX_ITERATIONS)
	fprintf(stderr, "emd: Maximum number of iterations has been reached (%d)\n",
		MAX_ITERATIONS);
    }

  /* COMPUTE THE TOTAL FLOW */
  totalCost = 0;
  if (Flow != NULL)
    FlowP = Flow;
  for(XP=_X; XP < _EndX; XP++)
    {
      if (XP == _EnterX)  /* _EnterX IS THE EMPTY SLOT */
	continue;
      if (XP->i == Signature1->n || XP->j == Signature2->n)  /* DUMMY FEATURE */
	continue;
      
      if (XP->val == 0)  /* ZERO FLOW */
	continue;

      totalCost += (double)XP->val * _C[XP->i][XP->j];
      if (Flow != NULL)
	{
	  FlowP->from = XP->i;
	  FlowP->to = XP->j;
	  FlowP->amount = XP->val;
	  FlowP++;
	}
    }
  if (Flow != NULL)
    *FlowSize = FlowP-Flow;

#if DEBUG_LEVEL > 0
  printf("\n*** OPTIMAL SOLUTION (%d ITERATIONS): %f ***\n", itr, totalCost);
#endif

  /* RETURN THE NORMALIZED COST == EMD */
  return (float)(totalCost / w);
}