2016-03-28 21 views
1

Bir geçiş matrisinin aksine HashMaps kullanarak 1., 2. ve 3. sırada bulunan Gizli Markov Modellerini uyguladım. Bu HMM'leri, notalara bağlı olarak 1 nota/2 nota/3 nota sonra notaların (0-128 arası tamsayı olarak modellenen) meydana gelme sayısını saymak için kullanıyorum.Katmanlı HashMap Nth Order HMM uygulaması

Örneğin 2 sipariş için uygulamasıdır:

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

aynı desen kullanarak keyfi bir düzen SMM uygulamak istiyoruz. Polimorfizm kullanmayı denedim ama her seferinde ClassCastException alıyorum. Bu konuda Generics'i nasıl kullanacağınızdan tamamen emin değilim. Tahmin ettiğim hile, son HashMap'ta ne zaman olduğunuzu bilmektir, böylece Double (Çift) değerini güncelleyebilirsiniz.

Herhangi bir öneri harika olurdu!

cevap

0

Nesne kalıtım ve özyineleme kullanarak sorunu çözmeyi başardım. Ağırlıklar şimdi, öğrenme verilerindeki notları yineleyerek ve bu işlevi her notada çağırarak güncellenir.

Işlevine, geçiş olasılıklarını, HMM sırasını ve öğrenme notları dizisinden bir not dizini içeren veri yapısı olan bir HashMap<HashMap<Integer, Object> örneğini geçirirsiniz.

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