Exemple #1
0
/* In stem(p,i,j), p is a char pointer, and the string to be stemmed is from
   p[i] to p[j] inclusive. Typically i is zero and j is the offset to the last
   character of a string, (p[j+1] == '\0'). The stemmer adjusts the
   characters p[i] ... p[j] and returns the new end-point of the string, k.
   Stemming never increases word length, so i <= k <= j. To turn the stemmer
   into a module, declare 'stem' as extern, and delete the remainder of this
   file.
*/
static ssize_t
stem(char *s, size_t z)
{
	/* copy the parameters into statics */
	b = s;
	k = z - 1;
	k0 = 0;

	if (k <= k0 + 1) {
		/*-DEPARTURE-*/
		return k;
	}

	/* With this line, strings of length 1 or 2 don't go through the
	   stemming process, although no mention is made of this in the
	   published algorithm. Remove the line to match the published
	   algorithm. */
	step1ab();
	step1c();
	step2();
	step3();
	step4();
	step5();
	return k;
}
Exemple #2
0
static size_t stem(char *p, size_t i, size_t j) {

    b  = p;
    k  = j;
    k0 = i; /* copy the parameters into statics */

    /*
     * DEPARTURE: This next 'if' statement prevents strings of length 1 or 2 from
     * going through the stemming process, although no mention is made of this in the
     * published algorithm. Remove the line to match the publishedalgorithm.
     */

    if (k <= k0 + 1)
        return k;

    step1ab();
    if (k > k0) {
        step1c();
        step2();
        step3();
        step4();
        step5();
    }

    return k;
}
Exemple #3
0
/* In `stem(p, i, j)`, `p` is a `char` pointer, and the
 * string to be stemmed is from `p[i]` to
 * `p[j]` (inclusive).
 *
 * Typically, `i` is zero and `j` is the offset to the
 * last character of a string, `(p[j + 1] == '\0')`.
 * The stemmer adjusts the characters `p[i]` ... `p[j]`
 * and returns the new end-point of the string, `k`.
 *
 * Stemming never increases word length, so `i <= k <= j`.
 *
 * To turn the stemmer into a module, declare 'stem' as
 * extern, and delete the remainder of this file. */
int
stem(char *p, int index, int position) {
  /* Copy the parameters into statics. */
  b = p;
  k = position;
  k0 = index;

  if (k <= k0 + 1) {
    return k; /* --DEPARTURE-- */
  }

  /* With this line, strings of length 1 or 2 don't
   * go through the stemming process, although no
   * mention is made of this in the published
   * algorithm. Remove the line to match the published
   * algorithm. */
  step1ab();

  if (k > k0) {
    step1c();
    step2();
    step3();
    step4();
    step5();
  }

  return k;
}
Exemple #4
0
void Munkres::step4() {
	/* find an uncovered zero and prime it
	 * if now starred zeros in row goto step 5
	 * else cover row and uncover colum of starred zero
	 *
	 * once no uncovered exist goto step 6.
	 */
	bool done = false;
	int i = 0;
	int j = 0;
	int a;
	while (!done) {
		if (find_zero(&i, &j)) {
			if (!is_covered(i, j)) {
				prime(i, j);
				a = starred_in_row(i);
				if (a == -1) // if no starred zeros
				{
					done = true;
					step5(i, j);
				} else {
					uncover_col(a);
					cover_row(i);
				}

			}
		} else {
			done = true;
			step6( min_uncovered());
		}
	}
}
    String BrazilianStemmer::stem(const String& term)
    {
        // creates CT
        createCT(term);

        if (!isIndexable(CT))
            return L"";
        if (!isStemmable(CT))
            return CT;

        R1 = getR1(CT);
        R2 = getR1(R1);
        RV = getRV(CT);
        TERM = term + L";" + CT;

        bool altered = step1();
        if (!altered)
            altered = step2();

        if (altered)
            step3();
        else
            step4();

        step5();

        return CT;
    }
int stem(char * p, int i, int j)
{  b = p; k = j; k0 = i; /* copy the parameters into statics */
   if (k <= k0+1) return k; /*-DEPARTURE-*/

   /* With this line, strings of length 1 or 2 don't go through the
      stemming process, although no mention is made of this in the
      published algorithm. Remove the line to match the published
      algorithm. */

   step1ab(); step1c(); step2(); step3(); step4(); step5();
   return k;
}
extern int stem_ts(struct stemmer * z, char * b, int k)
{
   if (k <= 1) return k; /*-DEPARTURE-*/
   z->b = b; z->k = k; /* copy the parameters into z */

   /* With this line, strings of length 1 or 2 don't go through the
      stemming process, although no mention is made of this in the
      published algorithm. Remove the line to match the published
      algorithm. */

   step1ab(z); step1c(z); step2(z); step3(z); step4(z); step5(z);
   return z->k;
}
	 bool PorterStemmer::stem() {
    //i = strlen(b);
		 k = i -1;
    k0 = 0;
    if (k > k0+1) {
      step1(); step2(); step3(); step4(); step5(); step6();
    }
    // Also, a word is considered dirty if we lopped off letters
    // Thanks to Ifigenia Vairelles for pointing this out.
    if (i != k+1)
      dirty = true;
    i = k+1;
    return dirty;
  }
Exemple #9
0
/* nml: this function slightly modified to not require external stemmer 
 * structure or length count (accepts NUL-terminated term) */
void stem_porters(void *opaque, char *term) {
   struct stemmer z;

   z.b = term; z.k = str_len(term) - 1; /* copy the parameters into z */
   if (z.k <= 1) return; /*-DEPARTURE-*/

   /* With this line, strings of length 1 or 2 don't go through the
      stemming process, although no mention is made of this in the
      published algorithm. Remove the line to match the published
      algorithm. */

   step1ab(&z); step1c(&z); step2(&z); step3(&z); step4(&z); step5(&z);
   term[z.k + 1] = '\0';  /* zero-terminate string */
}
// --------------------------------------------------------------------------
// Function Name : step3
// Auther : chen chen
// Data : 2016-02-15
// --------------------------------------------------------------------------
void AssignmentProblemSolver::step3(int *assignment, double *distMatrix, bool *starMatrix, bool *newStarMatrix, bool *primeMatrix, bool *coveredColumns, bool *coveredRows, int nOfRows, int nOfColumns, int minDim)
{
    bool zerosFound;
    int row, col, starCol;
    zerosFound = true;
    while(zerosFound)
    {
        zerosFound = false;
        for(col = 0; col < nOfColumns; col++)
        {
            if(!coveredColumns[col])
            {
                for(row = 0; row < nOfRows; row++)
                {
                    if((!coveredRows[row]) && (distMatrix[row + nOfRows*col] == 0))
                    {
                        /* prime zero */
                        primeMatrix[row + nOfRows*col] = true;

                        /* find starred zero in current row */
                        for(starCol = 0; starCol < nOfColumns; starCol++)
                            if(starMatrix[row + nOfRows*starCol])
                            {
                                break;
                            }

                            if(starCol == nOfColumns) /* no starred zero found */
                            {
                                /* move to step 4 */
                                step4(assignment, distMatrix, starMatrix, newStarMatrix, primeMatrix, coveredColumns, coveredRows, nOfRows, nOfColumns, minDim, row, col);
                                return;
                            }
                            else
                            {
                                coveredRows[row]        = true;
                                coveredColumns[starCol] = false;
                                zerosFound              = true;
                                break;
                            }
                    }
                }
            }
        }
    }

    /* move to step 5 */
    step5(assignment, distMatrix, starMatrix, newStarMatrix, primeMatrix, coveredColumns, coveredRows, nOfRows, nOfColumns, minDim);
}
Exemple #11
0
static void step3(int *ix, int *mdist, mat_t mstar, mat_t nmstar,
		  mat_t mprime, col_t ccol, col_t crow, int nrows, int ncols,
		  int dmin)
{
	int zerosFound;
	int row, col, cstar;

	zerosFound = 1;
	while (zerosFound) {
		zerosFound = 0;
		for (col = 0; col < ncols; col++) {
			if (GET1(ccol, col))
				continue;
			for (row = 0; row < nrows; row++) {
				if (mdist[row + nrows * col] != 0)
					continue;
				if (GET1(crow, row))
					continue;

				/* prime zero */
				SET2(mprime, row, col);

				/* find starred zero in current row */
				for (cstar = 0; cstar < ncols; cstar++)
					if (GET2(mstar, row, cstar))
						break;

				if (cstar == ncols) { /* no starred zero */
					/* move to step 4 */
					step4(ix, mdist, mstar, nmstar,
					      mprime, ccol, crow, nrows, ncols,
					      dmin, row, col);
					return;
				} else {
					SET1(crow, row);
					CLEAR1(ccol, cstar);
					zerosFound = 1;
					break;
				}
			}
		}
	}

	/* move to step 5 */
	step5(ix, mdist, mstar, nmstar,
	      mprime, ccol, crow, nrows, ncols,
	      dmin);
}
Exemple #12
0
wchar_t*
s_stem(wchar_t *word) {
	wchar_t* copy;

	copy = malloc(sizeof(*copy) * (wcslen(word)+1));
	wcscpy(copy, word);	
	
	step1a(copy);
	step1b(copy);
	step1c(copy);
	step2(copy);
	step3(copy);
	step4(copy);
	step5(copy);
	
	return copy;
}
Exemple #13
0
KuhnMunkres::Indexes KuhnMunkres::calculate(const Grid &grid)
{
    grid_ = grid;
    const Dimensions dimensions = ensure_grid_is_square();
    size = static_cast<int>(grid_.size());
#ifdef USE_STL
    row_covered.resize(size, false);
    column_covered.resize(size, false);
#else
    row_covered.fill(false, size);
    column_covered.fill(false, size);
#endif
    z0_row = 0;
    z0_column = 0;
    path = make_grid(size * 2, static_cast<int>(ZERO));
    marked = make_grid(size, static_cast<int>(ZERO));

    int step = 1;
    while (step) {
        switch (step) {
            case 1: step = step1(); break;
            case 2: step = step2(); break;
            case 3: step = step3(); break;
            case 4: step = step4(); break;
            case 5: step = step5(); break;
            case 6: step = step6(); break;
            default: break;
        }
    }

    Indexes indexes;
    for (int row = 0; row < size; ++row) {
        for (int column = 0; column < size; ++column) {
            if ((row < dimensions.first) &&
                (column < dimensions.second) &&
                marked.at(row).at(column) == STAR)
#ifdef USE_STL
                indexes.push_back(std::make_pair(row, column));
#else
                indexes.push_back(qMakePair(row, column));
#endif
        }
    }
    return indexes;
}
Exemple #14
0
void step3(double *assignment, double *distMatrix, char *starMatrix, char *newStarMatrix, char *primeMatrix, char *coveredColumns, char *coveredRows, int nOfRows, int nOfColumns, int minDim)
{
	char zerosFound;
	int row, col, starCol;

	zerosFound = 1;
	while(zerosFound)
	{
		zerosFound = 0;
		for(col=0; col<nOfColumns; col++)
			if(!coveredColumns[col])
				for(row=0; row<nOfRows; row++)
					if((!coveredRows[row]) && (distMatrix[row + nOfRows*col] == 0))
					{
						/* prime zero */
						primeMatrix[row + nOfRows*col] = 1;

						/* find starred zero in current row */
						for(starCol=0; starCol<nOfColumns; starCol++)
							if(starMatrix[row + nOfRows*starCol])
								break;

						if(starCol == nOfColumns) /* no starred zero found */
						{
							/* move to step 4 */
							step4(assignment, distMatrix, starMatrix, newStarMatrix, primeMatrix, coveredColumns, coveredRows, nOfRows, nOfColumns, minDim, row, col);
							return;
						}
						else
						{
							coveredRows[row]        = 1;
							coveredColumns[starCol] = 0;
							zerosFound              = 1;
							break;
						}
					}
	}

	/* move to step 5 */
	step5(assignment, distMatrix, starMatrix, newStarMatrix, primeMatrix, coveredColumns, coveredRows, nOfRows, nOfColumns, minDim);
}
    String FrenchStemmer::stem(const String& term)
    {
        if (!isStemmable(term))
            return term;
        
        // Use lowercase for medium stemming.
        stringBuffer = StringUtils::toLower(term);

        // reset the booleans
        modified = false;
        suite = false;

        treatVowels(stringBuffer);

        setStrings();

        step1();

        if (!modified || suite)
        {
            if (!RV.empty())
            {
                suite = step2a();
                if (!suite)
                    step2b();
            }
        }

        if (modified || suite)
            step3();
        else
            step4();

        step5();

        step6();

        return stringBuffer;
    }
Exemple #16
0
void 
Munkres::solve(Matrix<double> &m) {
  // Linear assignment problem solution
  // [modifies matrix in-place.]
  // matrix(row,col): row major format assumed.

  // Assignments are remaining 0 values
  // (extra 0 values are replaced with -1)
#ifdef DEBUG
  std::cout << "Munkres input matrix:" << std::endl;
  for ( int row = 0 ; row < m.rows() ; row++ ) {
    for ( int col = 0 ; col < m.columns() ; col++ ) {
      std::cout.width(8);
      std::cout << m(row,col) << ",";
    }
    std::cout << std::endl;
  }
  std::cout << std::endl;
#endif

  double highValue = 0;
  for ( int row = 0 ; row < m.rows() ; row++ ) {
    for ( int col = 0 ; col < m.columns() ; col++ ) {
      if ( m(row,col) != INFINITY && m(row,col) > highValue )
        highValue = m(row,col);
    }
  }
  highValue++;
  
  for ( int row = 0 ; row < m.rows() ; row++ )
    for ( int col = 0 ; col < m.columns() ; col++ )
      if ( m(row,col) == INFINITY )
        m(row,col) = highValue;

  bool notdone = true;
  int step = 1;

  this->matrix = m;
  // STAR == 1 == starred, PRIME == 2 == primed
  mask_matrix.resize(matrix.rows(), matrix.columns());

  row_mask = new bool[matrix.rows()];
  col_mask = new bool[matrix.columns()];
  for ( int i = 0 ; i < matrix.rows() ; i++ ) {
    row_mask[i] = false;
  }

  for ( int i = 0 ; i < matrix.columns() ; i++ ) {
    col_mask[i] = false;
  }

  while ( notdone ) {
    switch ( step ) {
      case 0:
        notdone = false;
        break;
      case 1:
        step = step1();
        break;
      case 2:
        step = step2();
        break;
      case 3:
        step = step3();
        break;
      case 4:
        step = step4();
        break;
      case 5:
        step = step5();
        break;
    }
  }

  // Store results
  for ( int row = 0 ; row < matrix.rows() ; row++ )
    for ( int col = 0 ; col < matrix.columns() ; col++ )
      if ( mask_matrix(row,col) == STAR )
        matrix(row,col) = 0;
      else
        matrix(row,col) = -1;

#ifdef DEBUG
  std::cout << "Munkres output matrix:" << std::endl;
  for ( int row = 0 ; row < matrix.rows() ; row++ ) {
    for ( int col = 0 ; col < matrix.columns() ; col++ ) {
      std::cout.width(1);
      std::cout << matrix(row,col) << ",";
    }
    std::cout << std::endl;
  }
  std::cout << std::endl;
#endif

  m = matrix;

  delete [] row_mask;
  delete [] col_mask;
}
Exemple #17
0
/*
 *
 * Linear assignment problem solution
 * [modifies matrix in-place.]
 * matrix(row,col): row major format assumed.
 *
 * Assignments are remaining 0 values
 * (extra 0 values are replaced with -1)
 *
 */
void 
Munkres::solve(Matrix<double> &m) {
  const size_t rows = m.rows(),
            columns = m.columns(),
               size = std::max(rows, columns);

#ifdef DEBUG
  std::cout << "Munkres input matrix:" << std::endl;
  for ( size_t row = 0 ; row < rows ; row++ ) {
    for ( size_t col = 0 ; col < columns ; col++ ) {
      std::cout.width(8);
      std::cout << m(row, col) << ",";
    }
    std::cout << std::endl;
  }
  std::cout << std::endl;
#endif

  // Copy input matrix
  this->matrix = m;

  if ( rows != columns ) {
    // If the input matrix isn't square, make it square
    // and fill the empty values with the largest value present
    // in the matrix.
    matrix.resize(size, size, matrix.max());
  }


  // STAR == 1 == starred, PRIME == 2 == primed
  mask_matrix.resize(size, size);

  row_mask = new bool[size];
  col_mask = new bool[size];
  for ( size_t i = 0 ; i < size ; i++ ) {
    row_mask[i] = false;
  }

  for ( size_t i = 0 ; i < size ; i++ ) {
    col_mask[i] = false;
  }

  // Prepare the matrix values...

  // If there were any infinities, replace them with a value greater
  // than the maximum value in the matrix.
  replace_infinites(matrix);

  minimize_along_direction(matrix, false);
  minimize_along_direction(matrix, true);

  // Follow the steps
  int step = 1;
  while ( step ) {
    switch ( step ) {
      case 1:
        step = step1();
        // step is always 2
        break;
      case 2:
        step = step2();
        // step is always either 0 or 3
        break;
      case 3:
        step = step3();
        // step in [3, 4, 5]
        break;
      case 4:
        step = step4();
        // step is always 2
        break;
      case 5:
        step = step5();
        // step is always 3
        break;
    }
  }

  // Store results
  for ( size_t row = 0 ; row < size ; row++ ) {
    for ( size_t col = 0 ; col < size ; col++ ) {
      if ( mask_matrix(row, col) == STAR ) {
        matrix(row, col) = 0;
      } else {
        matrix(row, col) = -1;
      }
    }
  }

#ifdef DEBUG
  std::cout << "Munkres output matrix:" << std::endl;
  for ( size_t row = 0 ; row < rows ; row++ ) {
    for ( size_t col = 0 ; col < columns ; col++ ) {
      std::cout.width(1);
      std::cout << matrix(row, col) << ",";
    }
    std::cout << std::endl;
  }
  std::cout << std::endl;
#endif


  // Remove the excess rows or columns that we added to fit the
  // input to a square matrix.
  matrix.resize(rows, columns);

  m = matrix;

  delete [] row_mask;
  delete [] col_mask;
}
void _HungarianAlgorithm<VAL>::solve(std::map<int,int>& o_result)
{
	for(int i=0; i<m_size; i++)
		m_mask[i] = 0;

	int step = 1;
	int nbOps = 0;
	while(step > 0)
	{
		//std::cout << "===== step " << step << " =====" << std::endl;

		if(nbOps%5000 == 0 && nbOps != 0)
			std::cout << "still alive ! (nbOps = " << nbOps << ")" << std::endl;

		switch(step)
		{
			case 1:
				step = step1();
				break;
			case 2:
				step = step2();
				break;
			case 3:
				step = step3();
				break;
			case 4:
				step = step4();
				break;
			case 5:
				step = step5();
				break;
			default:
				step = 0;
		}

		nbOps++;

//		 for(int j=0; j<m_height; j++)
//		 {
//			 for(int i=0; i<m_width; i++)
//			 {
//				 std::cout << m_matrix[j*m_height+i] << "\t";
//			 }
//			 std::cout << std::endl;
//		 }
//		 for(int j=0; j<m_height; j++)
//		 {
//			 for(int i=0; i<m_width; i++)
//			 {
//				 std::cout << m_mask[j*m_height+i] << "\t";
//			 }
//			 std::cout << std::endl;
//		}
//		std::cout<<"row : ";
//		for(int j=0; j<m_height; j++)
//			std::cout<<m_mask_row[j]<<" ";
//		std::cout<<std::endl<<"col : ";
//		for(int j=0; j<m_width; j++)
//			std::cout<<m_mask_col[j]<<" ";
//		std::cout<<std::endl;
	}

	for(int j=0; j<m_height; j++)
		for(int i=0; i<m_width; i++)
			if(m_mask[j*m_height+i] == 1)
				o_result[i] = j;
}
//Find a noncovered zero and prime it
//if there isn't a starred zero in its row then goto step 5
//if there is then cover the current row and uncover the column with the starred zero
//then look for more uncovered zeros
//if there are no uncovered zeros we go to step 6
bool munkres::step4(void)
{
	//To find the smallest uncovered value
	int smallest = 0;
	
	//iterate through rows
	for (int i = 0; i < num_rows; i++)
	{
		//if the current row isn't covered
		if (!row_cov[i])
		{
			
			//set smallest = first element in the current row
			while (smallest == 0){
				smallest = cell_array[i][0].weight;
				//if the first element is 0 then increase the row
				if (smallest == 0)
				{
					if (i < num_rows-1)
						i++;
					else
						break;
				}
			}
			
			//iterate through columns
			for (int j = 0; j < num_columns; j++)
			{
				//if the column and row aren't covered, the current index is zero,
				//and there isn't a star in the current row,
				//then prime the current index and go to step 5
				if (!column_cov[j] && !row_cov[i] && cell_array[i][j].weight == 0 && !row_starred[i])
				{
					//prime current index
					cell_array[i][j].primed = true;
					
					//if a primed zero with no star in the row exists
					//goto step 5
					if (diag_on)
					{
					std::cerr << "Step 4: " << i << ",  " << j <<std::endl;
					diagnostic(6);
					}
					return step5(i, j);
					
					
					
				}
				
				//if the column and row aren't covered, the current index is zero,
				//and there is a star in the current row,
				//then prime the current index, cover the current row,
				//and uncover the column with the starred zero
				//also reset indeces to 0 to look for zeros that may have been uncovered
				else if (!column_cov[j] && !row_cov[i] && cell_array[i][j].weight == 0)
				{
					//prime current index
					cell_array[i][j].primed = true;
					//cover current row
					row_cov[i] = true;
					//uncover column with starred zero
					column_cov[find_star_row(i)] = false;
					i = 0;
					j = 0;
				}
				
				//if the column isn't covered, the current index isn't zero,
				//and the current index is smaller than smallest so far,
				//then set smallest == current index
				else if (!column_cov[j] && cell_array[i][j].weight != 0 && cell_array[i][j].weight < smallest)
				{
					//set smallest == current index
					smallest = cell_array[i][j].weight;
				}
			}
		}
	}
	
	if (diag_on)
	{
		std::cerr << "Step 4" << std::endl;
		diagnostic(6);
	}
	
	//if we don't go to step 5 then go to step 6
	return step6(smallest);
}
/*!
	Linear assignment problem solution
	[modifies matrix in-place.]
	matrix(row,col): row major format assumed.

	Assignments are remaining 0 values on 'matrix'
	(extra 0 values are replaced with -1)

	The correspondence results are stored in the variables
	row2colMap and col2rowMap s.t.

		row2colMap(r) = c means that col c is assigned to row r
		col2rowMap(c) = r means that row r is assigned to column c

	@param initMaps if true the row/col maps are init to invalid values,
	                which help check the mapping by calling CheckMapping()
*/
double Munkres::Solve(const DoubleMatrix& m, UIntVector* row2colMap, 
					UIntVector* col2rowMap) 
{
#ifdef MUNKRES_DEBUG
	std::cout << "Munkres input matrix:" << std::endl;

	for ( int row = 0 ; row < m.rows() ; row++ ) 
	{
		for ( int col = 0 ; col < m.columns() ; col++ ) {
			std::cout.width(8);
			std::cout << m(row,col) << ",";
		}
		std::cout << std::endl;
	}
	std::cout << std::endl;
#endif

	bool notdone = true;
	int step = 1;

	// Copy m, because it need to be modified
	matrix = m;

	// Z_STAR == 1 == starred, Z_PRIME == 2 == primed
	mask_matrix.set_size(matrix.rows(), matrix.columns());
	mask_matrix.fill(0);

	row_mask = new bool[matrix.rows()];
	col_mask = new bool[matrix.columns()];

	for ( unsigned i = 0 ; i < matrix.rows() ; i++ ) {
		row_mask[i] = false;
	}

	for ( unsigned i = 0 ; i < matrix.columns() ; i++ ) {
		col_mask[i] = false;
	}

	while ( notdone ) {
		switch ( step ) {
			case 0:
				notdone = false;
				break;
			case 1:
				step = step1();
				break;
			case 2:
				step = step2();
				break;
			case 3:
				step = step3();
				break;
			case 4:
				step = step4();
				break;
			case 5:
				step = step5();
				break;
		}
	}

	delete[] row_mask;
	delete[] col_mask;

	// Store results
	/*
	for ( unsigned row = 0 ; row < matrix.rows() ; row++ )
		for ( unsigned col = 0 ; col < matrix.columns() ; col++ )
			if ( mask_matrix(row,col) == Z_STAR )
				matrix(row,col) = 0;
			else
				matrix(row,col) = -1;
				
	#ifdef MUNKRES_DEBUG
		std::cout << "Munkres output matrix:" << std::endl;
		for ( int row = 0 ; row < matrix.rows() ; row++ ) {
			for ( int col = 0 ; col < matrix.columns() ; col++ ) {
				std::cout.width(1);
				std::cout << matrix(row,col) << ",";
			}
			std::cout << std::endl;
		}
		std::cout << std::endl;
	#endif		
	*/

	// Store results in the row and col maps s.t.
	// row2colMap(r) = c means that col c is assigned to row r
	// col2rowMap(c) = r means that row r is assigned to column c

	// Init vector to "known" invalid values
	if (row2colMap)
	{
		row2colMap->set_size(matrix.rows());
		row2colMap->fill(UINT_MAX);
	}

	// Init vector to "known" invalid values
	if (col2rowMap)
	{
		col2rowMap->set_size(matrix.cols());
		col2rowMap->fill(UINT_MAX);
	}

	double matchCost = 0;

	for (unsigned row = 0 ; row < matrix.rows() ; ++row)
	{
		for (unsigned col = 0 ; col < matrix.columns() ; ++col)
		{
			if (mask_matrix(row,col) == Z_STAR)
			{
				if (row2colMap)
					(*row2colMap)[row] = col;

				if (col2rowMap)
					(*col2rowMap)[col] = row;

				matchCost += m[row][col]; // use given 'm' not 'matrix'!
			}
		}
	}

	return matchCost;
}
Exemple #21
0
/*
 *
 * Linear assignment problem solution
 * [modifies matrix in-place.]
 * matrix(row,col): row major format assumed.
 *
 * Assignments are remaining 0 values
 * (extra 0 values are replaced with -1)
 *
 */
void
Munkres::solve(cv::Mat_<int> &m) {
    const unsigned int rows = m.rows,
    columns = m.cols,
    size = std::max<unsigned int>(rows, columns);
    
    if(isDiag) {
        std::cout << "Munkres input matrix:" << std::endl;
        for ( unsigned int row = 0 ; row < rows ; row++ ) {
            for ( unsigned int col = 0 ; col < columns ; col++ ) {
                std::cout.width(4);
                std::cout << m(row, col) << ",";
            }
            std::cout << std::endl;
        }
        std::cout << std::endl;
    }
    
    bool notdone = true;
    int step = 1;
    
    // Copy input matrix
    this->matrix = m;
    
    if ( rows != columns ) {
        // If the input matrix isn't square, make it square
        // and fill the empty values with the largest value present
        // in the matrix.
        extendMat(matrix, size, size, maxValue(matrix));
    }
    
    
    // STAR == 1 == starred, PRIME == 2 == primed
    //    mask_matrix.resize(size, size);
    extendMat(mask_matrix, size, size);
    
    row_mask = new bool[size];
    col_mask = new bool[size];
    for ( unsigned int i = 0 ; i < size ; i++ ) {
        row_mask[i] = false;
    }
    
    for ( unsigned int i = 0 ; i < size ; i++ ) {
        col_mask[i] = false;
    }
    
    // Prepare the matrix values...
    
    // If there were any infinities, replace them with a value greater
    // than the maximum value in the matrix.
    replace_infinites(matrix);
    
    minimize_along_direction(matrix, false);
    minimize_along_direction(matrix, true);
    
    // Follow the steps
    while ( notdone ) {
        switch ( step ) {
            case 0:
                notdone = false;
                // end the step flow
                break;
            case 1:
                step = step1();
                // step is always 2
                break;
            case 2:
                step = step2();
                // step is always either 0 or 3
                break;
            case 3:
                step = step3();
                // step in [3, 4, 5]
                break;
            case 4:
                step = step4();
                // step is always 2
                break;
            case 5:
                step = step5();
                // step is always 3
                break;
        }
    }
    
    // Store results
    for ( unsigned int row = 0 ; row < size ; row++ ) {
        for ( unsigned int col = 0 ; col < size ; col++ ) {
            if ( mask_matrix(row, col) == STAR ) {
                matrix(row, col) = 0;
            } else {
                matrix(row, col) = -1;
            }
        }
    }
    
    if(isDiag) {
        std::cout << "Munkres output matrix:" << std::endl;
        for ( unsigned int row = 0 ; row < rows ; row++ ) {
            for ( unsigned int col = 0 ; col < columns ; col++ ) {
                std::cout.width(2);
                std::cout << matrix(row, col) << ",";
            }
            std::cout << std::endl;
        }
        std::cout << std::endl;
    }
    
    
    // Remove the excess rows or columns that we added to fit the
    // input to a square matrix.
    //    matrix.resize(rows, columns);
    extendMat(matrix, rows, columns);
    
    m = matrix;
    
    delete [] row_mask;
    delete [] col_mask;
}
Exemple #22
0
// usage: smp <IP address to connect to>
int main( int argc, char** argv )
{
    if ( argc != 2 ) /* argc should be 2 for correct execution */
    {
        printf( "usage:\n\tsmp <ipaddress>\n\tsmp server\n" );
        return EXIT_FAILURE;
    }
    
    int bServerMode = strstr( "server", argv[ 1 ] ) != 0;

    setup();
    
    unsigned char holder[ BUFFER_SIZE ];
    memset( holder, 0x00, BUFFER_SIZE );
    
    if ( !bServerMode )
    {
        // we are talking to the server at ip address argv[ 1 ]
        char input_string[ 256 ];
        printf( "Enter a shared secret: " );
        readLine( input_string, 256 );
        // TESTCODE: strcpy( input_string, "testme" );
        secret = binEncode( input_string, strlen( input_string ) );
        
        /*****************************************************/
        /*****************************************************/
        /*  Do Step 1 and send to other side */
        /*****************************************************/
        /*****************************************************/
        int len = step1( holder, BUFFER_SIZE );
        
        int serverfd = connect_to_server( argv[ 1 ] );
        if ( serverfd == 1 )
            return EXIT_FAILURE;
        
        write_to_server( serverfd, holder, len );
        // dumpBuff( holder, len );
        
        /*****************************************************/
        /*****************************************************/
        /*  Get results from other side. */
        /*  Other side performed Step 2. */
        /*****************************************************/
        /*****************************************************/
        memset( holder, 0x00, BUFFER_SIZE );
        len = revc_from_server( serverfd, holder, BUFFER_SIZE );
        // dumpBuff( holder, len );
        
        /*****************************************************/
        /*****************************************************/
        /*  Do Step 3 and send to the other side */
        /*****************************************************/
        /*****************************************************/
        step3( holder, BUFFER_SIZE );
        write_to_server( serverfd, holder, len );
        
        /*****************************************************/
        /*****************************************************/
        /*  Get bytes from other side and do Step 5 */
        /*****************************************************/
        /*****************************************************/
        memset( holder, 0x00, BUFFER_SIZE );
        len = revc_from_server( serverfd, holder, BUFFER_SIZE );
        // dumpBuff( holder, len );
        
        step5( holder, len );
        
        disconnect_from_server( serverfd );
    }
    else    // we are in server mode, other side will send us data first
    {
        int listenfd = listen_server();
        /*if ( listenfd == 1 )
            return EXIT_FAILURE;
        TODO: error checking
        */
        
        char input_string[ 256 ];
        printf( "Enter a shared secret: " );
        readLine( input_string, 256 );
        // TESTCODE: strcpy( input_string, "testme" );
        secret = binEncode( input_string, strlen( input_string ) );
        
        int len = revc_from_server( listenfd, holder, BUFFER_SIZE );
        // dumpBuff( holder, BUFFER_SIZE);
        
        /*****************************************************/
        /*****************************************************/
        /*  Do Step 2 and send to other side */
        /*****************************************************/
        /*****************************************************/
        len = step2( holder, BUFFER_SIZE  );
        write_to_server( listenfd, holder, len );

        len = revc_from_server( listenfd, holder, BUFFER_SIZE );
        // dumpBuff( holder, len );
        
        /*****************************************************/
        /*****************************************************/
        /*  Do Step 4 and send to other side */
        /*****************************************************/
        /*****************************************************/
        len = step4( holder, BUFFER_SIZE );
        write_to_server( listenfd, holder, len );
        
        disconnect_from_server( listenfd );
    }
    
    
    if ( match == 1 )
        printf( "Secrets match\n" );
    else
        printf( "Secrets do not match\n");
    
    cleanup();
    return EXIT_SUCCESS;
}