|
| 1 | +/* |
| 2 | + * JavaPermutationTools: A Java library for computation on permutations and sequences. |
| 3 | + * Copyright (C) 2018-2022 Vincent A. Cicirello, <https://www.cicirello.org/>. |
| 4 | + * |
| 5 | + * This file is part of JavaPermutationTools (https://jpt.cicirello.org/). |
| 6 | + * |
| 7 | + * JavaPermutationTools is free software: you can |
| 8 | + * redistribute it and/or modify it under the terms of the GNU |
| 9 | + * General Public License as published by the Free Software |
| 10 | + * Foundation, either version 3 of the License, or (at your |
| 11 | + * option) any later version. |
| 12 | + * |
| 13 | + * JavaPermutationTools is distributed in the hope |
| 14 | + * that it will be useful, but WITHOUT ANY WARRANTY; without even |
| 15 | + * the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 16 | + * PARTICULAR PURPOSE. See the GNU General Public License for more |
| 17 | + * details. |
| 18 | + * |
| 19 | + * You should have received a copy of the GNU General Public License |
| 20 | + * along with JavaPermutationTools. If not, see <http://www.gnu.org/licenses/>. |
| 21 | + */ |
| 22 | +package org.cicirello.permutations.distance; |
| 23 | + |
| 24 | +import org.cicirello.permutations.Permutation; |
| 25 | +import java.util.Arrays; |
| 26 | + |
| 27 | +/** |
| 28 | + * <p>This class implements the weighted Kendall tau distance. In the original |
| 29 | + * Kendall tau distance, each inverted pair of elements (i.e., such that element |
| 30 | + * x appears someplace before y in Permutation p1, but someplace after y in Permutation p2) |
| 31 | + * contributes 1 to the distance. Thus, since there are n(n-1)/2 pairs of elements, |
| 32 | + * the maximum of Kendall tau distance is n(n-1)/2 where n is the permutation length. |
| 33 | + * In this weighted Kendall tau distance, each element x of the permutation has an |
| 34 | + * associated weight w(x), and each inverted pair x, y (where x appears before sometime |
| 35 | + * prior to y in p1, but sometime after y in p2) contributes w(x) * w(y) to the weighted |
| 36 | + * Kendall tau distance.</p> |
| 37 | + * |
| 38 | + * <p>The weighted Kendall tau distance was first described in:<br> |
| 39 | + * "Failure proximity: a fault localization-based approach" (Liu and Han, SIGSOFT 2006, pages 46-56).</p> |
| 40 | + * |
| 41 | + * <p>The runtime of JPT's implementation is O(n lg n), where n is the permutation length. |
| 42 | + * This runtime is achieved using a modified version of mergesort to sum the weighted inversions.</p> |
| 43 | + * |
| 44 | + * @author <a href=https://www.cicirello.org/ target=_top>Vincent A. Cicirello</a>, |
| 45 | + * <a href=https://www.cicirello.org/ target=_top>https://www.cicirello.org/</a> |
| 46 | + */ |
| 47 | +public final class WeightedKendallTauDistance implements NormalizedPermutationDistanceMeasurerDouble { |
| 48 | + |
| 49 | + private final double[] weights; |
| 50 | + private final double maxDistance; |
| 51 | + |
| 52 | + /** |
| 53 | + * Constructs an instance of the WeightedKendallTauDistance. |
| 54 | + * @param weights An array of weights, such that weights[e] is the weight of |
| 55 | + * element e. |
| 56 | + */ |
| 57 | + public WeightedKendallTauDistance(double[] weights) { |
| 58 | + this.weights = weights.clone(); |
| 59 | + double max = 0; |
| 60 | + for (int i = 0; i < weights.length - 1; i++) { |
| 61 | + double runningSum = 0; |
| 62 | + for (int j = i+1; j < weights.length; j++) { |
| 63 | + runningSum += weights[j]; |
| 64 | + } |
| 65 | + max += weights[i] * runningSum; |
| 66 | + } |
| 67 | + maxDistance = max; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Gets the length of permutations supported by this instance of |
| 72 | + * WeightedKendallTauDistance, which is equal to the length of the |
| 73 | + * array of weights passed to the constructor. |
| 74 | + * |
| 75 | + * @return The length of supported Permutations. |
| 76 | + */ |
| 77 | + public int supportedLength() { |
| 78 | + return weights.length; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * {@inheritDoc} |
| 83 | + * |
| 84 | + * @throws IllegalArgumentException if p1.length() is not equal to supportedLength(), |
| 85 | + * or if p2.length() is not equal to supportedLength(). |
| 86 | + */ |
| 87 | + @Override |
| 88 | + public double distancef(Permutation p1, Permutation p2) { |
| 89 | + if (p1.length() != weights.length || p2.length() != weights.length) { |
| 90 | + throw new IllegalArgumentException("p1 and/or p2 not of supported length of this instance"); |
| 91 | + } |
| 92 | + // use inverse of p1 as a relabeling |
| 93 | + int[] invP1 = p1.getInverse(); |
| 94 | + |
| 95 | + // relabel array copy of p2 and likewise map weights to weights of relabeled copy |
| 96 | + int[] arrayP2 = new int[invP1.length]; |
| 97 | + double[] w = new double[weights.length]; |
| 98 | + for (int i = 0; i < arrayP2.length; i++) { |
| 99 | + arrayP2[i] = invP1[p2.get(i)]; |
| 100 | + w[arrayP2[i]] = weights[p2.get(i)]; |
| 101 | + } |
| 102 | + |
| 103 | + return countWeightedInversions(arrayP2, w); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * {@inheritDoc} |
| 108 | + * |
| 109 | + * <p><b>This implementation ignores the length parameter since this |
| 110 | + * distance is configured for one specific length based upon the weights |
| 111 | + * passed during construction.</b></p> |
| 112 | + */ |
| 113 | + @Override |
| 114 | + public double maxf(int length) { |
| 115 | + return maxDistance; |
| 116 | + } |
| 117 | + |
| 118 | + private double countWeightedInversions(int[] array, double[] w) { |
| 119 | + if (array.length <= 1) return 0; |
| 120 | + int m = array.length >> 1; |
| 121 | + int[] left = Arrays.copyOfRange(array, 0, m); |
| 122 | + int[] right = Arrays.copyOfRange(array, m, array.length); |
| 123 | + double weightedCount = countWeightedInversions(left, w) + countWeightedInversions(right, w); |
| 124 | + int i = 0; |
| 125 | + int j = 0; |
| 126 | + int k = 0; |
| 127 | + while (i < left.length && j < right.length) { |
| 128 | + if (left[i] < right[j]) { |
| 129 | + array[k] = left[i]; |
| 130 | + i++; |
| 131 | + k++; |
| 132 | + } else { |
| 133 | + // inversions |
| 134 | + double leftWeights = 0; |
| 135 | + for (int x = i; x < left.length; x++) { |
| 136 | + leftWeights += w[left[x]]; |
| 137 | + } |
| 138 | + weightedCount += w[right[j]] * leftWeights; |
| 139 | + array[k] = right[j]; |
| 140 | + j++; |
| 141 | + k++; |
| 142 | + } |
| 143 | + } |
| 144 | + while (i < left.length) { |
| 145 | + array[k] = left[i]; |
| 146 | + i++; |
| 147 | + k++; |
| 148 | + } |
| 149 | + while (j < right.length) { |
| 150 | + array[k] = right[j]; |
| 151 | + j++; |
| 152 | + k++; |
| 153 | + } |
| 154 | + return weightedCount; |
| 155 | + } |
| 156 | +} |
0 commit comments