2016-03-28 91 views
1

我已经实现了使用HashMaps的一阶,二阶和三阶隐马尔可夫模型,而不是转换矩阵。根据顺序,我使用这些HMM计算1音符/ 2音符/ 3音符后音符的出现次数(建模为0-128整数)。分层HashMap N阶HMM实现

例如,对于二阶的实现是:

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>>()); 
      } 
     } 
    } 
} 

我想要实现使用相同的模式任意顺序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); 
     } 
    } 
}