Skip to content

Commit f036843

Browse files
committed
Added normalizedDistance method and other minor edits
1 parent 1876e8d commit f036843

13 files changed

+304
-46
lines changed

lib/jpt1.jar

405 Bytes
Binary file not shown.

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
* where distance is an integer value.
2828
*
2929
* @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>
30-
* @version 1.19.5.8
30+
* @version 1.19.5.9
3131
* @since 1.0
3232
*
3333
*/
@@ -44,9 +44,36 @@ final public double distancef(Permutation p1, Permutation p2) {
4444

4545
/**
4646
* {@inheritDoc}
47+
*
48+
* This method is supported by any extending class that implements the max method.
49+
* If the max method is not supported by this class, then neither is maxf. Consult the
50+
* documentation of the max method for support information.
51+
*
52+
* @throws UnsupportedOperationException If this class doesn't support the max method.
4753
*/
4854
@Override
4955
final public double maxf(int length) {
5056
return max(length);
5157
}
58+
59+
60+
/**
61+
* <p>Measures the distance between two permutations, normalized to the interval [0.0, 1.0].</p>
62+
*
63+
* <p>This method is supported by any implementing class that implements the max method.
64+
* If max is unsupported, then so is normalizedDistance. Please consult the documentation
65+
* of max for support information.</p>
66+
*
67+
* @param p1 first permutation
68+
* @param p2 second permutation
69+
* @return distance between p1 and p2
70+
* @throws UnsupportedOperationException If this class doesn't support the max method.
71+
* @since 1.2.4
72+
*/
73+
@Override
74+
final public double normalizedDistance(Permutation p1, Permutation p2) {
75+
int m = max(p1.length());
76+
if (m==0) return 0;
77+
return distancef(p1,p2) / m;
78+
}
5279
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
* the constructor.</p>
3333
*
3434
* @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>
35-
* @version 1.19.5.8
35+
* @version 1.19.5.9
3636
* @since 1.0
3737
*
3838
*/
@@ -70,10 +70,10 @@ public int distance(Permutation p1, Permutation p2) {
7070
}
7171

7272
/**
73-
* {@inheritDoc}
74-
*
75-
* @throws UnsupportedOperationException The max method is unsupported when computing
73+
* The max method is unsupported when computing
7674
* distance with cyclic independence.
75+
*
76+
* @throws UnsupportedOperationException If method is invoked.
7777
*/
7878
@Override
7979
public int max(int length) {

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
* the constructor.</p>
3333
*
3434
* @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>
35-
* @version 1.19.5.8
35+
* @version 1.19.5.9
3636
* @since 1.0
3737
*
3838
*/
@@ -70,9 +70,10 @@ public double distancef(Permutation p1, Permutation p2) {
7070
}
7171

7272
/**
73-
* {@inheritDoc}
74-
* @throws UnsupportedOperationException The maxf method is unsupported when computing
73+
* The maxf method is unsupported when computing
7574
* distance with cyclic independence.
75+
*
76+
* @throws UnsupportedOperationException If method is invoked.
7677
*/
7778
@Override
7879
public double maxf(int length) {

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
* the constructor.</p>
3434
*
3535
* @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>
36-
* @version 1.19.5.8
36+
* @version 1.19.5.9
3737
* @since 1.0
3838
*
3939
*/
@@ -85,9 +85,10 @@ public int distance(Permutation p1, Permutation p2) {
8585
}
8686

8787
/**
88-
* {@inheritDoc}
89-
* @throws UnsupportedOperationException The max method is unsupported when computing
88+
* The max method is unsupported when computing
9089
* distance with cyclic and reversal independence.
90+
*
91+
* @throws UnsupportedOperationException If method is invoked.
9192
*/
9293
@Override
9394
public int max(int length) {

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
* the constructor.</p>
3434
*
3535
* @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>
36-
* @version 1.19.5.8
36+
* @version 1.19.5.9
3737
* @since 1.0
3838
*
3939
*/
@@ -85,9 +85,10 @@ public double distancef(Permutation p1, Permutation p2) {
8585
}
8686

8787
/**
88-
* {@inheritDoc}
89-
* @throws UnsupportedOperationException The maxf method is unsupported when computing
88+
* The maxf method is unsupported when computing
9089
* distance with cyclic and reversal independence.
90+
*
91+
* @throws UnsupportedOperationException If method is invoked.
9192
*/
9293
@Override
9394
public double maxf(int length) {

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

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
* in Proceedings of the 26th FLAIRS Conference. AAAI Press, May 2013, pp. 46–51.</p>
5454
*
5555
* @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>
56-
* @version 1.19.5.8
56+
* @version 1.19.5.9
5757
* @since 1.0
5858
*/
5959
public class EditDistance implements PermutationDistanceMeasurerDouble
@@ -92,8 +92,7 @@ public EditDistance() {
9292
public double distancef(Permutation p1, Permutation p2) {
9393
int L1 = p1.length();
9494
int L2 = p2.length();
95-
if (L1 != L2) throw new IllegalArgumentException("Permutations must be of the same length.");
96-
if (L1 <= 1) return 0;
95+
if (L1 == L2 && L1 <= 1) return 0;
9796
double[][] D = new double[L1 + 1][L2 + 1];
9897
for (int i = 1; i <= L1; i++) {
9998
D[i][0] = D[i-1][0] + deleteCost;
@@ -116,29 +115,36 @@ public double distancef(Permutation p1, Permutation p2) {
116115
}
117116

118117
/**
119-
* {@inheritDoc}
120-
* @throws UnsupportedOperationException The maxf method is not currently unsupported when computing
118+
* The maxf method is not currently unsupported when computing
121119
* edit distance.
120+
*
121+
* @throws UnsupportedOperationException If this method is invoked.
122122
*/
123123
@Override
124124
public double maxf(int length) {
125125
throw new UnsupportedOperationException("Unimplemented.");
126-
/* // Doesn't quite work. Might be too complicated to efficiently compute in general.
126+
/* // This is close but doesn't work. Might be too complex to be worth computing here.
127127
if (length <= 1) return 0;
128128
double combined = insertCost + deleteCost;
129-
boolean even = length % 2 == 0;
130-
if (even && combined <= changeCost*length/(length-1)) {
131-
return (length-1)*combined;
132-
}
133-
if (!even && combined <= changeCost) {
129+
if (combined <= changeCost) {
134130
return (length-1)*combined;
135131
}
136-
if (length == 3) {
137-
return Math.max(Math.min(combined, 3*changeCost), Math.min(2*combined, 2*changeCost));
138-
}
139-
double m1 = length*changeCost;
140-
double m2 = maxf(length-1)+combined;
141-
return Math.min(m1, m2); */
132+
if (length % 2 == 0) {
133+
double m1 = (length-1)*combined;
134+
double m2 = length*changeCost;
135+
double m3 = combined + (length-2)*changeCost;
136+
double max = m1 < m2 ? m1 : m2;
137+
if (m3 < max) max = m3;
138+
return max;
139+
} else {
140+
double m = (length-1)*changeCost;
141+
double m1 = (length-2)*combined;
142+
double m2 = length*changeCost;
143+
double m3 = combined + (length-2)*changeCost;
144+
double max = m1 < m2 ? m1 : m2;
145+
if (m3 < max) max = m3;
146+
return m > max ? m : max;
147+
} */
142148
}
143149

144150
}

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
* Implement this interface, PermutationDistanceMeasurer, to define a distance metric for permutations.
2727
*
2828
* @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>
29-
* @version 1.19.5.8
29+
* @version 1.19.5.9
3030
* @since 1.0
3131
*/
3232
public interface PermutationDistanceMeasurer
@@ -48,5 +48,20 @@ public interface PermutationDistanceMeasurer
4848
* @return the maximum distance between a pair of permutations of the specified length.
4949
* @since 1.2.4
5050
*/
51-
int max(int length);
51+
default int max(int length) {
52+
throw new UnsupportedOperationException("Unimplemented for this class.");
53+
}
54+
55+
/**
56+
* <p>Measures the distance between two permutations, normalized to the interval [0.0, 1.0].</p>
57+
*
58+
* @param p1 first permutation
59+
* @param p2 second permutation
60+
* @return distance between p1 and p2
61+
* @throws UnsupportedOperationException If this class doesn't support the max method.
62+
* @since 1.2.4
63+
*/
64+
default double normalizedDistance(Permutation p1, Permutation p2) {
65+
throw new UnsupportedOperationException("Unimplemented for this class.");
66+
}
5267
}

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

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
* where the distance is a floating-point value.
2828
*
2929
* @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>
30-
* @version 1.19.5.8
30+
* @version 1.19.5.9
3131
* @since 1.0
3232
*/
3333
public interface PermutationDistanceMeasurerDouble
@@ -49,6 +49,27 @@ public interface PermutationDistanceMeasurerDouble
4949
* @return the maximum distance between a pair of permutations of the specified length.
5050
* @since 1.2.4
5151
*/
52-
double maxf(int length);
52+
default double maxf(int length) {
53+
throw new UnsupportedOperationException("Unimplemented for this class.");
54+
}
55+
56+
/**
57+
* <p>Measures the distance between two permutations, normalized to the interval [0.0, 1.0].</p>
58+
*
59+
* <p>This method is supported by any implementing class that implements the maxf method.
60+
* If maxf is unsupported, then so is normalizedDistance. Please consult the documentation
61+
* of maxf for support information.</p>
62+
*
63+
* @param p1 first permutation
64+
* @param p2 second permutation
65+
* @return distance between p1 and p2
66+
* @throws UnsupportedOperationException If this class doesn't support the maxf method.
67+
* @since 1.2.4
68+
*/
69+
default double normalizedDistance(Permutation p1, Permutation p2) {
70+
double m = maxf(p1.length());
71+
if (m==0) return 0;
72+
return distancef(p1,p2) / m;
73+
}
5374

5475
}

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
* to the constructor.</p>
3333
*
3434
* @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>
35-
* @version 1.19.5.8
35+
* @version 1.19.5.9
3636
* @since 1.0
3737
*
3838
*/
@@ -69,9 +69,10 @@ public int distance(Permutation p1, Permutation p2) {
6969
}
7070

7171
/**
72-
* {@inheritDoc}
73-
* @throws UnsupportedOperationException The max method is unsupported when computing
74-
* distance with cyclic and reversal independence.
72+
* The max method is unsupported when computing
73+
* distance with reversal independence.
74+
*
75+
* @throws UnsupportedOperationException If method is invoked.
7576
*/
7677
@Override
7778
public int max(int length) {

0 commit comments

Comments
 (0)