diff --git a/CHANGELOG.md b/CHANGELOG.md index 9dab0e80..44a73d28 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,12 +4,25 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -## [Unreleased] - 2021-03-31 +## [Unreleased] - 2021-04-02 ### Added * Added the `Permutation.Mechanic.set(Permutation, int[], int, int, int)` method. ### Changed +* Refactored the SequenceDistanceMeasurer and SequenceDistanceMeasurerDouble interfaces + into a hierarchy, eliminated an abstract base class made obsolete by that change, and + changed all sequence distances in the library to use the new hierarchy. This is a non-breaking + change, as the only thing removed was a package-private abstract class, and the + change to the SequenceDistanceMeasurer interface was done in such a way that all inherited + methods have default implementations. +* Refactored the PermutationDistanceMeasurer, PermutationDistanceMeasurerDouble, + NormalizedPermutationDistanceMeasurer, and NormalizedPermutationDistanceMeasurerDouble + interfaces, and all of the classes that implement them, to move default implementations + from an abstract base class into the appropriate subinterfaces in hierarchy. This is a + non-breaking change, as the only thing removed was a package-private abstract class, and the + changes to interfaces were done in such a way that all inherited methods have default + implementations. ### Deprecated diff --git a/src/org/cicirello/permutations/distance/AbstractPermutationDistanceMeasurer.java b/src/org/cicirello/permutations/distance/AbstractPermutationDistanceMeasurer.java deleted file mode 100644 index 4eb0a80c..00000000 --- a/src/org/cicirello/permutations/distance/AbstractPermutationDistanceMeasurer.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright 2010, 2015, 2017-2021 Vincent A. Cicirello, . - * - * This file is part of JavaPermutationTools (https://jpt.cicirello.org/). - * - * JavaPermutationTools is free software: you can - * redistribute it and/or modify it under the terms of the GNU - * General Public License as published by the Free Software - * Foundation, either version 3 of the License, or (at your - * option) any later version. - * - * JavaPermutationTools 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 General Public License for more - * details. - * - * You should have received a copy of the GNU General Public License - * along with JavaPermutationTools. If not, see . * - */ -package org.cicirello.permutations.distance; - -import org.cicirello.permutations.Permutation; - -/** - * Extend this abstract class to define a distance metric for permutations - * where distance is an integer value. - * - * @author Vincent A. Cicirello, https://www.cicirello.org/ - * @version 1.28.2021 - * - */ -abstract class AbstractPermutationDistanceMeasurer implements PermutationDistanceMeasurer, NormalizedPermutationDistanceMeasurer { - - /** - * {@inheritDoc} - * - * @throws IllegalArgumentException if p1.length() is not equal to p2.length(). - */ - @Override - public final double distancef(Permutation p1, Permutation p2) { - return distance(p1,p2); - } - - @Override - public final double maxf(int length) { - return max(length); - } - - /** - * {@inheritDoc} - * - * @throws IllegalArgumentException if p1.length() is not equal to p2.length(). - */ - @Override - public final double normalizedDistance(Permutation p1, Permutation p2) { - int m = max(p1.length()); - if (m==0) return 0; - return 1.0 * distance(p1,p2) / m; - } -} diff --git a/src/org/cicirello/permutations/distance/AcyclicEdgeDistance.java b/src/org/cicirello/permutations/distance/AcyclicEdgeDistance.java index 3655b190..6ed58e52 100644 --- a/src/org/cicirello/permutations/distance/AcyclicEdgeDistance.java +++ b/src/org/cicirello/permutations/distance/AcyclicEdgeDistance.java @@ -42,9 +42,9 @@ * S. Ronald, "Distance functions for order-based encodings," in Proc. IEEE CEC. IEEE Press, 1997, pp. 49–54.

* * @author Vincent A. Cicirello, https://www.cicirello.org/ - * @version 1.28.2021 + * @version 4.2.2021 */ -public final class AcyclicEdgeDistance extends AbstractPermutationDistanceMeasurer { +public final class AcyclicEdgeDistance implements NormalizedPermutationDistanceMeasurer { /** * Constructs the distance measurer as specified in the class documentation. diff --git a/src/org/cicirello/permutations/distance/BlockInterchangeDistance.java b/src/org/cicirello/permutations/distance/BlockInterchangeDistance.java index 6c9881a2..544d119f 100644 --- a/src/org/cicirello/permutations/distance/BlockInterchangeDistance.java +++ b/src/org/cicirello/permutations/distance/BlockInterchangeDistance.java @@ -40,9 +40,9 @@ *

Runtime: O(n), where n is the permutation length.

* * @author Vincent A. Cicirello, https://www.cicirello.org/ - * @version 1.28.2021 + * @version 4.2.2021 */ -public class BlockInterchangeDistance extends AbstractPermutationDistanceMeasurer { +public class BlockInterchangeDistance implements NormalizedPermutationDistanceMeasurer { /** * Constructs the distance measurer as specified in the class documentation. diff --git a/src/org/cicirello/permutations/distance/CyclicEdgeDistance.java b/src/org/cicirello/permutations/distance/CyclicEdgeDistance.java index 01d2bb04..30d7d7d7 100644 --- a/src/org/cicirello/permutations/distance/CyclicEdgeDistance.java +++ b/src/org/cicirello/permutations/distance/CyclicEdgeDistance.java @@ -42,9 +42,9 @@ * S. Ronald, "Distance functions for order-based encodings," in Proc. IEEE CEC. IEEE Press, 1997, pp. 49–54.

* * @author Vincent A. Cicirello, https://www.cicirello.org/ - * @version 1.28.2021 + * @version 4.2.2021 */ -public final class CyclicEdgeDistance extends AbstractPermutationDistanceMeasurer { +public final class CyclicEdgeDistance implements NormalizedPermutationDistanceMeasurer { /** * Constructs the distance measurer as specified in the class documentation. diff --git a/src/org/cicirello/permutations/distance/CyclicIndependentDistance.java b/src/org/cicirello/permutations/distance/CyclicIndependentDistance.java index 32d0ad3f..aeaf2eef 100644 --- a/src/org/cicirello/permutations/distance/CyclicIndependentDistance.java +++ b/src/org/cicirello/permutations/distance/CyclicIndependentDistance.java @@ -32,7 +32,7 @@ * the constructor.

* * @author Vincent A. Cicirello, https://www.cicirello.org/ - * @version 1.28.2021 + * @version 4.2.2021 * */ public final class CyclicIndependentDistance implements PermutationDistanceMeasurer { @@ -68,19 +68,4 @@ public int distance(Permutation p1, Permutation p2) { } return result; } - - /** - * Measures the distance between two permutations, with cyclic independence: - * distance = min_{i in [0,N)} distance(p1,rotate(p2,i)) - * - * @param p1 first permutation - * @param p2 second permutation - * @return distance between p1 and p2 - * @throws IllegalArgumentException if p1.length() is not equal to p2.length(). - */ - @Override - public final double distancef(Permutation p1, Permutation p2) { - return distance(p1,p2); - } - } \ No newline at end of file diff --git a/src/org/cicirello/permutations/distance/CyclicRTypeDistance.java b/src/org/cicirello/permutations/distance/CyclicRTypeDistance.java index 639403df..15ee701c 100644 --- a/src/org/cicirello/permutations/distance/CyclicRTypeDistance.java +++ b/src/org/cicirello/permutations/distance/CyclicRTypeDistance.java @@ -44,9 +44,9 @@ * IEEE Transactions on Evolutionary Computation, 20(3):434-446, June 2016.

* * @author Vincent A. Cicirello, https://www.cicirello.org/ - * @version 1.28.2021 + * @version 4.2.2021 */ -public final class CyclicRTypeDistance extends AbstractPermutationDistanceMeasurer { +public final class CyclicRTypeDistance implements NormalizedPermutationDistanceMeasurer { /** * Constructs the distance measurer as specified in the class documentation. diff --git a/src/org/cicirello/permutations/distance/CyclicReversalIndependentDistance.java b/src/org/cicirello/permutations/distance/CyclicReversalIndependentDistance.java index d5ed52ca..546e406d 100644 --- a/src/org/cicirello/permutations/distance/CyclicReversalIndependentDistance.java +++ b/src/org/cicirello/permutations/distance/CyclicReversalIndependentDistance.java @@ -33,7 +33,7 @@ * the constructor.

* * @author Vincent A. Cicirello, https://www.cicirello.org/ - * @version 1.28.2021 + * @version 4.2.2021 * */ public final class CyclicReversalIndependentDistance implements PermutationDistanceMeasurer { @@ -83,19 +83,4 @@ public int distance(Permutation p1, Permutation p2) { } return result; } - - /** - * Measures the distance between two permutations, with cyclic and reversal independence: - * distance = min_{i in [0,N)} { distance(p1,rotate(p2,i)), distance(p1,rotate(reverse(p2),i)) } - * - * @param p1 first permutation - * @param p2 second permutation - * @return distance between p1 and p2 - * @throws IllegalArgumentException if p1.length() is not equal to p2.length(). - */ - @Override - public final double distancef(Permutation p1, Permutation p2) { - return distance(p1,p2); - } - } \ No newline at end of file diff --git a/src/org/cicirello/permutations/distance/DeviationDistance.java b/src/org/cicirello/permutations/distance/DeviationDistance.java index 5dfca656..fdc8a4f4 100644 --- a/src/org/cicirello/permutations/distance/DeviationDistance.java +++ b/src/org/cicirello/permutations/distance/DeviationDistance.java @@ -42,10 +42,10 @@ * S. Ronald, "More distance functions for order-based encodings," in Proc. IEEE CEC. IEEE Press, 1998, pp. 558–563.

* * @author Vincent A. Cicirello, https://www.cicirello.org/ - * @version 1.28.2021 + * @version 4.2.2021 * */ -public final class DeviationDistance extends AbstractPermutationDistanceMeasurer { +public final class DeviationDistance implements NormalizedPermutationDistanceMeasurer { /** * Constructs the distance measurer as specified in the class documentation. diff --git a/src/org/cicirello/permutations/distance/DeviationDistanceNormalized.java b/src/org/cicirello/permutations/distance/DeviationDistanceNormalized.java index f399f74e..616d3766 100644 --- a/src/org/cicirello/permutations/distance/DeviationDistanceNormalized.java +++ b/src/org/cicirello/permutations/distance/DeviationDistanceNormalized.java @@ -48,10 +48,10 @@ * Proc. IEEE CEC. IEEE Press, 1998, pp. 558–563.

* * @author Vincent A. Cicirello, https://www.cicirello.org/ - * @version 1.28.2021 + * @version 4.2.2021 * */ -public final class DeviationDistanceNormalized implements PermutationDistanceMeasurerDouble, NormalizedPermutationDistanceMeasurerDouble { +public final class DeviationDistanceNormalized implements NormalizedPermutationDistanceMeasurerDouble { private DeviationDistance devDistance; @@ -81,17 +81,4 @@ public double maxf(int length) { if (length <= 1) return 0; return (length * length - (length & 1)) / (2.0 * (length-1)); } - - /** - * {@inheritDoc} - * - * @throws IllegalArgumentException if p1.length() is not equal to p2.length(). - */ - @Override - public double normalizedDistance(Permutation p1, Permutation p2) { - double m = maxf(p1.length()); - if (m == 0.0) return 0; - return distancef(p1,p2) / m; - } - } \ No newline at end of file diff --git a/src/org/cicirello/permutations/distance/DeviationDistanceNormalized2005.java b/src/org/cicirello/permutations/distance/DeviationDistanceNormalized2005.java index 37248f07..001b249b 100644 --- a/src/org/cicirello/permutations/distance/DeviationDistanceNormalized2005.java +++ b/src/org/cicirello/permutations/distance/DeviationDistanceNormalized2005.java @@ -62,10 +62,10 @@ * management," in Proc. of MIC2005, 2005.

* * @author Vincent A. Cicirello, https://www.cicirello.org/ - * @version 1.28.2021 + * @version 4.2.2021 * */ -public final class DeviationDistanceNormalized2005 implements PermutationDistanceMeasurerDouble, NormalizedPermutationDistanceMeasurerDouble { +public final class DeviationDistanceNormalized2005 implements NormalizedPermutationDistanceMeasurerDouble { private DeviationDistance devDistance; diff --git a/src/org/cicirello/permutations/distance/ExactMatchDistance.java b/src/org/cicirello/permutations/distance/ExactMatchDistance.java index 2cf229a1..a3256fcc 100644 --- a/src/org/cicirello/permutations/distance/ExactMatchDistance.java +++ b/src/org/cicirello/permutations/distance/ExactMatchDistance.java @@ -33,9 +33,9 @@ * S. Ronald, "More distance functions for order-based encodings," in Proc. IEEE CEC. IEEE Press, 1998, pp. 558–563.

* * @author Vincent A. Cicirello, https://www.cicirello.org/ - * @version 1.28.2021 + * @version 4.2.2021 */ -public final class ExactMatchDistance extends AbstractPermutationDistanceMeasurer { +public final class ExactMatchDistance implements NormalizedPermutationDistanceMeasurer { /** * Constructs the distance measurer as specified in the class documentation. diff --git a/src/org/cicirello/permutations/distance/InterchangeDistance.java b/src/org/cicirello/permutations/distance/InterchangeDistance.java index ff7aad60..f1ad5bb5 100644 --- a/src/org/cicirello/permutations/distance/InterchangeDistance.java +++ b/src/org/cicirello/permutations/distance/InterchangeDistance.java @@ -40,9 +40,9 @@ * IEEE Transactions on Evolutionary Computation, 20(3):434-446, June 2016.

* * @author Vincent A. Cicirello, https://www.cicirello.org/ - * @version 1.28.2021 + * @version 4.2.2021 */ -public final class InterchangeDistance extends AbstractPermutationDistanceMeasurer { +public final class InterchangeDistance implements NormalizedPermutationDistanceMeasurer { /** * Constructs the distance measurer as specified in the class documentation. diff --git a/src/org/cicirello/permutations/distance/KendallTauDistance.java b/src/org/cicirello/permutations/distance/KendallTauDistance.java index 7b4e6616..49ed5899 100644 --- a/src/org/cicirello/permutations/distance/KendallTauDistance.java +++ b/src/org/cicirello/permutations/distance/KendallTauDistance.java @@ -48,10 +48,10 @@ * M. G. Kendall, "A new measure of rank correlation," Biometrika, vol. 30, no. 1/2, pp. 81–93, June 1938.

* * @author Vincent A. Cicirello, https://www.cicirello.org/ - * @version 1.28.2021 + * @version 4.2.2021 * */ -public final class KendallTauDistance extends AbstractPermutationDistanceMeasurer { +public final class KendallTauDistance implements NormalizedPermutationDistanceMeasurer { /** * Constructs the distance measurer as specified in the class documentation. diff --git a/src/org/cicirello/permutations/distance/LeeDistance.java b/src/org/cicirello/permutations/distance/LeeDistance.java index b079bef4..a94a2e4a 100644 --- a/src/org/cicirello/permutations/distance/LeeDistance.java +++ b/src/org/cicirello/permutations/distance/LeeDistance.java @@ -43,10 +43,10 @@ * C. Lee, "Some properties of nonbinary error-correcting codes," in IRE Transactions on Information Theory, vol. 4, no. 2, pp. 77-82, June 1958.

* * @author Vincent A. Cicirello, https://www.cicirello.org/ - * @version 1.28.2021 + * @version 4.2.2021 * */ -public final class LeeDistance extends AbstractPermutationDistanceMeasurer { +public final class LeeDistance implements NormalizedPermutationDistanceMeasurer { /** * Constructs the distance measurer as specified in the class documentation. diff --git a/src/org/cicirello/permutations/distance/NormalizedPermutationDistanceMeasurer.java b/src/org/cicirello/permutations/distance/NormalizedPermutationDistanceMeasurer.java index 0fd6aba2..e131bb9b 100644 --- a/src/org/cicirello/permutations/distance/NormalizedPermutationDistanceMeasurer.java +++ b/src/org/cicirello/permutations/distance/NormalizedPermutationDistanceMeasurer.java @@ -27,9 +27,9 @@ * normalizing the distance to the interval [0,1], but where the base distance is an integer value. * * @author Vincent A. Cicirello, https://www.cicirello.org/ - * @version 1.28.2021 + * @version 4.2.2021 */ -public interface NormalizedPermutationDistanceMeasurer extends NormalizedPermutationDistanceMeasurerDouble { +public interface NormalizedPermutationDistanceMeasurer extends NormalizedPermutationDistanceMeasurerDouble, PermutationDistanceMeasurer { /** * Computes the maximum possible distance between permutations @@ -39,4 +39,21 @@ public interface NormalizedPermutationDistanceMeasurer extends NormalizedPermuta * @return the maximum distance between a pair of permutations of the specified length. */ int max(int length); + + @Override + default double maxf(int length) { + return max(length); + } + + /** + * {@inheritDoc} + * + * @throws IllegalArgumentException if p1.length() is not equal to p2.length(). + */ + @Override + default double normalizedDistance(Permutation p1, Permutation p2) { + int m = max(p1.length()); + if (m==0) return 0; + return distance(p1,p2) / ((double)m); + } } \ No newline at end of file diff --git a/src/org/cicirello/permutations/distance/NormalizedPermutationDistanceMeasurerDouble.java b/src/org/cicirello/permutations/distance/NormalizedPermutationDistanceMeasurerDouble.java index ce753e68..15259642 100644 --- a/src/org/cicirello/permutations/distance/NormalizedPermutationDistanceMeasurerDouble.java +++ b/src/org/cicirello/permutations/distance/NormalizedPermutationDistanceMeasurerDouble.java @@ -27,19 +27,9 @@ * normalizing the distance to the interval [0,1]. * * @author Vincent A. Cicirello, https://www.cicirello.org/ - * @version 1.28.2021 + * @version 4.2.2021 */ -public interface NormalizedPermutationDistanceMeasurerDouble { - - /** - *

Measures the distance between two permutations, normalized to the interval [0.0, 1.0].

- * - * @param p1 first permutation - * @param p2 second permutation - * @return distance between p1 and p2 normalized to the interval [0.0, 1.0] - * @throws IllegalArgumentException if p1.length() is not equal to p2.length(). - */ - double normalizedDistance(Permutation p1, Permutation p2); +public interface NormalizedPermutationDistanceMeasurerDouble extends PermutationDistanceMeasurerDouble { /** * Computes the maximum possible distance between permutations @@ -49,4 +39,18 @@ public interface NormalizedPermutationDistanceMeasurerDouble { * @return the maximum distance between a pair of permutations of the specified length. */ double maxf(int length); + + /** + *

Measures the distance between two permutations, normalized to the interval [0.0, 1.0].

+ * + * @param p1 first permutation + * @param p2 second permutation + * @return distance between p1 and p2 normalized to the interval [0.0, 1.0] + * @throws IllegalArgumentException if p1.length() is not equal to p2.length(). + */ + default double normalizedDistance(Permutation p1, Permutation p2) { + double m = maxf(p1.length()); + if (m==0) return 0; + return distancef(p1,p2) / m; + } } \ No newline at end of file diff --git a/src/org/cicirello/permutations/distance/PermutationDistanceMeasurer.java b/src/org/cicirello/permutations/distance/PermutationDistanceMeasurer.java index 59d2ddcc..4099fbc8 100644 --- a/src/org/cicirello/permutations/distance/PermutationDistanceMeasurer.java +++ b/src/org/cicirello/permutations/distance/PermutationDistanceMeasurer.java @@ -26,7 +26,7 @@ * Implement this interface, PermutationDistanceMeasurer, to define a distance metric for permutations. * * @author Vincent A. Cicirello, https://www.cicirello.org/ - * @version 1.28.2021 + * @version 4.2.2021 */ public interface PermutationDistanceMeasurer extends PermutationDistanceMeasurerDouble { @@ -39,4 +39,14 @@ public interface PermutationDistanceMeasurer extends PermutationDistanceMeasurer * @throws IllegalArgumentException if p1.length() is not equal to p2.length(). */ int distance(Permutation p1, Permutation p2); + + /** + * {@inheritDoc} + * + * @throws IllegalArgumentException if p1.length() is not equal to p2.length(). + */ + @Override + default double distancef(Permutation p1, Permutation p2) { + return distance(p1,p2); + } } diff --git a/src/org/cicirello/permutations/distance/RTypeDistance.java b/src/org/cicirello/permutations/distance/RTypeDistance.java index f54c12b5..cd5d0247 100644 --- a/src/org/cicirello/permutations/distance/RTypeDistance.java +++ b/src/org/cicirello/permutations/distance/RTypeDistance.java @@ -44,9 +44,9 @@ * INFORMS Journal on Computing, vol. 17, no. 1, pp. 111–122, 2005.

* * @author Vincent A. Cicirello, https://www.cicirello.org/ - * @version 1.28.2021 + * @version 4.2.2021 */ -public final class RTypeDistance extends AbstractPermutationDistanceMeasurer { +public final class RTypeDistance implements NormalizedPermutationDistanceMeasurer { /** * Constructs the distance measurer as specified in the class documentation. diff --git a/src/org/cicirello/permutations/distance/ReinsertionDistance.java b/src/org/cicirello/permutations/distance/ReinsertionDistance.java index f49de510..650300fd 100644 --- a/src/org/cicirello/permutations/distance/ReinsertionDistance.java +++ b/src/org/cicirello/permutations/distance/ReinsertionDistance.java @@ -57,10 +57,10 @@ * Communications of the ACM, 20(5):350-353, May, 1977.

* * @author Vincent A. Cicirello, https://www.cicirello.org/ - * @version 1.28.2021 + * @version 4.2.2021 * */ -public final class ReinsertionDistance extends AbstractPermutationDistanceMeasurer { +public final class ReinsertionDistance implements NormalizedPermutationDistanceMeasurer { /** * Constructs the distance measurer as specified in the class documentation. diff --git a/src/org/cicirello/permutations/distance/ReversalDistance.java b/src/org/cicirello/permutations/distance/ReversalDistance.java index 94f3bc14..8c8b5610 100644 --- a/src/org/cicirello/permutations/distance/ReversalDistance.java +++ b/src/org/cicirello/permutations/distance/ReversalDistance.java @@ -48,9 +48,9 @@ *

We have not used this for N > 10. Warning: time to construct distance measure increases factorially.

* * @author Vincent A. Cicirello, https://www.cicirello.org/ -* @version 2.11.2021 +* @version 4.2.2021 */ -public final class ReversalDistance extends AbstractPermutationDistanceMeasurer { +public final class ReversalDistance implements NormalizedPermutationDistanceMeasurer { private byte[] dist; private final int PERM_LENGTH; diff --git a/src/org/cicirello/permutations/distance/ReversalIndependentDistance.java b/src/org/cicirello/permutations/distance/ReversalIndependentDistance.java index 3ba088db..6cd0feca 100644 --- a/src/org/cicirello/permutations/distance/ReversalIndependentDistance.java +++ b/src/org/cicirello/permutations/distance/ReversalIndependentDistance.java @@ -32,7 +32,7 @@ * to the constructor.

* * @author Vincent A. Cicirello, https://www.cicirello.org/ - * @version 1.28.2021 + * @version 4.2.2021 * */ public final class ReversalIndependentDistance implements PermutationDistanceMeasurer { @@ -67,19 +67,4 @@ public int distance(Permutation p1, Permutation p2) { } return result; } - - /** - * Measures the distance between two permutations, with reversal independence: - * distance = min { distance(p1,p2), distance(p1,reverse(p2)) } - * - * @param p1 first permutation - * @param p2 second permutation - * @return distance between p1 and p2 - * @throws IllegalArgumentException if p1.length() is not equal to p2.length(). - */ - @Override - public final double distancef(Permutation p1, Permutation p2) { - return distance(p1,p2); - } - } \ No newline at end of file diff --git a/src/org/cicirello/permutations/distance/ScrambleDistance.java b/src/org/cicirello/permutations/distance/ScrambleDistance.java index ce745324..9e33cad6 100644 --- a/src/org/cicirello/permutations/distance/ScrambleDistance.java +++ b/src/org/cicirello/permutations/distance/ScrambleDistance.java @@ -32,9 +32,9 @@ *

Runtime: O(n), where n is the permutation length.

* * @author Vincent A. Cicirello, https://www.cicirello.org/ - * @version 1.28.2021 + * @version 4.2.2021 */ -public final class ScrambleDistance extends AbstractPermutationDistanceMeasurer { +public final class ScrambleDistance implements NormalizedPermutationDistanceMeasurer { /** * Constructs the distance measurer as specified in the class documentation. diff --git a/src/org/cicirello/permutations/distance/SquaredDeviationDistance.java b/src/org/cicirello/permutations/distance/SquaredDeviationDistance.java index 71fa6dc3..6e302785 100644 --- a/src/org/cicirello/permutations/distance/SquaredDeviationDistance.java +++ b/src/org/cicirello/permutations/distance/SquaredDeviationDistance.java @@ -44,10 +44,10 @@ * The 6th Metaheuristics International Conference, August, 2005.

* * @author Vincent A. Cicirello, https://www.cicirello.org/ - * @version 1.28.2021 + * @version 4.2.2021 * */ -public final class SquaredDeviationDistance extends AbstractPermutationDistanceMeasurer { +public final class SquaredDeviationDistance implements NormalizedPermutationDistanceMeasurer { /** * Constructs the distance measurer as specified in the class documentation. diff --git a/src/org/cicirello/sequences/distance/AbstractSequenceDistanceMeasurer.java b/src/org/cicirello/sequences/distance/AbstractSequenceDistanceMeasurer.java deleted file mode 100644 index 76d5ce28..00000000 --- a/src/org/cicirello/sequences/distance/AbstractSequenceDistanceMeasurer.java +++ /dev/null @@ -1,128 +0,0 @@ -/* - * Copyright 2018-2019 Vincent A. Cicirello, . - * - * This file is part of JavaPermutationTools (https://jpt.cicirello.org/). - * - * JavaPermutationTools is free software: you can - * redistribute it and/or modify it under the terms of the GNU - * General Public License as published by the Free Software - * Foundation, either version 3 of the License, or (at your - * option) any later version. - * - * JavaPermutationTools 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 General Public License for more - * details. - * - * You should have received a copy of the GNU General Public License - * along with JavaPermutationTools. If not, see . - */ -package org.cicirello.sequences.distance; - -import java.util.List; - -/** - *

Extend this abstract class to define a distance metric for - * permutations where distance is an integer value.

- * - *

If your sequences are guaranteed not to contain duplicates, and - * the pair of sequences is guaranteed to contain the same set of elements, and are of the same length, - * then consider instead extending or using the classes that extend - * the AbstractPermutationDistanceMeasurer - * class. Those classes are specifically for distance between permutations of the integers from 0 to N-1.

- * - * @author Vincent A. Cicirello, https://www.cicirello.org/ - * @version 1.19.6.10 - * @since 1.1 - */ -abstract class AbstractSequenceDistanceMeasurer implements SequenceDistanceMeasurer, SequenceDistanceMeasurerDouble { - - /** - * {@inheritDoc} - */ - @Override - public final double distancef(long[] s1, long[] s2) { - return distance(s1, s2); - } - - /** - * {@inheritDoc} - */ - @Override - public final double distancef(int[] s1, int[] s2) { - return distance(s1, s2); - } - - /** - * {@inheritDoc} - */ - @Override - public final double distancef(short[] s1, short[] s2) { - return distance(s1, s2); - } - - /** - * {@inheritDoc} - */ - @Override - public final double distancef(byte[] s1, byte[] s2) { - return distance(s1, s2); - } - - /** - * {@inheritDoc} - */ - @Override - public final double distancef(char[] s1, char[] s2) { - return distance(s1, s2); - } - - /** - * {@inheritDoc} - */ - @Override - public final double distancef(double[] s1, double[] s2) { - return distance(s1, s2); - } - - /** - * {@inheritDoc} - */ - @Override - public final double distancef(float[] s1, float[] s2) { - return distance(s1, s2); - } - - /** - * {@inheritDoc} - */ - @Override - public final double distancef(boolean[] s1, boolean[] s2) { - return distance(s1, s2); - } - - /** - * {@inheritDoc} - */ - @Override - public final double distancef(String s1, String s2) { - return distance(s1, s2); - } - - /** - * {@inheritDoc} - */ - @Override - public final double distancef(Object[] s1, Object[] s2) { - return distance(s1, s2); - } - - /** - * {@inheritDoc} - */ - @Override - public final double distancef(List s1, List s2) { - return distance(s1, s2); - } -} \ No newline at end of file diff --git a/src/org/cicirello/sequences/distance/EditDistance.java b/src/org/cicirello/sequences/distance/EditDistance.java index 4052731f..3ea04e32 100644 --- a/src/org/cicirello/sequences/distance/EditDistance.java +++ b/src/org/cicirello/sequences/distance/EditDistance.java @@ -1,5 +1,5 @@ /* - * Copyright 2018-2019 Vincent A. Cicirello, . + * Copyright 2018-2019, 2021 Vincent A. Cicirello, . * * This file is part of JavaPermutationTools (https://jpt.cicirello.org/). * @@ -52,10 +52,9 @@ * R. A. Wagner and M. J. Fischer, "The string-to-string correction problem," Journal of the ACM, vol. 21, no. 1, pp. 168–173, January 1974.

* * @author Vincent A. Cicirello, https://www.cicirello.org/ - * @version 1.19.6.10 - * @since 1.1 + * @version 4.2.2021 */ -public class EditDistance implements SequenceDistanceMeasurer, SequenceDistanceMeasurerDouble { +public class EditDistance implements SequenceDistanceMeasurer { private final int insert_i; private final int delete_i; @@ -394,9 +393,6 @@ public final int distance(List s1, List s2) { return distance(s1.toArray(), s2.toArray()); } - /** - * {@inheritDoc} - */ @Override public double distancef(int[] s1, int[] s2) { double[][] D = new double[s1.length + 1][s2.length + 1]; @@ -420,9 +416,6 @@ public double distancef(int[] s1, int[] s2) { return D[s1.length][s2.length]; } - /** - * {@inheritDoc} - */ @Override public double distancef(long[] s1, long[] s2) { double[][] D = new double[s1.length + 1][s2.length + 1]; @@ -446,9 +439,6 @@ public double distancef(long[] s1, long[] s2) { return D[s1.length][s2.length]; } - /** - * {@inheritDoc} - */ @Override public double distancef(short[] s1, short[] s2) { double[][] D = new double[s1.length + 1][s2.length + 1]; @@ -472,9 +462,6 @@ public double distancef(short[] s1, short[] s2) { return D[s1.length][s2.length]; } - /** - * {@inheritDoc} - */ @Override public double distancef(byte[] s1, byte[] s2) { double[][] D = new double[s1.length + 1][s2.length + 1]; @@ -498,9 +485,6 @@ public double distancef(byte[] s1, byte[] s2) { return D[s1.length][s2.length]; } - /** - * {@inheritDoc} - */ @Override public double distancef(char[] s1, char[] s2) { double[][] D = new double[s1.length + 1][s2.length + 1]; @@ -524,9 +508,6 @@ public double distancef(char[] s1, char[] s2) { return D[s1.length][s2.length]; } - /** - * {@inheritDoc} - */ @Override public double distancef(boolean[] s1, boolean[] s2) { double[][] D = new double[s1.length + 1][s2.length + 1]; @@ -550,9 +531,6 @@ public double distancef(boolean[] s1, boolean[] s2) { return D[s1.length][s2.length]; } - /** - * {@inheritDoc} - */ @Override public double distancef(double[] s1, double[] s2) { double[][] D = new double[s1.length + 1][s2.length + 1]; @@ -576,9 +554,6 @@ public double distancef(double[] s1, double[] s2) { return D[s1.length][s2.length]; } - /** - * {@inheritDoc} - */ @Override public double distancef(float[] s1, float[] s2) { double[][] D = new double[s1.length + 1][s2.length + 1]; @@ -602,9 +577,6 @@ public double distancef(float[] s1, float[] s2) { return D[s1.length][s2.length]; } - /** - * {@inheritDoc} - */ @Override public double distancef(String s1, String s2) { double[][] D = new double[s1.length() + 1][s2.length() + 1]; @@ -628,9 +600,6 @@ public double distancef(String s1, String s2) { return D[s1.length()][s2.length()]; } - /** - * {@inheritDoc} - */ @Override public double distancef(Object[] s1, Object[] s2) { double[][] D = new double[s1.length + 1][s2.length + 1]; @@ -654,9 +623,6 @@ public double distancef(Object[] s1, Object[] s2) { return D[s1.length][s2.length]; } - /** - * {@inheritDoc} - */ @Override public final double distancef(List s1, List s2) { return distancef(s1.toArray(), s2.toArray()); diff --git a/src/org/cicirello/sequences/distance/ExactMatchDistance.java b/src/org/cicirello/sequences/distance/ExactMatchDistance.java index 81999f40..54dffe11 100644 --- a/src/org/cicirello/sequences/distance/ExactMatchDistance.java +++ b/src/org/cicirello/sequences/distance/ExactMatchDistance.java @@ -1,5 +1,5 @@ /* - * Copyright 2018-2019 Vincent A. Cicirello, . + * Copyright 2018-2019, 2021 Vincent A. Cicirello, . * * This file is part of JavaPermutationTools (https://jpt.cicirello.org/). * @@ -44,19 +44,15 @@ * S. Ronald, "More distance functions for order-based encodings," in Proc. IEEE CEC. IEEE Press, 1998, pp. 558–563.

* * @author Vincent A. Cicirello, https://www.cicirello.org/ - * @version 1.19.6.10 - * @since 1.1 + * @version 4.2.2021 */ -public final class ExactMatchDistance extends AbstractSequenceDistanceMeasurer { +public final class ExactMatchDistance implements SequenceDistanceMeasurer { /** * Constructs the distance measurer as specified in the class documentation. */ public ExactMatchDistance() {} - /** - * {@inheritDoc} - */ @Override public int distance(int[] s1, int[] s2) { int n = s1.length; @@ -75,9 +71,6 @@ public int distance(int[] s1, int[] s2) { return cost; } - /** - * {@inheritDoc} - */ @Override public int distance(long[] s1, long[] s2) { int n = s1.length; @@ -96,9 +89,6 @@ public int distance(long[] s1, long[] s2) { return cost; } - /** - * {@inheritDoc} - */ @Override public int distance(short[] s1, short[] s2) { int n = s1.length; @@ -117,9 +107,6 @@ public int distance(short[] s1, short[] s2) { return cost; } - /** - * {@inheritDoc} - */ @Override public int distance(byte[] s1, byte[] s2) { int n = s1.length; @@ -138,9 +125,6 @@ public int distance(byte[] s1, byte[] s2) { return cost; } - /** - * {@inheritDoc} - */ @Override public int distance(char[] s1, char[] s2) { int n = s1.length; @@ -159,9 +143,6 @@ public int distance(char[] s1, char[] s2) { return cost; } - /** - * {@inheritDoc} - */ @Override public int distance(boolean[] s1, boolean[] s2) { int n = s1.length; @@ -180,9 +161,6 @@ public int distance(boolean[] s1, boolean[] s2) { return cost; } - /** - * {@inheritDoc} - */ @Override public int distance(double[] s1, double[] s2) { int n = s1.length; @@ -201,9 +179,6 @@ public int distance(double[] s1, double[] s2) { return cost; } - /** - * {@inheritDoc} - */ @Override public int distance(float[] s1, float[] s2) { int n = s1.length; @@ -222,9 +197,6 @@ public int distance(float[] s1, float[] s2) { return cost; } - /** - * {@inheritDoc} - */ @Override public int distance(String s1, String s2) { int n = s1.length(); @@ -243,9 +215,6 @@ public int distance(String s1, String s2) { return cost; } - /** - * {@inheritDoc} - */ @Override public int distance(Object[] s1, Object[] s2) { int n = s1.length; @@ -264,9 +233,6 @@ public int distance(Object[] s1, Object[] s2) { return cost; } - /** - * {@inheritDoc} - */ @Override public int distance(List s1, List s2) { int n = s1.size(); diff --git a/src/org/cicirello/sequences/distance/KendallTauSequenceDistance.java b/src/org/cicirello/sequences/distance/KendallTauSequenceDistance.java index 0b5f6dab..ab79aa45 100644 --- a/src/org/cicirello/sequences/distance/KendallTauSequenceDistance.java +++ b/src/org/cicirello/sequences/distance/KendallTauSequenceDistance.java @@ -77,9 +77,9 @@ * Industrial Networks and Intelligent Systems, 7(23), Article e1, April 2020.

* * @author Vincent A. Cicirello, https://www.cicirello.org/ - * @version 1.30.2021 + * @version 4.2.2021 */ -public final class KendallTauSequenceDistance extends AbstractSequenceDistanceMeasurer { +public final class KendallTauSequenceDistance implements SequenceDistanceMeasurer { private final boolean USE_HASHMAP; @@ -283,6 +283,8 @@ public int distance(Object[] s1, Object[] s2) { /** * {@inheritDoc} + * @throws IllegalArgumentException if s1.size() is not equal to s2.size(), or if they contain + * different elements. */ @Override public int distance(List s1, List s2) { diff --git a/src/org/cicirello/sequences/distance/LongestCommonSubsequenceDistance.java b/src/org/cicirello/sequences/distance/LongestCommonSubsequenceDistance.java index 78c48281..b05b7969 100644 --- a/src/org/cicirello/sequences/distance/LongestCommonSubsequenceDistance.java +++ b/src/org/cicirello/sequences/distance/LongestCommonSubsequenceDistance.java @@ -1,5 +1,5 @@ /* - * Copyright 2018-2019 Vincent A. Cicirello, . + * Copyright 2018-2019, 2021 Vincent A. Cicirello, . * * This file is part of JavaPermutationTools (https://jpt.cicirello.org/). * @@ -46,8 +46,7 @@ * R. A. Wagner and M. J. Fischer, "The string-to-string correction problem," Journal of the ACM, vol. 21, no. 1, pp. 168–173, January 1974.

* * @author Vincent A. Cicirello, https://www.cicirello.org/ - * @version 1.19.6.10 - * @since 1.1 + * @version 4.2.2021 */ public final class LongestCommonSubsequenceDistance extends EditDistance { diff --git a/src/org/cicirello/sequences/distance/SequenceDistanceMeasurer.java b/src/org/cicirello/sequences/distance/SequenceDistanceMeasurer.java index 7ec08f8b..94db8337 100644 --- a/src/org/cicirello/sequences/distance/SequenceDistanceMeasurer.java +++ b/src/org/cicirello/sequences/distance/SequenceDistanceMeasurer.java @@ -1,5 +1,5 @@ /* - * Copyright 2018-2019 Vincent A. Cicirello, . + * Copyright 2018-2019, 2021 Vincent A. Cicirello, . * * This file is part of JavaPermutationTools (https://jpt.cicirello.org/). * @@ -23,20 +23,32 @@ import java.util.List; /** - *

Implement this interface, SequenceDistanceMeasurer, to define a distance metric for sequences. A sequence may have duplicate elements, unlike - * a Permutation which must have unique elements. Some SequenceDistanceMeasurers may require the pair of sequences to be the same length, while - * others do not have that requirement. Some SequenceDistanceMeasurers may require the pair of sequences to contain the same set of elements, while - * others do not have that requirement. Implementations of this interface compute distances that are integer valued.

+ *

Implement this interface, SequenceDistanceMeasurer, to define + * a distance metric for sequences. A sequence may have duplicate elements, unlike + * a Permutation which must have unique elements. Some SequenceDistanceMeasurers + * may require the pair of sequences to be the same length, while + * others do not have that requirement. Some SequenceDistanceMeasurers may + * require the pair of sequences to contain the same set of elements, while + * others do not have that requirement. Implementations of this interface + * compute distances that are integer valued.

* - *

If your sequences are guaranteed not to contain duplicates, and the pair is guaranteed to contain the same set of elements, and are of the same length, - * then consider instead using the classes that implement the {@link org.cicirello.permutations.distance.PermutationDistanceMeasurer PermutationDistanceMeasurer} - * interface. Those classes are specifically for distance between permutations of the integers from 0 to N-1.

+ *

This interface extends the {@link SequenceDistanceMeasurerDouble} interface, + * providing default implementations of all of that superinterface's distancef methods + * by delegating the behavior to the various distance methods. In this way, + * implementers of SequenceDistanceMeasurer should not have a reason to implement + * the distancef methods of {@link SequenceDistanceMeasurerDouble}.

+ * + *

If your sequences are guaranteed not to contain duplicates, and the + * pair is guaranteed to contain the same set of elements, and are of the same length, + * then consider instead using the classes that implement + * the {@link org.cicirello.permutations.distance.PermutationDistanceMeasurer PermutationDistanceMeasurer} + * interface. Those classes are specifically for distance between + * permutations of the integers from 0 to N-1.

* * @author Vincent A. Cicirello, https://www.cicirello.org/ - * @version 1.19.6.10 - * @since 1.1 + * @version 4.2.2021 */ -public interface SequenceDistanceMeasurer { +public interface SequenceDistanceMeasurer extends SequenceDistanceMeasurerDouble { /** * Measures the distance between two arrays. @@ -117,7 +129,6 @@ public interface SequenceDistanceMeasurer { * The objects in the arrays must be of a class that has overridden the * Object.equals method. * - * @since 1.2.3 * @param s1 First array. * @param s2 Second array. * @return distance between s1 and s2 @@ -129,12 +140,69 @@ public interface SequenceDistanceMeasurer { * The objects in the lists must be of a class that has overridden the * Object.equals method. * - * @since 1.5 * @param s1 First list. * @param s2 Second list. * @param Type of List elements. * @return distance between s1 and s2 */ int distance(List s1, List s2); + + // Default implementations of superinterface methods, + // delegating behavior of distancef methods to distance methods. + + @Override + default double distancef(long[] s1, long[] s2) { + return distance(s1, s2); + } + + @Override + default double distancef(int[] s1, int[] s2) { + return distance(s1, s2); + } + + @Override + default double distancef(short[] s1, short[] s2) { + return distance(s1, s2); + } + + @Override + default double distancef(byte[] s1, byte[] s2) { + return distance(s1, s2); + } + + @Override + default double distancef(char[] s1, char[] s2) { + return distance(s1, s2); + } + + @Override + default double distancef(double[] s1, double[] s2) { + return distance(s1, s2); + } + + @Override + default double distancef(float[] s1, float[] s2) { + return distance(s1, s2); + } + + @Override + default double distancef(boolean[] s1, boolean[] s2) { + return distance(s1, s2); + } + + @Override + default double distancef(String s1, String s2) { + return distance(s1, s2); + } + + @Override + default double distancef(Object[] s1, Object[] s2) { + return distance(s1, s2); + } + + @Override + default double distancef(List s1, List s2) { + return distance(s1, s2); + } } diff --git a/src/org/cicirello/sequences/distance/SequenceDistanceMeasurerDouble.java b/src/org/cicirello/sequences/distance/SequenceDistanceMeasurerDouble.java index 471b4dc0..185567ae 100644 --- a/src/org/cicirello/sequences/distance/SequenceDistanceMeasurerDouble.java +++ b/src/org/cicirello/sequences/distance/SequenceDistanceMeasurerDouble.java @@ -1,5 +1,5 @@ /* - * Copyright 2018-2019 Vincent A. Cicirello, . + * Copyright 2018-2019, 2021 Vincent A. Cicirello, . * * This file is part of JavaPermutationTools (https://jpt.cicirello.org/). * @@ -33,8 +33,7 @@ * interface. Those classes are specifically for distance between permutations of the integers from 0 to N-1.

* * @author Vincent A. Cicirello, https://www.cicirello.org/ - * @version 1.19.6.10 - * @since 1.1 + * @version 4.2.2021 */ public interface SequenceDistanceMeasurerDouble { @@ -117,7 +116,6 @@ public interface SequenceDistanceMeasurerDouble { * The objects in the arrays must be of a class that has overridden the * Object.equals method. * - * @since 1.2.3 * @param s1 First array. * @param s2 Second array. * @return distance between s1 and s2 @@ -129,7 +127,6 @@ public interface SequenceDistanceMeasurerDouble { * The objects in the lists must be of a class that has overridden the * Object.equals method. * - * @since 1.5 * @param s1 First list. * @param s2 Second list. * @param Type of List elements. diff --git a/tests/org/cicirello/math/rand/RandomIndexerSampleTests.java b/tests/org/cicirello/math/rand/RandomIndexerSampleTests.java index ef1c2b02..b42d7dfd 100644 --- a/tests/org/cicirello/math/rand/RandomIndexerSampleTests.java +++ b/tests/org/cicirello/math/rand/RandomIndexerSampleTests.java @@ -1466,6 +1466,30 @@ public void testNextWindowedIntTriple_SR() { assertTrue(result[0] < result[1]); assertTrue(result[1] < result[2]); } + n = 10000; + w = n-1; + for (int i = 0; i < 30; i++) { + int[] result = RandomIndexer.nextWindowedIntTriple(n, w, null, true, gen); + assertEquals("Length of result should be 3", 3, result.length); + assertTrue(result[0] < result[1]); + assertTrue(result[1] < result[2]); + } + n = 100; + w = n-1; + for (int i = 0; i < 30; i++) { + int[] result = RandomIndexer.nextWindowedIntTriple(n, w, null, true, gen); + assertEquals("Length of result should be 3", 3, result.length); + assertTrue(result[0] < result[1]); + assertTrue(result[1] < result[2]); + } + n = 10; + w = n-1; + for (int i = 0; i < 30; i++) { + int[] result = RandomIndexer.nextWindowedIntTriple(n, w, null, true, gen); + assertEquals("Length of result should be 3", 3, result.length); + assertTrue(result[0] < result[1]); + assertTrue(result[1] < result[2]); + } } for (int n = 3; n <= 10; n++) {