Ejemplo n.º 1
0
/**********************
   init
**********************/
static float init(signature_t *Signature1, signature_t *Signature2, 
		  float (*Dist)(feature_t *, feature_t *))
{
  int i, j;
  double sSum, dSum, diff;
  feature_t *P1, *P2;
  double S[MAX_SIG_SIZE1], D[MAX_SIG_SIZE1];
 
  _n1 = Signature1->n;
  _n2 = Signature2->n;

  if (_n1 > MAX_SIG_SIZE || _n2 > MAX_SIG_SIZE)
    {
      fprintf(stderr, "emd: Signature size is limited to %d\n", MAX_SIG_SIZE);
      exit(1);
    }
  
  /* COMPUTE THE DISTANCE MATRIX */
  _maxC = 0;
  for(i=0, P1=Signature1->Features; i < _n1; i++, P1++)
    for(j=0, P2=Signature2->Features; j < _n2; j++, P2++) 
      {
	_C[i][j] = Dist(P1, P2);
	if (_C[i][j] > _maxC)
	  _maxC = _C[i][j];
      }
	
  /* SUM UP THE SUPPLY AND DEMAND */
  sSum = 0.0;
  for(i=0; i < _n1; i++)
    {
      S[i] = Signature1->Weights[i];
      sSum += Signature1->Weights[i];
      _RowsX[i] = NULL;
    }
  dSum = 0.0;
  for(j=0; j < _n2; j++)
    {
      D[j] = Signature2->Weights[j];
      dSum += Signature2->Weights[j];
      _ColsX[j] = NULL;
    }

  /* IF SUPPLY DIFFERENT THAN THE DEMAND, ADD A ZERO-COST DUMMY CLUSTER */
  diff = sSum - dSum;
  if (fabs(diff) >= EPSILON * sSum)
    {
      if (diff < 0.0)
	{
	  for (j=0; j < _n2; j++)
	    _C[_n1][j] = 0;
	  S[_n1] = -diff;
	  _RowsX[_n1] = NULL;
	  _n1++;
	}
      else
	{
	  for (i=0; i < _n1; i++)
	    _C[i][_n2] = 0;
	  D[_n2] = diff;
	  _ColsX[_n2] = NULL;
	  _n2++;
	}
    }

  /* INITIALIZE THE BASIC VARIABLE STRUCTURES */
  for (i=0; i < _n1; i++)
    for (j=0; j < _n2; j++)
	_IsX[i][j] = 0;
  _EndX = _X;
   
  _maxW = sSum > dSum ? sSum : dSum;

  /* FIND INITIAL SOLUTION */
  russel(S, D);

  _EnterX = _EndX++;  /* AN EMPTY SLOT (ONLY _n1+_n2-1 BASIC VARIABLES) */

  return (float)(sSum > dSum ? dSum : sSum);
}
Ejemplo n.º 2
0
float CEmdWrapper::init(signature_t *Signature1, signature_t *Signature2){
	int i, j;
	float sSum, dSum, diff;
	feature_t *P1, *P2;
	float S[MAX_SIG_SIZE1], D[MAX_SIG_SIZE1];
	
	_n1 = Signature1->n;
	_n2 = Signature2->n;
	
	if (_n1 > MAX_SIG_SIZE || _n2 > MAX_SIG_SIZE){
		fprintf(stderr, "emd: Signature size is limited to %d\n", MAX_SIG_SIZE);
		exit(1);
    }
	
	_maxC = 0;
	
	float DeltaX,DeltaY;
	for(i = 0, P1 = Signature1->Features; i < _n1; i++, P1++){
		for(j = 0, P2 = Signature2->Features; j < _n2; j++, P2++){
			DeltaX=(float)fabs(P1->px-P2->px);
			DeltaY=(float)fabs(P1->py-P2->py);
			
			DeltaX=DeltaX>110?110:DeltaX;
			DeltaY=DeltaY>350?350:DeltaY;
			
			int IndexX=(int)((DeltaX+0.25)/5);
			int IndexY=(int)((DeltaY+0.25)/5);
			
			int Index=IndexX*71+IndexY;
			_C[i][j]=(float)sqrt(DeltaX*DeltaX+DeltaY*DeltaY);
			
			if (_C[i][j] > _maxC)
				_maxC = _C[i][j];
		}
	}

	memcpy(S,Signature1->Weights,sizeof(float)*_n1);
	memcpy(D,Signature2->Weights,sizeof(float)*_n2);
	
	sSum = 0.0;
	for(i=0; i < _n1; i++){
		sSum += Signature1->Weights[i];
		_RowsX[i] = NULL;
	}
	
	dSum = 0.0;
	for(j=0; j < _n2; j++){
		dSum += Signature2->Weights[j];
		_ColsX[j] = NULL;
	}
	
	diff = sSum - dSum;
	if (fabs(diff) >= EPSILON * sSum){
		if (diff < 0.0){
			memset(_C[_n1],0,sizeof(float)*_n2);
			S[_n1] = -diff;
			_RowsX[_n1] = NULL;
			_n1++;
		}else{
			for (i=0; i < _n1; i++)
				_C[i][_n2] = 0;
			D[_n2] = diff;
			_ColsX[_n2] = NULL;
			_n2++;
		}
	}
	
	memset(_IsX[0],0,MAX_SIG_SIZE1*MAX_SIG_SIZE1);
	
	_EndX = _X;
	
	_maxW = sSum > dSum ? sSum : dSum;
	
	russel(S, D);
	
	_EnterX = _EndX++;  /* AN EMPTY SLOT (ONLY _n1+_n2-1 BASIC VARIABLES) */
	
	return sSum > dSum ? dSum : sSum;
}