Skip to content

Commit a7b1418

Browse files
committed
Upper bounds for distance metrics
1 parent 291b374 commit a7b1418

13 files changed

+612
-12
lines changed

lib/jpt1.jar

1.15 KB
Binary file not shown.

src/org/cicirello/permutations/distance/AbstractNormalizedPermutationDistanceMeasurer.java

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@
3131
* @since 1.2.5
3232
*
3333
*/
34-
abstract class AbstractNormalizedPermutationDistanceMeasurer extends AbstractPermutationDistanceMeasurer implements NormalizedPermutationDistanceMeasurer {
34+
abstract class AbstractNormalizedPermutationDistanceMeasurer extends AbstractPermutationDistanceMeasurer implements NormalizedPermutationDistanceMeasurer, NormalizedPermutationDistanceMeasurerDouble {
3535

3636
/**
3737
* {@inheritDoc}
3838
*/
3939
@Override
40-
final public double maxf(int length) {
40+
public final double maxf(int length) {
4141
return max(length);
4242
}
4343

@@ -46,9 +46,33 @@ final public double maxf(int length) {
4646
* {@inheritDoc}
4747
*/
4848
@Override
49-
final public double normalizedDistance(Permutation p1, Permutation p2) {
49+
public final double normalizedDistance(Permutation p1, Permutation p2) {
5050
int m = max(p1.length());
5151
if (m==0) return 0;
5252
return 1.0 * distance(p1,p2) / m;
5353
}
54+
55+
/**
56+
* {@inheritDoc}
57+
*/
58+
@Override
59+
public final double boundf(int length) {
60+
return max(length);
61+
}
62+
63+
/**
64+
* {@inheritDoc}
65+
*/
66+
@Override
67+
public final int bound(int length) {
68+
return max(length);
69+
}
70+
71+
/**
72+
* {@inheritDoc}
73+
*/
74+
@Override
75+
public final double normalizedByBound(Permutation p1, Permutation p2) {
76+
return normalizedDistance(p1, p2);
77+
}
5478
}

src/org/cicirello/permutations/distance/AbstractPermutationDistanceMeasurer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@
3131
* @since 1.0
3232
*
3333
*/
34-
abstract class AbstractPermutationDistanceMeasurer implements PermutationDistanceMeasurer {
34+
abstract class AbstractPermutationDistanceMeasurer implements PermutationDistanceMeasurer, PermutationDistanceMeasurerDouble {
3535

3636
/**
3737
* {@inheritDoc}
3838
*/
3939
@Override
40-
final public double distancef(Permutation p1, Permutation p2) {
40+
public final double distancef(Permutation p1, Permutation p2) {
4141
return distance(p1,p2);
4242
}
4343
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2019 Vincent A. Cicirello, <https://www.cicirello.org/>.
3+
*
4+
* This file is part of JavaPermutationTools (https://jpt.cicirello.org/).
5+
*
6+
* JavaPermutationTools is free software: you can
7+
* redistribute it and/or modify it under the terms of the GNU
8+
* General Public License as published by the Free Software
9+
* Foundation, either version 3 of the License, or (at your
10+
* option) any later version.
11+
*
12+
* JavaPermutationTools is distributed in the hope
13+
* that it will be useful, but WITHOUT ANY WARRANTY; without even
14+
* the implied warranty of MERCHANTABILITY or FITNESS FOR A
15+
* PARTICULAR PURPOSE. See the GNU General Public License for more
16+
* details.
17+
*
18+
* You should have received a copy of the GNU General Public License
19+
* along with JavaPermutationTools. If not, see <http://www.gnu.org/licenses/>.
20+
*/
21+
package org.cicirello.permutations.distance;
22+
23+
import org.cicirello.permutations.Permutation;
24+
25+
/**
26+
* Implement this interface to define a distance metric for permutations
27+
* for which it is feasible to compute an upper bound
28+
* on the distance metric for a given length.
29+
*
30+
* @author <a href=https://www.cicirello.org/ target=_top>Vincent A. Cicirello</a>, <a href=https://www.cicirello.org/ target=_top>https://www.cicirello.org/</a>
31+
* @version 1.19.5.10
32+
* @since 1.2.5
33+
*/
34+
public interface BoundedPermutationDistanceMeasurer extends PermutationDistanceMeasurer {
35+
36+
/**
37+
* <p>Measures the distance between two permutations, normalized to the interval [0.0, 1.0],
38+
* using an upper bound on the maximum distance, as specified by the bound method.</p>
39+
*
40+
* @param p1 first permutation
41+
* @param p2 second permutation
42+
* @return distance between p1 and p2 normalized to the interval [0.0, 1.0]
43+
* using an upper bound on maximum distance
44+
* @since 1.2.5
45+
*/
46+
double normalizedByBound(Permutation p1, Permutation p2);
47+
48+
/**
49+
* <p>Computes an upper bound on the maximum possible distance between permutations
50+
* of a specified length.</p>
51+
*
52+
* @param length Permutation length.
53+
* @return an upper bound on the maximum distance between a pair of permutations of the specified length.
54+
* @since 1.2.5
55+
*/
56+
int bound(int length);
57+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2019 Vincent A. Cicirello, <https://www.cicirello.org/>.
3+
*
4+
* This file is part of JavaPermutationTools (https://jpt.cicirello.org/).
5+
*
6+
* JavaPermutationTools is free software: you can
7+
* redistribute it and/or modify it under the terms of the GNU
8+
* General Public License as published by the Free Software
9+
* Foundation, either version 3 of the License, or (at your
10+
* option) any later version.
11+
*
12+
* JavaPermutationTools is distributed in the hope
13+
* that it will be useful, but WITHOUT ANY WARRANTY; without even
14+
* the implied warranty of MERCHANTABILITY or FITNESS FOR A
15+
* PARTICULAR PURPOSE. See the GNU General Public License for more
16+
* details.
17+
*
18+
* You should have received a copy of the GNU General Public License
19+
* along with JavaPermutationTools. If not, see <http://www.gnu.org/licenses/>.
20+
*/
21+
package org.cicirello.permutations.distance;
22+
23+
import org.cicirello.permutations.Permutation;
24+
25+
/**
26+
* Implement this interface to define a distance metric for permutations
27+
* for which it is feasible to compute an upper bound
28+
* on the distance metric for a given length.
29+
*
30+
* @author <a href=https://www.cicirello.org/ target=_top>Vincent A. Cicirello</a>, <a href=https://www.cicirello.org/ target=_top>https://www.cicirello.org/</a>
31+
* @version 1.19.5.10
32+
* @since 1.2.5
33+
*/
34+
public interface BoundedPermutationDistanceMeasurerDouble extends PermutationDistanceMeasurerDouble {
35+
36+
/**
37+
* <p>Measures the distance between two permutations, normalized to the interval [0.0, 1.0],
38+
* using an upper bound on the maximum distance, as specified by the boundf method.</p>
39+
*
40+
* @param p1 first permutation
41+
* @param p2 second permutation
42+
* @return distance between p1 and p2 normalized to the interval [0.0, 1.0]
43+
* using an upper bound on maximum distance
44+
* @since 1.2.5
45+
*/
46+
double normalizedByBound(Permutation p1, Permutation p2);
47+
48+
/**
49+
* <p>Computes an upper bound on the maximum possible distance between permutations
50+
* of a specified length.</p>
51+
*
52+
* @param length Permutation length.
53+
* @return an upper bound on the maximum distance between a pair of permutations of the specified length.
54+
* @since 1.2.5
55+
*/
56+
double boundf(int length);
57+
}

src/org/cicirello/permutations/distance/DeviationDistanceNormalized.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
* @since 1.0
5353
*
5454
*/
55-
public final class DeviationDistanceNormalized implements PermutationDistanceMeasurerDouble, NormalizedPermutationDistanceMeasurerDouble {
55+
public final class DeviationDistanceNormalized implements NormalizedPermutationDistanceMeasurerDouble {
5656

5757
private DeviationDistance devDistance;
5858

@@ -91,5 +91,21 @@ public double normalizedDistance(Permutation p1, Permutation p2) {
9191
if (m == 0.0) return 0;
9292
return distancef(p1,p2) / m;
9393
}
94+
95+
/**
96+
* {@inheritDoc}
97+
*/
98+
@Override
99+
public double boundf(int length) {
100+
return maxf(length);
101+
}
102+
103+
/**
104+
* {@inheritDoc}
105+
*/
106+
@Override
107+
public double normalizedByBound(Permutation p1, Permutation p2) {
108+
return normalizedDistance(p1, p2);
109+
}
94110

95111
}

src/org/cicirello/permutations/distance/DeviationDistanceNormalized2005.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
* @since 1.2.3
6767
*
6868
*/
69-
public final class DeviationDistanceNormalized2005 implements PermutationDistanceMeasurerDouble, NormalizedPermutationDistanceMeasurerDouble {
69+
public final class DeviationDistanceNormalized2005 implements NormalizedPermutationDistanceMeasurerDouble {
7070

7171
private DeviationDistance devDistance;
7272

@@ -102,5 +102,21 @@ public double maxf(int length) {
102102
public double normalizedDistance(Permutation p1, Permutation p2) {
103103
return distancef(p1,p2);
104104
}
105+
106+
/**
107+
* {@inheritDoc}
108+
*/
109+
@Override
110+
public double boundf(int length) {
111+
return maxf(length);
112+
}
113+
114+
/**
115+
* {@inheritDoc}
116+
*/
117+
@Override
118+
public double normalizedByBound(Permutation p1, Permutation p2) {
119+
return normalizedDistance(p1, p2);
120+
}
105121

106122
}

src/org/cicirello/permutations/distance/EditDistance.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
* @version 1.19.5.10
5757
* @since 1.0
5858
*/
59-
public final class EditDistance implements PermutationDistanceMeasurerDouble
59+
public final class EditDistance implements BoundedPermutationDistanceMeasurerDouble
6060
{
6161
private double insertCost;
6262
private double deleteCost;
@@ -114,6 +114,38 @@ public double distancef(Permutation p1, Permutation p2) {
114114
return D[L1][L2];
115115
}
116116

117+
/**
118+
* {@inheritDoc}
119+
*
120+
* <p>The upper bound is computed as the minimum of either the maximum
121+
* cost if only change operations are used ( n*changeCost ),
122+
* or the maximum cost if no change operations are used ( (n-1)*(insertCost + deleteCost) ).</p>
123+
*/
124+
@Override
125+
public double boundf(int length) {
126+
if (length <= 1) return 0;
127+
double combined = insertCost + deleteCost;
128+
final double EPSILON = 1e-10;
129+
if (combined <= changeCost) {
130+
return (length-1)*combined + EPSILON;
131+
}
132+
// Max if don't use changes
133+
double m1 = (length-1)*combined;
134+
// Max if only use changes
135+
double m2 = length*changeCost;
136+
return (m1 < m2 ? m1 : m2) + EPSILON;
137+
}
138+
139+
/**
140+
* {@inheritDoc}
141+
*/
142+
@Override
143+
public double normalizedByBound(Permutation p1, Permutation p2) {
144+
double m = boundf(p1.length());
145+
if (m==0) return 0;
146+
return distancef(p1,p2) / m;
147+
}
148+
117149
/* // REMOVED FOR NOW
118150
* The maxf method is not currently unsupported when computing
119151
* edit distance.

src/org/cicirello/permutations/distance/NormalizedPermutationDistanceMeasurer.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,17 @@
3030
* @version 1.19.5.10
3131
* @since 1.2.5
3232
*/
33-
public interface NormalizedPermutationDistanceMeasurer extends NormalizedPermutationDistanceMeasurerDouble {
33+
public interface NormalizedPermutationDistanceMeasurer extends BoundedPermutationDistanceMeasurer {
34+
35+
/**
36+
* <p>Measures the distance between two permutations, normalized to the interval [0.0, 1.0].</p>
37+
*
38+
* @param p1 first permutation
39+
* @param p2 second permutation
40+
* @return distance between p1 and p2 normalized to the interval [0.0, 1.0]
41+
* @since 1.2.5
42+
*/
43+
double normalizedDistance(Permutation p1, Permutation p2);
3444

3545
/**
3646
* Computes the maximum possible distance between permutations

src/org/cicirello/permutations/distance/NormalizedPermutationDistanceMeasurerDouble.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@
3030
* @version 1.19.5.10
3131
* @since 1.2.5
3232
*/
33-
public interface NormalizedPermutationDistanceMeasurerDouble {
33+
public interface NormalizedPermutationDistanceMeasurerDouble extends BoundedPermutationDistanceMeasurerDouble {
3434

3535
/**
3636
* <p>Measures the distance between two permutations, normalized to the interval [0.0, 1.0].</p>
3737
*
3838
* @param p1 first permutation
3939
* @param p2 second permutation
40-
* @return distance between p1 and p2
40+
* @return distance between p1 and p2 normalized to the interval [0.0, 1.0]
4141
* @since 1.2.5
4242
*/
4343
double normalizedDistance(Permutation p1, Permutation p2);

0 commit comments

Comments
 (0)