Composing with Genetic Algorithms
Polyphonic music composition is an NP-hard problem, and with NP-hard problems, the only way we know of to arrive at a good, albeit imperfect solution is to use an approximation algorithm of some kind or another. I usually work with deep learning, but as I did get a computer science education, that algorithms background does come back to me.
Here’s an example of music composition using a genetic algorithm, from the paper GGA-MG: Generative Genetic Algorithm for Music Generation, by Majid Farzaneh and Rahil Mahdian Toroghi.
This post is not going to be a primer on genetic algorithms, but this tutorial is good, as are really any on the front page of the Google results. The basis is pretty simple — generate melodies as though they are genetic material, randomly choose pairs and recombine them, apply a cost function, and choose the best-performing children to breed in a new generation.
In this paper, the musical chromosomes are created randomly, and then an LSTM network is applied as one of the cost functions, and the other cost function is what they call “rhythm cost”, to determine if a melody broke the rhythm earlier by having a melody end before a bar line, or be a strange length.
An example follows here — as with anything else generated with a genetic algorithm, it can only be as good as its cost function, and creating a cost function for music is difficult, to say the least.
Personally, I think it is often a good idea to try out different algorithmic tools for generation, and not blindly slap a neural network on everything. This does bring up the question for me at least — what makes good music? Why is it that some generated music is better than others?
Originally published at http://www.neuralmuse.ca on June 11, 2020.