0

I'm trying to write a bin-packing program using worst-fit heuristic so that the it adds weights into bins until they cannot store any more as they are read by the file and put the sequence of bins into a priority queue so that it places the bin with the least remaining space on the top. But I'm having trouble with writing the comparator of the Bin class. Here's the full code:

public class BinPacking{

    public static class Bin implements Comparable<Bin> {

        int ID ;
        int remSpace;
        ArrayList<Integer> weights = new ArrayList<Integer>();

        public Bin(int ID){
            this.ID = ID;
            remSpace = 100;
        }

        public void add(int size){
            remSpace -= size;
            weights.add(size);
        }

        @Override
        public int compareTo(Bin o) {
            return remSpace;
        }

}



    public static void main(String[] args) throws FileNotFoundException{


        PriorityQueue<Bin> pq =new PriorityQueue<Bin>();

        File myFile = new File("input.txt");

        int binId = 1;
        Bin d = new Bin(binId);
        pq.add(d);
        int size;


        Scanner input = new Scanner(myFile); 

        while (input.hasNext())
        {

            size = input.nextInt();

            d = (Bin)pq.peek();

            if (d.remSpace >= size)
            {
                pq.remove(d);
                d.add(size);
                pq.add(d);

            }
            else
            {
                binId++;
                d = new Bin(binId);
                d.add(size);
                pq.add(d);



            }
      }
       System.out.println("Number of bins used: " + binId); 

       int mylst[][] = new int[binId][1000];
       int k =1;
       for(int i=0;i<binId;i++){
           System.out.println("Bin" + k + ": ");
           k++;
           for(int j=0;j<pq.peek().weights.size();j++){

              mylst[i][j] = pq.peek().weights.get(j);
              System.out.print(" "+mylst[i][j]);
           }
          System.out.println();
           pq.poll();
       }



    }
}
Sinkingpoint
  • 7,449
  • 2
  • 29
  • 45
Umut
  • 409
  • 3
  • 7
  • 20

1 Answers1

2

Comparable#compareTo(T) is meant to return the difference between two objects, allowing an algorithm to decide whether one item i equal, less than or greater than another. Returning the remaining space of one will not give the order than you want, as this does not compare the two objects. Try:

public int compareTo(Bin o) {
     if(o == null)return 1;
     if(remSpace > o.remSpace)return 1;
     else if(remSpace < o.remSpace)return -1;
     return 0;
}

Notice how this returns the difference in the spaces of the two Bins, so that a difference may be gauged.

Sinkingpoint
  • 7,449
  • 2
  • 29
  • 45
  • While it's an edge-case, straight subtraction isn't recommended since then overflows give you incorrect results. The typical approach is to actually perform the comparison, and return -1, 0 or 1 as appropriate. As well, guarding against null is worthwhile. – dlev May 01 '13 at 00:28
  • 1
    To be honest, it's likely a non-issue in this case, since I can't imagine any sensible code elsewhere assigns a negative value to `remSpace`, so overflow shouldn't ever occur. Just something to keep in mind down the road :) – dlev May 01 '13 at 00:35