Java背包:NullPointerException [英] Java Knapsack: NullPointerException

查看:105
本文介绍了Java背包:NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的代码尝试求解/创建最大的背包时(最大背包使其中装有物品的背包的总总价值最大化,并使重量最小化),我试图让我的遗传算法在十点后停止运行如果传递给它的最好的背包与以前一样,那就是世代相传.

When my code is trying to solve/create the greatest Knapsack (greatest Knapsack maximizes the overall total value of the Knapsack with items in it and minimizes amount of weight), I am trying to get my genetic algorithm to stop running after ten generations if the best Knapsack passed to it is the same as before.

标有背包的参考是一个背包,其中包含可以放入后代背包中的所有潜在物品.标有背包的副本是参考的副本,但是将从该副本背包中删除项目,以避免在新生成的背包中重复.用//...注释的部分已删除代码,我认为这并不是问题的一部分,但是我在指令中标记了代码的作用.

The knapsack labeled reference is a knapsack that contains all potential items that can be put into the later generation knapsacks. The knapsack labeled copy is a copy of the reference, but items will be removed from that copy Knapsack to avoid duplicates within the new generated Knapsack. The portions commented with // ... is code removed that I believe are not apart of the problem, but I labeled the instructions on what the code would do.

ArrayList<Knapsack> knapsackPopulation = new ArrayList<Knapsack>();
  // creates initial population to find Fittest to compare to later
  // ...

     Knapsack generated = new Knapsack();
     boolean canRun = true;
     int currentWeight = 0;
     int itemsContained = 0;

     // this is the process to find the best/fittest Knapsack, which will be used later to compare to
     // ...

  Knapsack bestKnapsack = knapsackPopulation.get(indexOfBest); // best Knapsack found

  // Print out results, weight, and value of overall best Knapsack.

  // ...

  // Mutation portion of code, comparing it to the fittest Knapsack we found earlier 
  boolean hasNotChangedInTenGenerations = true;
  int generationsUnchanged = 0; 

  while ((hasNotChangedInTenGenerations != false) && (generationsUnchanged < 10)) {   
  for (int m=0; m<100; m++) {

     Knapsack copy = new Knapsack(); 
     for (int j=0; j<reference.size(); j++) {
        copy.add(reference.get(j));
     }

     Knapsack copyBest = new Knapsack();
     for (int p=0; p<bestKnapsack.size(); p++) {
        copyBest.add(bestKnapsack.get(p));
     }

     Knapsack generated = new Knapsack();
     boolean canRun = true;
     int currentWeight = 0;
     int itemsContained = 0;

     while (canRun && (currentWeight <= reference.getMaxWeight())) {
        int randomNum = (int)(Math.random() * (((copy.size()-1) + 1)));
        int randomNumBest = (int)(Math.random() * (((copyBest.size()-1) + 1)));
        if (((currentWeight + copy.get(randomNum).getWeight()) < reference.getMaxWeight()) && ((currentWeight + copyBest.get(randomNumBest).getWeight()) < reference.getMaxWeight()) && (copy.get(randomNum) != null) && (copyBest.get(randomNumBest) != null)){

           int randomTwoThirds = (int)(Math.random() * 3);

           if (randomTwoThirds >= 2) { 
              currentWeight += copy.get(randomNum).getWeight();
              generated.add(copy.get(randomNum));
              copy.remove(randomNum);
           } else {
              currentWeight += copyBest.get(randomNumBest).getWeight();
              generated.add(copyBest.get(randomNumBest));
              copyBest.remove(randomNumBest);
           }

           itemsContained++;

        } else {
           canRun = false;
        }
     }
     knapsackPopulation.add(generated);      
  }
  indexOfBest = 0;
  valueOfBest = 0;
  for(int k=0; k<knapsackPopulation.size(); k++) {
     if (knapsackPopulation.get(k).getTotalValue() > valueOfBest){
        indexOfBest = k;
     }         
  }
  if (bestKnapsack.getTotalValue() == knapsackPopulation.get(indexOfBest).getTotalValue()) {
     generationsUnchanged++;
  } else if (bestKnapsack.getTotalValue() < knapsackPopulation.get(indexOfBest).getTotalValue()) {
     bestKnapsack = knapsackPopulation.get(indexOfBest);
  }

  if(generationsUnchanged == 10){
     hasNotChangedInTenGenerations = false;
  }

具体来说,错误在这一行,返回IndexOutOfBoundsException:Index:0,Size:0.

Specifically, the error is at this line, returning an IndexOutOfBoundsException: Index: 0, Size: 0.

if (((currentWeight + copy.get(randomNum).getWeight()) < reference.getMaxWeight()) && ((currentWeight + copyBest.get(randomNumBest).getWeight()) < reference.getMaxWeight()) && (copy.get(randomNum) != null) && (copyBest.get(randomNumBest) != null)) {
// ...
}

我不确定如何可能会出现越界错误异常/空指针异常,因为每次将项目添加到新生成的背包时,带有背包标签的副本都会从自身中删除同一项目,以便于将来删除重复.

I am unsure on how there could be an out of bounds error exception/null pointer exception as for every time an item is added to the new generated knapsack, the Knapsack labeled copy removes the same item from itself in order to remove future duplicates.

推荐答案

İf如果您使用List或类似的背包,则此调用会被抛出IndexOutOfBounds:copy.get(randomNum) != null. 如果您控制此copy.size()>randomNum,就可以解决.

İf You use List or something Like that in Knapsack , this call can be thrown an IndexOutOfBounds: copy.get(randomNum) != null . İf you control this copy.size()>randomNum and it can be solve.

这篇关于Java背包:NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆