All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.cicirello.replication.arxiv2019may.CompareKendallTauSequenceDistAlgsInts Maven / Gradle / Ivy

Go to download

This package contains several example programs of the usage of the JavaPermutationTools (JPT) library. JPT is a Java library that enables representing and generating permutations and sequences, as well as performing computation on permutations and sequences. It includes implementations of a variety of permutation distance metrics as well as distance metrics on sequences (i.e., Strings, arrays, and other ordered data types). In addition to programs demonstrating the usage of the JPT library, the jpt-examples package also contains programs for replicating the experiments from a few published papers that utilized the library or implementations on which the library is based. JPT's source code is maintained on GitHub, and the prebuilt jars of the library can be imported from Maven Central using maven or other build tools. The purpose of the package jpt-examples is to demonstrate usage of the major functionality of the JPT library. You can find out more about the JPT library itself from its website: https://jpt.cicirello.org/.

The newest version!
/*
 * Example programs for JavaPermutationTools library.
 * Copyright (C) 2019-2023 Vincent A. Cicirello
 *
 * This program 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.
 *
 * This program 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 this program.  If not, see .
 */
package org.cicirello.replication.arxiv2019may;

import java.lang.management.*;
import java.lang.ref.WeakReference;
import java.util.concurrent.ThreadLocalRandom;
import org.cicirello.examples.jpt.ExamplesShared;
import org.cicirello.sequences.distance.KendallTauSequenceDistance;

/**
 * This program replicates the data for the paper:
* V.A. Cicirello, "Kendall Tau Sequence Distance: Extending Kendall Tau from Ranks to Sequences," * arXiv preprint arXiv:1905.02752 [cs.DM], May 2019. * * @author Vincent A. Cicirello, https://www.cicirello.org/ */ public class CompareKendallTauSequenceDistAlgsInts { private static int toTime(KendallTauSequenceDistance d, int[][] a, int[][] b) { // Summing and returning the sum of distances is // just to prevent the Java JIT from considering this dead code and optimizing it away. int total = 0; for (int i = 0; i < a.length; i++) { int distance = d.distance(a[i], b[i]); total += distance; } return total; } /** * Runs the replication program. * * @param args Ignored, because there are no command line arguments. */ public static void main(String[] args) { ExamplesShared.printCopyrightAndLicense(); KendallTauSequenceDistance dHash = new KendallTauSequenceDistance(); KendallTauSequenceDistance dSort = new KendallTauSequenceDistance(true); final int MIN_L = 256; final int MAX_L = 131072; final int N = 100; int[] alphabetSize = {1, 4, 16, 64, 256, 1024, 4096, 16384, 65536}; ThreadMXBean bean = ManagementFactory.getThreadMXBean(); bean.setThreadCpuTimeEnabled(true); // WARM UP THE JAVA JIT. int[][] iArrays = new int[N * N][]; int[][] iShuffled = new int[N * N][]; for (int i = 0; i < iArrays.length; i++) { iArrays[i] = randIntSequence(1000, 100); iShuffled[i] = shuffleCopy(iArrays[i]); } toTime(dHash, iArrays, iShuffled); toTime(dSort, iArrays, iShuffled); // force garbage collection of extra large arrays used during warmup WeakReference ref1 = new WeakReference(iArrays); WeakReference ref2 = new WeakReference(iShuffled); iArrays = null; iShuffled = null; while (ref1.get() != null || ref2.get() != null) { System.gc(); } // END WARM UP PHASE. System.out.printf("%8s\t%8s\t%8s\t%8s%n", "L", "Alphabet", "TimeHash", "TimeSort"); int x = 0; for (int L = MIN_L; L <= MAX_L; L *= 2) { for (int j = 0; j < alphabetSize.length; j++) { int R = alphabetSize[j]; // if (R < 1) R = 1; iArrays = new int[N][]; iShuffled = new int[N][]; for (int i = 0; i < N; i++) { iArrays[i] = randIntSequence(L, R); iShuffled[i] = shuffleCopy(iArrays[i]); } System.out.printf("%8d\t%8d", L, R); long start = bean.getCurrentThreadCpuTime(); int dSum1 = toTime(dHash, iArrays, iShuffled); long end = bean.getCurrentThreadCpuTime(); double seconds = 1.0 * (end - start) / 1000000000 / N; System.out.printf("\t%8.6f", seconds); start = bean.getCurrentThreadCpuTime(); int dSum2 = toTime(dSort, iArrays, iShuffled); end = bean.getCurrentThreadCpuTime(); seconds = 1.0 * (end - start) / 1000000000 / N; System.out.printf("\t%8.6f", seconds); System.out.println(); // Do something with the return values to trick Java JIT. x += (dSum1 - dSum2); } } System.out.println("Done " + x); } private static int[] shuffleCopy(int[] s) { int[] r = s.clone(); for (int i = s.length - 1; i > 0; i--) { int j = ThreadLocalRandom.current().nextInt(i + 1); if (i == j) continue; int temp = r[i]; r[i] = r[j]; r[j] = temp; } return r; } private static int[] randIntSequence(int length, int range) { int[] s = new int[length]; for (int i = 0; i < length; i++) { s[i] = ThreadLocalRandom.current().nextInt(range); } return s; } }