2016-03-28 10 views
1

私は、遷移マトリックスではなくHashMapsを使用して1,2,3次隠れマルコフモデルを実装しました。私は、これらのHMMを使用して、音符の出現回数を1ノート/ 2ノート/ 3ノートの後に順序に応じて(整数0〜128としてモデル化)カウントします。レイヤードHashMap N次HMMの実装

例えば2次の実装は、次のとおり

public void updateWeigths(ArrayList<Note> notes, HashMap<Integer, HashMap<Integer, HashMap<Integer, Double>>> hm) { 
    for (int i=0; i<notes.size()-2; i++) { 
     int prevPitch1 = notes.get(i).getPitch(); 
     int prevPitch2 = notes.get(i+1).getPitch(); 
     int nextPitch = notes.get(i+2).getPitch(); 
     if (prevPitch1 > 0 && prevPitch2 > 0 && nextPitch > 0) { 
      if (hm.containsKey(prevPitch1)) { 
       HashMap<Integer, HashMap<Integer, Double>> nextMapping1 = hm.get(prevPitch1); 
       if (nextMapping1.containsKey(prevPitch2)){ 
        HashMap<Integer, Double> nextMapping2 = nextMapping1.get(prevPitch2); 
        if (nextMapping2.containsKey(nextPitch)) { 
         double prob = nextMapping2.get(nextPitch); 
         nextMapping2.put(nextPitch, prob+1); 
        } 
        else { 
         nextMapping2.put(nextPitch, 1.0); 
        } 
       } 
       else { 
        nextMapping1.put(prevPitch2, new HashMap<Integer, Double>()); 
       } 
      } 
      else { 
       hm.put(prevPitch1, new HashMap<Integer,HashMap<Integer,Double>>()); 
      } 
     } 
    } 
} 

Iは同じパターンを使用して、任意の順序のHMMを実装します。私は多型を使ってみましたが、毎回ClassCastExceptionを取得します。これについてジェネリックスを使用する方法は完全にはわかりません。私が推測するトリックは、あなたが最後のHashMapにいる時を知っているので、カウントDouble値を更新することができます。

どのような提案も素晴らしいでしょう。

答えて

0

私はオブジェクトの継承と再帰を使用して問題を解決することができました。ウェイトは、ラーニングデータのノートを反復し、すべてのノートでこの関数を呼び出すことで更新されるようになりました。

HashMap<HashMap<Integer, Object>インスタンスに渡す関数には、遷移確率、学習ノートの配列からのHMMとノートインデックスの順番を含むデータ構造体が渡されます。

public void updateTransitionProb(Object mapping, int ord, int noteIndex) { 
    int note = notesList.get(noteIndex).getPitch(); 
    HashMap<Integer, Object> hm = (HashMap<Integer, Object>) mapping; 

    if (ord == 0) { 
     hm.put(note, (hm.get(note) != null) ? ((Double) hm.get(note)) + 1.0 : new Double(1.0)); 
    } 
    else { 
     if (hm.containsKey(note)) { 
      this.updateTransitionProb(hm.get(note), --ord, ++noteIndex); 
     } 
     else { 
      hm.put(note, new HashMap<Integer, Object>()); 
      this.updateTransitionProb(hm.get(note), --ord, ++noteIndex); 
     } 
    } 
} 
関連する問題