GenJam-Mixed
Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)
WHAT IS IT?
This model aims to demonstrate the concept of a genetic algorithm in the context of rhythm production. Using the rhythmic "rules" from Ghanian drum circles, low, medium, and high-drum patterns work in concert with evolution to create jamming beats. This is the 'mixed' version, where there are either 3 or 2 'beats' per chromosome.
HOW IT WORKS
There are 16 drums, 5 high drummers (INSTRUMENT), 5 medium drummers (INSTRUMENT), and 6 low drummers (INSTRUMENT). Note that we use "low", "medium", and "high" to describe the pitch and tembre of the drum. Each drummer has a set of "rhythm chromosomes" which dictate what pattern it plays.
In this model, each rhythmic chromosome is represented by a list of 3 or 4 binary digits. A '0' indicates a rest while a '1' indicates a hit. In this model, you can think of each of these digits as 'eighth-notes', that is if you pretend we exist in a time signature with an 8 on the bottom. This means there are 2^2 + 2^3 = 12 different types of chromosomes. Like the related GenJam models, the user specifies the number of chromosomes that each player has. However, in this mixed meter version, every chromosomes are assigned in groups of 3, with 2 two-beat chromosomes and 1 three-beat chromosomes.
To begin with, every player starts with the same chromosomes (each player only plays on the downbeat). Each player plays through its chromosomes in sync with the other players (i.e. all turtles play note 0 of chromosome 0, then note 1 of chromosome 0, etc...). After the turtles have finished playing through their chromosomic rhythm, the turtles "evolve."
The basic idea here is stolen from the concept of genetic algorithms in computer science. After each pattern play, two of each breed of turtle (high, medium, and low drummers) are selected to 'reproduce' and 'evolve.' Mate selection is based on a fitness function (different for each breed). Turtles with higher fitness are more likely to be selected as mates.
In Ghanian drumming, low drummers are considered the base of the rhythm. Therefore, their fitness is determined by the number of strong-beats (i.e. the 0th and 2nd entries in each chromosome) that they play on.
Medium drummers are typically a little bit more soloistic than low drummers, but often emphasize off-beats. So their fitness is evaluated on the number of off-beats that they play on.
Finally, high drummers are considered the soloists of the ensemble. They provide rhythmic tension and tend to play in clusters. Therefore, their fitness is based on the number of 3-clusters [1 1 1] that they play, which are considered regardless of position in the pattern.
Once you've selected a pair of mates, each mate selects some chromosomes to contribute to the offspring. Then, depending on user parameters, some of these chromosomes are randomly mutated into other chromosomes. At the end of this process, this offspring replaces the least fit player in that breed.
Once this evolution has taken place, the whole process repeats again!
Note: This differs from a "pure" genetic algorithm in the fact that the turtles that are selected to "evolve" are randomly selected. This was a design decision that should not dramatically affect the evolution of the model.
HOW TO USE IT
The whole idea of this model is to experiement with the parameters in order to build a "great" rhythm. What makes a great rhythm is entirely up to you.
There are two interface elements that must be set before pressing the SETUP button:
- NUM-CHROMOSOMES This specifies the number of rhythmic chromosomes each player has
- SHUFFLE-PARTS? This is simply a GUI change that shuffles around the player's lines on the view (just to make things look cooler)
GO-ONCE is used to ask all the turtles to play their pattern exactly once and then evolve GO is used to loop the process of playing and evolving over and over again GO-ONCE-NO-EVOLVE can be used to play any particular pattern exactly once with no evolution GO-NO-EVOLVE is used to loop a pattern over and over again with no evolution
SOUND? is used to toggle sound output
The following are options that the learner can tweak to modify the evolution process:
TEMPO-BPM changes how fast each pattern is played (measured in beats (or chromosomes) per minute) HIT-LIMIT defines how long a turtle can go without being forced to evolve in someway (Note: again, this is a deviation from the traditional genetic algorithm but can be used to escape 'stale' rhythms) HIT-DENSITY-MODIFIER is a modifier to specify how "dense" a pattern should be (hits vs rests) NUM-MUTATIONS is how many mutations are applied to an off-springs chromosomes MUTATION-STRENGTH dictates how much a particular chromosome can mutate SOLOER is a chooser that allows you to single out a player SOLO? dictates whether or not to allow SOLOER to solo
The following are outputs of the model:
GENERATIONS counts how many generations of players we've seen DENSITY represents the current ratio of hits to rests
The following are plots of the model:
AVERAGE FITNESS plots the average fitness of each type of turtle and the average overall fitness over time HITS SINCE EVOLUTION is a histogram showing how long it has been since each player has 'evolved' HITS PER DRUMMER is a histogram showing how many hits each player has done
THINGS TO NOTICE
Notice the tension created by a mixed meter. How does this feel in comparison to the other two models? (duple and triple)
When you first start the model, everyone starts with the same chromosomes. So if you disable mutation by making MUTATION-STRENGTH 0, your model won't evolve! That's because the same chromosomes are just being combined together for each player.
Notice that sometimes, a single turtle sticks around for a really long time (because it's super fit!)
Checkout the Hits per Drummer graph. Do you see any general trends over the course of running the model? Why might that trend be taking place?
Notice that the SOLO function doesn't just make one player louder, it makes it so that player doesn't evolve or mutate. How does that affect the model?
THINGS TO TRY
Use the SOLO function to force a rhythm to evolve around a single stagnant player.
Try and make a rhythm where all three types of players are "equally" fit!
Try to evolve a rhythm where the high drums are very sparse.
EXTENDING THE MODEL
Try changing the instrument sounds for the turtles. Can you switch from a drum circle feel to more of a rock beat feel by just changing instruments?
Add more types of drummers. See how that changes the evolution.
Change the criteria for a mate for each breed and see how that effects the evolution of the rhythm.
NETLOGO FEATURES
Notice that this model purposefully slows down the NetLogo world. That's because music doesn't happen as fast as possible! Unfortunately, this doesn't work for the first tick, so the space between the "first beat" and "second beat" is not equal to the space betwen the rest of the beats.
In addition, the sound extension is particularly robust. In fact, the turtles aren't actually playing in parallel, they're playing one right after the other but close enough to where you can't hear the difference. Unfortunately, the way that the Java MIDI synthesizer works, if a particular channel is overwhelmed, it drops notes, so occasionally, you won't actually hear every note.
NetLogo does not provide a random select (based on weights) so we use a rather slow work around that is only possible because the world is slowed down in order to maintain a "steady" tempo.
RELATED MODELS
To play with other rhythmic feels, check out GenJam-Duple and GenJam-Triple. Also be sure to check out the Sound Machines and Drum Machine models!
CREDITS AND REFERENCES
Author: Connor Bain Written for Multi-agent Modeling 2016.
Comments and Questions
extensions [ sound ] globals [ chromosomes-duple ;; list of all possible duple chromosomes chromosomes-triple ;; list of all possible triple chromosomes random-whos ;; keeps a shuffled list for pretty-ness generations ;; number of generations we've seen ] turtles-own [ my-chromosomes my-pattern ;; lists to hold the chromosomes and hit-pattern of a drummer my-velocity my-instrument ;; the (int) velocity value and (string) MIDI instrument for that turtle mutation-rate ;; variables to control "reproduction" hits ;; counts the number of drum hits for a turtle hits-since-evolve ;; the number of hits since a mutation or evolution ] breed [ low-drums low-drummer ] breed [ med-drums med-drummer ] breed [ high-drums high-drummer ] to setup ca ;; Make the view big enough to show 16 lines and however many 'beats' resize-world 0 ((num-chromosomes / 3 * 7) - 1) 0 15 set-globals set-initial-turtle-variables reset-ticks update-view end ;; Method to play a pattern without any evolution to go-no-evolve ask turtles [ play ;; includes end of life ] update-view tick wait 60 / TEMPO-BPM / 7 end ;; Method to play a pattern with evolution to go ask turtles [ play ] ;; If we've reached the end of a pattern, do some evolution! if (ticks mod (num-chromosomes / 3 * 7) = 0) and (ticks != 0) [ set generations generations + 1 go-evolve ] update-view tick wait 60 / TEMPO-BPM / 7 ;; This roughly sets temp end ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; PLAY FUNCTIONS ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; to play-my-drum ;; turtle proceudre let temp my-velocity if sound? [ if solo? [ ;; If you're a soloer, play out! Otherwise, SHHHH. ifelse who = soloer [ set temp my-velocity + 50 ] [ set temp my-velocity - 50 ] ] sound:play-drum my-instrument temp ] end to play ;; turtle procedure if is-low-drummer? self [ if item (ticks mod (num-chromosomes / 3 * 7)) my-pattern = 1 [ play-my-drum set hits hits + 1 set hits-since-evolve hits-since-evolve + 1 ] ] if is-med-drummer? self [ if item (ticks mod (num-chromosomes / 3 * 7)) my-pattern = 1 [ play-my-drum set hits hits + 1 set hits-since-evolve hits-since-evolve + 1 ] ] if is-high-drummer? self [ if item (ticks mod (num-chromosomes / 3 * 7)) my-pattern = 1 [ play-my-drum set hits hits + 1 set hits-since-evolve hits-since-evolve + 1 ] ] end ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; END PLAY FUNCTIONS ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; EVOLUTION FUNCTIONS ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; to go-evolve ;; If there isn't a soloist, ask 2 of each type to evolve ifelse not solo? [ ask n-of 2 low-drums [ evolve ] ask n-of 2 med-drums [ evolve ] ask n-of 2 high-drums [ evolve ] ;; If a drummer hasn't changed in a while, mutate ask turtles with [hits-since-evolve > hit-limit] [ mutate set hits-since-evolve 0 ] ] [ ;; If there is a soloist, do the same, but don't include the soloer ask n-of 2 low-drums with [who != soloer] [ evolve ] ask n-of 2 med-drums with [who != soloer] [ evolve ] ask n-of 2 high-drums with [who != soloer] [ evolve ] ;; If a drummer hasn't changed in a while, mutate ask turtles with [hits-since-evolve > hit-limit and who != soloer] [ mutate set hits-since-evolve 0 ] ] end to evolve ;; turtle procedure let mate nobody let list-of-fitnesses [] let search-fitness 0 if is-low-drummer? self [ set list-of-fitnesses [fitness] of other breed set search-fitness select-random-weighted-fitness list-of-fitnesses set mate one-of other breed with [fitness = search-fitness] ] if is-med-drummer? self [ set list-of-fitnesses [fitness] of turtles with [breed != [breed] of myself] set search-fitness select-random-weighted-fitness list-of-fitnesses set mate one-of turtles with [(breed != [breed] of myself) and (fitness = search-fitness)] ] if is-high-drummer? self [ set list-of-fitnesses [fitness] of other breed set search-fitness select-random-weighted-fitness list-of-fitnesses set mate one-of other breed with [fitness = search-fitness] ] let offspring-chromosomes reproduce-with mate ask min-one-of other breed with [who != soloer] [fitness] [ set my-chromosomes offspring-chromosomes update-pattern set hits-since-evolve 0 ] end ;; This is where the basic genetic algorithm comes in to-report reproduce-with [mate] ;; turtle procedure ;; ASKER IS 1st Parent MATE is 2nd parent ;;; my-chromosomes let her-chromosomes [my-chromosomes] of mate ;; Pick a random cross-over point let crossover-point random length my-chromosomes ;; Combine the chromosomes let baby-chromosomes sentence (sublist my-chromosomes 0 crossover-point) (sublist her-chromosomes crossover-point length her-chromosomes) ;; Do a little mutation let mutation-chance 0 if is-low-drummer? self [ set mutation-chance 50 ] if is-med-drummer? self [ set mutation-chance 25 ] if is-high-drummer? self [ set mutation-chance 10 ] ;; Maybe actually mutate if random 100 > mutation-chance [ set baby-chromosomes mutate-chromosomes baby-chromosomes ] report baby-chromosomes end ;; FITNESS FUNCTIONS ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Dependent on breed, because you can lose fitness or gain fitness by fitting to your particular proclivities to-report fitness ;; turtle procedure ;; Arbirtrary 10% window around target density let my-fitness 0 ;; Want to be under the hit-density and be on the downbeats if is-low-drummer? self [ set my-fitness downbeat-fitness if my-density-fitness > (hit-density-modifier - 10) [ set my-fitness my-fitness / 1.5 ] ] ;; Want to be at the hit-density and be on the off-beats if is-med-drummer? self [ set my-fitness offbeat-fitness if (my-density-fitness < hit-density-modifier - 10) or (my-density-fitness > hit-density-modifier + 10) [ set my-fitness my-fitness / 2 ] ] ;; Want to be above the hit-density and have lots o' clusters if is-high-drummer? self [ set my-fitness offbeat-fitness if my-density-fitness < hit-density-modifier + 10 [ set my-fitness my-fitness / 2 ] ] ;; use add 1 smoothing report my-fitness + 1 end to-report my-density-fitness ;; turtle procedure report sum my-pattern / length my-pattern * 100 end to-report cluster-fitness ;; turtle procedure ;; window size at 3 let i 4 let cluster-count 0 while [i <= length my-pattern] [ if (sum sublist my-pattern (i - 4) i) = 3 [ set cluster-count cluster-count + 1 ] ] ;; Lots of clusters relative to the notes I play report cluster-count / sum my-pattern * 100 end to-report offbeat-fitness ;; turtle procedure let offbeat-count 0 foreach n-values length my-pattern [?] [ if member? (? mod 7) [1 3 5 6] [ if item ? my-pattern = 1 [ set offbeat-count offbeat-count + 1 ] ] ] if offbeat-count = 0 [ report 0 ] ;; You want more off-beats and less down-beats report offbeat-count / sum my-pattern * 100 end to-report downbeat-fitness ;; turtle procedure let downbeat-count 0 foreach n-values length my-pattern [?] [ if member? (? mod 7) [0 2 4] [ if item ? my-pattern = 1 [ set downbeat-count downbeat-count + 1 ] ] ] if downbeat-count = 0 [ report 0 ] ;; In other words, you want lots of downbeats in comparison to your other notes report downbeat-count / sum my-pattern * 100 end to mutate ;; turtle procedure set my-chromosomes mutate-chromosomes my-chromosomes update-pattern end ;; Method to mutate a chromosome to-report mutate-chromosomes [the-chromosomes] ;; basically picks a chromosome, mutates it, returns a new set let new-chromosomes the-chromosomes repeat num-mutations [ let temp random num-chromosomes ifelse (temp + 1) mod 3 != 0 [ set new-chromosomes replace-item temp new-chromosomes ((round (random-normal (item temp new-chromosomes) mutation-strength)) mod 4) ;; DUPLE CHROMOSOME ] [ set new-chromosomes replace-item temp new-chromosomes ((round (random-normal (item temp new-chromosomes) mutation-strength)) mod 8) ] ] report new-chromosomes end ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; END EVOLUTION FUNCTIONS ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; HELPER FUNCTIONS ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Called after a chromosome update in order to redefine that turtle's pattern to update-pattern ;; turtle method set my-pattern [] let i 0 foreach my-chromosomes [ ifelse (i + 1) mod 3 != 0 [ ;; DUPLE set my-pattern sentence my-pattern (get-duple-chromosome ?) ] [ ;; TRIPLE set my-pattern sentence my-pattern (get-triple-chromosome ?) ] set i i + 1 ] end to set-globals set generations 0 set random-whos n-values 16 [?] ;; this is just for looks. if shuffle-parts? [ set random-whos shuffle random-whos set random-whos shuffle random-whos ] ;; CHROMOSOME LIBRARY let c0 [0 0 0] let c1 [1 0 0] let c2 [0 1 0] let c3 [0 0 1] let c4 [1 0 1] let c5 [1 1 0] let c6 [0 1 1] let c7 [1 1 1] set chromosomes-triple (list c0 c1 c2 c3 c4 c5 c6 c7) set c0 [0 0] set c1 [1 0] set c2 [0 1] set c3 [1 1] set chromosomes-duple (list c0 c1 c2 c3) ;; END CHROMOSOME LIBRARY end to set-initial-turtle-variables create-low-drums 6 [ set my-instrument "LOW CONGA" set my-velocity 85 set color red set mutation-rate 64 ] create-med-drums 5 [ set my-instrument "Bass Drum 1" set color green set my-velocity 85 set mutation-rate 32 ] create-high-drums 5 [ set my-instrument "Closed Hi Hat" set color blue set my-velocity 85 set mutation-rate 16 ] ask turtles [ set my-pattern [] set my-chromosomes (n-values num-chromosomes [1]) ht update-pattern set hits 0 ] end ;; Method to update the view (simplified music notation) to update-view ask turtles [ let temp 0 let row item who random-whos foreach my-pattern [ ifelse ? = 1 [ ifelse solo? and (soloer = who) [ ask patch temp row [set pcolor white] ] [ ask patch temp row [set pcolor [color] of myself] ] ] [ ask patch temp row [set pcolor black] ] set temp temp + 1 ] ] ask patches with [pxcor = (ticks mod (num-chromosomes / 3 * 7))] [set pcolor yellow] end ;; Method to get a triple chromosomes pattern from the library to-report get-triple-chromosome [index] report item index chromosomes-triple end ;; Method to get a duple chromosomes pattern from the library to-report get-duple-chromosome [index] report item index chromosomes-duple end ;; This is my version picking a weighted random turtle to-report select-random-weighted-fitness [theList] let weighted-list [] foreach theList [ ;; add one smoothing let temp ? foreach n-values round ((? / sum theList * 100) + 1) [?] [ set weighted-list fput temp weighted-list ] ] report item (random length weighted-list) weighted-list end ;;; OLD CODE ;breed [ low-drums low-drummer ] ;;; Atsimevu ;;; 64 Low Conga ;;; 63 Open Hi Conga ;;; 52 Mute Hi Conga ;;; open-hand open-fingers slap-fingers slap-two-fingers ;; Options for hit ; ;;; Sogo or Kidi ;;; MIDI INSTRUMENT 117. Taiko Drum ;breed [ med-drums med-drummer ] ; ;;; Kagan ;;; 65. Hi Timbale ;breed [ high-drums high-drummer ]
There is only one version of this model, created about 9 years ago by Connor Bain.
Attached files
File | Type | Description | Last updated | |
---|---|---|---|---|
GenJam-Mixed.png | preview | Preview for 'GenJam-Mixed' | about 9 years ago, by Connor Bain | Download |
This model does not have any ancestors.
This model does not have any descendants.