私は、遷移マトリックスではなく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値を更新することができます。
どのような提案も素晴らしいでしょう。