Beispiel #1
0
        public static int[][] GetPitchOutput(List <NeatPlayer> players)
        {
            //convert inputs to correct values
            int[] inputs = currentPitchPhrase;

            //inputs = currentKey.Pitches.Select(p => p.Value).ToArray();


            int[][] outputs = new int[players.Count][];

            for (int i = 0; i < outputs.Length; i++)
            {
                outputs[i] = new int[inputs.Length];
            }

            for (int i = 0; i < inputs.Length; i++)
            {
                for (int p = 0; p < players.Count; p++)
                {
                    var brainInput = MusicEnvironment.getBrainInputDelayed(outputs, inputs, p, players.Count, i);

                    outputs[p][i] = MusicEnvironment.convertToMidiClass(players[p].calculateOutput(brainInput));
                }
            }

            return(outputs);
        }
Beispiel #2
0
        public static bool[][] RestsInTicks(double[][] durations, List <int>[] rests, int length)
        {
            bool[][] isRest = new bool[durations.Length][];

            for (int k = 0; k < durations.Length; k++)
            {
                isRest[k] = new bool[length];

                var restLocation = new List <int>();

                foreach (int rest in rests[k])
                {
                    double location = durations[k].TakeWhile((d, i) => i < rest).Sum();
                    restLocation.Add((int)MusicEnvironment.durationToTicks(location));
                }

                for (int i = 0; i < restLocation.Count; i++)
                {
                    int loc = restLocation[i];
                    int dur = (int)MusicEnvironment.durationToTicks(durations[k][rests[k][i]]);
                    for (int j = loc; j < loc + dur; j++)
                    {
                        isRest[k][j] = true;
                    }
                }
            }

            return(isRest);
        }
Beispiel #3
0
        public static double[][] GetRhythmOutput(List <NeatPlayer> players, out List <int>[] rests, out double[][] outputs)
        {
            //convert inputs to correct values
            double[] inputs = durationInputs;
            //calculate the outputs, inputs are conbined in a cascading manner
            outputs = new double[players.Count][];

            for (int i = 0; i < outputs.Length; i++)
            {
                outputs[i] = new double[inputs.Length];
            }

            for (int i = 0; i < inputs.Length; i++)
            {
                for (int p = 0; p < players.Count; p++)
                {
                    var brainInput = MusicEnvironment.getBrainInputDelayed(outputs, inputs, p, players.Count, i);

                    outputs[p][i] = players[p].calculateOutput(brainInput);
                }
            }

            double[][] durations = new double[outputs.Length][];

            rests = new List <int> [outputs.Length];

            for (int i = 0; i < outputs.Length; i++)
            {
                durations[i] = MusicEnvironment.OutputsToDurations(outputs[i], out rests[i]);
            }

            return(durations);
        }
Beispiel #4
0
        public static int RestCheck(double[] inputs, double[][] durations, List <int>[] rests)
        {
            int result = 0;

            double restDurations = 0.0;

            foreach (int rest in rests[0])
            {
                restDurations += durations[0][rest];
            }

            double inputDuration = MusicEnvironment.ticksToDuration(inputs.Length);

            if (restDurations > 2 * inputDuration * (1.0 / MusicEnvironment.MODULE_COUNT + 0.005))
            {
                result += (int)restDurations * 20;
            }
            if (rests[0].Count < 5)
            {
                result += (5 - rests[0].Count) * 10;
            }

            return(result);
        }
Beispiel #5
0
        public override void Create()
        {
            double[] currentDurationPhrase = MusicLibrary.CurrentSongDuration();
            int[]    currentPitchPhrase    = MusicLibrary.CurrentSongPitch();

            double[] rhythmInputs = MusicEnvironment.createDurationInputs(currentDurationPhrase);
            int[]    pitchInputs  = MusicEnvironment.createPitchInputs(currentPitchPhrase, currentDurationPhrase);

            string CHAMPION_FILE = @"..\..\..\NeatMusic\bin\Debug\host_parasite_champion.xml";

            List <NeatGenome> anns;

            XmlConfigurator.Configure(new System.IO.FileInfo("log4net.properties"));

            // Load config XML.
            XmlDocument xmlConfig = new XmlDocument();
            //load genomes
            // Save the best genome to file
            XmlReaderSettings xwSettings = new XmlReaderSettings();

            using (XmlReader xw = XmlReader.Create(CHAMPION_FILE, xwSettings))
            {
                anns = NeatGenomeXmlIO.ReadCompleteGenomeList(xw, false);
            }

            foreach (var neatGenome in anns)
            {
                Console.WriteLine("id_" + neatGenome.Id);
            }



            // Load config XML.
            XmlDocument xmlConfig1 = new XmlDocument();

            xmlConfig1.Load(@"..\..\..\NeatMusic\bin\Debug\config.xml");
            XmlElement xmlElement = xmlConfig1.DocumentElement;
            var        activation = ExperimentUtils.CreateActivationScheme(xmlElement, "Activation");

            IGenomeDecoder <NeatGenome, IBlackBox> genomeDecoder = new NeatGenomeDecoder(activation);

            int MODULE_COUNT = anns.Count / 2;
            int LENGHT       = rhythmInputs.Length;
            int LENGTHPITCH  = currentPitchPhrase.Length;
            //int LENGTHPITCH = LENGHT;
            int SEED_PITCH = 0;

            var rhythmPlayers = new List <NeatPlayer>();
            var pitchPlayers  = new List <NeatPlayer>();

            for (int i = 0; i < MODULE_COUNT; i++)
            {
                rhythmPlayers.Add(new NeatPlayer(genomeDecoder.Decode(anns[i])));
            }
            for (int i = MODULE_COUNT; i < MODULE_COUNT * 2; i++)
            {
                pitchPlayers.Add(new NeatPlayer(genomeDecoder.Decode(anns[i])));
            }

            double[][] rhythmOutput = new double[MODULE_COUNT][];
            int[][]    pitchOutput  = new int[MODULE_COUNT][];


            for (int i = 0; i < MODULE_COUNT; i++)
            {
                rhythmOutput[i] = new double[LENGHT];
                pitchOutput[i]  = new int[LENGTHPITCH];
            }

            for (int i = 0; i < MODULE_COUNT; i++)
            {
                rhythmOutput[i] = new double[LENGHT];
                pitchOutput[i]  = new int[LENGHT];
            }

            for (int i = 0; i < LENGHT; i++)
            {
                for (int p = 0; p < MODULE_COUNT; p++)
                {
                    var brainInput = MusicEnvironment.getBrainInputDelayed(pitchOutput, pitchInputs, p, MODULE_COUNT, i);

                    pitchOutput[p][i] = MusicEnvironment.convertToMidiClass(pitchPlayers[p].calculateOutput(brainInput));
                }
            }

            for (int i = 0; i < LENGHT; i++)
            {
                for (int p = 0; p < MODULE_COUNT; p++)
                {
                    var brainInput = MusicEnvironment.getBrainInputDelayed(rhythmOutput, rhythmInputs, p, MODULE_COUNT, i);

                    rhythmOutput[p][i] = rhythmPlayers[p].calculateOutput(brainInput);
                }
            }


            printFitness(MODULE_COUNT, pitchPlayers, rhythmPlayers);

            //get standard deviation
            Console.WriteLine(@"Input deviation: {0}", SmoothedZSmoothening.StandardDeviation(rhythmInputs));

            for (int i = 0; i < rhythmOutput.Length; i++)
            {
                double standardDev = SmoothedZSmoothening.StandardDeviation(rhythmOutput[i]);

                Console.WriteLine(@"Module {0} deviation: {1}", i, standardDev);
            }

            //look at outputs and find out when there are new notes and what they are
            //new note when current value is higher than previous one

            List <double>[] durationsLists = new List <double> [MODULE_COUNT];
            List <int>[]    pitchLists     = new List <int> [MODULE_COUNT];

            for (int i = 0; i < MODULE_COUNT; i++)
            {
                durationsLists[i] = new List <double>();
                pitchLists[i]     = new List <int>();
                findNewNotesFromOutput(rhythmOutput[i], pitchOutput[i], LENGHT, out durationsLists[i], out pitchLists[i]);
                mergeRests(durationsLists[i], pitchLists[i]);
                printResults(durationsLists[i], pitchLists[i], i + 1);
            }

            Sequence seq = new Sequence();

            seq.Add(getTrack(currentPitchPhrase, currentDurationPhrase, 60));
            //save input phrase in separate file
            seq.Save("base.mid");

            for (int i = 0; i < MODULE_COUNT; i++)
            {
                //int offset = i%2 == 0 ? 60 + 12 * (i/2 + 1) : 48 - 12 * (i/2 + 1); //how should this be done?
                int offset = 48;

                Track t = getTrack(pitchLists[i].ToArray(), durationsLists[i].ToArray(), offset);

                Sequence singleTrack = new Sequence();
                singleTrack.Add(t);
                singleTrack.Save("track" + (i + 1) + ".mid");

                seq.Add(t);
            }

            seq.Save("test.mid");

            // Hit return to quit.
            Console.ReadLine();
        }
Beispiel #6
0
        static void Generate(int songId)
        {
            Console.WriteLine("_________________________________________________________________________");
            Console.WriteLine("Now generating song " + songId);

            // Initialise log4net (log to console).
            XmlConfigurator.Configure(new FileInfo("log4net.properties"));

            // Experiment classes encapsulate much of the nuts and bolts of setting up a NEAT search.
            MusicExperiment experiment = new MusicExperiment(MODULES_COUNT);

            // Set the currentFitness
            MusicEnvironment.SetFitnessById(songId);

            // Set the currentSong
            MusicEnvironment.SetSongById(songId);

            // Load config XML.
            XmlDocument xmlConfig = new XmlDocument();

            xmlConfig.Load("config.xml");
            experiment.Initialize("NeatMusic", xmlConfig.DocumentElement);

            // Create evolution algorithm and attach update event.
            _ea = experiment.CreateEvolutionAlgorithms();
            _ea[0].UpdateEvent += new EventHandler(ea_UpdateEvent);

            // Start algorithm (it will run on a background thread).
            foreach (var neatEvolutionAlgorithm in _ea)
            {
                neatEvolutionAlgorithm.StartContinue();
            }

            // Continue until certain generations.
            while (_ea[0].CurrentGeneration < GENERATIONS_PER_SONG)
            {
            }

            foreach (var neatEvolutionAlgorithm in _ea)
            {
                neatEvolutionAlgorithm.Stop();
            }


            // Wait for threads to pause.
            while (_ea.All(ea => ea.RunState != RunState.Paused))
            {
                Thread.Sleep(100);
            }

            // Save the best genome to file
            //Console.Write("\nFinal Genomes: ");
            Monitor.Enter(_ea);
            List <NeatGenome> finalGenomes = new List <NeatGenome>();

            foreach (var a in _ea)
            {
                //Console.Write("\t" + a.CurrentChampGenome.EvaluationInfo.Fitness + ", id_" + a.CurrentChampGenome.Id);
                finalGenomes.Add(a.CurrentChampGenome);
            }
            var    doc          = NeatGenomeXmlIO.SaveComplete(finalGenomes, false);
            string championFile = @"..\..\..\NeatMusic\bin\Debug\Champions\" + songId + ".xml";

            Console.WriteLine("\nSaving champion file...");
            Console.WriteLine("_________________________________________________________________________");
            doc.Save(championFile);
            Monitor.Exit(_ea);
        }