我正在使用supplyCounter这是一个全局变量,用于计算制造商为库存生成新随机数的次数
UILabel
输出
Firstfit花费的时间是:5009697 nanoSeconds
制造商为Firstfit提供的绳索数量为:649根绳子
Bestfit花费的时间是:5358148 nanoSeconds
制造商为Bestfit提供的绳索数量为:0绳索
但如果我只运行最佳拟合算法,则计数器返回一个合理的值,该值不是0
public class test {
private static Random manufacturer;
private static ArrayList<Integer> ordersForFirstFit, stocksForFirstFit, ordersForBestFit, stocksForBestFit;
private static int supplyCounter = 0;
public test() {
}
public static void main(String[] args)
{
ordersForFirstFit = createCustomerOrders(1000);
ordersForBestFit = ordersForFirstFit;// using same data as first fit for best fit
stocksForFirstFit = createStockOfRope();
stocksForBestFit = stocksForFirstFit;// using same data as first fit for best fit
manufacturer = new Random();
supplyCounter = 0;
firstFitAlgorithm(stocksForFirstFit, ordersForFirstFit);
//showCustomerOrders(ordersForFirstFit);//for testing and debugging
//showStockOfRope(stocksForFirstFit);//for testing and debugging
supplyCounter = 0;
bestFitAlgorithm(stocksForBestFit, ordersForBestFit);
//showCustomerOrders(ordersForBestFit);//for testing and debugging
//showStockOfRope(stocksForBestFit);//for testing and debugging
}
public static ArrayList<Integer> createCustomerOrders(int n) {
ArrayList<Integer> ropeOrder = new ArrayList<Integer>();
Random randomOrders = new Random();
for(int i = 0; i < n+1; i++)
{
ropeOrder.add(randomOrders.nextInt(100)+1);// because the range include 0 but exclude 100, therefore +1
//System.out.println("Rope Orders "+i+": "+ropeOrder.get(i));
}
return ropeOrder;
}
public static void showCustomerOrders(ArrayList<Integer> ropeOrder) {
for(int i = 0; i < ropeOrder.size()-1; i++)
{
System.out.println("Rope Orders "+i+": "+ropeOrder.get(i));
}
}
public static ArrayList<Integer> createStockOfRope() {
ArrayList<Integer> ropeStock = new ArrayList<Integer>();
Random randomStocks = new Random();
for(int i = 0; i < 6; i++)
{
ropeStock.add(i,randomStocks.nextInt(100)+101);//for a range between 100 to 200
//System.out.println("Rope(s) in Stock "+i+": "+ropeStock.get(i));
}
return ropeStock;
}
public static void showStockOfRope(ArrayList<Integer> ropeStock) {
for(int i = 0; i < ropeStock.size()-1; i++)
{
System.out.println("Rope(s) in Stock "+i+": "+ropeStock.get(i));
}
}
public static void insertionSort(ArrayList<Integer> data) {
//initialize variables
int key = 0, j = 0;
for(int i = 1; i < data.size(); i++)
{
key = data.get(i);
j = i;
while( (j>0)&&(key < data.get(j-1)) )
{
data.set(j, data.get(j-1));
j = j-1;
}
data.set(j, key);
}
}
public static void firstFitAlgorithm(ArrayList<Integer> ropeStock, ArrayList<Integer> ropeOrder) {
//int supplyCounter = 0;
long begin = System.nanoTime();
for(int i = 0; i < ropeOrder.size(); i++)
{
int j = 0;
while( ropeOrder.get(i) > ropeStock.get(j) )
{
if(j == ropeStock.size()-1)
{
ropeStock.add(j,manufacturer.nextInt(100)+101);//Buy a rope from manufacturer then put the rope to the stock
supplyCounter++;
//then check to see whether it fits the requirement, if not, do the previous step again
}
else
j++;
}
if( ropeOrder.get(i) <= ropeStock.get(j) )
{
if(ropeStock.get(j) - ropeOrder.get(i) > 5)//Assume rope coils shorter than 5m are scrapped
{
ropeStock.set(j, ropeStock.get(j) - ropeOrder.get(i));
ropeOrder.set(i, 0);
}
else
{
ropeStock.remove(j);
if(ropeStock.size() == 0)//when the last rope in stock is sold, buy a new coil of rope from manufacturer
ropeStock.add(manufacturer.nextInt(100)+101);
supplyCounter++;
ropeOrder.set(i, 0);
}
}
}
long total = System.nanoTime() - begin;
System.out.println("The time spent for Firstfit is: "+total+" nanoSeconds");
System.out.println("The number of rope supplied by manufacturer for Firstfit is: "+supplyCounter+" ropes");
}
public static void bestFitAlgorithm(ArrayList<Integer> ropeStock, ArrayList<Integer> ropeOrder) {
//int supplyCounter = 0;
long begin = System.nanoTime();
insertionSort(ropeStock);
insertionSort(ropeOrder);
int j = 0;
for(int i = 0; i < ropeOrder.size(); i++)
{
while( ropeOrder.get(i) > ropeStock.get(j) )
{
if(j == ropeStock.size()-1)
{
ropeStock.add(manufacturer.nextInt(100)+101);//Buy a rope from manufacturer then put the rope to the stock
insertionSort(ropeStock);//then check to see whether it fits the requirement, if not, do the previous step again
j++;
supplyCounter++;
}
else
j++;
}
if( ropeOrder.get(i) <= ropeStock.get(j) )
{
if(ropeStock.get(j) - ropeOrder.get(i) > 5)//Assume rope coils shorter than 5m are scrapped
{
ropeStock.set(j, ropeStock.get(j) - ropeOrder.get(i));
insertionSort(ropeStock);
ropeOrder.set(i, 0);
}
else
{
ropeStock.remove(j);
if(ropeStock.size() == 0)//when the last rope in stock is sold, buy a new coil of rope from manufacturer
ropeStock.add(manufacturer.nextInt(100)+101);
supplyCounter++;
ropeOrder.set(i, 0);
}
j = 0;
}
}
long total = System.nanoTime() - begin;
System.out.println("The time spent for Bestfit is: "+total+" nanoSeconds");
System.out.println("The number of rope supplied by manufacturer for Bestfit is: "+supplyCounter+" ropes");
}
}
结果:
Bestfit花费的时间是:31895376 nanoSeconds
制造商为Bestfit提供的绳索数量为:465绳索
我也尝试在方法中创建计数器而不是使用全局变量,但我得到了相同的结果。
有谁知道发生了什么事?
答案 0 :(得分:1)
您的问题是您在两种算法之间共享数据。
更改
ordersForBestFit = ordersForFirstFit;
要
ordersForBestFit = new ArrayList<Integer>(ordersForFirstFit);
并改变
stocksForBestFit = stocksForFirstFit;
要
stocksForBestFit = new ArrayList<Integer>(stocksForFirstFit);
ArrayList的内容将是相同的整数。但是当您在第一个算法中对列表进行排序时,它会影响ArrayList的状态,这会导致下一个算法出现问题。通过创建一个新的ArrayList并将其传递给第二个算法,可以避免第一个算法的副作用。