private static void ParserGenerator() { var g = Ebnf.GrammarLayout(); var earley = new EarleyParser2(g); var sentence1 = Sentence.FromLetters(Grammars.Properties.Resources.Arithmetic_ebnf); // var sentence1 = Sentence.FromLetters(Grammars.Properties.Resources.Bnf); // var sentence2 = Sentence.FromLetters("<S> ::= <S> '+' <S>\r\n<S> ::= '1'\r\n"); // if (!sentence1.Equals(sentence2)) { } // int index = sentence1.Zip(sentence2, (c1, c2) => c1 == c2).TakeWhile(b => b).Count() + 1; var sppf = earley.ParseGetForest(sentence1); if (sppf == null) { throw new Exception(); } DotRunner.Run(DotBuilder.GetRawDot(sppf), "arithmetic_ebnf"); //var traversal = new Traversal(sppf, sentence1, g); //var result = traversal.Traverse(); //if (result.Count() != 1) { // throw new Exception(); //} //var generatedGrammar = new Grammar((IEnumerable<Production>)result.First().Payload, Nonterminal.Of("S")); //Console.WriteLine(generatedGrammar); }
private static void TraversePlay() { var g = new Grammar(new List <Production> { CFGParser.Production("<A> → <B>"), CFGParser.Production("<B> → <A>"), CFGParser.Production("<B> → 'x'"), }, Nonterminal.Of("A")); g = IdentityActions.Annotate(g); var earley2 = new EarleyParser2(g); var sentence = Sentence.FromWords("x"); var sppf2 = earley2.ParseGetForest(sentence); DotRunner.Run(DotBuilder.GetRawDot(sppf2), "infinite"); var t2 = new Traversal(sppf2, g); var r2 = t2.Traverse(); //foreach (var option in r2) { // var s2 = (Sentence)option.Payload; // if (!sentence.SequenceEqual(s2)) { // throw new Exception(); // } //} }
private static void DevelopAnnotations() { var sw = Stopwatch.StartNew(); var input = Sentence.FromLetters(Grammars.Properties.Resources.Arithmetic_annotated); var inputWithoutLayout = Ebnf.RemoveLayout(input, out _); var grammar = Ebnf.GrammarSyntax(); var parser = new EarleyParser2(grammar); var sppf = parser.ParseGetForest(inputWithoutLayout); if (sppf == null) { throw new Exception(); } var ms1 = sw.Elapsed.TotalMilliseconds; Console.WriteLine("Parse: {0:0.#}ms", ms1); DotRunner.Run(DotBuilder.GetRawDot(sppf), "annotations"); var traversal = new Traversal(sppf, grammar); var resCollection = traversal.Traverse(); if (resCollection.Count() > 1) { throw new Exception("ambiguous"); } var res = resCollection.Single(); Console.WriteLine(res); }
// from http://dx.doi.org/10.1016/j.entcs.2008.03.044 private static void PaperExamples() { var ex3 = new Grammar(new List <Production> { CFGParser.Production("<S> → <A> <T>"), CFGParser.Production("<S> → 'a' <T>"), CFGParser.Production("<A> → 'a'"), CFGParser.Production("<A> → <B> <A>"), CFGParser.Production("<B> → ε"), CFGParser.Production("<T> → 'b' 'b' 'b'"), }, Nonterminal.Of("S")); var input = Sentence.FromLetters("abbb"); var sppf1 = new EarleyParser(ex3).ParseGetForest(input); var sppf2 = new EarleyParser2(ex3).ParseGetForest(input); DotRunner.Run(DotBuilder.GetRawDot(sppf1), "example3_old"); DotRunner.Run(DotBuilder.GetRawDot(sppf2), "example3_new"); }
private static void BnfPlay() { var bnf = Bnf.Grammar(); var earley = new EarleyParser2(bnf); var sentence1 = Sentence.FromLetters(Grammars.Properties.Resources.Arithmetic); // var sentence1 = Sentence.FromLetters(Grammars.Properties.Resources.Bnf); // var sentence2 = Sentence.FromLetters("<S> ::= <S> '+' <S>\r\n<S> ::= '1'\r\n"); // if (!sentence1.Equals(sentence2)) { } // int index = sentence1.Zip(sentence2, (c1, c2) => c1 == c2).TakeWhile(b => b).Count() + 1; var sppf = earley.ParseGetForest(sentence1); if (sppf == null) { throw new Exception(); } DotRunner.Run(DotBuilder.GetRawDot(sppf), "bnfPlay"); }
public void CrawlGenome() { // 3--2, 0--7, Switch: 4, Key: 3, Door: 9--8, Key: 9, Door: 4--6, 9--0, Key: 8, Door: 8--5, Piston: 7--1, Key: 7, Door: 5--6, Piston: 2--6 // 0--7, 0--9, 2--3, Key: 3, Key: 7, Key: 8, Key: 9, Door: 4--6, Door: 5--6, Door: 5--8, Door: 8--9, Switch: 4, Piston: 1--7, Piston: 2--6 var builder = new ZeldaDungeonBuilder(); var lookup = builder.Lookup; // 0--7, 0--9, 2--3 builder.AddUndirectedTransition(0, 7, IdentityScript.Default); builder.AddUndirectedTransition(0, 9, IdentityScript.Default); builder.AddUndirectedTransition(2, 3, IdentityScript.Default); // Key: 3, Key: 7, Key: 8, Key: 9 builder.AddScript(3, ZeldaScripts.CreateKey(0, lookup)); builder.AddScript(7, ZeldaScripts.CreateKey(0, lookup)); builder.AddScript(8, ZeldaScripts.CreateKey(0, lookup)); builder.AddScript(9, ZeldaScripts.CreateKey(0, lookup)); // Door: 4--6, Door: 5--6, Door: 5--8, Door: 8--9 builder.AddUndirectedTransition(4, 6, ZeldaScripts.CreateDoor(0, lookup)); builder.AddUndirectedTransition(5, 6, ZeldaScripts.CreateDoor(0, lookup)); builder.AddUndirectedTransition(5, 8, ZeldaScripts.CreateDoor(0, lookup)); builder.AddUndirectedTransition(8, 9, ZeldaScripts.CreateDoor(0, lookup)); // Switch: 4, Piston: 1--7, Piston: 2--6 builder.AddScript(4, ZeldaScripts.CreateColorSwitch(0, lookup)); // Piston: 1--7, Piston: 2--6 builder.AddUndirectedTransition(1, 7, ZeldaScripts.CreateBluePiston(0, lookup)); builder.AddUndirectedTransition(2, 6, ZeldaScripts.CreateRedPiston(0, lookup)); var crawler = builder.Build(); var dot = new DotBuilder(); crawler.Express(dot); var initialState = emptyVariables.ToState(builder.Lookup); crawler.Crawl(new[] { initialState }, 10000); // test for exit reached Assert.True(crawler.DebugGetStep(0, initialState).HasDistanceFromExit); }
public void TestEvolution() { Profiler.Reset(); var random = new SystemRandom(42); var configuration = new ZeldaConfiguration(random); var factory = new ZeldaGenomeFactory(configuration); var initialVariables = new VariableAssignment(); var environment = new ZeldaEnvironment(new[] { initialVariables }, 10000); var writer = File.CreateText("stats.csv"); var evolution = new ElitistEvolution(200, 100, factory, environment, ZeldaIndividual.NumAttributes, writer); var genomes = evolution.Initialize(); // TODO Jonas: replace fixed weight multirank optimization by dynamic randomized weighting // i.e. in some generations prefer some attribute over others // evolve for (int i = 0; i < 1000; i++) { Profiler.BeginFrame(); Console.WriteLine("gen " + i); genomes = evolution.Evolve(genomes, random); Profiler.EndFrame(); } writer.Close(); var best = (ZeldaIndividual)evolution.GetBest(new TestComparer()); Console.WriteLine(best); var crawler = best.Crawler; var builder = new DotBuilder(); crawler.Express(builder); // TODO: output genome to puzzle unit test (puzzle building statements) Profiler.ExportToUnityProfileAnalyzer("w:\\EvolutionTest-TestEvolution.pdata"); }
private static void VisitorPlay() { var gp = AdditionGrammar(argList => string.Format("({0} + {1})", argList[0].Payload, argList[2].Payload)); //var actions2 = new Dictionary<Production, IParserAction> { // [p1] = new ParserAction<long>((argList) => (long)(argList[0].Payload) + (long)(argList[2].Payload)), //}; //var termAction2 = new ParserAction<long>(x => Convert.ToInt64(x[0].Payload)); //foreach (var num in nums) { // actions2[num] = termAction2; //} var ep = new EarleyParser2(gp); var inputString = AdditionInput(5); var input = Sentence.FromWords(inputString); var sppf = ep.ParseGetForest(input); var rawdot = DotBuilder.GetRawDot(sppf); DotRunner.Run(rawdot, "newSppf"); Console.WriteLine(sppf.ToString()); Console.WriteLine(); Console.WriteLine("Starting Traversal..."); var trav = new Traversal(sppf, gp); var resultList = trav.Traverse(); Console.WriteLine("-----------------"); foreach (var result in resultList) { Console.WriteLine(result.Payload); } //Console.WriteLine("-----------------"); //foreach (var result in new Traversal(sppf, input, gp).Traverse()) { // Console.WriteLine(result.Payload); //} }
private static void DebugGrammar() { BaseGrammar g = new Grammar(new List <Production> { CFGParser.Production("<S> → ε"), }, Nonterminal.Of("S")); var sentence = Sentence.FromWords("1 + 1 + 1"); var grammar = AdditionGrammar(argList => string.Format("({0} + {1})", argList[0].Payload, argList[2].Payload)); g = grammar; var earley = new EarleyParser(g); var earley2 = new EarleyParser2(g); //DotRunner.Run(earley.ParseGetForest(sentence).GetRawDot(), "testEarleyOld"); //DotRunner.Run(earley2.ParseGetForest(sentence).GetRawDot(), "testEarleyNew"); DotRunner.Run(DotBuilder.GetRawDot(earley.ParseGetForest(sentence)), "testEarleyOld"); DotRunner.Run(DotBuilder.GetRawDot(earley2.ParseGetForest(sentence)), "testEarleyNew"); // DotRunner.Run(DotBuilder.GetFlattenedDot(earley2.ParseGetForest(sentence)), "testEarleyFlat"); // var prob0 = earley.ParseGetProbability(sentence); var prob = earley2.ParseGetProbability(sentence); }
public void Express(DotBuilder builder) { builder.AddEdge(source.Id, destination.Id, script.Name); }
private void createMatrix_Click(object sender, EventArgs e) { //this.matrixTable.AcceptChanges(); //this.dataSet1.AcceptChanges(); int anzahl = this.matrixTable.Columns.Count; // DataTable table = this.dataSet1.Tables[0]; for (int y = 0; y < anzahl; y++) { Double[] dRow = new Double[anzahl]; for (int x = 0; x < anzahl; x++) { string wert = String.Empty; if (this.dataGridView1.Rows[y].Cells[x].Value != DBNull.Value) wert = (string)this.dataGridView1.Rows[y].Cells[x].Value; if (wert == null || wert == String.Empty || wert.Length == 0) { dRow[x] = PSEUDO; } else dRow[x] = Convert.ToDouble(wert); } Row row = new Row(dRow); this.matrix.AddRow(row); } builder = new DotBuilder(matrix, anzahl); builder.Build(fake, matrix, start); this.mapleTextBox.Text = builder.getRestrictions(Convert.ToInt32(this.mengeTextBox.Text), Convert.ToInt32(this.quelleTextBox.Text), Convert.ToInt32(this.senkeTextBox.Text)); this.tabControl1.SelectedTab = this.tabPage2; //Bild erstellen / Anzeigen PictureForm picForm = new PictureForm(); this.createPicture("StartGraph"); picForm.ShowPicture("StartGraph.jpg"); picForm.Show(); this.createPicture("start"); }