All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.citec.tcs.alignment.sets.GreedySetAlignmentFullAlgorithm Maven / Gradle / Ivy

Go to download

This module provides algorithms to compare sets, that is, order-invariant lists. These algorithms are implementations of the AlignmentAlgorithm interface defined in the algorithms module. In particular, this module contains the StrictSetAlignmentScoreAlgorithm for computing the cost of the optimal unordered alignment of two sets, the StrictSetAlignmentFullAlgorithm which provides the Alignment itself as well, and the GreedySetAlignmentScoreAlgorithm as well as the GreedySetAlignmentFullAlgorithm for computing a potentially sub-optimal but faster alignment of two sets. The optimal alignments rely on the HungarianAlgorithm for solving the assignment problem in bipartite graphs. Here, we rely on the implementation provided by Kevin L. Stern which is provided within this distribution.

The newest version!
/* 
 * TCS Alignment Toolbox Version 3
 * 
 * Copyright (C) 2016-2017
 * Benjamin Paaßen
 * AG Machine Learning
 * Centre of Excellence Cognitive Interaction Technology (CITEC)
 * University of Bielefeld
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see .
 */
package de.citec.tcs.alignment.sets;

import de.citec.tcs.alignment.Alignment;
import de.citec.tcs.alignment.Operation;
import de.citec.tcs.alignment.comparators.Comparator;
import de.citec.tcs.alignment.comparators.OperationType;
import static de.citec.tcs.alignment.sets.AbstractStrictSetAlignmentAlgorithm.distance;
import static de.citec.tcs.alignment.sets.AbstractStrictSetAlignmentAlgorithm.normalizeDissimilarity;
import java.util.List;

/**
 * This is an implementation of the Hungarian algorithm for the optimal set alignment. For more
 * details, please refer to the AbstractStrictSetAlignmentAlgorithm. Note that this
 * AlignmentAlgorithm returns the normalized alignment distance which is guaranteed to lie
 * in the range [0, 1].
 *
 *
 * @author Benjamin Paassen - bpaassen(at)techfak.uni-bielefeld.de
 * @param  The node class for the left input set.
 * @param  The node class for the right input set.
 */
public class GreedySetAlignmentFullAlgorithm extends AbstractGreedySetAlignmentAlgorithm {

	public GreedySetAlignmentFullAlgorithm(Comparator comparator) {
		super(comparator, Alignment.class);
	}

	@Override
	public Alignment transformToResult(int[] assignment, double[][] repCosts, double[] delCosts, double[] insCosts, List a, List b) {
		// retrieve the overall distance
		final double d = distance(repCosts, delCosts, insCosts, assignment);
		final double normalized_d = normalizeDissimilarity(d, a, b);
		// construct the alignment object
		final Alignment ali = new Alignment<>(getComparator(), a, b, normalized_d);
		// construct the alignment operations; deletions and replacements are first, then insertions
		final int m = a.size();
		final int n = b.size();
		for (int i = 0; i < m; i++) {
			final OperationType type;
			final X left = a.get(i);
			final Y right;
			final double cost;
			if (assignment[i] < n) {
				type = OperationType.REPLACEMENT;
				right = b.get(assignment[i]);
				cost = repCosts[i][assignment[i]];
			} else {
				type = OperationType.DELETION;
				right = null;
				cost = delCosts[i];
			}
			ali.add(new Operation<>(left, right, type, cost));
		}
		for (int j = 0; j < n; j++) {
			if (assignment[m + j] == j) {
				ali.add(new Operation<>((X) null, b.get(j), OperationType.INSERTION, insCosts[j]));
			}
		}
		// return the finished alignment
		return ali;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy