-
Notifications
You must be signed in to change notification settings - Fork 0
/
fitnessfunction(malkov)
37 lines (30 loc) · 1.11 KB
/
fitnessfunction(malkov)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import mido
import time
import numpy as np
#0对应E3 1对应F3...26对应G5
#单位时8分音符
note = np.load("notes.npy")
dataset=[]
for sublist in note:
for item in sublist:
if(item>=52 and item<=79):
dataset.append(int(item-52))
malkov = np.zeros((30, 30, 30,30,30), dtype=np.float)
total=np.zeros((30, 30,30,30), dtype=np.int)
for i in range(4,len(dataset)):
malkov[dataset[i-4]][dataset[i-3]][dataset[i-2]][dataset[i-1]][dataset[i]]+=1
total[dataset[i-4]][dataset[i-3]][dataset[i-2]][dataset[i-1]]+=1
for i in range(0,28):
for j in range(0,28):
for k in range(0,28):
for s in range (0,28):
for t in range (0,28):
if total[i][j][k][s]!=0:
malkov[i][j][k][s][t]=malkov[i][j][k][s][t]/total[i][j][k][s]
def fitnessfunction(notes):
eachpitch=float(100/32)#默认一共有32个八分音符
totalscore=eachpitch*4
for i in range(4,len(notes)):
if(notes[i]-notes[i-1]<=15):
totalscore+=eachpitch*malkov[notes[i-4]][notes[i-3]][notes[i-2]][notes[i-1]][notes[i]]
return totalscore