예제 #1
0
        private void beginGenerationButton_Click(object sender, EventArgs e)
        {
            try
            {
                var retriever  = new PianoNoteRetriever();
                var midiEvents = new InstrumentMidiEventProducer(this.files.Select(x => new Sequence(x)));
                IReadOnlyList <MidiEvent> midi = midiEvents.GetOrderedMessages(GeneralMidiInstrument.AcousticGrandPiano);

                Chord.AllowForComplexSimplification = this.checkBox1.Checked;
                var accords = Chord.RetrieveChords(midi, retriever);

                INGrams <Chord> grams = null;
                if (this.homogeneous)
                {
                    grams = HomogenousNGrams <Chord> .BuildNGrams((int)this.leftRangeNumericUpDown.Value, accords);
                }
                else
                {
                    grams = HeterogenousNGrams <Chord> .BuildNGrams((int)this.leftRangeNumericUpDown.Value, (int)this.rightRangeNumericUpDown.Value, accords);
                }

                NGramGraphMarkovChain <Chord> graph = new NGramGraphMarkovChain <Chord>(grams);
                this.Save(graph);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
예제 #2
0
        private async void startMusicGeneration_Click(object sender, EventArgs e)
        {
            this.model = TemperaryVariables.Graph;
            if (null == this.model || null == this.neuralNetwork)
            {
                MessageBox.Show("Load neccessary files");
            }
            INGramWeightAssigner <Chord> assi = null;

            if (this.useWeightAssignerCheckBox.Checked)
            {
                NGramLinearSizeVsTimeWeightAssigner <Chord> sizetime = new NGramLinearSizeVsTimeWeightAssigner <Chord>(1F);
                NGramLinearSizeDistrubutionAssigner <Chord> size     = new NGramLinearSizeDistrubutionAssigner <Chord>(this.model);

                Dictionary <INGramWeightAssigner <Chord>, float> dic = new Dictionary <INGramWeightAssigner <Chord>, float>()
                {
                    { sizetime, this.sizeVsWeightAssigner.Value / 100F },
                    { size, this.countDistAssigner.Value / 100F }
                };
                assi = new NGramMetaWeightAssigner <Chord>(dic);
            }
            else
            {
                assi = new NGramIDWeightAssigner <Chord>(this.model);
            }
            this.trainingThread = new Thread(this.GeneticSearch);
            this.trainingThread.Start(assi);
        }
        private float DistanceCompleteWithRespectToLeft(NGramGraphMarkovChain <T> left, NGramGraphMarkovChain <T> right)
        {
            float sum = 0;

            foreach (var item in left)
            {
                var node = item.Key;
                if (!right.ValidNode(node))
                {
                    sum += this.ToleranceTransform(this.TransformFunction(this.EmptyNodeWeight));
                    continue;
                }
                var rightConnections = right.Edges(node).Select(y => this.TransformFunction(y.Probability)).ToArray();
                var total            = this.TransformFunction((float)this.ProbabilityDistace.Distance(item.Value.Select(x => this.TransformFunction(x.Probability)).ToArray(), rightConnections));
                if (total / (float)left.Count() < this.TolerancePerNode)
                {
                    sum += 0;
                }
                else
                {
                    sum += total;
                }
            }
            return(sum / (this.TransformFunction(1F) * (float)right.Grams.Count()));
        }
        private void beginGenerationButton_Click(object sender, EventArgs e)
        {
            try
            {
                PianoNoteRetriever retriever = new PianoNoteRetriever();
                var midiEvents = new InstrumentMidiEventProducer(this.files.Select(x => new Sequence(x)));
                var midi = midiEvents.GetOrderedMessages(GeneralMidiInstrument.AcousticGrandPiano);
                Chord.AllowForComplexSimplification = this.checkBox1.Checked;
                var accords = Chord.RetrieveChords(midi, retriever);

                INGrams<Chord> grams = null;
                if (this.homogeneous)
                {
                    grams = HomogenousNGrams<Chord>.BuildNGrams((int)this.leftRangeNumericUpDown.Value, accords);
                }
                else
                {
                    grams = HeterogenousNGrams<Chord>.BuildNGrams((int)this.leftRangeNumericUpDown.Value, (int)this.rightRangeNumericUpDown.Value, accords);
                }
                NGramGraphMarkovChain<Chord> graph = new NGramGraphMarkovChain<Chord>(grams);
                this.Save(graph);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        public ChordChainGeneticFunction(
            DiscreteNeuralNetworkByChord dnn,
            NGramGraphMarkovChain <Chord> chord,
            INGramWeightAssigner <Chord> assigner,
            ChordRandomFunctionType type = ChordRandomFunctionType.NoRandomSelection,
            ChordCrossFunctionType cross = ChordCrossFunctionType.DiscreteChoice)
        {
            dnn.NullCheck();
            chord.NullCheck();
            assigner.NullCheck();

            this.NeuralNetwork      = dnn;
            this.NGramGraph         = chord;
            this.RandomFunctionType = type;
            this.CrossFunctionType  = cross;
            this.ChordDepth         = this.NeuralNetwork.InputsCount * 2;
            this.NGramDepth         = 10;
            this.MutateCoefficient  = 0.05D;
            this.Assigner           = assigner;

            this.random = new Random();

            switch (type)
            {
            case ChordRandomFunctionType.AllowRandomSelection:
                this.RandomSelectionCoefficient = .05D;
                return;
            }
        }
예제 #6
0
        public NGramLinearSizeDistrubutionAssigner(NGramGraphMarkovChain <T> fullGraph, int windowSize = 20)
        {
            (windowSize > 5).AssertTrue();
            fullGraph.Any().AssertTrue();

            this.WindowSize   = windowSize;
            this.Distrubution = RetrieveDistrubution(fullGraph.Grams);
        }
예제 #7
0
        private void UpdateGraph(NGramGraphMarkovChain <T> chain)
        {
            this.wpfContainer.Child = GenerateWpfVisuals(GraphUIHelper.GenerateGraphUI <T>(chain));

            GraphArea.GenerateGraph(true);
            GraphArea.SetVerticesDrag(true, true);
            ZoomControl.ZoomToFill();
        }
        public NGramGraphChainRetriever(NGramGraphMarkovChain <T> a)
        {
            a.NullCheck();

            this.graph = a;
            this.NodeDepthBeforeGreedy = 100;
            this.Max = int.MaxValue;
        }
예제 #9
0
        private void ShowMelodyDifference(NGram <Chord>[] nGram)
        {
            NGramGraphMarkovChain <Chord>      melodyGraph = new NGramGraphMarkovChain <Chord>(HomogenousNGrams <Chord> .DirectBuiltUnsafe(nGram, 1));
            NGramSemanticGraphDistance <Chord> a           = new Library.GraphOperations.NGramSemanticGraphDistance <Chord>();

            string text = a.Distance(melodyGraph, TemporaryVariables.Graph, (this.useSubGraphCheckBox.Checked) ?
                                     NGramGraphDistanceType.SubGraph : NGramGraphDistanceType.CompleteGraph).ToString();

            this.graphDifferenceTextBox.Invoke((Action)(() => this.graphDifferenceTextBox.Text = text));
        }
        private List <NGram <Chord>[]> RetrieveOkay()
        {
            NGramGraphMarkovChain <Chord>    graph        = new NGramGraphMarkovChain <Chord>(this.HomogenousWindowingData);
            NGramGraphChainRetriever <Chord> mostProbable = new NGramGraphChainRetriever <Chord>(graph);

            return(mostProbable
                   .FromEachNodeRandom(
                       (this.Assigner == null) ? new NGramIDWeightAssigner <Chord>(graph) : this.Assigner,
                       this.WindowSize - 1,
                       10));
        }
예제 #11
0
        private void Save(NGramGraphMarkovChain <Chord> a)
        {
            TemperaryVariables.Graph = a;
            this.Dispose();

            return;

            SaveFileDialog saveFileDialog1 = new SaveFileDialog();

            saveFileDialog1.Filter           = "model files (*.smf)|*.smf";
            saveFileDialog1.FilterIndex      = 2;
            saveFileDialog1.RestoreDirectory = true;
            saveFileDialog1.CreatePrompt     = true;
            saveFileDialog1.ShowDialog();

            //a.Save(saveFileDialog1.FileName);
        }
        public float Distance(NGramGraphMarkovChain <T> left, NGramGraphMarkovChain <T> right, NGramGraphDistanceType type = NGramGraphDistanceType.CompleteGraph)
        {
            left.NullCheck();
            right.NullCheck();

            switch (type)
            {
            case NGramGraphDistanceType.CompleteGraph:
                return((this.DistanceCompleteWithRespectToLeft(left, right) + this.DistanceCompleteWithRespectToLeft(right, left)) / 2F);

            case NGramGraphDistanceType.SubGraph:
                if (left.Count() > right.Count())
                {
                    return(this.DistanceCompleteWithRespectToLeft(right, left));
                }
                return(this.DistanceCompleteWithRespectToLeft(left, right));
            }
            throw new NotImplementedException();
        }
        private Dictionary <NGram <T>, float> NextPossibleStateAssignment(NGramGraphMarkovChain <T> subGraph)
        {
            int timeMax = subGraph.Max(x => x.Value.Any() ? x.Value.Count(y => y.Edge.N) : 0);
            int sizeMax = subGraph.Max(y => y.Key.N);

            Func <int, int, float, float> func = new Func <int, int, float, float>((max, value, weight) => ((float)value / (float)max) * weight);

            var k = subGraph
                    .Skip(1)
                    .ToDictionary(
                x => x.Key,
                y => func(sizeMax, y.Key.N, this.SizeWeight)
                + func(timeMax, y.Value.Any() ? y.Value.Max(z => z.Edge.N) : 0, this.TimeWeight));

            var lastmax = k.Max(x => x.Value);

            return(k.ToDictionary(x => x.Key,
                                  y => y.Value / lastmax));
        }
예제 #14
0
        public override void FormLoad(object sender, EventArgs e)
        {
            this.DisableButtons();
            this.serachTextBox.ReadOnly = true;
            this.Size = this.Size;
            // List<string> corp = new List<string>() { "a", "b", "c", "a", "e", "f", "g", "h", "e", "f", "h", "b", "b", "c", "h", "g" };
            List <string> corp = TextCorpus.RetrieveAnyCompleteWordCorpus();

            HeterogenousNGrams <string> grams = HeterogenousNGrams <string> .BuildNGrams(1, 3, corp);

            //HomogenousNGrams<string> grams = HomogenousNGrams<string>.BuildNGrams(1, corp);

            //NGramGraphMarkovChain<string> graphHetero = new NGramGraphMarkovChain<string>(heterograms);
            NGramGraphMarkovChain <string> graph = new NGramGraphMarkovChain <string>(grams);

            this.MarkovGraph = graph;

            this.wpfContainer.Child = GenerateWpfVisuals(GraphUIHelper.GenerateGraphUI <string>(NGramGraphMarkovChain <string> .Empty(true)));
            ZoomControl.ZoomToFill();

            this.serachTextBox.ReadOnly = false;
        }
        private List<NGram<Chord>[]> RetrieveOkay()
        {
            NGramGraphMarkovChain<Chord> graph = new NGramGraphMarkovChain<Chord>(this.HomogenousWindowingData);
            NGramGraphChainRetriever<Chord> mostProbable = new NGramGraphChainRetriever<Chord>(graph);

            return mostProbable
                .FromEachNodeRandom(
                        (this.Assigner == null) ? new NGramIDWeightAssigner<Chord>(graph) : this.Assigner,
                        this.WindowSize - 1,
                        10);
        }
        private void Save(NGramGraphMarkovChain<Chord> a)
        {
            TemperaryVariables.Graph = a;
            this.Dispose();

            return;
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.Filter = "model files (*.smf)|*.smf";
            saveFileDialog1.FilterIndex = 2;
            saveFileDialog1.RestoreDirectory = true;
            saveFileDialog1.CreatePrompt = true;
            saveFileDialog1.ShowDialog();

            //a.Save(saveFileDialog1.FileName);
        }
예제 #17
0
        public Dictionary <NGram <T>, float> NextPossibleStateAssignment(NGram <T>[] past, NGramGraphMarkovChain <T> subGraph)
        {
            var start = past.Last();

            return(graph.Edges(start).ToDictionary(item => item.Edge, item => item.Probability));
        }
예제 #18
0
        public Dictionary <NGram <T>, float> NextPossibleStateAssignment(NGram <T>[] past, NGramGraphMarkovChain <T> subGraph)
        {
            Dictionary <INGramWeightAssigner <T>, Dictionary <NGram <T>, float> > list = this.functionsWithWeight
                                                                                         .ToDictionary(
                item => item.Key,
                item => item.Key.NextPossibleStateAssignment(past, subGraph));

            Dictionary <NGram <T>, float> built = new Dictionary <NGram <T>, float>();

            foreach (var assigner in list)
            {
                foreach (var item in assigner.Value)
                {
                    if (built.ContainsKey(item.Key))
                    {
                        built[item.Key] = built[item.Key] + (this.functionsWithWeight[assigner.Key] * item.Value);
                        continue;
                    }
                    built.Add(item.Key, (this.functionsWithWeight[assigner.Key] * item.Value));
                }
            }
            return(built);
        }
 public Dictionary <NGram <T>, float> NextPossibleStateAssignment(NGram <T>[] past, NGramGraphMarkovChain <T> subGraph)
 {
     return(this.NextPossibleStateAssignment(subGraph));
 }
예제 #20
0
 private void Save(NGramGraphMarkovChain <Chord> a)
 {
     TemporaryVariables.Graph = a;
     this.Dispose();
 }
예제 #21
0
        public Dictionary <NGram <T>, float> NextPossibleStateAssignment(NGram <T>[] past, NGramGraphMarkovChain <T> subGraph)
        {
            var possibleChoices = subGraph.Select(x => x.Key).ToList();

            var num = Math.Min(possibleChoices.Count, past.Length);

            var tempDictionary = NGramLinearSizeDistrubutionAssigner <T> .RetrieveDistrubution(possibleChoices.GetRange(possibleChoices.Count - num, num));

            var subtracted = NGramLinearSizeDistrubutionAssigner <T> .SubtractedDistrubutions(this.Distrubution, tempDictionary);

            var normalizationFactor = subtracted.Sum(x => Math.Abs(x.Value));
            var shiftFactor         = subtracted.Where(x => x.Value < 0).Select(x => x.Value).Sum();

            return(possibleChoices.ToDictionary(
                       item => item,
                       item => ((float)this.Distrubution[item.N] + (float)shiftFactor) / normalizationFactor));
        }
        public void LoadGraph(NGramGraphMarkovChain <T> graph)
        {
            graph.NullCheck();

            this.Graph = graph;
        }
예제 #23
0
 public NGramIDWeightAssigner(NGramGraphMarkovChain <T> graph)
 {
     this.graph = graph;
 }