Skip to content

Commit b836c8e

Browse files
committed
Refactored permutation distance related interfaces, abstract base classes
1 parent daf08b8 commit b836c8e

31 files changed

+258
-226
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
* Extend this abstract class to define a distance metric for permutations
27+
* where distance is an integer value, and which supports normalizing the distance.
28+
*
29+
* @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.10
31+
* @since 1.2.5
32+
*
33+
*/
34+
abstract class AbstractNormalizedPermutationDistanceMeasurer extends AbstractPermutationDistanceMeasurer implements NormalizedPermutationDistanceMeasurer {
35+
36+
/**
37+
* {@inheritDoc}
38+
*/
39+
@Override
40+
final public double maxf(int length) {
41+
return max(length);
42+
}
43+
44+
45+
/**
46+
* {@inheritDoc}
47+
*/
48+
@Override
49+
final public double normalizedDistance(Permutation p1, Permutation p2) {
50+
int m = max(p1.length());
51+
if (m==0) return 0;
52+
return 1.0 * distance(p1,p2) / m;
53+
}
54+
}

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

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -27,53 +27,17 @@
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.9
30+
* @version 1.19.5.10
3131
* @since 1.0
3232
*
3333
*/
34-
abstract class AbstractPermutationDistanceMeasurer implements PermutationDistanceMeasurer, PermutationDistanceMeasurerDouble {
34+
abstract class AbstractPermutationDistanceMeasurer implements PermutationDistanceMeasurer {
3535

36-
3736
/**
3837
* {@inheritDoc}
3938
*/
4039
@Override
4140
final public double distancef(Permutation p1, Permutation p2) {
4241
return distance(p1,p2);
4342
}
44-
45-
/**
46-
* {@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.
53-
*/
54-
@Override
55-
final public double maxf(int length) {
56-
return max(length);
57-
}
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-
}
7943
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@
4242
* S. Ronald, "Distance functions for order-based encodings," in Proc. IEEE CEC. IEEE Press, 1997, pp. 49–54.</p>
4343
*
4444
* @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>
45-
* @version 1.19.5.8
45+
* @version 1.19.5.10
4646
* @since 1.0
4747
*/
48-
public class AcyclicEdgeDistance extends AbstractPermutationDistanceMeasurer {
48+
public class AcyclicEdgeDistance extends AbstractNormalizedPermutationDistanceMeasurer {
4949

5050

5151
/**

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@
4242
* S. Ronald, "Distance functions for order-based encodings," in Proc. IEEE CEC. IEEE Press, 1997, pp. 49–54.</p>
4343
*
4444
* @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>
45-
* @version 1.19.5.8
45+
* @version 1.19.5.10
4646
* @since 1.0
4747
*/
48-
public class CyclicEdgeDistance extends AbstractPermutationDistanceMeasurer {
48+
public class CyclicEdgeDistance extends AbstractNormalizedPermutationDistanceMeasurer {
4949

5050

5151

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

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@
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.9
35+
* @version 1.19.5.10
3636
* @since 1.0
3737
*
3838
*/
39-
public final class CyclicIndependentDistance implements PermutationDistanceMeasurer {
39+
public final class CyclicIndependentDistance extends AbstractPermutationDistanceMeasurer {
4040

4141
private PermutationDistanceMeasurer d;
4242

@@ -69,14 +69,4 @@ public int distance(Permutation p1, Permutation p2) {
6969
return result;
7070
}
7171

72-
/**
73-
* The max method is unsupported when computing
74-
* distance with cyclic independence.
75-
*
76-
* @throws UnsupportedOperationException If method is invoked.
77-
*/
78-
@Override
79-
public int max(int length) {
80-
throw new UnsupportedOperationException("Unimplemented.");
81-
}
8272
}

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

Lines changed: 1 addition & 11 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.9
35+
* @version 1.19.5.10
3636
* @since 1.0
3737
*
3838
*/
@@ -69,14 +69,4 @@ public double distancef(Permutation p1, Permutation p2) {
6969
return result;
7070
}
7171

72-
/**
73-
* The maxf method is unsupported when computing
74-
* distance with cyclic independence.
75-
*
76-
* @throws UnsupportedOperationException If method is invoked.
77-
*/
78-
@Override
79-
public double maxf(int length) {
80-
throw new UnsupportedOperationException("Unimplemented.");
81-
}
8272
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@
4444
* IEEE Transactions on Evolutionary Computation, 20(3):434-446, June 2016.</p>
4545
*
4646
* @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>
47-
* @version 1.19.5.8
47+
* @version 1.19.5.10
4848
* @since 1.0
4949
*/
50-
public class CyclicRTypeDistance extends AbstractPermutationDistanceMeasurer {
50+
public class CyclicRTypeDistance extends AbstractNormalizedPermutationDistanceMeasurer {
5151

5252

5353

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

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@
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.9
36+
* @version 1.19.5.10
3737
* @since 1.0
3838
*
3939
*/
40-
public final class CyclicReversalIndependentDistance implements PermutationDistanceMeasurer {
40+
public final class CyclicReversalIndependentDistance extends AbstractPermutationDistanceMeasurer {
4141

4242
private PermutationDistanceMeasurer d;
4343

@@ -84,14 +84,4 @@ public int distance(Permutation p1, Permutation p2) {
8484
return result;
8585
}
8686

87-
/**
88-
* The max method is unsupported when computing
89-
* distance with cyclic and reversal independence.
90-
*
91-
* @throws UnsupportedOperationException If method is invoked.
92-
*/
93-
@Override
94-
public int max(int length) {
95-
throw new UnsupportedOperationException("Unimplemented.");
96-
}
9787
}

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

Lines changed: 1 addition & 11 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.9
36+
* @version 1.19.5.10
3737
* @since 1.0
3838
*
3939
*/
@@ -84,14 +84,4 @@ public double distancef(Permutation p1, Permutation p2) {
8484
return result;
8585
}
8686

87-
/**
88-
* The maxf method is unsupported when computing
89-
* distance with cyclic and reversal independence.
90-
*
91-
* @throws UnsupportedOperationException If method is invoked.
92-
*/
93-
@Override
94-
public double maxf(int length) {
95-
throw new UnsupportedOperationException("Unimplemented.");
96-
}
9787
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@
4242
* S. Ronald, "More distance functions for order-based encodings," in Proc. IEEE CEC. IEEE Press, 1998, pp. 558–563.</p>
4343
*
4444
* @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>
45-
* @version 1.19.5.8
45+
* @version 1.19.5.10
4646
* @since 1.0
4747
*
4848
*/
49-
public class DeviationDistance extends AbstractPermutationDistanceMeasurer {
49+
public class DeviationDistance extends AbstractNormalizedPermutationDistanceMeasurer {
5050

5151
/**
5252
* {@inheritDoc}

0 commit comments

Comments
 (0)