Created
October 8, 2024 09:36
-
-
Save coderodde/d68d24471acc59f0841aa157ce917a7d to your computer and use it in GitHub Desktop.
The research program for fetching the finger configurations producing minimum and maximum configurations.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.List; | |
public class ComputeFingerListConfigurationsWithEntropies { | |
public static void main(String[] args) { | |
FingerList fingerList = new FingerList(); | |
int lineNumber = 1; | |
double highestEntropy = Double.NEGATIVE_INFINITY; | |
double lowestEntropy = Double.POSITIVE_INFINITY; | |
int highestEntropySum = Integer.MIN_VALUE; | |
int lowestEntropySum = Integer.MAX_VALUE; | |
List<int[]> highestEntropyConfigurations = new ArrayList<>(); | |
List<int[]> lowestEntropyConfigurations = new ArrayList<>(); | |
do { | |
int sum = fingerList.getEntropySum(); | |
double entropy = fingerList.getEntropy(); | |
int[] configuration = fingerList.getFingerConfiguration(); | |
if (highestEntropy < entropy) { | |
highestEntropy = entropy; | |
highestEntropySum = sum; | |
highestEntropyConfigurations.clear(); | |
highestEntropyConfigurations.add( | |
fingerList.getFingerConfiguration()); | |
} else if (highestEntropy == entropy) { | |
highestEntropyConfigurations.add( | |
fingerList.getFingerConfiguration()); | |
} | |
if (lowestEntropy > entropy) { | |
lowestEntropy = entropy; | |
lowestEntropySum = sum; | |
lowestEntropyConfigurations.clear(); | |
lowestEntropyConfigurations.add( | |
fingerList.getFingerConfiguration()); | |
} else if (lowestEntropy == entropy) { | |
lowestEntropyConfigurations.add( | |
fingerList.getFingerConfiguration()); | |
} | |
System.out.printf("%4d %s: %f\n", | |
lineNumber++, | |
fingerList, | |
fingerList.getEntropy()); | |
} while (fingerList.incrementFingerList()); | |
// Print statistics: | |
System.out.printf("Highest entropy sum: %d.\n", highestEntropySum); | |
System.out.printf("Lowest entropy sum: %d.\n", lowestEntropySum); | |
System.out.printf("Highest entropy: %f.\n", highestEntropy); | |
System.out.printf("Lowest entropy: %f.\n", lowestEntropy); | |
System.out.println("Highest entropy configurations:"); | |
for (final int[] configuration : highestEntropyConfigurations) { | |
System.out.printf( | |
"%s, H = %f, sum = %d.\n", | |
Arrays.toString(configuration), | |
fingerList.getEntropy(configuration), | |
fingerList.getEntropySum(configuration)); | |
} | |
System.out.println("Lowest entropy configurations:"); | |
for (final int[] configuration : lowestEntropyConfigurations) { | |
System.out.printf( | |
"%s, H = %f, sum = %d.\n", | |
Arrays.toString(configuration), | |
fingerList.getEntropy(configuration), | |
fingerList.getEntropySum(configuration)); | |
} | |
} | |
} | |
class FingerList { | |
private static final int N = 16; | |
private static final int FINGER_COUNT = 4; | |
private final int[] fingerList; | |
FingerList() { | |
this.fingerList = new int[FINGER_COUNT + 1]; | |
for (int i = 0; i < FINGER_COUNT; i++) { | |
fingerList[i] = i; | |
} | |
fingerList[FINGER_COUNT] = N; | |
} | |
@Override | |
public String toString() { | |
final StringBuilder sb = new StringBuilder().append("["); | |
sb.append(fingerList[0]); | |
for (int i = 1; i <= FINGER_COUNT; i++) { | |
sb.append(", ").append(fingerList[i]); | |
} | |
return sb.append("]").toString(); | |
} | |
public int[] getFingerConfiguration() { | |
return fingerList.clone(); | |
} | |
public boolean incrementFingerList() { | |
for (int i = FINGER_COUNT - 1; i >= 0; i--) { | |
if (fingerList[i] + 1 < fingerList[i + 1]) { | |
fingerList[i]++; | |
for (int j = i + 1; j < FINGER_COUNT; j++) { | |
fingerList[j] = fingerList[j - 1] + 1; | |
} | |
return true; | |
} | |
} | |
return false; | |
} | |
public double getEntropy() { | |
return getEntropy(fingerList); | |
} | |
public double getEntropy(final int[] fingers) { | |
int sum = getEntropySum(fingers); | |
return 1.0 - sum / ((double) N); | |
} | |
public int getEntropySum(final int[] fingers) { | |
int sum = 0; | |
for (int i = 0; i < fingers.length - 1; i++) { | |
int finger1 = fingers[i]; | |
int finger2 = fingers[i + 1]; | |
sum += Math.abs(finger2 - finger1 - (fingers.length - 1)); | |
} | |
return sum; | |
} | |
public int getEntropySum() { | |
return getEntropySum(fingerList); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment