0

I have an algorithm that does the following:

Given I have an array array of length n It's goal is to merge certain elements based on some condition (it this case entropy). It calculates the entropy e_all of the entire array and calculates the entropy e_merged of the array where element n and n+1 are merged. It does that for each pair of adjacent elements. The pair where the difference in e_all - e_merged is greatest are marged. If there is a merge, the algorithm is applied again on the new array with length n-1.

As you can see, this takes in the worst case n^2 - 1 iterations and if n is big it might take minutes or even hours to complete. Therefore I was wondering how I can parallelise this algorithms. Basically it should be able calculate the entropies on i cores and when all the elements are evaluated the results should be merged and a conclusion can be drawn.

How can I do such a thing? Which kinds of code pieces or idea's must I implement for it to work this way? Or is there a better way?

public double[] applyAlgorithm(double[] array) {
    boolean merging = false;
    for (int i = 0; i < array.length - 1; i++) {
        double[] entropy = getEntropy(array); // returns list of entropy for all adjacent intervals
        int idx = 0;
        double max = Double.NEGATIVE_INFINITY;
        for (int j = 0; j < entropy.length; j++) {
            if (entropy[j] > max) {
                max = entropy[j];
                idx = j;
            }
        }
        if (max > 0) {
            array = mergeAdjacentIntervals(array, idx); //merge intervals that have the max entropy, if the entropy is > 0
            merging = true;
            break;
        }
    }
    if (merging) {
        array = applyAlgorithm(array);
    }
    return array;
}

private double[] getEntropy(double[] array) {
    double[] entropy = new double[array.length - 1];
    double[] tempArray = new double[array.length - 1];

    double baseEntropy = calculateEntropy(array);
    for (int i = 0; i < entropy.length; i++) {
        tempArray = mergeAdjacentIntervals(array, idx);
        entropy[i] = baseEntropy - calculateEntropy(tempArray);
    }
    return entropy;
}
user3354890
  • 367
  • 1
  • 3
  • 10
  • Do you have any code? If you have it, please post it. It will be easier to solve – rpax Mar 13 '14 at 13:47
  • A simple example, with an array of 10 elements, may be useful too – rpax Mar 13 '14 at 13:49
  • It's not clear how the output should be. It's a single number? – rpax Mar 13 '14 at 16:31
  • The output can be anything. From an array with length ``n`` (the original length) to an array with a single element, but always an array. Basically, the split only occurs if the measurement (entropy) is accepted. If and only if there was a split, the algorithm will do the same thing, only with the new array of length ``n-1``. If there was no split, the algorithm is finished. – user3354890 Mar 14 '14 at 12:31
  • Ok. I think I've got the idea. Just for being sure, can you post one example, or two? For testing, basically. – rpax Mar 14 '14 at 13:41
  • I think the problem is here: _Basically, the split only occurs if the measurement (entropy) is accepted._ I can't help you if you don't explain better – rpax Mar 15 '14 at 13:20
  • @rpax While trying to write a minimal example I went over my code and realized that there was a bug somewhere... Posting bugged code doesn't make any sense so I will first try to fix it. – user3354890 Mar 19 '14 at 09:09
  • I'm not asking for _real code_, only some kind of pseudocode. The entropy you are talking about is Shanon's, right? – rpax Mar 19 '14 at 09:13
  • @rpax Yes, I'm using Shannon's entropy. – user3354890 Mar 19 '14 at 09:17
  • How is `getEntropy(array);` implemented? It seems to be a `for loop` wich calculates `mergeAdjacentIntervals(array, i)``for every i. I'm right? Can you post that too? – rpax Mar 19 '14 at 11:50
  • Also, how are you _merging_ the values? – rpax Mar 19 '14 at 12:49
  • I think there's another bug on your code : Shouldn't be `array=applyAlgorithm(array);` ? – rpax Mar 19 '14 at 13:15
  • @rpax I'm trying to minimze the code, so by translating I might have forgotten the ``array=applyAlgorithm(array);`` code. In the original code it is modifying an object so that line is not necessary. – user3354890 Mar 19 '14 at 13:52
  • `mergeAdjacentIntervals` is missing, and also, the `merge` function I asked you before. – rpax Mar 19 '14 at 14:44
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/50131/discussion-between-rpax-and-user3354890) – rpax Mar 20 '14 at 13:22

0 Answers0