2011-03-11 17 views
3

私はこの奇妙な例外を取得していますが、私は本当に理解していない:ダイナミックプログラミングArrayIndexOutOfBoundException

opt[i][j] = Double.POSITIVE_INFINITY; 

とするとき、私== 0とj == 1ですが、この場合optは9x6の行列なので、このようなことは起こりません。

これは私のコードです:

public class Versie3 { 

    private int desCap; 
    private int currentCap; 
    private int maxCap; 
    private int timeSlot; 
    private static ArrayList<Double> prices; 
    private double[][] opt = new double[timeSlot + 1][maxCap + 1]; 

    public Versie3() throws FileNotFoundException { 

    } 

    public void readInput(String s) throws FileNotFoundException 
    { 
     FileReader fr = new FileReader(s); 
     Scanner sc = new Scanner(fr); 

     timeSlot = sc.nextInt(); 
     maxCap = sc.nextInt(); 
     currentCap = sc.nextInt(); 
     desCap = sc.nextInt(); 
     prices = new ArrayList<Double>(timeSlot); 

     while (sc.hasNextDouble()) { 
      prices.add(sc.nextDouble()); 

     } 
    } 

    public double calculateOptimal() 
    { 
     for (int i = 0; i <= timeSlot; i++) 
     { 
      for (int j = 0; j <= maxCap; j++) 
      { 
       if (i == 0) 
       { 
        if (j != desCap) 
        { 

         opt[i][j] = Double.POSITIVE_INFINITY; // <--here it goes Wrong! 
        } 
        else 
        { 
         opt[i][j] = 0; 
        } 
       } 
       else if (j == 0) 
       { 
        opt[i][j] = Math.min(opt[i - 1][j], 
          opt[i - 1][j + 1] 
            - prices.get(i-1)); 
       } 
       else if (j == maxCap) 
       { 
        opt[i][j] = Math.min(opt[i - 1][j], 
          opt[i - 1][j - 1] 
            + prices.get(i-1)); 
       } 
       else 
       { 
        opt[i][j] = Math.min(Math.min(opt[i - 1][j], 
        opt[i - 1][j - 1] 
        + prices.get(i - 1)),opt[i - 1][j + 1]- prices.get(i-1)); 
       } 
      } 
     } 
     return opt[timeSlot][currentCap]; 
    } 

    public static void main(String[] args) throws FileNotFoundException { 
     Versie3 v3 = new Versie3(); 
     v3.readInput("input.txt"); 
     System.out.println("prices: " + prices.toString()); 
     System.out.println("timeSlot: " + v3.timeSlot); 
     System.out.println("maxCap: " + v3.maxCap); 
     System.out.println("currentCap: " + v3.currentCap); 
     System.out.println("desCap: " + v3.desCap); 
     //System.out.println("minimum cost: "+v3.calculateOptimal()); 
     System.out.println(v3.prices.size()); 

    } 

} 

そして、これは私が読んでいる入力ファイルです:第二列が表示さ

timeSlot = 8 
maxCap = 5 
currentCap = 2 
desCap = 5 

8 5 2 5 
2.2 3 5 6.5 5 5 3 1.8 

をこの場合、タイムスロット当たりの価格。合計で8です。

ご協力いただきありがとうございます。

+1

+1すべての必要な情報が記載された適切な構造の質問。 –

答えて

2

maxCaptimeSlotで配列を作成していますが、デフォルト値はまだ0です。 readInput()はまだ呼び出されていないので、配列を作成するにはどのようなサイズが分かりますか?

maxCaptimeSlotで読み取った後に配列を作成します。

1

opt配列を初期化してから、maxCapの値を決定します。

3

opttimeSlot前とmaxcapが設定されている構築時にintializedなっています。

だから、あなたは、ユーザーが値を入力した後readInput方法で配列を作成する必要があり、配列

private double[][] opt = new double[0 + 1][0 + 1]; 

を作成します。あなたは、クラスのオブジェクトを作成すると

1

Versie3maxCaptimeSlot0のデフォルト値を取得し、配列opt1 x 1のサイズで作成されます。あなたが行くとmaxCaptimeSlotの値を書き換えますが、配列のサイズは変わらないままファイルを読み込む。この後

この問題を解決するには、ディメンションを読んだあとで、関数readFileで配列のメモリを割り当てます。

関連する問題