-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththesis.py
More file actions
735 lines (611 loc) · 32 KB
/
Copy paththesis.py
File metadata and controls
735 lines (611 loc) · 32 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
import os
import pandas as pd
import numpy as np
import collections
import matplotlib.pyplot as plt
from typing import Optional
import re
import tqdm
import copy
import music21
import pickle
import scipy.stats as stats
class Utils:
def extract_progression(song:list,part:str):
"""Extract and clean chord progression from SALAMI files.
Args:
song (list): SALAMI chords file
part (str): part to extract {intro,verse,chorus,...}
"""
song_clean = song.split("\n")[5:]
chord_progressions = []
in_target = False
for line in song_clean:
if line.strip(): # Check if line is not empty
lel, label = line.strip().split('\t')
if re.findall(r"^., "+ part,label):
in_target = True
chord_progression = label.split(',')[2].strip()
chord_progressions.append([chord_progression])
continue
if re.findall(r"^., [^("+part+")]", label):
in_target = False
if in_target:
chord_progressions[-1].append(label.split(",")[0])
return chord_progressions
def chords_to_music21(chord_progression:list, slash_chords:bool=False)->tuple[list,list]:
"""Converts chord list, obtained using extract progression, into Music21 ChordSymbol.
Note: only the first set of chords will be analyzed (e.g. if there are 3 verses found by extract_progression, only the first one will be analyzed)
Args:
chord_progression (list): list obtained from extract progression method.
slash_chord (bool): parse also the slash in the chords. If set to false, slashes will not be considered.
Returns:
clean: list of music21 chord symbols.
beat_number = the beat where the relative chord in clean happens
"""
if(len(chord_progression)==0):
raise Exception("chord_progression is empty")
tmp = "".join(chord_progression[0]).split("|")
clean = []
beat_number = []
curr_beat = 0
for chord in tmp:
if chord.strip():
chord_bar_list = chord.split(" ")
curr_beat += 1
for chord_bar in chord_bar_list :
if chord_bar.strip() and not re.findall("\(.+/.+\)",chord_bar):
chord_clean = re.sub(":","",chord_bar)
chord_clean = re.sub("b","-", chord_clean)
chord_clean = re.sub("\(|\)","", chord_clean)
chord_clean = re.sub("maj6","6", chord_clean)
bass_flag = re.findall(".?/\d.?",chord_clean)
if bass_flag and slash_chords:
if re.findall("min",chord_clean):
scale = music21.scale.MinorScale(chord_clean[0])
else:
scale = music21.scale.MajorScale(chord_clean[0])
bass_number = int(bass_flag[0][-1])
bass_note = scale.getPitches()[bass_number%7].name
chord_clean = re.sub("/\d","/" + bass_note,chord_clean)
if bass_flag and not slash_chords:
chord_clean = re.sub(".?/\d.?","",chord_clean)
try:
if clean!="":
clean.append(music21.harmony.ChordSymbol(chord_clean))
beat_number.append(curr_beat)
except ValueError:
continue
return clean,beat_number
def chords_to_notes(chord:music21.harmony.ChordSymbol)->list:
notes = []
for note in chord:
notes.append(note.name)
return notes
def used_notes(chords:list)->list:
return list(set().union(*chords))
def find_scale(chords:list, tonic:str)->str:
sc1 = music21.scale.MajorScale()
sc2 = music21.scale.MinorScale()
# This weird fix is because music21 does not see "G#" and "Ab" as the same note
if tonic == "A-":
tonic_music21 = music21.pitch.Pitch("G#")
else:
tonic_music21 = music21.pitch.Pitch(tonic)
pos_scales = sc1.deriveRanked(set().union(*chords),6) + sc2.deriveRanked(set().union(*chords),6)
for i in pos_scales:
if(i[1].tonic==tonic_music21):
return i[1].type
def harmonize_scale(scale:music21.scale.Scale):
harmonized = []
pitches = scale.pitches
if re.findall("major", scale.name):
harmonized.append(music21.harmony.ChordSymbol(pitches[0].name)) # I
harmonized.append(music21.harmony.ChordSymbol(pitches[1].name + "m")) # II
harmonized.append(music21.harmony.ChordSymbol(pitches[2].name + "m")) # III
harmonized.append(music21.harmony.ChordSymbol(pitches[3].name)) # IV
harmonized.append(music21.harmony.ChordSymbol(pitches[4].name)) # V
harmonized.append(music21.harmony.ChordSymbol(pitches[5].name + "m")) # VI
harmonized.append(music21.harmony.ChordSymbol(pitches[6].name + "dim")) # VII
else:
harmonized.append(music21.harmony.ChordSymbol(pitches[0].name + "m")) # I
harmonized.append(music21.harmony.ChordSymbol(pitches[1].name + "dim")) # II
harmonized.append(music21.harmony.ChordSymbol(pitches[2].name)) # III
harmonized.append(music21.harmony.ChordSymbol(pitches[3].name + "m")) # IV
harmonized.append(music21.harmony.ChordSymbol(pitches[4].name + "m")) # V
harmonized.append(music21.harmony.ChordSymbol(pitches[5].name)) # VI
harmonized.append(music21.harmony.ChordSymbol(pitches[6].name)) # VII
return harmonized
def highest_match(lists):
max_length = -1
max_indices = []
for i, lst in enumerate(lists):
current_length = len(lst)
if current_length > max_length:
max_length = current_length
max_indices = [i]
elif current_length == max_length:
max_indices.append(i)
return max_indices
class Measures:
table_intervals = [2, 240, 72, 30, 20, 12, 35, 6, 40, 15, 28, 120]
def number_of_beats(beats:list)->int:
"""Return number of beats of the song.
Args:
beats (list): list of chord/beat timing
Returns:
int: number of beats
"""
return beats[-1]
def chord_density(beats:list)->float:
"""Return the chord density of the progression.
Values greater than one mean use of more than one chord per measure, lower than one mean use of chords that span multiple beats
Args:
beats (list): list of chord/beat timing.
Returns:
float: Chord density
"""
# Magic number is needed to bound the metric to be at max 1
return len(beats)/(Measures.number_of_beats(beats)*4)
def k(interval:int,table_intervals=table_intervals):
int_revised = (interval%12)
return table_intervals[(int_revised)]
def chord_dissonance(chord_intervals:list )-> np.float32:
"""Returns normalized dissonance score of a chord.
Args:
chord_intervals (list): a list of chord intervals
Returns:
np.float: Dissonance score in [0,1].
"""
score = 0
if(len(chord_intervals)==1):
score = 2
else:
for x_i in chord_intervals[1:]:
score += Measures.k(x_i)
score_norm = (np.log(score)-0.6931471805599453)/(6.579251212010101-0.6931471805599453)
return score_norm
def harmonic_density(used_notes:list)->float:
"""Returns the harmonic density of a song. This is defined as the ratio between the used notes over all the possible ones (12).
Args:
used_notes (list): set of notes used in the chord progression
Returns:
float: harmonic density
"""
return(len(used_notes)/12)
def non_scale_notes(chord_notes:list,tonic:str,mode:str)->float:
non_scale = 0
if mode == "minor":
scale_notes = set([note.name for note in music21.scale.MinorScale(tonic).pitches])
else:
scale_notes = set([note.name for note in music21.scale.MajorScale(tonic).pitches])
used_notes = [note for chord in chord_notes for note in chord]
for note in used_notes:
if note not in scale_notes:
non_scale += 1
return non_scale/len(used_notes)
def key_centered(chord_notes:list,tonic:str)->float:
"""Return the key-center score for a chord progression. This is a ratio between the number of tonics and dominants included in all the chords and the overall number of notes played
Args:
chord_notes (list): list of chord notes
tonic (str): tonic of the song
Returns:
float: key-centered score
"""
counter = 0
fifth = music21.interval.GenericInterval(5)
tonic_dominant = set([tonic,fifth.transposeNote(music21.note.Note(tonic)).name])
used_notes = [note for chord in chord_notes for note in chord]
for note in used_notes:
if note in tonic_dominant:
counter += 1
return counter/len(used_notes)
def average_chord_complexity(chord_notes:list)->float:
n = len(chord_notes)
notes = np.sum([len(chord) for chord in chord_notes])
# minimum number of notes is 3, maximum 4
return (notes-(n*3))/(n*4)
def common_notes(chord_notes:list)->float:
common_ratio = []
for i in range(len(chord_notes)):
total = len(chord_notes[i]+chord_notes[(i+1)%len(chord_notes)])
common = total - len(set(chord_notes[i]+chord_notes[(i+1)%len(chord_notes)]))
common_ratio.append(common/total)
return np.mean(common_ratio)
def min_distance(note_1:str,note_2:str)->int:
note = {
'C': 0, 'C#': 1, 'D-':1, 'D': 2, 'D#': 3,'E-':3, 'E': 4,'F-':4, 'F': 5, 'F#': 6,
'G-':7,'G': 7, 'G#': 8,'A-':8, 'A': 9,'B-':10, 'A#': 10, 'B': 11, "C-": 11
}
# Calcola la distanza in semitoni tra le due note
semitoni_nota1 = note.get(note_1)
semitoni_nota2 = note.get(note_2)
if semitoni_nota1 is None or semitoni_nota2 is None:
raise ValueError("Una delle note inserite non è valida.")
# Calcola la distanza in semitoni, considerando il ciclo continuo dell'ottava
distance = np.min([(semitoni_nota2 - semitoni_nota1 + 12) % 12,(semitoni_nota1 - semitoni_nota2 + 12) % 12])
#return result normalized using maximum distance
return (distance)
def movement(chords:list, tonic:str)->tuple:
"""Return root movement and global movement.
Root movement is the mean of all the movements between chord roots measured in semitones.
Global movement is the mean of all the movements between chord roots, thirds and fifths measured in semitones.
Args:
chords (list): list of music21 chords
Returns:
tuple: (root movement, global movement)
"""
root_dist = 0
root_dist_cum = 0
global_dist = 0
n_chords = len(chords)
for i in range(n_chords):
root_dist = Measures.min_distance(chords[(i+1)%n_chords].root().name,(chords[(i)].root().name))
root_dist_cum += root_dist
try:
# following row is different because of problems with B7 chord
third_dist = Measures.min_distance(chords[(i+1)%n_chords].notes[1].name,(chords[(i)].notes[1].name))
except ValueError:
third_dist = 0
try:
fifth_dist = Measures.min_distance(chords[(i+1)%n_chords].fifth.name,(chords[(i)].fifth.name))
except (ValueError,AttributeError) as e:
fifth_dist = 0
global_dist += root_dist + third_dist + fifth_dist
return root_dist_cum/((len(chords)-1)*6), global_dist/((len(chords)-1)*18)
def repeated_chords(chords:list)->float:
repeated = 0
for i in range(len(chords)):
repeated += int(chords[i]==chords[(i+1)%len(chords)])
return repeated/len(chords)
def excessive_chord_rep(chords, threshold=3):
if not chords:
return False
count = 1 # Start with a count of 1 for the first element
last_element = chords[0]
for element in chords[1:]:
if element == last_element:
count += 1
if count > threshold:
return True
else:
count = 1
last_element = element
return False
def unresolved_dominant(chords:list,tonic:str):
dom = music21.harmony.ChordSymbol(music21.roman.RomanNumeral("V",music21.key.Key(tonic)).root().name + "7").notes
tonic_notes = music21.harmony.ChordSymbol(tonic).notes
for i in range(len(chords)):
if chords[i].notes == dom:
if chords[(i+1)%len(chords)] not in [dom,tonic_notes]:
return True
return False
def coherence_functional_harmony(chords:list,key:music21.key.Key=music21.key.Key("C"))->float:
# https://iastate.pressbooks.pub/comprehensivemusicianship/chapter/6-1-diatonic-harmony-tutorial/
counter = 0
for i in range(len(chords)):
curr_chord = music21.roman.romanNumeralFromChord(chords[i],key).romanNumeral
next = music21.roman.romanNumeralFromChord(chords[(i+1)%len(chords)],key).romanNumeral
if curr_chord == "I" and next != "I":
counter +=1
continue
if curr_chord in ["IV","ii"] and next in ["V","vii","I"]:
counter +=1
continue
if curr_chord == "iii" and next in ["IV","ii","vi"]:
counter +=1
continue
if curr_chord in ["V","vii"] and next in ["vi","I"]:
counter +=1
continue
if curr_chord == "vi" and next in ["IV","ii"]:
counter +=1
continue
if curr_chord =="IV" and next == "ii":
counter += 0.5
if curr_chord =="ii" and next == "IV":
counter += 0.5
if curr_chord =="V" and next == "vii":
counter += 0.5
if curr_chord =="vii" and next == "V":
counter += 0.5
return counter/len(chords)
def evaluate(song:pd.Series,curriculum_step = -1)->list:
"""Returns a list of statistics computed on the song. The order is the following:
- Chord density [0]
- Mean chord dissonance [1]
- Max chord dissonance [2]
- Harmonic density [3]
- Non Scale notes [4]
- Key-Centered score [5]
- Average chord complexity [6]
- Common notes [7]
- Fraction of repeated chords [8]
Args:
song (pd.Series): Row containing song info
Returns:
list: list of statistics
"""
results = []
if curriculum_step == -1:
chord_diss = [Measures.chord_dissonance(ch) for ch in song["chord_intervals"]]
results.append(np.mean(chord_diss))
results.append(np.max(chord_diss))
results.append(Measures.harmonic_density(Utils.used_notes(song["chord_notes"])))
results.append(Measures.non_scale_notes(song["chord_notes"], song["tonic"], song["scale"]))
results.append(Measures.key_centered(song["chord_notes"], song["tonic"]))
results.append(Measures.average_chord_complexity(song["chord_notes"]))
results.append(Measures.common_notes(song["chord_notes"]))
results.append(Measures.repeated_chords(song["chords"]))
results.append(Measures.coherence_functional_harmony(song["chords"]))
movement = Measures.movement(song["chords"],song["tonic"])
results.append(movement[0])
results.append(movement[1])
elif curriculum_step == 0:
results.append(Measures.non_scale_notes(song["chord_notes"], song["tonic"], song["scale"]))
results.append(Measures.coherence_functional_harmony(song["chords"]))
elif curriculum_step == 1:
results.append(Measures.non_scale_notes(song["chord_notes"], song["tonic"], song["scale"]))
results.append(Measures.average_chord_complexity(song["chord_notes"]))
results.append(Measures.movement(song["chords"],song["tonic"])[1])
results.append(Measures.common_notes(song["chord_notes"]))
elif curriculum_step == 2:
results.append(Measures.non_scale_notes(song["chord_notes"], song["tonic"], song["scale"]))
results.append(Measures.movement(song["chords"],song["tonic"])[1])
results.append(Measures.average_chord_complexity(song["chord_notes"]))
results.append(Measures.harmonic_density(Utils.used_notes(song["chord_notes"])))
results.append(Measures.common_notes(song["chord_notes"]))
results.append(Measures.repeated_chords(song["chords"]))
chord_diss = [Measures.chord_dissonance(ch) for ch in song["chord_intervals"]]
results.append(np.mean(chord_diss))
results.append(Measures.coherence_functional_harmony(song["chords"]))
return results
class SAMUEL:
def __init__(self):
self.status = "Active"
def evaluate_distance(stat_song:list, fitness_loc:np.array):
sum = np.sum(np.abs(stat_song-fitness_loc))
return sum
def evaluate_fitness(self,stat_song:np.array, fitness_loc:np.array, thrs = 0.5):
"""Evaluate fitness of a generated song.
Args:
stat_song (np.array): Statistics of the song
thrs (float, optional): Threshold to cap reward, this is the minimum distance we take into account,
if song is closer than this it will not receive a greater reward but just 1/threshold. Defaults to 0.5.
Returns:
float: Reward of the song
"""
sum = np.sum(np.abs(stat_song-fitness_loc))
reward = 0
if sum < thrs:
reward = 1/thrs
else:
reward = 1/sum
return reward
def extract_statistics(self,chords,beats,tonic,scale,curriculum_step = -1):
"""Extract statistics from generated song.
Args:
chords (list): list of music21.harmony.ChordSymbol chords
beats (list): list of chord location
tonic(str): tonic used to generate the songs
scale(str): scale used to generate songs
"""
statistics = []
for i in range(len(chords)):
notes = []
intervals_absolute = []
notes.append([Utils.chords_to_notes(chord) for chord in chords[i]])
intervals_absolute.append([chord.primeForm for chord in chords[i]])
tmp_df = pd.DataFrame({"chords":[chords[i]],"beats":[beats[i]],"chord_notes":notes, "chord_intervals":intervals_absolute,"tonic":tonic,"scale":scale})
statistics.append(Measures.evaluate(tmp_df.loc[0],curriculum_step))
return statistics
def generate_song(self,agent, starting_chord,fitness_loc,fitness_thrs, n_beats = 8, max_applications = 50, curriculum_step = -1, min_quality = 0, verboso = 0):
if type(starting_chord)==str:
starting_chord = music21.harmony.ChordSymbol(starting_chord)
fitness = 0
while fitness < min_quality:
chords_tmp = [starting_chord]
beats_tmp = [1]
counter = 0
while beats_tmp[-1] < n_beats:
scores = [self.match_score(rule.body,chords_tmp[-1]) for rule in agent]
current_match = Utils.highest_match(scores)
probabilites = [agent[rule].strength for rule in current_match]
probabilites /= np.sum(probabilites)
chosen = np.random.choice(current_match,p=probabilites)
agent[chosen].apply_rule(chords_tmp,beats_tmp)
counter +=1
if counter == max_applications:
break
stat_generated_songs = self.extract_statistics([chords_tmp],[beats_tmp],"C","major",curriculum_step)
fitness = self.evaluate_fitness(stat_generated_songs,fitness_loc, thrs = fitness_thrs)
if(verboso):
print(stat_generated_songs)
print(fitness)
return (chords_tmp,beats_tmp,fitness)
def print_rule_statistics(self,agent):
print("|N.\t|Utility\t|Utility Var.\t|Strength\t|Activity\t|")
print("-----------------------------------------------------------------------")
i=0
for rule in agent:
print("|{}.\t|{:.2f}\t\t|{:.2f}\t\t|{:.2f}\t\t|{:.2f}\t\t|".format(i,rule.utility,rule.utility_var,rule.strength,rule.activity))
i += 1
class Rule:
def __init__(self,body,head) -> None:
self.body = body # should be a chord
self.head = head # one of functions below
self.utility = .5
self.utility_var = 0
self.strength = .5
self.activity = 0.5
def __str__(self):
tmp = str([str(rule.root().name +" "+ rule.commonName) for rule in self.body])
return "Body:"+ tmp + "\nHead:"+ str(self.head)
def update_utility(self,r,alpha,gamma):
"""Update utility,utility_variance and strength of the rule.
Args:
r (float): reward earned
alpha (float): parameter in (0,1)for gradual update of Utility and Utility Variance
gamma (float): parameter in (0,1) for strength update
"""
self.utility = (1-alpha)*self.utility + alpha*r
self.utility_var = (1-alpha)*self.utility_var + alpha*(self.utility - r)**2
self.strength = np.max([0,self.utility - gamma*self.utility_var])
def increase_activity(self,beta):
"""Update activity level of the rule if rule has been used.
Args:
beta (float): Activity parameter in [0,1]
"""
self.activity = (1-beta)*self.activity + beta
def decrease_activity(self,beta):
"""Update activity level of the rule if rule has not been used.
Args:
beta (float): Decrease activity parameter in [0,1]
"""
self.activity = beta*self.activity
def reset_statistics(self):
self.utility = .5
self.utility_var = 0
self.strength = .5
self.activity = 0.5
def apply_rule(self,chords,beats):
self.head(chords,beats)
class Insert_Chord(Rule):
def __init__(self,body,head,chord):
super().__init__(body,head)
self.chord = chord
def __str__(self):
tmp = str([str(rule.root().name +" "+ rule.chordKind) for rule in self.body])
return "Body:"+ tmp + "\nHead:"+ str(self.head) + "\nInsert:"+ self.chord.root().name +" "+ self.chord.chordKind
def apply_rule(self,chords,beats):
self.head(self.chord,chords, beats)
class Change_Root(Rule):
def __init__(self,body,head,note):
super().__init__(body,head)
self.note = note
def __str__(self):
tmp = str([str(rule.root().name +" "+ rule.commonName) for rule in self.body])
return "Body:"+ tmp+ "\nHead:"+ str(self.head) + "\nNew root:"+ self.note.name
def apply_rule(self, chords, beats):
self.head(self.note.name,chords, beats)
# Rules
class PrototypeRules:
def insert_chord_new_beat(new_chord,chords,beats):
beats.append(beats[-1]+1)
chords.append(new_chord)
def insert_chord_curr_beat(new_chord,chords,beats):
beats.append(beats[-1])
chords.append(new_chord)
# def flip_quality(chords,beats):
# if chords[-1].quality == "minor":
# chords[-1] = music21.harmony.ChordSymbol(chords[-1].root().name)
# else:
# chords[-1] = music21.harmony.ChordSymbol(chords[-1].root().name + "m")
def repeat_chord_next_beat(chords,beats):
beats.append(beats[-1]+1)
chords.append(chords[-1])
rules_list = [insert_chord_new_beat]
def create_random_agent(self, n=1):
a =["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"] # possible roots
b = ["maj","min","7","maj 7","min 7"] # possible qualities
individual = []
for i in range(n):
base = [music21.harmony.ChordSymbol(a[np.random.randint(len(a))]+ " "+ b[np.random.randint(len(b))])]
chord_to_insert = music21.harmony.ChordSymbol(a[np.random.randint(len(a))]+ " "+ b[np.random.randint(len(b))])
init_rule = self.Insert_Chord(base,self.PrototypeRules.rules_list[0],chord_to_insert)
individual.append(init_rule)
return individual
def match_score(self,rule_body,curr_chord):
scores = [len(set(chord.pitchClasses).intersection(set(curr_chord.pitchClasses))) for chord in rule_body]
return scores
def update_rules_statistics(self,agent,fitness,used_rules,alpha,gamma):
if len(used_rules) == 0:
used_rules = np.arange(len(agent))
for rule in used_rules:
agent[rule].update_utility(fitness,alpha,gamma)
class Mutation:
def rule_deletion(agent,rule,thrs_activity, thrs_strength):
if agent[rule].activity < thrs_activity or agent[rule].strength < thrs_strength:
agent.pop(rule)
# TODO, in a distant future
def rule_merging(agent,rule,closeness=.9):
"""Merge two sufficiently close rules if they share the same action.
Args:
agent (list): a list of rules
rule (int): the rule index in the agent
closeness (float): % of closeness between the states. Default is 0.9
"""
agents_id = np.arange(0,len(agent))
np.delete(agents_id,rule)
for candidate in agents_id:
if agent[candidate].head == agent[rule].head:
total_states = len(agent[candidate].body) + len(agent[rule].body)
overlap = len(set(agent[candidate].body.append(agent[rule].body)))
if overlap/total_states > closeness:
agent[candidate].body = list(set(agent[candidate].body.append(agent[rule].body)))
agent.pop(rule)
break
def rule_specialization(agent,rule,thrs_strength,n_states):
if agent[rule].strength < thrs_strength and len(agent[rule].body) > n_states-1:
tmp = copy.deepcopy(agent[rule])
del tmp.body[np.random.randint(len(agent[rule].body))]
agent.append(tmp)
def rule_generalization(agent,rule,thrs_strength,n_states):
a =["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"] # possible roots
b = ["maj","min","7","maj 7","min 7"] # possible qualities
if agent[rule].strength > thrs_strength and len(agent[rule].body) <= n_states:
tmp = copy.deepcopy(agent[rule])
flag = False
while flag == False:
new_chord = music21.harmony.ChordSymbol(a[np.random.randint(len(a))]+ " "+ b[np.random.randint(len(b))])
if new_chord not in tmp.body:
tmp.body.append(new_chord)
flag = True
agent.append(tmp)
def hillclimb_mutation(agent,rule,thrs_strength):
if agent[rule].strength < thrs_strength:
tmp = copy.deepcopy(agent[rule])
coin = np.random.random()
if(coin <= 0.25):
tmp.chord = agent[rule].chord.transpose(1)
elif coin > .25 and coin <= .5:
tmp.chord = agent[rule].chord.transpose(-1)
elif coin > .5 and coin <= .75:
if agent[rule].chord.isSeventh():
if agent[rule].chord.chordKind[:3] == "min":
tmp.chord = music21.harmony.ChordSymbol(agent[rule].chord.root().name + "maj 7")
else:
tmp.chord = music21.harmony.ChordSymbol(agent[rule].chord.root().name + " min 7")
else:
if agent[rule].chord.chordKind[:3] == "min":
tmp.chord = music21.harmony.ChordSymbol(agent[rule].chord.root().name + " maj")
else:
tmp.chord = music21.harmony.ChordSymbol(agent[rule].chord.root().name + " min")
else:
if agent[rule].chord.isSeventh() and agent[rule].chord.chordKind[:3] != 'dom':
# TODO: dominant seventh chords are bypassed, should be replaced with min7 by logic
tmp.chord = music21.harmony.ChordSymbol(agent[rule].chord.root().name + " " + agent[rule].chord.chordKind[:3])
else:
tmp.chord = music21.harmony.ChordSymbol(agent[rule].chord.root().name + " " + agent[rule].chord.chordKind[:3] + " 7")
agent.append(tmp)
def rule_random_mutation(agent, rule):
a =["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"] # possible roots
b = ["maj","min","7","maj 7","min 7"] # possible qualities
agent[rule].chord = music21.harmony.ChordSymbol(a[np.random.randint(len(a))]+ " "+ b[np.random.randint(len(b))])
def uniform_crossover(first_parent, second_parent, prob_crossover):
first_child = copy.deepcopy(first_parent)
second_child = copy.deepcopy(second_parent)
np.random.shuffle(first_child)
np.random.shuffle(second_child)
max_rules = np.min([len(first_child),len(second_child)])
for i in range(max_rules):
if np.random.random() < prob_crossover:
first_child[i],second_child[i] = second_child[i], first_child[i]
return first_child,second_child
def mutation_rate(curr_mutation,last_fitness,curr_fitness, delta=0.01, max_mutation_rate = 1, min_mutation_rate=0):
if(last_fitness > curr_fitness) and curr_mutation < max_mutation_rate:
return curr_mutation + delta #* 1.1
if(last_fitness < curr_fitness) and curr_mutation >= min_mutation_rate:
return curr_mutation - delta #* 0.9
else:
return curr_mutation