public void Translate_XOR_Set_By_Name_Value_Return_StringResult() { var context = GetXorContext(); var codebook = new Codebook(context, GetMetaInfo()); codebook.Translate("x1", 0).Should().Be("0"); }
public GameStateReader(Stream stream, GameStateReaderDebugSettings debugSettings = null) { _reader = new BinaryReader(stream, Encoding.UTF8, true); _packetParser = new PacketParser(); _codebook = new Codebook(); _debugSettings = debugSettings ?? new GameStateReaderDebugSettings(); }
public void Learn(List <BaseAttribute <T> > attributeColumns, BaseAttribute <T> outputAttributeColumn) { // Create a new instance of the ID3 algorithm ID3Learning id3learning = new ID3Learning(DecisionTree); // Translate our training data into integer symbols using our codebook: DataTable symbols = Codebook.Apply(Data); var columnNames = attributeColumns.Select(attribute => attribute.Name).ToArray(); int[][] inputs = symbols.ToJagged <int>(columnNames); int[] outputs = symbols.ToJagged <int>(outputAttributeColumn.Name).GetColumn(0); // Learn the training instances! DecisionTree = id3learning.Learn(inputs, outputs); double error = new ZeroOneLoss(outputs).Loss(DecisionTree.Decide(inputs)); Debug.WriteLine(error); // Moreover, we may decide to convert our tree to a set of rules: DecisionSet rules = DecisionTree.ToRules(); // And using the codebook, we can inspect the tree reasoning: string ruleText = rules.ToString(Codebook as Codification <string>, outputAttributeColumn.Name, System.Globalization.CultureInfo.InvariantCulture); Debug.WriteLine(ruleText); }
public void EncodeFrameReturnsExpectedCodes() { // Arrange var expectedCodes = new int[2, 2]; expectedCodes[1, 0] = 0; expectedCodes[1, 1] = 1; var expectedFrame = Frame.FromArray2D(expectedCodes); var codebook = new Codebook<string>(new Dictionary<string, IMapper> { {"col1", new LazyOrdinalMapper<string>()}, {"col2", new LazyOrdinalMapper<string>()} }); var inputFrame = Frame.FromRecords(new[] { new { col1 = "a", col2 = "a" }, new { col1 = "a", col2 = "b" } }); // Act var encodedFrame = codebook.Encode(inputFrame); // Assert var actualCodes = encodedFrame.ToArray2D<int>(); Assert.AreEqual(expectedCodes.Length, actualCodes.Length); Assert.AreEqual(expectedCodes.GetLength(0), actualCodes.GetLength(0)); Assert.AreEqual(expectedCodes.GetLength(1), actualCodes.GetLength(1)); Assert.AreEqual(expectedCodes[0, 0], actualCodes[0, 0]); Assert.AreEqual(expectedCodes[1, 0], actualCodes[1, 0]); Assert.AreEqual(expectedCodes[0, 1], actualCodes[0, 1]); Assert.AreEqual(expectedCodes[1, 1], actualCodes[1, 1]); }
public HMMGenerator(PatchNames instrument) { this.book = new Codebook<Note>(); this.instrument = instrument; DotNetLearn.Data.SampleSet asdasd; Accord.Math.Tools.SetupGenerator(10); // Consider some phrases: // string[][] phrases = { "The Big Brown Fox Jumps Over the Ugly Dog".Split(new char[]{' '}, StringSplitOptions.RemoveEmptyEntries), "This is too hot to handle".Split(new char[]{' '}, StringSplitOptions.RemoveEmptyEntries), "I am flying away like a gold eagle".Split(new char[]{' '}, StringSplitOptions.RemoveEmptyEntries), "Onamae wa nan desu ka".Split(new char[]{' '}, StringSplitOptions.RemoveEmptyEntries), "And then she asked, why is it so small?".Split(new char[]{' '}, StringSplitOptions.RemoveEmptyEntries), "Great stuff John! Now you will surely be promoted".Split(new char[]{' '}, StringSplitOptions.RemoveEmptyEntries), "Jayne was taken aback when she found out her son was gay".Split(new char[]{' '}, StringSplitOptions.RemoveEmptyEntries), }; // Let's begin by transforming them to sequence of // integer labels using a codification codebook: var codebook = new Codification("Words", phrases); // Now we can create the training data for the models: int[][] sequence = codebook.Translate("Words", phrases); // To create the models, we will specify a forward topology, // as the sequences have definite start and ending points. // var topology = new Forward(states: codebook["Words"].Symbols); int symbols = codebook["Words"].Symbols; // We have 7 different words // Create the hidden Markov model HiddenMarkovModel hmm = new HiddenMarkovModel(topology, symbols); // Create the learning algorithm var teacher = new ViterbiLearning(hmm); // Teach the model about the phrases double error = teacher.Run(sequence); // Now, we can ask the model to generate new samples // from the word distributions it has just learned: // List<int> sample = new List<int>(); int count = 10; sample.Add(hmm.Generate(1)[0]); while(sample.Count < count) { var k = hmm.Predict(sample.ToArray(), 1); sample.AddRange(k); } // And the result will be: "those", "are", "words". string[] result = codebook.Translate("Words", sample.ToArray()); }
public void EncodeFrameThrowsExceptionIfFrameHasColumnWithoutMapper() { // Arrange var codebook = new Codebook<int>(new Dictionary<int, IMapper>()); var frame = Frame.FromArray2D(new object[2,2]); // Act var exception = AssertX.Throws<InvalidOperationException>(() => codebook.Encode(frame)); // Assert }
public void EncodeFrameSourceParameterCannotBeNull() { // Arrange var codebook = new Codebook<string>(new Dictionary<string, IMapper>()); Frame<int, string> frame = null; // Act var exception = AssertX.Throws<ArgumentNullException>(() => codebook.Encode(frame)); // Assert Assert.AreEqual("source", exception.ParamName); }
public string Decide(List <BaseAttribute <T> > attributes, BaseAttribute <T> attributeColumn) { var columnNames = attributes.Select(attribute => attribute.Name).ToArray(); var values = attributes.Select(attribute => attribute.Value).ToArray(); int[] query = Codebook.Transform(columnNames, values); int output = DecisionTree.Decide(query); var answer = Codebook.Revert(attributeColumn.Name, output); return(answer.ToString()); }
public bool Analyze(LogEntry entry) { double[] log = Codebook.Transform(entry.EventData); bool result = classifier.Classify(log); if (result) { return(result); } return(result); //bool c1 = reg.Decide(log); //return c1; }
public bool TestAnalyze(string[] entry) { double[] log = Codebook.Transform(entry); bool result = classifier.Classify(log); if (result) { return(result); } return(result); //bool c1 = reg.Decide(log); //return c1; }
public void Translate_XOR_Set_Return_String_Representation(string[] keys, string[] values, int[] expected) { var context = GetXorContext(); var codebook = new Codebook(context, GetMetaInfo()); var input = new Dictionary <string, string>(); for (var i = 0; i < keys.Length; ++i) { input[keys[i]] = values[i]; } var result = codebook.Translate(input); result.SequenceEqual(expected).Should().BeTrue(); }
public void Convert_XOR_Set_To_ArrayInt() { var context = GetXorContext(); var codebook = new Codebook(context, GetMetaInfo()); var inputs = codebook.GetArray("x1", "x2"); var outputs = codebook.GetArray("result"); var result = inputs[0].SequenceEqual(new[] { 0, 0 }) && inputs[1].SequenceEqual(new[] { 0, 1 }) && inputs[2].SequenceEqual(new[] { 1, 0 }) && inputs[3].SequenceEqual(new[] { 1, 1 }) && outputs.SequenceEqual(new int[] { 0, 1, 1, 0 }); result.Should().Be(true); }
public IEnumerable <string> Generate(int length = 0, long max = long.MaxValue) { length = length == 0 ? (Length ?? 8) : length; var column = Codebook.Columns[0].ColumnName; int[] sample = Model.Generate(Math.Max(Math.Min(length, 32), 1)); string[] result = Codebook.Revert(column, sample); while (max-- >= 0) { yield return(string.Join("", result)); try { sample = Model.Generate(Math.Max(Math.Min(length, 32), 1)); result = Codebook.Revert(column, sample); } catch { } } }
static void Main(string[] args) { /*Feed forward neural network Group notes into 3 Classify all groups in existing songs as 1, all other combinations as 0. Use as fitness function Use HMM decode as fitness function GA*/ /* Databank db = new Databank("lib"); List<MelodySequence> drumSeqs = new List<MelodySequence>(); var comps = new Composition[][] { db.Load("Classic Rock").Compositions, db.Load("Jazz").Compositions, Utils.LoadCompositionsParallel(@"D:\Sync\4th year\Midi\Library2\Drums") }; foreach(Composition[] catt in comps) { foreach(var c in catt) { foreach(var t in c.Tracks) { if(t.Channel == 10) { drumSeqs.Add(t.GetMainSequence() as MelodySequence); } } } } List<Composition> drums = new List<Composition>(); foreach(var m in drumSeqs) { Composition c = new Composition(); Track t = new Track(PatchNames.Acoustic_Grand, 10); t.AddSequence(m); c.Add(t); drums.Add(c); } var catdrum = new CompositionCategory("Drums", "lib/Drums", drums.ToArray()); catdrum.Save("lib/Drums"); Console.ReadLine(); */ /*Composition comp = Composition.LoadFromMIDI(@"C:\Users\1gn1t0r\Documents\git\GeneticMIDI\GeneticMIDI\bin\Debug\test\other\twinkle.mid"); float time = Note.ToRealDuration(comp.GetLongestTrack().Duration); Console.WriteLine("Total time: {0}", time); MusicPlayer player = new MusicPlayer(); player.Play(comp); Console.ReadLine();*/ var twink = Composition.LoadFromMIDI(@"C:\Users\1gn1t0r\Documents\git\GeneticMIDI\GeneticMIDI\bin\Debug\test\other\twinkle.mid"); Databank db = new Databank("lib"); var cat = db.Load("Jazz"); Codebook<Note> book = new Codebook<Note>(); List<int[]> sequences = new List<int[]>(); foreach(var comp in cat.Compositions) { foreach(var track in comp.Tracks) { if(track.Instrument == PatchNames.Trumpet) { var mel = track.GetMelodySequence(); mel.Trim(80); mel.StandardizeDuration(); book.Add(mel.Notes); sequences.Add(book.ToCodes(mel.Notes)); } } if (sequences.Count > 10) break; } DotNetLearn.Markov.HiddenMarkovModel hmm = new DotNetLearn.Markov.HiddenMarkovModel(60, book.TotalUniqueSymbols); hmm.UniformRandomPriors(); hmm.MaxIterations = 50; hmm.Train(sequences.ToArray()); var o = hmm.PredictObservationSequence(80); var newmel = new MelodySequence(book.Translate(o)); Track t = new Track(PatchNames.Trumpet, 2); t.AddSequence(newmel); Console.ReadLine(); MusicPlayer player = new MusicPlayer(); player.Play(t); player.Stop(); Console.ReadLine(); /* var gen = new SamplingWithReplacement(cat, PatchNames.Acoustic_Grand); var mel = gen.Generate(); Console.WriteLine(mel.ToString()); MusicPlayer player = new MusicPlayer(); player.Play(mel); Console.WriteLine("Press enter to save"); Console.ReadLine(); WriteMelodyToFile(gen.OriginalSequence, "sampling_in.mid"); WriteMelodyToFile(mel, "sampling_out.mid"); */ /* MusicPlayer player = new MusicPlayer(); //db.LoadAll(); AccompanyGeneratorMarkov genMark = new AccompanyGeneratorMarkov(cat); var targetSeq = cat.Compositions[2].GetLongestTrack().GetMainSequence() as MelodySequence; var seqTest = genMark.Generate(targetSeq,5); Track trackTest = new Track(PatchNames.Orchestral_Strings, 2); trackTest.AddSequence(seqTest); Track targetTrack = new Track(PatchNames.Acoustic_Grand, 3); targetTrack.AddSequence(targetSeq); Composition comp = new Composition(); comp.Add(trackTest); comp.Add(targetTrack); player.Play(comp); return;*/ /* Databank db = new Databank("lib"); var cat = db.Load("Classical"); //AccompanimentGenerator2 Test AccompanimentGenerator2 gen = new AccompanimentGenerator2(cat, PatchNames.Orchestral_Strings); gen.Train(); // Composition comp = Composition.LoadFromMIDI(@"D:\Sync\4th year\Midi\Library2\Classical\Mixed\dvorak.mid"); //var comp = Composition.LoadFromMIDI(@"C:\Users\1gn1t0r\Documents\git\GeneticMIDI\GeneticMIDI\bin\Debug\test\ff7tifa.mid"); gen.SetSequence(comp.Tracks[0].GetMainSequence() as MelodySequence); MusicPlayer player = new MusicPlayer(); Console.WriteLine("Press enter to listen"); Console.ReadLine(); var mel = gen.Generate(); Composition newComp = new Composition(); Track newTrack = new Track(PatchNames.Orchestral_Strings , 2); newTrack.AddSequence(mel); newComp.Add(newTrack); /*comp.Tracks[0].Instrument = PatchNames.Acoustic_Grand; (comp.Tracks[0].GetMainSequence() as MelodySequence).ScaleVelocity(0.8f); /* newComp.Tracks.Add(comp.Tracks[0]); player.Play(newComp);*/ Console.ReadLine(); }
private void Build(string infix) { int argCount = 0, opCount = 0, parCount = 0; var tArgs = new Codebook <string>(); bool expectsArg = true, signedArg = false; var postfix = new StringBuilder(200); var s = new Stack <char>(); int i = 0; while (i < infix.Length) { char c = infix[i++]; if (c == ' ') { continue; } if (c == '(') { ++parCount; // increase the amount of currently opened parentheses signedArg = false; // an argument is expected now (or another open parentheses), and so the flags are reset expectsArg = true; s.Push(c); } else if (c == '*' || c == '/' || c == '^' || c == '%') { if (expectsArg) { throw new ArgumentException("Expected an argument at [" + i.ToString() + "] - found an operator '" + c.ToString() + "' instead."); } // increase the amount of found operators ++opCount; // and set the flag to expect an argument (or open parentheses) expectsArg = true; // at this point, signedArg flag is already set to false (done after argument parsing) while (s.Count > 0 && OperatorPrecedence(c) <= OperatorPrecedence(s.Peek())) { postfix.Append(s.Pop()); } s.Push(c); // operators with higher precedence get popped and added to the postfix string } else if (c == '+' || c == '-') { if (!expectsArg) { ++opCount; // same as with operators above expectsArg = true; while (s.Count > 0 && OperatorPrecedence(c) <= OperatorPrecedence(s.Peek())) { postfix.Append(s.Pop()); } s.Push(c); } else if (!signedArg) { signedArg = true; // additionally, + and - signs can be interpreted as unary operators s.Push(c == '-' ? 'N' : 'P'); // so when an argument is expected, but one of those operators is found instead } // it is treated as a sign of a hopefully upcoming argument else { throw new ArgumentException("Expected an argument at [" + i.ToString() + "] - found an operator '" + c.ToString() + "' instead."); } } else if (c == ')') { if (--parCount < 0) { throw new ArgumentException("Encountered too many closing parentheses at [" + i.ToString() + "]."); } expectsArg = false; // after a closing parentheses an operator is expected, also all operators within that parentheses get popped while (s.Count > 0 && s.Peek() != '(') { postfix.Append(s.Pop()); } if (s.Count > 0) { s.Pop(); // pop the open parentheses and check if the whole parentheses was signed if (s.Count > 0 && (s.Peek() == 'N' || s.Peek() == 'P')) { postfix.Append(s.Pop()); } } } else if (expectsArg) { postfix.Append('{'); // marks the beginning of an argument token int j = i; if (char.IsDigit(c)) { // handles a constant numeric value parsing, isReal is here to accept only one decimal point symbol bool isReal = false; // also ignores spaces and can't deal with scientific notation (yet) while (j < infix.Length && (char.IsDigit(infix[j]) || infix[j] == ' ' || ((infix[j] == '.' || infix[j] == ',') && (isReal = !isReal)))) { ++j; } decimal n = decimal.Parse(infix.Substring(i - 1, j - i + 1).Replace(" ", string.Empty).Replace(',', '.'), System.Globalization.CultureInfo.InvariantCulture); // for decimal point postfix.Append(n.ToString()).Append('}'); // adds the number and closes the token } else { --j; // finds the last symbol index for the argument's valid name while (j < infix.Length && IsValidArgChar(infix[j])) { ++j; } string a = infix.Substring(i - 1, j - i + 1).Trim(' '); // TrimEnd should be enough if (string.IsNullOrWhiteSpace(a)) { throw new ArgumentException("Expected an argument at [" + i.ToString() + "] - it appears to be unnamed."); } tArgs.Add(a); // adds the argument and closes the token postfix.Append("Arg").Append(tArgs.GetCode(a).ToString()).Append('}'); } // checks if the argument was signed, if so, pop the operator and add it to the postfix string if (s.Count > 0 && (s.Peek() == 'N' || s.Peek() == 'P')) { postfix.Append(s.Pop()); } expectsArg = signedArg = false; ++argCount; i = j; // skips characters scanned during argument parsing } else { throw new ArgumentException("Expected an operator at [" + i.ToString() + "] - found an argument instead."); } } if (argCount == 0) { throw new ArgumentException("Encountered an empty expression."); } if (argCount != opCount + 1) { throw new ArgumentException("Argument count must be equal to operator count + 1."); } if (parCount > 0) { throw new ArgumentException("Too many opening parentheses in the expression."); // parantheses could be ignored here? } if (parCount < 0) { throw new ArgumentException("Too many closing parentheses in the expression."); } while (s.Count > 0) { postfix.Append(s.Pop()); } TokenizedPostfix = postfix.ToString(); _argBook = tArgs; InputString = infix; }
public bool Predict(Record instance) { int[] inputs = Codebook.Translate(Parent.ExcludeLast(instance.Values)); return(Math.Sign(Machine.Compute(Parent.ToDoubles(inputs))) > 0); }
public MyBusiness(Codebook codebook) { Codebook = codebook; }