ICMC 2015 – Sept. 25 - Oct. 1, 2015 – CEMI, University of North Texas

Minimal Fitness Functions in Genetic Algorithms for the Composition of Piano Music

Rodney Waschka II North Carolina State University [email protected]

ABSTRACT Perhaps the most significant problem facing anyone em- A general strategy for creating extremely simple but effec- ploying genetic algorithms for music is the determination tive fitness functions in genetic algorithms is described. of an appropriate . Two major factors cause Called “minimal fitness functions”, these fitness functions the fitness function to become a point of high tension in the are designed to 1) consist of the smallest amount of infor- creation of a for music composition. mation or restrictions possible, 2) avoid the “fitness bot- First, it is usually the case that the fitness function serves tleneck” problem, 3) be “aesthetically neutral”, and 4) be as the main method for determination of the musical con- musically useful. A summary of the general background in tent of a piece made with genetic algorithms. Second, even music for this work on fitness functions is provided, the when composing intuitively -- in the “old-fashioned way” - rationale and strategy of minimal fitness functions are set - it is often incredibly difficult for a composer to determine forth, and then specific examples of this type of fitness what constitutes a good reason to incorporate or omit some function are presented. Finally, examples of the musical note, phrase, sound, or group of sounds into or from a results generated by these minimal functions to help com- composition. Explaining the reasons for such inclusions or pose piano music are provided. While the examples show omissions is not necessarily easier after the piece has been the results in the composition of piano music, this work completed. In other words, the composer faces the problem indicates that the use of minimal fitness functions is not of developing an appropriate fitness function when she or limited to the writing of piano music and other kinds of he may not be able to imagine or sometimes even recog- pieces made using these minimal techniques are cited. nize an appropriate solution. Consequently, it can be extremely time consuming and 1. INTRODUCTION problematic to try to develop a set of rules encoded in a fitness function for the composition of a new work. This Genetic algorithms are used in attempts to solve a variety most troublesome aspect of composing with genetic algo- of problems across a range of disciplines. Implementations rithms -- creating an efficient and effective fitness function of these algorithms in music have been cited in Biles [1], -- has been described by Biles [1] as the “fitness bottle- Miranda and Biles [2], Waschka [3], Klügel et al [4], and neck.” It has been discussed by Waschka [3], Klügel et al Ando [5]. These algorithms model some of the basic as- [4], and Cho [6]. pects of natural selection. Genetic algorithms employ an initial (digitally represented) population of “individuals” -- 2. BACKGROUND AND PROBLEM potential (insufficient) solutions -- a fitness function, and a set of “breeding” rules including crossover and mutation. The author has utilized genetic algorithms to help compose The process brings together pairs or groups of parents to music in the past. For this study, most of the attributes of “breed” resulting in a new generation. That new generation previously used genetic algorithms remained in place. A is subjected to the same process, as are the succeeding single measure of music constitutes an individual, usually generations. Typically, given an appropriate initial popula- there is only one “breakpoint” or point of crossover, and tion and fitness function, a solution or group of solutions mutations vary from a single set mutation used in every will emerge after much iteration. case to mutations chosen intuitively by the composer on a case-by-case basis. In addition, rather than iterating nu- merous times in order to determine a single measure, each Copyright © 2015 Rodney Waschka II. This is an open-access article generation created is included in the piece and heard. distributed under the terms of the Creative Commons Attribution License This exploration attempted to determine if it is possible 3.0 Unported, which permits unrestricted use, distribution, and to use extremely simple fitness functions that specify the reproduction in any medium, provided the original author and source are minimal amount of restriction to create compelling con- credited. temporary art music. The goal was to develop fitness func-

– 408 – ICMC 2015 – Sept. 25 - Oct. 1, 2015 – CEMI, University of North Texas

tions that 1) could be stated in one sentence and imple- ing two constraints. The other five clearly make use of a mented with few lines of code, 2) were aesthetically neu- single constraint. Two of the seven minimal fitness func- tral, 3) were musically useful, and 4) developed results that tions (for pieces 2 and 5) relate specifically to piano play- were computationally efficient and did not require either ing. re-workings of the function or other types of intervention. These minimal fitness functions do not push the pieces in any particular aesthetic direction. Only two of the func- 3. FITNESS FUNCTION DEVELOPMENT tions implicate pitch choice in any way (the fitness func- tions for pieces 3 and 7). Of much greater impact in terms The fitness functions described here were used in the crea- of the musical aesthetics of the pieces are other factors. tion of a set of seven piano pieces, the Enigmatic Sonatas These include: [7]. Each piece is separately named. The resulting set of 1) the decision to use genetic algorithms in the first pieces is, in some ways, similar to the sonatas of Domenico place, Scarlatti, [8] which served as one of the inspirations for 2) the decision to implement the algorithm in such a these works. The pieces may be played singly, in groups of way that each generation is part of the piece and is heard, less than seven in any order, or, all seven may be played in 3) the decision to make the initial population not only a a different order, or all seven may be played in the order bank of individuals from which the breeding parents of the given. Each piece has the same form – initial population, a next generation would be drawn, but to also have that ini- set number of generations created by the algorithm, and tial population played as the beginning of the piece, and then a repeat of the initial population. Each of the pieces 4) the actual composition or choice of the initial popu- contains the same number of measures. The fitness func- lation, which, as previously mentioned, in this case often tions developed were, in some cases, general, and could featured tonal centers of some kind. easily be applied to the composition of other types of Most of the individual/measures within an initial popula- pieces. Others were quite specific to the piano and not nec- tion would fulfill the fitness function. essarily applicable to other instruments or ensembles. Various functions were tried. Only those that worked and 4. OTHER ASPECTS OF THE GENETIC succeeded in producing pieces of some interest are de- scribed here. ALGORITHM It should be noted that the initial populations for these Some of the other aspects of these genetic algorithms are pieces often contained, or were based on, tonal material usual, such as the use of a two-parent model, while other including folk songs. Using this kind of material has the aspects are potentially less typical. advantage, in many instances, of making it easier to follow the alteration of the material brought on by the genetic al- 4.1 Breeding gorithm. The algorithms used in these pieces featured a two-parent model and a single crossover point randomly determined. 3.1 The Seven Fitness Functions The individuals in the initial population were not differen- The fitness functions for each of the pieces consisted of tiated in any way or split into groups. Any individ- a constraint that could be stated in one sentence. The fit- ual/measure from the initial population or succeeding gen- ness functions for each piece is given below: erations could serve as parent one or parent two and could Piece 1: The individual must contain a rest or a tuplet. function as parent one in one instance and parent two in Piece 2: The individual must have notes played by the another. An individual could also breed with itself, which left hand of the pianist. would, of course, result in an exact replication. Piece 3: The individual must contain an accidental. As implied by the statement above, some individuals Piece 4: The individual must not contain any half notes. would breed more than once while other individuals in the Piece 5: The individual must have notes played by the initial and subsequent populations who met the fitness right hand of the pianist. function requirements would not breed. A random selec- Piece 6: The individual must contain at least one six- tion from those individuals that met the fitness function teenth note. requirements was made. Consequently, as in life, some Piece 7: The individual must not contain any A-flats or genetic material was stripped away by the fitness function, G-flats in the melody. while other material was lost or emphasized due to chance. 4.2 Mutation 3.2 Notes on the Functions In the genetic algorithm for each of the seven pieces there Of the seven different fitness functions, only two contained existed the possibility of mutation. There were multiple the word “or” and could, therefore, be considered as hav- ways this could happen. For some pieces, one randomly

– 409 – ICMC 2015 – Sept. 25 - Oct. 1, 2015 – CEMI, University of North Texas

chosen individual per generation could contain a mutation. Examples from the resultant music associated with Ta- This mutation aspect of the algorithm essentially func- ble 1 are given below in Figures 1-6. tioned as a kind of “wild card.” What the mutation would be for the one individual in each generation was left to the composer’s discretion. Of course, given the fitness func- tion and the breeding procedures, it was possible that a given mutation would not necessarily enter the next gen- eration of individuals. A second possibility was chosen for some pieces. Selection for breeding was done first and if neither parent passed the fitness function test, the measure would be left as a rest. Figure 1. Measure 25 (left) and Measure 4 (right).

5. EXAMPLES Figure 1 shows the two parents for measure 38, which is shown below in Figure 2. An example of the breeding process is given below fol- lowed by specific examples of the musical realization.

Bar # Parent 1 Parent 2 Crossover 38 25 4 2 and Mutate 39 6 6 1 40 18 23 2 41 3 11 3 42 11 11 2 Figure 2. Measure 38, mutated offspring of Bars 25 and 4. 43 16 33 1 Figure 2 also shows the mutation present. In the case of Table 1. Determination of measures 38-43 in the first piece in- this particular measure, everything after the first two beats cluding crossover information and mutation designations. was changed to a rest, effectively removing the genetic Table 1 shows the outcomes for measures 38-43 of the first contribution of the second parent, measure 4. No figure Enigmatic Sonata together with the crossover point and associated with measure 39 is given since it is an exact whether or not the random mutation indicator appeared. copy of measure 6 and does not illustrate crossover or oth- Enigmatic Sonata No. 1 is mainly in a meter of four-four er aspects of the breeding procedure. Figure 3 shows the with occasional measures of five-four. When measures of parents of bar 40, measures 18 and 23 in that order. five-four constitute one of the parents for an individual in a succeeding generation, the possible range of crossover points remain unchanged. For bar 38, the program calls for the first parent to be measure 25 and the second to be measure 4 with a cross- over halfway through the measures (after beat 2). It also calls for a mutation in this measure. For measure 39, the process calls for measure 6 to be both the first and second parent, so no matter where the crossover point, the off- Figure 3. Measure 18 (left) and 23 (right), parents of bar 40. spring will be identical to measure 6. Measure 40 will be Measure 40, like bar 38, has a crossover point after the made up of the first half of measure 18 and the second half second beat. of measure 23. Measure 41 will be made up of the first three beats of measure 3 and the final beat of measure 11. Measure 42 will be a full repeat of measure 11 and meas- ure 43 will consist of the first beat of measure 16 and the last three beats of measure 33. This process will continue until the first generation of offspring is produced from the initial population. Then, a second-produced generation will be derived from the first generation. The process continues until the requisite num- ber of generations has been produced--in this case, three. Figure 4. Measure 40, offspring of bars 18 and 23.

– 410 – ICMC 2015 – Sept. 25 - Oct. 1, 2015 – CEMI, University of North Texas

A final example, Figures 5 and 6, shows a crossover point the composition of a string quartet [9], a work for flute and at a different location. Measure 41 is produced by the par- electronic music [10], and other pieces. ent measures 3 and 11 and has a crossover point after the third beat. The parents of measure 41 are shown below in Figure 5. Acknowledgments

This work was supported by a grant from the College of Humanities and Social Sciences at North Carolina State University. The author wishes to acknowledge the support and comments of various pianists including Olga Kle- iankina and Walton Lott.

8. REFERENCES Figure 5. Measure 3 (left) and 11 (right), parents of bar 41. [1] J.A. Biles, “GenJam: A Genetic Algorithm for Generating Jazz Solos,” Proceedings of the 1994 Below, in Figure 6, the resulting measure 41 is shown. International Computer Music Conference, Aarhus, 1994, pp. 131-137. [2] E. R. Miranda and A. Biles (eds.), Evolutionary Computer Music. Springer-Verlag, 2007. [3] R. Waschka II, “Avoiding the Fitness ‘Bottleneck’: Using Genetic Algorithms to Compose Orchestral Music,” Proceedings of the 1999 International

Figure 6. Measure 41, offspring of bars 3 and 11. Computer Music Conference, Beijing, 1999, pp. 201- 203. [4] N. Klügel, A. Lindström and G. Groh, “A Genetic 6. COMPOSITIONAL PREROGATIVES Algorithm Approach to Collaborative Music Creation Figure 6 contains an example of a supplementary point, the on a Multi-Touch Table,” Proceedings of the 2014 use of fermatas. Throughout the composition of these International Computer Music Conference, Athens, pieces the use of articulations, fermatas, tempo changes, 2014, pp. 286-292. dynamic levels, and other techniques that did not involve [5] D. Ando, “Real-time Breeding Composition System changing the pitches or the notated durations of the indi- by means of and Breeding vidual notes were left to the composer’s discretion. The Procedure,” Proceedings of the 2014 International most common uses of these prerogatives involved the em- Computer Music Conference, Athens, 2014, pp. 402- ployment of fermatas, accent marks, slurs, phrase mark- 407. ings, and tempo changes. [6] Y. Cho, “Evolutionary Theory in Composition: Works 7. CONCLUSIONS by Biles, Waschka, Xenakis, and Miranda,” World Forum on Acoustic Ecology, Web, 2011. The use of minimal fitness functions as described here [7] R. Waschka II, Enigmatic Sonatas, 2014. proved to be extremely useful strategy for dealing with complex musical issues. These functions greatly simplified [8] Robert White, “The mercurial masestro of Madrid,” the fitness function problem, effectively breaking the “bot- The Guardian, Web, 2007. tleneck.” While consisting of the smallest amount of in- [9] R. Waschka II, String Quartet No. 3, 2013. formation or restrictions possible – usually employing only a single restriction, and even by the most rigorous assess- [10] R. Waschka II, CHATting Up, 2009. ment not containing more than two fitness tests – these functions turned out to be of tremendous musical value and usefulness. The minimal nature of these fitness functions meant that they could be applied in conjunction with a va- riety of aesthetic approaches to the composition of art mu- sic. Additionally, they are easy to understand and relatively easy to code. The author has also used these techniques in

– 411 –