1
\$\begingroup\$

if I have a large 2D array let's call it a dataset and I want to divide it into a number of 2D array of equal size that is smaller and easy to processing it(let's treat it as chunks), by using PSO algorithm I take the best position values, utilize it as an index of the dataset then putting these value in the chunks.. this code gives me a number of repeated chunks that has the same value but I want different values for each chunk from all the dataset

// partitioning the dataset 
        int k = 0;//chunks counter
        double[][] ch = new double[125][14];
        while(k!=dataset.length){
        best = swarm.getBestPosition();
        ch = DatasetChunks(best, dataset);  
        ChunksPrint(ch, k);
       
        k++;
        }
 private static double[][] DatasetChunks(double[] best, double[][] dataset) {
        dataset = LoadData();
        int row = 125, col = 14;
        double[][] ch1 = new double[row][col];
        int[] s2 = new int[best.length];
        //treat the negative and repeated value of best position
        Set<Integer> st = new HashSet<>();
        for (int i = 0; i < s2.length - 1; i++) {
            s2[i] = (int) best[i];
            if (s2[i] < 0) {
                s2[i] *= -1;
            }
        }

        for (int i = 0; i < s2.length; i++) {
            // check whether the element is 
            // repeated or not 
            if (!st.contains(s2[i])) {
                st.add(s2[i]);
            } else {
                // find the next greatest element 
                for (int j = (int)(s2[i] + Math.random()); j < Integer.MAX_VALUE; j++) {
                    if (!st.contains(j) ) {
                        s2[i] = j;
                        st.add(j);
                        break;
                    }
                }
            }
        }
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                ch1[i][j] = dataset[s2[i]][j];
            }
        }
        return ch1;
    }
\$\endgroup\$
3
  • 4
    \$\begingroup\$ [... ]this code gives me a number of repeated chunks that has the same value but I want different values for each chunk from all the dataset Hello, if the code doesn't give you the correct result, then the question is off-topic. What topics can I ask about here? Code Review aims to help improve working code. If you are trying to figure out why your program crashes or produces a wrong result, ask on Stack Overflow instead. \$\endgroup\$ – Doi9t Nov 1 '20 at 13:22
  • \$\begingroup\$ Assuming swarm.getBestPosition() to return the same result every time, what do you expect DatasetChunks() to return? \$\endgroup\$ – greybeard Nov 2 '20 at 19:36
  • \$\begingroup\$ @greybeard it must partition the whole dataset based on the best position without any repeat \$\endgroup\$ – Hanin Farhan Nov 3 '20 at 8:03

Browse other questions tagged or ask your own question.