public static List<TokenGroup> TokenGroupsFromCA(CA ca) { // A list of grids that each contain a list of the coordinates of cells that are alive. // Init the live cells list. One token group per live cell in the initial population. List<List<int[]>> liveCells = (from cell in ca.Populations[0].Cells where cell.Value >= 1 select new List<int[]>()).ToList(); // For each population, iterate through each cell. For each cell, find out which of it's tokens are maximal, and // then set that cell to being alive in the CAs that represent those maximal tokens. for (int popIndex = 0; popIndex < ca.NumberOfGens; popIndex++) { for (int cellIndex = 0; cellIndex < ca.PopulationSize; cellIndex++) { if (ca.Populations[popIndex].Cells[cellIndex].Value < 1) continue; List<Tokens> domTokens = ca.Populations[popIndex].Cells[cellIndex].Tokens.MaximalTokens(); foreach (Tokens token in domTokens) { liveCells[(int)token].Add(new []{popIndex, cellIndex}); } } } // Make the TokenGroups out of the CAs that were filled for each token. return new List<TokenGroup>(liveCells.Select((x, index) => new TokenGroup(ca, (Tokens)index, liveCells[index]))); }
// Note: currently defaults to each note has a single beat, and the CMajor scale. public static Song SongFromEntireCA(CA ca) { List<Chord> chords = new List<Chord>(ca.NumberOfGens); float beat = 0; // Iterate through all of the cells and construct chords out of all the living cells in each column. foreach (Population pop in ca.Populations) { List<Note> notes = new List<Note>(ca.PopulationSize); for (int cellIndex = 0; cellIndex < ca.PopulationSize; cellIndex++) { Cell cell = pop[cellIndex]; if (cell.Value < 1) continue; notes.Add(new Note(Scales.CMajor[cellIndex], 1, beat)); } if (notes.Count != 0) { chords.Add(new Chord(notes, beat)); } beat += 1; } return new Song(chords, ca.PopulationSize, .25F, beat); // Hardcoded for now - minimum length of a note is 1/4 a beat }
public TokenGroup(CA ca, Tokens token, List<int[]> liveCells) { Token = token; PopulationSize = ca.PopulationSize; NumberOfGens = ca.NumberOfGens; NhbdSize = ca.NhbdSize; Cells = liveCells; }
public MusicWindow(CA ca, string ruleString) : this() { this._ca = ca; this._ruleString = ruleString; }
/// <summary> /// Generates a certain number of generations of a particular CA. /// </summary> /// <param name="ca">The input CA to generate.</param> /// <param name="generations">Number of generations to output.</param> /// <returns>The generated CA.</returns> public CA GenerateCA(CA ca, int generations) { Contract.Requires(ca != null); Contract.Requires(generations > 0); Contract.Requires(Rule > 0); CA retCA = new CA(ca.PopulationSize, ca.NumberOfGens, ca.NhbdSize); // Initialize the first population to random values. Population currentPop = GenRandomPopulation(ca.PopulationSize); //Population currentPop = GenSimpleStartPopulation(ca.PopulationSize); // Hand out tokens to the initial population if (UseTokens) AssignInitialTokens(currentPop); int currentGen = 0; retCA.Populations.Add(currentPop); currentGen++; // For each of the generations of the CA, generate a new population using the rule. while (currentGen < generations) { currentPop = AdvancePopulation(currentPop, Rule, ca.NhbdSize, currentGen); retCA.Populations.Add(currentPop); currentGen++; } return retCA; }