我正在尝试使用最差拟合启发式编写一个装箱程序,以便它将权重添加到垃圾箱中,直到它们无法再存储,因为它们被文件读取并将垃圾箱序列放入优先级队列中,以便它将剩余空间最少的垃圾箱放在顶部。但是我在编写 Bin 类的比较器时遇到了麻烦。这是完整的代码:
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();
}
}
}