public void Feedback2() { CharSetSolver solver = new CharSetSolver(BitWidth.BV64); var al = new HashSet<char>(new char[] { 'a', 'b' }); var dfa1 = new PDLModSetEq(new PDLIndicesOf("a"), 2, 1).GetDFA(al, solver); var a = solver.MkCharConstraint(false, 'a'); var b = solver.MkCharConstraint(false, 'b'); var movescorrect = new List<Move<BDD>>(); movescorrect.Add(new Move<BDD>(0, 0, b)); movescorrect.Add(new Move<BDD>(0, 1, a)); movescorrect.Add(new Move<BDD>(1, 0, a)); movescorrect.Add(new Move<BDD>(1, 0, b)); var dfa2 = Automaton<BDD>.Create(0, new int[] { 1 }, movescorrect); var v4 = DFAGrading.GetGrade(dfa1, dfa2, al, solver, timeout, 10, FeedbackLevel.Hint); Console.WriteLine("Grade: {0}", v4.First); foreach (var v in v4.Second) Console.WriteLine("Feedback: {0}", v); }
public void Feedback2() { CharSetSolver solver = new CharSetSolver(BitWidth.BV64); var al = new HashSet <char>(new char[] { 'a', 'b' }); var dfa1 = new PDLModSetEq(new PDLIndicesOf("a"), 2, 1).GetDFA(al, solver); var a = solver.MkCharConstraint(false, 'a'); var b = solver.MkCharConstraint(false, 'b'); var movescorrect = new List <Move <BDD> >(); movescorrect.Add(new Move <BDD>(0, 0, b)); movescorrect.Add(new Move <BDD>(0, 1, a)); movescorrect.Add(new Move <BDD>(1, 0, a)); movescorrect.Add(new Move <BDD>(1, 0, b)); var dfa2 = Automaton <BDD> .Create(0, new int[] { 1 }, movescorrect); var v4 = DFAGrading.GetGrade(dfa1, dfa2, al, solver, timeout, 10, FeedbackLevel.Hint); Console.WriteLine("Grade: {0}", v4.First); foreach (var v in v4.Second) { Console.WriteLine("Feedback: {0}", v); } }
public void TestCsAlgebra_Mk() { var css = new CharSetSolver(); var aA = css.MkCharConstraint('a', true); var a = css.MkCharConstraint('a', false); var b = css.MkCharConstraint('b'); var ab = css.MkOr(a, b); var csa = new CsAlgebra <BDD>(css, new ICounter[] { new BoundedCounter(0, 4, 5), new BoundedCounter(1, 4, 5), new BoundedCounter(2, 4, 5), }); var alpha = csa.MkPredicate(ab, true, CsCondition.TRUE, CsCondition.CANEXIT, CsCondition.TRUE); var alpha_cases = alpha.ToArray(); Assert.IsTrue(alpha_cases.Length == 1); Assert.IsTrue(alpha_cases[0].Item2.Equals(ab)); Assert.IsTrue(alpha_cases[0].Item1.Equals(CsConditionSeq.MkAND(CsCondition.TRUE, CsCondition.CANEXIT, CsCondition.TRUE))); var beta = csa.MkPredicate(aA, true, CsCondition.TRUE, CsCondition.CANLOOP, CsCondition.TRUE); var gamma = csa.MkAnd(alpha, beta); var res = new List <Tuple <CsConditionSeq, BDD> >(gamma.GetSumOfProducts()); Assert.IsTrue(res.Count == 1); var counter_cond = res[0].Item1; var input_pred = res[0].Item2; Assert.IsTrue(input_pred.Equals(a)); Assert.IsTrue(counter_cond[1] == CsCondition.MIDDLE); }
public void TextP48() { var solver = new CharSetSolver(CharacterEncoding.Unicode); var alph = new List<char> { 'a', 'b' }; var al = new HashSet<char>(alph); var moves = new List<Move<CharSet>>(); var a = solver.MkCharConstraint(false, 'a'); var b = solver.MkCharConstraint(false, 'b'); moves.Add(new Move<CharSet>(0, 1, a)); moves.Add(new Move<CharSet>(0, 4, b)); moves.Add(new Move<CharSet>(1, 4, a)); moves.Add(new Move<CharSet>(1, 2, b)); moves.Add(new Move<CharSet>(2, 3, a)); moves.Add(new Move<CharSet>(2, 3, b)); moves.Add(new Move<CharSet>(3, 2, a)); moves.Add(new Move<CharSet>(3, 2, b)); moves.Add(new Move<CharSet>(4, 4, a)); moves.Add(new Move<CharSet>(4, 4, b)); var dfa = Automaton<CharSet>.Create(0, new int[] { 2 }, moves).Determinize(solver).Minimize(solver); var sb = new StringBuilder(); DFAUtilities.printDFA(dfa, al, sb); System.Console.WriteLine(sb); }
public void TextP48() { var solver = new CharSetSolver(CharacterEncoding.Unicode); var alph = new List <char> { 'a', 'b' }; var al = new HashSet <char>(alph); var moves = new List <Move <CharSet> >(); var a = solver.MkCharConstraint(false, 'a'); var b = solver.MkCharConstraint(false, 'b'); moves.Add(new Move <CharSet>(0, 1, a)); moves.Add(new Move <CharSet>(0, 4, b)); moves.Add(new Move <CharSet>(1, 4, a)); moves.Add(new Move <CharSet>(1, 2, b)); moves.Add(new Move <CharSet>(2, 3, a)); moves.Add(new Move <CharSet>(2, 3, b)); moves.Add(new Move <CharSet>(3, 2, a)); moves.Add(new Move <CharSet>(3, 2, b)); moves.Add(new Move <CharSet>(4, 4, a)); moves.Add(new Move <CharSet>(4, 4, b)); var dfa = Automaton <CharSet> .Create(0, new int[] { 2 }, moves).Determinize(solver).Minimize(solver); var sb = new StringBuilder(); DFAUtilities.printDFA(dfa, al, sb); System.Console.WriteLine(sb); }
public void DileepTest1() { PDLEnumerator pdlEnumerator = new PDLEnumerator(); var solver = new CharSetSolver(BitWidth.BV64); List<char> alph = new List<char> { 'a', 'b' }; HashSet<char> al = new HashSet<char>(alph); PDLPred phi = new PDLModSetEq(new PDLIndicesOf("a"), 2, 1); phi = new PDLAnd(new PDLStartsWith("a"), phi); var dfa1 = phi.GetDFA(al, solver); var a = solver.MkCharConstraint(false, 'a'); var b = solver.MkCharConstraint(false, 'b'); var moves = new List<Move<BDD>>(); moves.Add(new Move<BDD>(0, 0, a)); moves.Add(new Move<BDD>(0, 5, a)); moves.Add(new Move<BDD>(5, 0, a)); moves.Add(new Move<BDD>(5, 5, b)); var dfa2 = Automaton<BDD>.Create(0, new int[] { 5 }, moves); var feedbackGrade = DFAGrading.GetGrade(dfa1, dfa2, al, solver, timeout, 10, FeedbackLevel.Solution, true, false, false); var feedString = "<ul>"; foreach (var feed in feedbackGrade.Second) feedString += string.Format("<li>{0}</li>", feed); feedString += "</ul>"; Console.Write( string.Format("<div>Grade: {0} <br /> Feedback: {1}</div>", feedbackGrade.First, feedString)); }
public void TestMoveCharToggling3() { var solver = new CharSetSolver(BitWidth.BV64); List <char> alph = new List <char> { 'a', 'b' }; HashSet <char> al = new HashSet <char>(alph); var a = solver.MkCharConstraint(false, 'a'); var b = solver.MkCharConstraint(false, 'b'); var moves1 = new List <Move <BDD> >(); moves1.Add(new Move <BDD>(0, 1, b)); moves1.Add(new Move <BDD>(0, 2, a)); moves1.Add(new Move <BDD>(1, 2, a)); moves1.Add(new Move <BDD>(1, 2, b)); var nfa1 = Automaton <BDD> .Create(0, new int[] { 2 }, moves1); var moves2 = new List <Move <BDD> >(); moves2.Add(new Move <BDD>(0, 1, b)); moves2.Add(new Move <BDD>(1, 2, a)); moves2.Add(new Move <BDD>(1, 2, b)); var nfa2 = Automaton <BDD> .Create(0, new int[] { 2 }, moves2); var sb = new StringBuilder(); NFAEditDistanceProvider nfaedp = new NFAEditDistanceProvider(nfa1, al, solver, timeout); var distanceNfa1Nfa2 = nfaedp.GetNFAOptimalEdit(nfa2); Assert.IsTrue(distanceNfa1Nfa2.GetCost() == 1); }
public void TestMSO_Succ() { var solver = new CharSetSolver(BitWidth.BV32); var x = new Variable("x", true); var y = new Variable("y", true); MSOFormula<BDD> phi = new MSOForall<BDD>(x, new MSOImplies<BDD>( new MSOPredicate<BDD>(solver.MkCharConstraint( 'c'), x), new MSOExists<BDD>(y, new MSOAnd<BDD>( new MSOSuccN<BDD>(x, y, 1), new MSOPredicate<BDD>(solver.MkCharConstraint('a'), y) ) ) ) ); var aut = phi.GetAutomaton(solver); for (int i = 0; i < 10; i++) { var s = solver.GenerateMember(aut); Assert.IsTrue(System.Text.RegularExpressions.Regex.IsMatch(s, "^(ca|[^c])*$")); } var aut2 = solver.RegexConverter.Convert("^(ca|[^c])*$"); Assert.IsTrue(aut2.IsEquivalentWith(aut)); }
public void CheckAmbiguity() { var solver = new CharSetSolver(BitWidth.BV7); var a = solver.MkCharConstraint('a'); var b = solver.MkCharConstraint('b'); var moves = new List <Move <BDD> >(); moves.Add(new Move <BDD>(0, 1, a)); moves.Add(new Move <BDD>(0, 2, a)); moves.Add(new Move <BDD>(1, 3, a)); moves.Add(new Move <BDD>(2, 3, b)); var aut1 = Automaton <BDD> .Create(solver, 0, new int[] { 2, 3 }, moves); Automaton <BDD> ambl; Assert.IsFalse(aut1.IsAmbiguous(out ambl)); Assert.IsTrue(ambl.IsEmpty); var aut2 = Automaton <BDD> .Create(solver, 0, new int[] { 2, 3, 1 }, moves); Assert.IsTrue(aut2.IsAmbiguous(out ambl)); //solver.ShowGraph(ambl.Determinize(solver).Minimize(solver),"amb"); Assert.IsFalse(ambl.IsEmpty); }
public void Test27() // # of a even and # of b mod 3 = 0 { var solver = new CharSetSolver(BitWidth.BV64); var alph = new List <char> { 'a', 'b' }; var al = new HashSet <Char>(alph); var a = solver.MkCharConstraint(false, 'a'); var b = solver.MkCharConstraint(false, 'b'); var moves = new List <Move <BDD> >(); moves.Add(new Move <BDD>(0, 3, a)); moves.Add(new Move <BDD>(0, 1, b)); moves.Add(new Move <BDD>(1, 4, a)); moves.Add(new Move <BDD>(1, 2, b)); moves.Add(new Move <BDD>(2, 5, a)); moves.Add(new Move <BDD>(2, 0, b)); moves.Add(new Move <BDD>(3, 0, a)); moves.Add(new Move <BDD>(3, 4, b)); moves.Add(new Move <BDD>(4, 1, a)); moves.Add(new Move <BDD>(4, 5, b)); moves.Add(new Move <BDD>(5, 2, a)); moves.Add(new Move <BDD>(5, 3, b)); var dfa = Automaton <BDD> .Create(0, new int[] { 0 }, moves).Determinize(solver).Minimize(solver); PrintDFA(dfa, "Test27", al); }
public void CounterexampleFeedback() { var solver = new CharSetSolver(BitWidth.BV64); List <char> alph = new List <char> { 'a', 'b' }; HashSet <char> al = new HashSet <char>(alph); var a = solver.MkCharConstraint(false, 'a'); var b = solver.MkCharConstraint(false, 'b'); var movescorrect = new List <Move <BDD> >(); movescorrect.Add(new Move <BDD>(0, 0, a)); movescorrect.Add(new Move <BDD>(0, 1, b)); movescorrect.Add(new Move <BDD>(1, 1, a)); movescorrect.Add(new Move <BDD>(1, 1, b)); var dfa_correct = Automaton <BDD> .Create(0, new int[] { 0 }, movescorrect); var moves1 = new List <Move <BDD> >(); moves1.Add(new Move <BDD>(0, 0, a)); moves1.Add(new Move <BDD>(0, 0, b)); var dfa1 = Automaton <BDD> .Create(0, new int[] { 0 }, moves1); var v1 = DFAGrading.GetGrade(dfa_correct, dfa1, al, solver, timeout, 10, FeedbackLevel.Minimal, true, false, false); Console.WriteLine("Grade: {0}", v1.First); foreach (var feed in v1.Second) { Console.WriteLine(feed.ToString()); } }
public void MyTest() { PDLEnumerator pdlEnumerator = new PDLEnumerator(); var solver = new CharSetSolver(BitWidth.BV64); List <char> alph = new List <char> { 'a', 'b' }; HashSet <char> al = new HashSet <char>(alph); var a = solver.MkCharConstraint(false, 'a'); var b = solver.MkCharConstraint(false, 'b'); var moves = new List <Move <BDD> >(); moves.Add(new Move <BDD>(0, 1, a)); moves.Add(new Move <BDD>(0, 3, b)); moves.Add(new Move <BDD>(1, 2, b)); moves.Add(new Move <BDD>(2, 1, a)); moves.Add(new Move <BDD>(1, 1, a)); moves.Add(new Move <BDD>(2, 2, b)); moves.Add(new Move <BDD>(3, 4, a)); moves.Add(new Move <BDD>(4, 3, b)); moves.Add(new Move <BDD>(3, 3, b)); moves.Add(new Move <BDD>(4, 4, a)); var dfa1 = Automaton <BDD> .Create(0, new int[] { 0, 1, 3 }, moves).Determinize(solver).Minimize(solver); foreach (var v in pdlEnumerator.SynthesizePDL(al, dfa1, solver, new StringBuilder(), 5000)) { Console.WriteLine(PDLUtil.ToEnglishString(v)); break; } }
public void MyTest() { PDLEnumerator pdlEnumerator = new PDLEnumerator(); var solver = new CharSetSolver(BitWidth.BV64); List<char> alph = new List<char> { 'a', 'b' }; HashSet<char> al = new HashSet<char>(alph); var a = solver.MkCharConstraint(false, 'a'); var b = solver.MkCharConstraint(false, 'b'); var moves = new List<Move<BDD>>(); moves.Add(new Move<BDD>(0, 1, a)); moves.Add(new Move<BDD>(0, 3, b)); moves.Add(new Move<BDD>(1,2, b)); moves.Add(new Move<BDD>(2, 1, a)); moves.Add(new Move<BDD>(1, 1, a)); moves.Add(new Move<BDD>(2, 2, b)); moves.Add(new Move<BDD>(3, 4, a)); moves.Add(new Move<BDD>(4, 3, b)); moves.Add(new Move<BDD>(3, 3, b)); moves.Add(new Move<BDD>(4, 4, a)); var dfa1 = Automaton<BDD>.Create(0, new int[] { 0,1,3 }, moves).Determinize(solver).Minimize(solver); foreach (var v in pdlEnumerator.SynthesizePDL(al, dfa1, solver, new StringBuilder(), 5000)) { Console.WriteLine(PDLUtil.ToEnglishString(v)); break; } }
public void WS1SFormula() { var solver = new CharSetSolver(BitWidth.BV64); //new solver using ASCII encoding List <char> alph = new List <char> { 'a', 'b' }; HashSet <char> al = new HashSet <char>(alph); WS1SFormula f1 = new WS1SUnaryPred("X", solver.MkCharConstraint(false, 'a')); WS1SFormula f2 = new WS1SUnaryPred("Y", solver.MkCharConstraint(false, 'b')); WS1SFormula f3 = new WS1SSucc("X", "Y"); WS1SFormula phi = new WS1SAnd(new WS1SAnd(f1, f2), f3); WS1SFormula psi = new WS1SSucc("Y", "Z"); WS1SFormula formula = new WS1SExists("X", new WS1SExists("Y", new WS1SAnd(phi, new WS1SNot(new WS1SExists("Z", psi))))); StringBuilder sb = new StringBuilder(); formula.ToString(sb); Console.WriteLine(sb.ToString()); var dfa = formula.getDFA(al, solver); var test = solver.Convert(@"^(a|b)*ab$"); string file = "../../../MSOZ3Test/DotFiles/endsinab"; solver.SaveAsDot(dfa, "aut", file); //extension .dot is added automatically when missing Assert.IsTrue(dfa.IsEquivalentWith(test, solver)); }
public void MkRegexFromAutomatonOf_3_Divisibility() { var solver = new CharSetSolver(BitWidth.BV7); var _0 = solver.MkCharConstraint('a'); var _3 = solver.MkCharConstraint('d'); var _03 = solver.MkOr(_0, _3); var _1 = solver.MkCharConstraint('b'); var _2 = solver.MkCharConstraint('c'); var moves = new Move <BDD>[] { Move <BDD> .Create(0, 0, _03), Move <BDD> .Create(0, 1, _2), Move <BDD> .Create(0, 2, _1), Move <BDD> .Create(1, 1, _03), Move <BDD> .Create(1, 0, _1), Move <BDD> .Create(1, 2, _2), Move <BDD> .Create(2, 2, _03), Move <BDD> .Create(2, 0, _2), Move <BDD> .Create(2, 1, _1) }; var aut = Automaton <BDD> .Create(solver, 0, new int[] { 0 }, moves); //solver.ShowGraph(aut, "div3a"); string regex = solver.ConvertToRegex(aut).Replace("[ad]", "(a|d)").Replace("[b]", "b").Replace("[c]", "c"); var aut2 = solver.Convert(regex).Determinize().MinimizeHopcroft(); // solver.ShowGraph(aut2, "div3b"); bool equiv = aut.IsEquivalentWith(aut2); Assert.IsTrue(equiv); //binary version of the regex string regex01 = regex.Replace("a", "00").Replace("b", "01").Replace("c", "10").Replace("d", "11"); var bits30 = solver.Convert("^[01]{10,30}\\z"); var aut01_ = solver.Convert(regex01).Determinize(); //solver.ShowGraph(aut01_, "aut01_"); var aut01 = aut01_.MinimizeHopcroft(); //solver.ShowGraph(aut01, "aut01"); string regex01small = solver.ConvertToRegex(aut01); aut01 = aut01.Intersect(bits30); //genarate some random paths in this automaton and check that the binary representation is a numer that is divisible by 3. for (int i = 0; i < 1000; i++) { string sample = solver.ChooseString(aut01.ChoosePathToSomeFinalState(solver.Chooser)); int m = 0; for (int j = sample.Length - 1; j >= 0; j--) { if (sample[j] == '0') { m = m << 1; } else { m = (m << 1) | 1; } } bool div3 = ((m % 3) == 0); Assert.IsTrue(div3); } }
public void MarioBianucciNFAs() { var solver = new CharSetSolver(BitWidth.BV64); List <char> alph = new List <char> { 'a', 'b' }; HashSet <char> al = new HashSet <char>(alph); var a = solver.MkCharConstraint(false, '0'); var b = solver.MkCharConstraint(false, '1'); //PDLPred phi2 = new PDLIf(new PDLStartsWith("b"), new PDLEndsWith("b")); var movesSolution = new List <Move <BDD> >(); movesSolution.Add(new Move <BDD>(0, 0, a)); movesSolution.Add(new Move <BDD>(0, 1, b)); movesSolution.Add(new Move <BDD>(1, 2, a)); movesSolution.Add(new Move <BDD>(1, 1, b)); movesSolution.Add(new Move <BDD>(2, 2, a)); movesSolution.Add(new Move <BDD>(2, 3, b)); movesSolution.Add(new Move <BDD>(3, 0, a)); movesSolution.Add(new Move <BDD>(3, 3, b)); var dfaSolution = Automaton <BDD> .Create(0, new int[] { 0, 1 }, movesSolution); var movesAttempt = new List <Move <BDD> >(); movesAttempt.Add(new Move <BDD>(0, 0, a)); movesAttempt.Add(new Move <BDD>(0, 1, b)); movesAttempt.Add(new Move <BDD>(0, 1, null)); movesAttempt.Add(new Move <BDD>(1, 2, a)); movesAttempt.Add(new Move <BDD>(1, 1, b)); movesAttempt.Add(new Move <BDD>(2, 2, a)); movesAttempt.Add(new Move <BDD>(2, 3, b)); movesAttempt.Add(new Move <BDD>(3, 0, a)); movesAttempt.Add(new Move <BDD>(3, 3, b)); var dfaAttempt = Automaton <BDD> .Create(0, new int[] { 0, 1 }, movesAttempt); var gr = NFAGrading.GetGrade(dfaSolution, dfaAttempt, al, solver, 4000, 10, FeedbackLevel.Hint); Console.WriteLine(gr.First); foreach (var f in gr.Second) { Console.WriteLine(f.ToString()); } gr = NFAGrading.GetGrade(dfaAttempt, dfaSolution, al, solver, 4000, 10, FeedbackLevel.Hint); Console.WriteLine(gr.First); foreach (var f in gr.Second) { Console.WriteLine(f.ToString()); } }
public void Grade2DFAs() { var solver = new CharSetSolver(BitWidth.BV64); List <char> alph = new List <char> { 'a', 'b' }; HashSet <char> al = new HashSet <char>(alph); PDLPred phi = new PDLAnd(new PDLIntGeq(new PDLIndicesOf("a"), 2), new PDLIntGeq(new PDLIndicesOf("b"), 2)); //PDLPred phi2 = new PDLIf(new PDLStartsWith("b"), new PDLEndsWith("b")); var dfaCorr = phi.GetDFA(al, solver); var a = solver.MkCharConstraint(false, 'a'); var b = solver.MkCharConstraint(false, 'b'); var moves = new List <Move <BDD> >(); moves.Add(new Move <BDD>(0, 1, a)); moves.Add(new Move <BDD>(0, 1, b)); moves.Add(new Move <BDD>(1, 2, a)); moves.Add(new Move <BDD>(1, 2, b)); moves.Add(new Move <BDD>(2, 3, a)); moves.Add(new Move <BDD>(2, 3, b)); moves.Add(new Move <BDD>(3, 3, a)); moves.Add(new Move <BDD>(3, 3, b)); //moves.Add(new Move<BDD>(3, 4, a)); //moves.Add(new Move<BDD>(3, 2, b)); //moves.Add(new Move<BDD>(4, 4, a)); //moves.Add(new Move<BDD>(4, 5, b)); //moves.Add(new Move<BDD>(5, 6, a)); //moves.Add(new Move<BDD>(5, 4, b)); //moves.Add(new Move<BDD>(6, 6, a)); //moves.Add(new Move<BDD>(6, 6, b)); var dfa2 = Automaton <BDD> .Create(0, new int[] { 3 }, moves); //Assert.IsTrue(phi2.GetDFA(al,solver).IsEquivalentWith(dfa2,solver)); solver.SaveAsDot(dfaCorr, "aa", "corr"); solver.SaveAsDot(dfa2, "aa", "wrong"); //var v0 = DFADensity.GetDFADifferenceRatio(dfa1, dfa2, al, solver); //var v1 = PDLEditDistance.GetMinimalFormulaEditDistanceRatio(dfa1, dfa2, al, solver, timeout); //var v2 = DFAEditDistance.GetDFAOptimalEdit(dfa1, dfa2, al, solver, 4, new StringBuilder()); //Console.WriteLine("density ratio: {0}; pdl edit distance: {1}; dfa edit distance: {2}", v0, v1, v2); var gr = DFAGrading.GetGrade(dfaCorr, dfa2, al, solver, 2000, 10, FeedbackLevel.Hint, true, true, true); Console.WriteLine(gr.First); foreach (var f in gr.Second) { Console.WriteLine(f.ToString()); } }
public void GradingNFAEps() { var solver = new CharSetSolver(BitWidth.BV64); List <char> alph = new List <char> { 'a', 'b' }; HashSet <char> al = new HashSet <char>(alph); var a = solver.MkCharConstraint(false, 'a'); var b = solver.MkCharConstraint(false, 'b'); var movesSol = new List <Move <BDD> >(); movesSol.Add(new Move <BDD>(5, 0, null)); movesSol.Add(new Move <BDD>(0, 0, a)); movesSol.Add(new Move <BDD>(0, 0, b)); movesSol.Add(new Move <BDD>(0, 1, a)); movesSol.Add(new Move <BDD>(1, 2, b)); var nfa1 = Automaton <BDD> .Create(5, new int[] { 2 }, movesSol); var movesAtt = new List <Move <BDD> >(); movesAtt.Add(new Move <BDD>(0, 0, a)); movesAtt.Add(new Move <BDD>(0, 1, b)); movesAtt.Add(new Move <BDD>(1, 1, b)); movesAtt.Add(new Move <BDD>(1, 2, a)); movesAtt.Add(new Move <BDD>(2, 2, a)); movesAtt.Add(new Move <BDD>(2, 3, b)); movesAtt.Add(new Move <BDD>(3, 3, b)); movesAtt.Add(new Move <BDD>(3, 4, a)); movesAtt.Add(new Move <BDD>(4, 4, a)); movesAtt.Add(new Move <BDD>(4, 4, b)); var nfa2 = Automaton <BDD> .Create(0, new int[] { 4 }, movesAtt); List <Pair <int, IEnumerable <NFAFeedback> > > tests = new List <Pair <int, IEnumerable <NFAFeedback> > >(); tests.Insert(0, NFAGrading.GetGrade(nfa1, nfa2, al, solver, timeout, 10, FeedbackLevel.Hint)); tests.Insert(0, NFAGrading.GetGrade(nfa2, nfa1, al, solver, timeout, 10, FeedbackLevel.Hint)); tests.Insert(0, NFAGrading.GetGrade(nfa1, nfa1, al, solver, timeout, 10, FeedbackLevel.Hint)); tests.Insert(0, NFAGrading.GetGrade(nfa2, nfa2, al, solver, timeout, 10, FeedbackLevel.Hint)); foreach (var test in tests) { Console.WriteLine("grade: {0}, ", test.First); foreach (var f in test.Second) { Console.WriteLine(f.ToString()); } } }
public void FlabioNFA2() { var solver = new CharSetSolver(BitWidth.BV64); List <char> alph = new List <char> { '1', '0' }; HashSet <char> al = new HashSet <char>(alph); var o = solver.MkCharConstraint(false, '1'); var z = solver.MkCharConstraint(false, '0'); var movesSolution = new List <Move <BDD> >(); movesSolution.Add(new Move <BDD>(0, 1, null)); movesSolution.Add(new Move <BDD>(0, 2, null)); movesSolution.Add(new Move <BDD>(1, 1, z)); movesSolution.Add(new Move <BDD>(1, 1, o)); movesSolution.Add(new Move <BDD>(1, 3, z)); movesSolution.Add(new Move <BDD>(2, 2, o)); var dfaSolution = Automaton <BDD> .Create(0, new int[] { 2, 3 }, movesSolution); var movesAttempt = new List <Move <BDD> >(); movesAttempt.Add(new Move <BDD>(0, 1, null)); movesAttempt.Add(new Move <BDD>(0, 3, null)); movesAttempt.Add(new Move <BDD>(1, 2, z)); movesAttempt.Add(new Move <BDD>(2, 1, o)); movesAttempt.Add(new Move <BDD>(2, 2, z)); movesAttempt.Add(new Move <BDD>(1, 1, o)); movesAttempt.Add(new Move <BDD>(3, 4, o)); movesAttempt.Add(new Move <BDD>(4, 4, o)); var dfaAttempt = Automaton <BDD> .Create(0, new int[] { 4, 2 }, movesAttempt); var gr = NFAGrading.GetGrade(dfaSolution, dfaAttempt, al, solver, 1500, 10, FeedbackLevel.Hint); Console.WriteLine(gr.First); foreach (var f in gr.Second) { Console.WriteLine(f.ToString()); } gr = NFAGrading.GetGrade(dfaAttempt, dfaSolution, al, solver, 1500, 10, FeedbackLevel.Hint); Console.WriteLine(gr.First); foreach (var f in gr.Second) { Console.WriteLine(f.ToString()); } }
public void GradingTest1() { var solver = new CharSetSolver(BitWidth.BV64); List <char> alph = new List <char> { 'a', 'b' }; HashSet <char> al = new HashSet <char>(alph); var a = solver.MkCharConstraint(false, 'a'); var b = solver.MkCharConstraint(false, 'b'); var moves = new List <Move <BDD> >(); moves.Add(new Move <BDD>(0, 0, a)); moves.Add(new Move <BDD>(0, 1, b)); moves.Add(new Move <BDD>(1, 1, b)); moves.Add(new Move <BDD>(1, 2, a)); moves.Add(new Move <BDD>(2, 2, a)); moves.Add(new Move <BDD>(2, 3, b)); moves.Add(new Move <BDD>(3, 3, b)); moves.Add(new Move <BDD>(3, 4, a)); moves.Add(new Move <BDD>(4, 4, a)); moves.Add(new Move <BDD>(4, 5, b)); moves.Add(new Move <BDD>(5, 6, a)); moves.Add(new Move <BDD>(5, 5, b)); moves.Add(new Move <BDD>(6, 6, a)); moves.Add(new Move <BDD>(6, 6, b)); var dfa1 = Automaton <BDD> .Create(0, new int[] { 4, 5 }, moves); var moves1 = new List <Move <BDD> >(); moves1.Add(new Move <BDD>(0, 0, a)); moves1.Add(new Move <BDD>(0, 1, b)); moves1.Add(new Move <BDD>(1, 1, b)); moves1.Add(new Move <BDD>(1, 2, a)); moves1.Add(new Move <BDD>(2, 2, a)); moves1.Add(new Move <BDD>(2, 3, b)); moves1.Add(new Move <BDD>(3, 3, b)); moves1.Add(new Move <BDD>(3, 4, a)); moves1.Add(new Move <BDD>(4, 4, a)); moves1.Add(new Move <BDD>(4, 4, b)); var dfa2 = Automaton <BDD> .Create(0, new int[] { 4 }, moves1); var v1 = DFAGrading.GetGrade(dfa1, dfa2, al, solver, timeout, 10, FeedbackLevel.Hint); Console.WriteLine("grade0: {0}, ", v1); }
public void TestNFA2() { CharSetSolver solver = new CharSetSolver(BitWidth.BV7); var a = solver.MkCharConstraint('a'); var na = solver.MkNot(a); var nfa = Automaton <BDD> .Create(solver, 0, new int[] { 1 }, new Move <BDD>[] { new Move <BDD>(0, 1, solver.True), new Move <BDD>(0, 2, solver.True), new Move <BDD>(2, 1, solver.True), new Move <BDD>(1, 1, a), new Move <BDD>(1, 2, na) }); var min_nfa = nfa.Minimize(); nfa.isDeterministic = true; //pretend that nfa is equivalent, causes the deterministic version to be executed that provides the wrong result var min_nfa_wrong = nfa.Minimize(); nfa.isDeterministic = false; min_nfa_wrong.isDeterministic = false; //min_nfa.ShowGraph("min_nfa"); //min_nfa_wrong.ShowGraph("min_nfa_wrong"); //min_nfa.Determinize().Minimize().ShowGraph("min_nfa1"); //nfa.Determinize().Minimize().ShowGraph("dfa"); //nfa.ShowGraph("nfa"); //min_nfa_wrong.Determinize().Minimize().ShowGraph("min_nfa2"); Assert.IsFalse(min_nfa.IsEquivalentWith(min_nfa_wrong)); Assert.IsTrue(min_nfa.IsEquivalentWith(nfa)); //concrete witness "abab" distinguishes nfa from min_nfa_wrong Assert.IsTrue(solver.Convert("^abab$").Intersect(nfa).IsEmpty); Assert.IsFalse(solver.Convert("^abab$").Intersect(min_nfa_wrong).IsEmpty); }
public void MakeOneStep(Transduction move) { char c = TransductionUtil.ToChar(move); IEnumerable <Move <BvSet> > movesFromCurrent = moveAutomaton.GetMovesFrom(currentState); foreach (Move <BvSet> m in movesFromCurrent) { if (!solver.MkAnd(m.Condition, solver.MkCharConstraint(true, c)).IsEmpty) { currentState = m.TargetState; return; } } throw new Exception("Move not available"); }
public void WS1SNot() { var solver = new CharSetSolver(BitWidth.BV64); //new solver using ASCII encoding List <char> alph = new List <char> { 'a', 'b' }; HashSet <char> al = new HashSet <char>(alph); WS1SFormula f1 = new WS1SUnaryPred("X", solver.MkCharConstraint(false, 'a')); WS1SFormula f2 = new WS1SSingleton("X"); WS1SFormula f = new WS1SAnd(f1, f2); WS1SFormula phi = new WS1SNot(new WS1SExists("X", f)); var dfa = phi.getDFA(al, solver); var test = solver.Convert(@"^b*$"); Assert.IsTrue(dfa.IsEquivalentWith(test, solver)); string file = "../../../MSOZ3Test/DotFiles/nota"; solver.SaveAsDot(dfa, "aut", file); //extension .dot is added automatically when missing }
public void TestStateDistinguisher_RandomCharSelector() { var solver = new CharSetSolver(); var A = solver.Convert("[A-Z].*[A-Z]").Determinize().RemoveEpsilons(); var B = solver.Convert("[0-9].*[0-9].*[0-9]").Determinize().RemoveEpsilons(); var AB = A.Intersect(B); var states = new List <int>(AB.GetStates()); var dist = new StateDistinguisher_Moore <BDD>(AB, solver.ChooseUniformly); //check that the states in the minimized automaton are all distiguishable var ABMin = AB.Minimize(); //here we know that the minimization algorithm keeps the same state ids foreach (var p in ABMin.States) { foreach (var q in ABMin.States) { if (p != q) { string w; Assert.IsTrue(dist.AreDistinguishable(p, q, out w)); var w_preds = Array.ConvertAll(w.ToCharArray(), c => solver.MkCharConstraint(c)); var p1 = ABMin.GetTargetState(p, w_preds); var q1 = ABMin.GetTargetState(q, w_preds); Assert.IsTrue(ABMin.IsFinalState(p1) != ABMin.IsFinalState(q1)); } } } }
public static Pair<HashSet<char>, Automaton<BDD>> parseDFAFromString(string str, CharSetSolver solver) { var lines = Regex.Split(str, "\r\n|\r|\n"); HashSet<char> al = new HashSet<char>(); var line = lines[0]; var tokens = line.Split(new char[] { ' ' }); for (int i = 1; i < tokens.Length; i++) al.Add(tokens[i].ToCharArray()[0]); var finalStates = new List<int>(); line = lines[2]; tokens = line.Split(new char[] { ' ' }); for (int i = 2; i < tokens.Length; i++) finalStates.Add(Convert.ToInt32(tokens[i])); var moves = new List<Move<BDD>>(); for (int i = 3; i < lines.Length; i++) { tokens = lines[i].Split(new char[] { ',' }); if (tokens.Length > 1) moves.Add(new Move<BDD>(Convert.ToInt32(tokens[0]), Convert.ToInt32(tokens[1]), solver.MkCharConstraint(false, tokens[2].ToCharArray()[0]))); } return new Pair<HashSet<char>, Automaton<BDD>>(al, Automaton<BDD>.Create(0, finalStates, moves)); }
public Automaton<BDD> getDFA(HashSet<char> alphabet, CharSetSolver solver) { //Predicate representing the alphabet var alphPred = solver.False; foreach (var ch in alphabet) alphPred = solver.MkOr(solver.MkCharConstraint(false, ch), alphPred); var dfa1 = this.Normalize(solver).PushQuantifiers().getDFA(new List<string>(), alphPred, solver); var moves = new List<Move<BDD>>(); foreach (var move in dfa1.GetMoves()) foreach (var ch in solver.GenerateAllCharacters(solver.MkAnd(move.Label,alphPred),false)) moves.Add(new Move<BDD>(move.SourceState,move.TargetState,solver.MkCharConstraint(false,ch))); return Automaton<BDD>.Create(dfa1.InitialState,dfa1.GetFinalStates(),moves,true,true).Determinize(solver).Minimize(solver); }
public void AgutssonNFAs() { var solver = new CharSetSolver(BitWidth.BV64); List <char> alph = new List <char> { 'a', 'b' }; HashSet <char> al = new HashSet <char>(alph); var a = solver.MkCharConstraint(false, 'a'); var b = solver.MkCharConstraint(false, 'b'); var movesSolution = new List <Move <BDD> >(); movesSolution.Add(new Move <BDD>(0, 1, a)); movesSolution.Add(new Move <BDD>(1, 2, a)); movesSolution.Add(new Move <BDD>(0, 3, a)); movesSolution.Add(new Move <BDD>(0, 3, b)); movesSolution.Add(new Move <BDD>(3, 0, a)); movesSolution.Add(new Move <BDD>(3, 0, b)); var dfaSolution = Automaton <BDD> .Create(0, new int[] { 2 }, movesSolution); var movesAttempt = new List <Move <BDD> >(); movesAttempt.Add(new Move <BDD>(0, 0, a)); movesAttempt.Add(new Move <BDD>(0, 0, b)); movesAttempt.Add(new Move <BDD>(0, 1, a)); movesAttempt.Add(new Move <BDD>(1, 2, a)); movesAttempt.Add(new Move <BDD>(2, 3, a)); var dfaAttempt = Automaton <BDD> .Create(0, new int[] { 3 }, movesAttempt); var gr = NFAGrading.GetGrade(dfaSolution, dfaAttempt, al, solver, 1500, 10, FeedbackLevel.Hint); Console.WriteLine(gr.First); foreach (var f in gr.Second) { Console.WriteLine(f.ToString()); } gr = NFAGrading.GetGrade(dfaAttempt, dfaSolution, al, solver, 1500, 10, FeedbackLevel.Hint); Console.WriteLine(gr.First); foreach (var f in gr.Second) { Console.WriteLine(f.ToString()); } }
public void GradingTest1() { var solver = new CharSetSolver(BitWidth.BV64); List<char> alph = new List<char> { 'a', 'b' }; HashSet<char> al = new HashSet<char>(alph); var a = solver.MkCharConstraint(false, 'a'); var b = solver.MkCharConstraint(false, 'b'); var moves = new List<Move<BDD>>(); moves.Add(new Move<BDD>(0, 0, a)); moves.Add(new Move<BDD>(0, 1, b)); moves.Add(new Move<BDD>(1, 1, b)); moves.Add(new Move<BDD>(1, 2, a)); moves.Add(new Move<BDD>(2, 2, a)); moves.Add(new Move<BDD>(2, 3, b)); moves.Add(new Move<BDD>(3, 3, b)); moves.Add(new Move<BDD>(3, 4, a)); moves.Add(new Move<BDD>(4, 4, a)); moves.Add(new Move<BDD>(4, 5, b)); moves.Add(new Move<BDD>(5, 6, a)); moves.Add(new Move<BDD>(5, 5, b)); moves.Add(new Move<BDD>(6, 6, a)); moves.Add(new Move<BDD>(6, 6, b)); var dfa1 = Automaton<BDD>.Create(0, new int[] { 4,5 }, moves); var moves1 = new List<Move<BDD>>(); moves1.Add(new Move<BDD>(0, 0, a)); moves1.Add(new Move<BDD>(0, 1, b)); moves1.Add(new Move<BDD>(1, 1, b)); moves1.Add(new Move<BDD>(1, 2, a)); moves1.Add(new Move<BDD>(2, 2, a)); moves1.Add(new Move<BDD>(2, 3, b)); moves1.Add(new Move<BDD>(3, 3, b)); moves1.Add(new Move<BDD>(3, 4, a)); moves1.Add(new Move<BDD>(4, 4, a)); moves1.Add(new Move<BDD>(4, 4, b)); var dfa2 = Automaton<BDD>.Create(0, new int[] { 4 }, moves1); var v1 = DFAGrading.GetGrade(dfa1, dfa2, al, solver, timeout, 10, FeedbackLevel.Hint); Console.WriteLine("grade0: {0}, ", v1); }
public override WS1SFormula ToWS1S(CharSetSolver solver) { if (WS1S == null) { WS1S = new WS1SUnaryPred(set, solver.MkCharConstraint(false, label)); } return(WS1S); }
public void TestCountNoProduct2() { int maxvars = 13; var solver = new CharSetSolver(BitWidth.BV7); Stopwatch sw = new Stopwatch(); var tries = 1; var times = new long[maxvars - 1]; var x = new Variable("x", true); var y = new Variable("y", true); for (int k = 0; k < tries; k++) { for (int vars = 8; vars <= maxvars; vars++) { MSOFormula <BDD> phi = new MSOTrue <BDD>(); for (int i = 1; i < vars; i++) { phi = new MSOAnd <BDD>(phi, new MSOLt <BDD>(new Variable("x" + i, true), new Variable("x" + (i + 1), true))); phi = new MSOAnd <BDD>(phi, new MSOPredicate <BDD>(solver.MkCharConstraint('a'), new Variable("x" + i, true))); phi = new MSOOr <BDD>(phi, new MSOPredicate <BDD>(solver.MkCharConstraint('k'), new Variable("x" + i, true))); } phi = new MSOAnd <BDD>(phi, new MSOPredicate <BDD>(solver.MkCharConstraint('a'), new Variable("x" + vars, true))); phi = new MSOOr <BDD>(phi, new MSOExists <BDD>(y, new MSOPredicate <BDD>(solver.MkCharConstraint('c'), y))); for (int i = vars; i >= 1; i--) { phi = new MSOExists <BDD>(new Variable("x" + i, true), phi); } sw.Restart(); //var aut1 = phi.GetAutomaton(new CartesianAlgebraBDD<BDD>(solver)); var aut1 = phi.GetAutomaton(solver); sw.Stop(); times[vars - 2] += sw.ElapsedMilliseconds; //Console.WriteLine("States {0} Trans {1}",aut1.StateCount,aut1.MoveCount); if (k == tries - 1) { TestContext.WriteLine(string.Format("{0} variables; {1} ms", vars, times[vars - 2] / tries)); } //solver.ShowGraph(aut1, "a"+vars); } } }
public static Pair<HashSet<char>, Automaton<BDD>> parseNFAFromXML(XElement Automaton1, CharSetSolver solver) { HashSet<char> al = new HashSet<char>(); var moves = new List<Move<BDD>>(); var finalStates = new List<int>(); int start = 0; XElement Automaton = XElement.Parse(RemoveAllNamespaces(Automaton1.ToString())); XElement xmlAlphabet = Automaton.Element("alphabet"); foreach (XElement child in xmlAlphabet.Elements()) { char element = Convert.ToChar(child.Value); if (element != 'ε' && element != '?') al.Add(element); } XElement trans = Automaton.Element("transitionSet"); foreach (XElement child in trans.Elements()) { if (child.Name == "transition") { char element = Convert.ToChar(child.Element("read").Value); if (element != 'ε' && element != '?') moves.Add(new Move<BDD>(Convert.ToInt32(child.Element("from").Value), Convert.ToInt32(child.Element("to").Value), solver.MkCharConstraint(false, element))); else moves.Add(Move<BDD>.Epsilon(Convert.ToInt32(child.Element("from").Value), Convert.ToInt32(child.Element("to").Value))); } } XElement acc = Automaton.Element("acceptingSet"); foreach (XElement child in acc.Elements()) { if (child.Name == "state") { finalStates.Add((int)child.Attribute("sid")); } } XElement states = Automaton.Element("initState"); foreach (XElement child in states.Elements()) { if (child.Name == "state") { start = (int)child.Attribute("sid"); } } return new Pair<HashSet<char>, Automaton<BDD>>(al, Automaton<BDD>.Create(start, finalStates, moves)); }
public void TestWS1S_Label() { var solver = new CharSetSolver(BitWidth.BV7); var x = new WS1SVariable <BDD>("x"); var pred = new WS1SPred <BDD>(solver.MkCharConstraint('c'), x); var ca = new CartesianAlgebraBDD <BDD>(solver); var lab = pred & !x; var lab_aut = lab.GetAutomaton(ca, x); //lab_aut.ShowGraph("lab_aut"); }
private static BDD BDDOf(IEnumerable <char> alphabet, CharSetSolver solver) { bool fst = true; BDD safeCharCond = null; foreach (var c in alphabet) { if (fst) { fst = false; safeCharCond = solver.MkCharConstraint(false, c); } else { safeCharCond = solver.MkOr(safeCharCond, solver.MkCharConstraint(false, c)); } } return(safeCharCond); }
public void TestMSO_Or() { var solver = new CharSetSolver(BitWidth.BV32); MSOFormula<BDD> phi = new MSOForall<BDD>(V1("x"), new MSOOr<BDD>( new MSOPredicate<BDD>(solver.MkCharConstraint( 'c'), V1("x")), new MSOPredicate<BDD>(solver.MkCharConstraint( 'a'), V1("x")) ) ); var aut = phi.GetAutomaton(solver); for (int i = 0; i < 10; i++) { var s = solver.GenerateMember(aut); Assert.IsTrue(System.Text.RegularExpressions.Regex.IsMatch(s, "^[ac]*$")); } var aut2 = solver.RegexConverter.Convert("^[ac]*$"); Assert.IsTrue(aut2.IsEquivalentWith(aut)); }
public void TestWS1S_Label() { var solver = new CharSetSolver(BitWidth.BV7); var x = new Variable("X", false); var pred = new MSOPredicate <BDD>(solver.MkCharConstraint('c'), x); var ca = new CartesianAlgebraBDD <BDD>(solver); var lab = pred & new MSOIsSingleton <BDD>(x); var lab_aut = lab.GetAutomaton(ca); //lab_aut.ShowGraph("lab_aut"); }
public void WS1SSubset() { var solver = new CharSetSolver(BitWidth.BV64); //new solver using ASCII encoding List <char> alph = new List <char> { 'a', 'b' }; HashSet <char> al = new HashSet <char>(alph); WS1SFormula f1 = new WS1SUnaryPred("X", solver.MkCharConstraint(false, 'a')); WS1SFormula f2 = new WS1SUnaryPred("Y1", solver.MkCharConstraint(false, 'b')); WS1SFormula f3 = new WS1SUnaryPred("Z", solver.MkCharConstraint(false, 'a')); WS1SFormula f = new WS1SAnd(f1, new WS1SAnd(f2, f3)); WS1SFormula s1 = new WS1SSucc("X", "Y1"); WS1SFormula s2 = new WS1SSucc("Y2", "Z"); WS1SFormula s3 = new WS1SSubset("Y1", "Y2"); WS1SFormula s = new WS1SAnd(new WS1SAnd(s1, s2), s3); WS1SFormula phi = new WS1SExists("X", new WS1SExists("Y1", new WS1SExists("Y2", new WS1SExists("Z", new WS1SAnd(f, s))))); WS1SFormula phit = new WS1SExists("X", new WS1SExists("Y", new WS1SSubset("X", "Y"))); var dd = phit.getDFA(al, solver); //solver.SaveAsDot(phit.getDFA(al, solver), "bla","bla.dot"); var dfa = phi.getDFA(al, solver); var test = solver.Convert(@"^(a|b)*aba(a|b)*$"); Assert.IsTrue(dfa.IsEquivalentWith(test, solver)); string file = "../../../MSOZ3Test/DotFiles/aba"; solver.SaveAsDot(dfa, "aut", file); //extension .dot is added automatically when missing }
//[TestMethod] public void TestIgnoreCaseTransformer() { CharSetSolver solver = new CharSetSolver(); int t = System.Environment.TickCount; IgnoreCaseTransformer ic = new IgnoreCaseTransformer(solver); //simple test first: //BDD a2c = solver.MkRangeConstraint('a', 'c'); //BDD a2cA2C = ic.Apply(a2c); //BDD a2cA2C_expected = a2c.Or(solver.MkRangeConstraint('A', 'C')); //Assert.AreEqual<BDD>(a2cA2C, a2cA2C_expected); // //comprehensive test: // //test that the whole array is correct: // Microsoft.Automata.Generated.IgnoreCaseRelation.ignorecase // (generated by:) // // IgnoreCaseRelationGenerator.Generate( // "Microsoft.Automata.Generated", // "IgnoreCaseRelation", // @"C:\GitHub\AutomataDotNet\Automata\src\Automata\Internal\Generated"); // //test that all characters in it are truly equivalent wrt the igore-case option of regex // for (int i = 0; i <= 0xFFFF; i++) { char c = (char)i; if (ic.IsInDomain(c)) { BDD cC = ic.Apply(solver.MkCharConstraint(c)); foreach (char d in solver.GenerateAllCharacters(cC)) { Assert.IsTrue(Regex.IsMatch(d.ToString(), "^(?i:" + StringUtility.Escape(c) + ")$")); } } } // //second, test that all characters outside the domain are only equivalent (up-to-case) to themsevles // // for some reson this does not succeed, ??? some characters, e.g. '\xF7', are // equivalent to some other characters in the below test, but not when tested individually // there is a bug in Regex.IsMatch with ignore-case combined with intervals // //for (int i = 2; i <= 0xFFFD; i++) //{ // char c = (char)i; // if (!ic.IsInDomain(c)) // { // if (Regex.IsMatch(c.ToString(), @"^([\0-" + StringUtility.Escape((char)(i - 1)) + StringUtility.Escape((char)(i + 1)) + @"-\uFFFF])$", RegexOptions.IgnoreCase)) // Console.WriteLine(StringUtility.Escape(c)); // } //} }
public void MkLengthAutomatonTest() { var solver = new CharSetSolver(BitWidth.BV7); string regexA = "^(((((00|11)|10((00|11))*01)|(01|10((00|11))*10)(((00|11)|01((00|11))*10))*(10|01((00|11))*01)))*)$"; BDD _1 = solver.MkCharConstraint('1'); var A = solver.Convert(regexA).RelpaceAllGuards(x => (x == null ? null : _1)); var B = A.Determinize().MinimizeHopcroft(); var lengthregex = solver.ConvertToRegex(B); Assert.AreEqual <string>("^((11)*)$", lengthregex); }
public void TestIgnoreCaseTransformer() { CharSetSolver solver = new CharSetSolver(); int t = System.Environment.TickCount; IgnoreCaseTransformer ic = new IgnoreCaseTransformer(solver); //simple test first: BDD a2c = solver.MkRangeConstraint('a', 'c'); BDD a2cA2C = ic.Apply(a2c); BDD a2cA2C_expected = a2c.Or(solver.MkRangeConstraint('A', 'C')); Assert.AreEqual<BDD>(a2cA2C, a2cA2C_expected); // //comprehensive test: // //test that the whole array is correct: // Microsoft.Automata.Internal.Generated.IgnoreCaseRelation.ignorecase // (generated by:) // // IgnoreCaseRelationGenerator.Generate( // "Microsoft.Automata.Internal.Generated", // "IgnoreCaseRelation", // @"C:\GitHub\AutomataDotNet\Automata\src\Automata\Internal\Generated"); // //test that all characters in it are truly equivalent wrt the igore-case option of regex // for (int i = 0; i <= 0xFFFF; i++) { char c = (char)i; if (ic.IsInDomain(c)) { BDD cC = ic.Apply(solver.MkCharConstraint(c)); foreach (char d in solver.GenerateAllCharacters(cC)) { Assert.IsTrue(Regex.IsMatch(d.ToString(), "^(?i:" + StringUtility.Escape(c) + ")$")); } } } // //second, test that all characters outside the domain are only equivalent (up-to-case) to themsevles // // for some reson this does not succeed, ??? some characters, e.g. '\xF7', are // equivalent to some other characters in the below test, but not when tested individually // there is a bug in Regex.IsMatch with ignore-case combined with intervals // //for (int i = 2; i <= 0xFFFD; i++) //{ // char c = (char)i; // if (!ic.IsInDomain(c)) // { // if (Regex.IsMatch(c.ToString(), @"^([\0-" + StringUtility.Escape((char)(i - 1)) + StringUtility.Escape((char)(i + 1)) + @"-\uFFFF])$", RegexOptions.IgnoreCase)) // Console.WriteLine(StringUtility.Escape(c)); // } //} }
public void TestMoveCharToggling1() { var solver = new CharSetSolver(BitWidth.BV64); List<char> alph = new List<char> { 'a', 'b' }; HashSet<char> al = new HashSet<char>(alph); var a = solver.MkCharConstraint(false, 'a'); var b = solver.MkCharConstraint(false, 'b'); var moves = new List<Move<BDD>>(); moves.Add(new Move<BDD>(0, 0, a)); moves.Add(new Move<BDD>(0, 1, b)); moves.Add(new Move<BDD>(1, 1, a)); moves.Add(new Move<BDD>(1, 2, b)); moves.Add(new Move<BDD>(2, 2, a)); moves.Add(new Move<BDD>(2, 3, b)); moves.Add(new Move<BDD>(3, 3, a)); moves.Add(new Move<BDD>(3, 3, b)); var nfa1 = Automaton<BDD>.Create(0, new int[] { 0, 3 }, moves); var moves3 = new List<Move<BDD>>(); moves3.Add(new Move<BDD>(0, 1, b)); moves3.Add(new Move<BDD>(1, 1, a)); moves3.Add(new Move<BDD>(1, 2, b)); moves3.Add(new Move<BDD>(2, 2, a)); moves3.Add(new Move<BDD>(3, 3, a)); moves3.Add(new Move<BDD>(3, 3, b)); var nfa3 = Automaton<BDD>.Create(0, new int[] { 0, 3 }, moves3); var sb = new StringBuilder(); NFAEditDistanceProvider nfaedp = new NFAEditDistanceProvider(nfa1, al, solver, timeout); var distanceNfa1Nfa3 = nfaedp.GetNFAOptimalEdit(nfa3); Assert.IsTrue(distanceNfa1Nfa3.GetCost() == 2); }
public static Pair<HashSet<char>, Automaton<BDD>> parseDFAFromXML(XElement Automaton1, CharSetSolver solver) { HashSet<char> al = new HashSet<char>(); //XElement Automaton = XElement.Parse(xmlString); //All DFAs in problem set on automata tutor are over a,b var moves = new List<Move<BDD>>(); var finalStates = new List<int>(); int start = 0; XElement Automaton = XElement.Parse(RemoveAllNamespaces(Automaton1.ToString())); XElement trans = Automaton.Element("transitionSet"); foreach (XElement child in trans.Elements()) { if (child.Name == "transition") { moves.Add(new Move<BDD>(Convert.ToInt32(child.Element("from").Value), Convert.ToInt32(child.Element("to").Value), solver.MkCharConstraint(false, Convert.ToChar(child.Element("read").Value)))); al.Add(Convert.ToChar(child.Element("read").Value)); } } XElement acc = Automaton.Element("acceptingSet"); foreach (XElement child in acc.Elements()) { if (child.Name == "state") { finalStates.Add((int)child.Attribute("sid")); } } XElement states = Automaton.Element("initState"); foreach (XElement child in states.Elements()) { if (child.Name == "state") { start = (int)child.Attribute("sid"); } } return new Pair<HashSet<char>, Automaton<BDD>>(al, Automaton<BDD>.Create(start, finalStates, moves)); }
public void CheckEquivalence() { CharSetSolver solver = new CharSetSolver(); var moves = new List<Move<BDD>>(); moves.Add(new Move<BDD>(0, 1, solver.True)); moves.Add(new Move<BDD>(1, 2, solver.True)); var sfa1 = ThreeAutomaton<BDD>.Create(solver, 0, new int[] { 0 }, new int[] { 2 }, moves); var c = solver.MkCharConstraint('c'); moves = new List<Move<BDD>>(); moves.Add(new Move<BDD>(0, 1, c)); moves.Add(new Move<BDD>(1, 2, solver.True)); moves.Add(new Move<BDD>(0, 3, solver.MkNot(c))); moves.Add(new Move<BDD>(3, 2, solver.True)); var sfa2 = ThreeAutomaton<BDD>.Create(solver, 0, new int[] { 0 }, new int[] { 2 }, moves); Assert.IsTrue(sfa1.IsEquivalentWith(sfa2,solver)); }
public void WS1SLabelTest() { var solver = new CharSetSolver(BitWidth.BV64); //new solver using ASCII encoding List<char> alph = new List<char> { 'a', 'b' }; HashSet<char> al = new HashSet<char>(alph); WS1SFormula f = new WS1SUnaryPred("X", solver.MkCharConstraint(false, 'a')); WS1SFormula phi = new WS1SExists("X", f); var dfa = phi.getDFA(al, solver); var test = solver.Convert(@"^(a|b)*$").Determinize(solver).Minimize(solver); string file = "../../../MSOZ3Test/DotFiles/sigmastar"; solver.SaveAsDot(dfa, "aut", file); //extension .dot is added automatically when missing Assert.IsTrue(dfa.IsEquivalentWith(test, solver)); }
public void CheckDeMorgan() { CharSetSolver solver = new CharSetSolver(); var moves = new List<Move<BDD>>(); var c = solver.MkCharConstraint('a'); moves.Add(new Move<BDD>(0, 1, c)); moves.Add(new Move<BDD>(1, 2, solver.True)); var sfa1 = ThreeAutomaton<BDD>.Create(solver, 0, new int[] { 0 }, new int[] { 2 }, moves); moves = new List<Move<BDD>>(); moves.Add(new Move<BDD>(0, 1, c)); moves.Add(new Move<BDD>(1, 2, solver.True)); moves.Add(new Move<BDD>(0, 3, solver.MkNot(c))); moves.Add(new Move<BDD>(3, 2, solver.True)); var sfa2 = ThreeAutomaton<BDD>.Create(solver, 0, new int[] { 0 }, new int[] { 2 }, moves); var inters = sfa1.Intersect(sfa2,solver); var union = sfa1.Union(sfa2, solver); var u2 = sfa1.MkComplement().Intersect(sfa1.MkComplement(), solver).MkComplement(); }
public void TestNFA2() { CharSetSolver solver = new CharSetSolver(BitWidth.BV7); var a = solver.MkCharConstraint('a'); var na = solver.MkNot(a); var nfa = Automaton<BDD>.Create(solver, 0, new int[] { 1 }, new Move<BDD>[] { new Move<BDD>(0, 1, solver.True), new Move<BDD>(0, 2, solver.True), new Move<BDD>(2, 1, solver.True), new Move<BDD>(1, 1, a), new Move<BDD>(1, 2, na) }); var min_nfa = nfa.Minimize(); nfa.isDeterministic = true; //pretend that nfa is equivalent, causes the deterministic version to be executed that provides the wrong result var min_nfa_wrong = nfa.Minimize(); nfa.isDeterministic = false; min_nfa_wrong.isDeterministic = false; //min_nfa.ShowGraph("min_nfa"); //min_nfa_wrong.ShowGraph("min_nfa_wrong"); //min_nfa.Determinize().Minimize().ShowGraph("min_nfa1"); //nfa.Determinize().Minimize().ShowGraph("dfa"); //nfa.ShowGraph("nfa"); //min_nfa_wrong.Determinize().Minimize().ShowGraph("min_nfa2"); Assert.IsFalse(min_nfa.IsEquivalentWith(min_nfa_wrong)); Assert.IsTrue(min_nfa.IsEquivalentWith(nfa)); //concrete witness "abab" distinguishes nfa from min_nfa_wrong Assert.IsTrue(solver.Convert("^abab$").Intersect(nfa).IsEmpty); Assert.IsFalse(solver.Convert("^abab$").Intersect(min_nfa_wrong).IsEmpty); }
public void FlabioNFA2() { var solver = new CharSetSolver(BitWidth.BV64); List<char> alph = new List<char> { '1', '0' }; HashSet<char> al = new HashSet<char>(alph); var o = solver.MkCharConstraint(false, '1'); var z = solver.MkCharConstraint(false, '0'); var movesSolution = new List<Move<BDD>>(); movesSolution.Add(new Move<BDD>(0, 1, null)); movesSolution.Add(new Move<BDD>(0, 2, null)); movesSolution.Add(new Move<BDD>(1, 1, z)); movesSolution.Add(new Move<BDD>(1, 1, o)); movesSolution.Add(new Move<BDD>(1, 3, z)); movesSolution.Add(new Move<BDD>(2, 2, o)); var dfaSolution = Automaton<BDD>.Create(0, new int[] { 2, 3 }, movesSolution); var movesAttempt = new List<Move<BDD>>(); movesAttempt.Add(new Move<BDD>(0, 1, null)); movesAttempt.Add(new Move<BDD>(0, 3, null)); movesAttempt.Add(new Move<BDD>(1, 2, z)); movesAttempt.Add(new Move<BDD>(2, 1, o)); movesAttempt.Add(new Move<BDD>(2, 2, z)); movesAttempt.Add(new Move<BDD>(1, 1, o)); movesAttempt.Add(new Move<BDD>(3, 4, o)); movesAttempt.Add(new Move<BDD>(4, 4, o)); var dfaAttempt = Automaton<BDD>.Create(0, new int[] { 4,2 }, movesAttempt); var gr = NFAGrading.GetGrade(dfaSolution, dfaAttempt, al, solver, 1500, 10, FeedbackLevel.Hint); Console.WriteLine(gr.First); foreach (var f in gr.Second) Console.WriteLine(f.ToString()); gr = NFAGrading.GetGrade(dfaAttempt, dfaSolution, al, solver, 1500, 10, FeedbackLevel.Hint); Console.WriteLine(gr.First); foreach (var f in gr.Second) Console.WriteLine(f.ToString()); }
public void GradingNFAEps() { var solver = new CharSetSolver(BitWidth.BV64); List<char> alph = new List<char> { 'a', 'b' }; HashSet<char> al = new HashSet<char>(alph); var a = solver.MkCharConstraint(false, 'a'); var b = solver.MkCharConstraint(false, 'b'); var movesSol = new List<Move<BDD>>(); movesSol.Add(new Move<BDD>(5, 0, null)); movesSol.Add(new Move<BDD>(0, 0, a)); movesSol.Add(new Move<BDD>(0, 0, b)); movesSol.Add(new Move<BDD>(0, 1, a)); movesSol.Add(new Move<BDD>(1, 2, b)); var nfa1 = Automaton<BDD>.Create(5, new int[] { 2 }, movesSol); var movesAtt = new List<Move<BDD>>(); movesAtt.Add(new Move<BDD>(0, 0, a)); movesAtt.Add(new Move<BDD>(0, 1, b)); movesAtt.Add(new Move<BDD>(1, 1, b)); movesAtt.Add(new Move<BDD>(1, 2, a)); movesAtt.Add(new Move<BDD>(2, 2, a)); movesAtt.Add(new Move<BDD>(2, 3, b)); movesAtt.Add(new Move<BDD>(3, 3, b)); movesAtt.Add(new Move<BDD>(3, 4, a)); movesAtt.Add(new Move<BDD>(4, 4, a)); movesAtt.Add(new Move<BDD>(4, 4, b)); var nfa2 = Automaton<BDD>.Create(0, new int[] { 4 }, movesAtt); List<Pair<int, IEnumerable<NFAFeedback>>> tests = new List<Pair<int, IEnumerable<NFAFeedback>>>(); tests.Insert(0,NFAGrading.GetGrade(nfa1, nfa2, al, solver, timeout, 10, FeedbackLevel.Hint)); tests.Insert(0, NFAGrading.GetGrade(nfa2, nfa1, al, solver, timeout, 10, FeedbackLevel.Hint)); tests.Insert(0, NFAGrading.GetGrade(nfa1, nfa1, al, solver, timeout, 10, FeedbackLevel.Hint)); tests.Insert(0, NFAGrading.GetGrade(nfa2, nfa2, al, solver, timeout, 10, FeedbackLevel.Hint)); foreach (var test in tests) { Console.WriteLine("grade: {0}, ", test.First); foreach (var f in test.Second) Console.WriteLine(f.ToString()); } }
public void AgutssonNFAs() { var solver = new CharSetSolver(BitWidth.BV64); List<char> alph = new List<char> { 'a', 'b' }; HashSet<char> al = new HashSet<char>(alph); var a = solver.MkCharConstraint(false, 'a'); var b = solver.MkCharConstraint(false, 'b'); var movesSolution = new List<Move<BDD>>(); movesSolution.Add(new Move<BDD>(0, 1, a)); movesSolution.Add(new Move<BDD>(1, 2, a)); movesSolution.Add(new Move<BDD>(0, 3, a)); movesSolution.Add(new Move<BDD>(0, 3, b)); movesSolution.Add(new Move<BDD>(3, 0, a)); movesSolution.Add(new Move<BDD>(3, 0, b)); var dfaSolution = Automaton<BDD>.Create(0, new int[] { 2 }, movesSolution); var movesAttempt = new List<Move<BDD>>(); movesAttempt.Add(new Move<BDD>(0, 0, a)); movesAttempt.Add(new Move<BDD>(0, 0, b)); movesAttempt.Add(new Move<BDD>(0, 1, a)); movesAttempt.Add(new Move<BDD>(1, 2, a)); movesAttempt.Add(new Move<BDD>(2, 3, a)); var dfaAttempt = Automaton<BDD>.Create(0, new int[] { 3 }, movesAttempt); var gr = NFAGrading.GetGrade(dfaSolution, dfaAttempt, al, solver, 1500, 10, FeedbackLevel.Hint); Console.WriteLine(gr.First); foreach (var f in gr.Second) Console.WriteLine(f.ToString()); gr = NFAGrading.GetGrade(dfaAttempt, dfaSolution, al, solver, 1500, 10, FeedbackLevel.Hint); Console.WriteLine(gr.First); foreach (var f in gr.Second) Console.WriteLine(f.ToString()); }
public void MarioBianucciNFAs() { var solver = new CharSetSolver(BitWidth.BV64); List<char> alph = new List<char> { 'a', 'b' }; HashSet<char> al = new HashSet<char>(alph); var a = solver.MkCharConstraint(false, '0'); var b = solver.MkCharConstraint(false, '1'); //PDLPred phi2 = new PDLIf(new PDLStartsWith("b"), new PDLEndsWith("b")); var movesSolution = new List<Move<BDD>>(); movesSolution.Add(new Move<BDD>(0, 0, a)); movesSolution.Add(new Move<BDD>(0, 1, b)); movesSolution.Add(new Move<BDD>(1, 2, a)); movesSolution.Add(new Move<BDD>(1, 1, b)); movesSolution.Add(new Move<BDD>(2, 2, a)); movesSolution.Add(new Move<BDD>(2, 3, b)); movesSolution.Add(new Move<BDD>(3, 0, a)); movesSolution.Add(new Move<BDD>(3, 3, b)); var dfaSolution = Automaton<BDD>.Create(0, new int[] {0,1 }, movesSolution); var movesAttempt = new List<Move<BDD>>(); movesAttempt.Add(new Move<BDD>(0, 0, a)); movesAttempt.Add(new Move<BDD>(0, 1, b)); movesAttempt.Add(new Move<BDD>(0, 1, null)); movesAttempt.Add(new Move<BDD>(1, 2, a)); movesAttempt.Add(new Move<BDD>(1, 1, b)); movesAttempt.Add(new Move<BDD>(2, 2, a)); movesAttempt.Add(new Move<BDD>(2, 3, b)); movesAttempt.Add(new Move<BDD>(3, 0, a)); movesAttempt.Add(new Move<BDD>(3, 3, b)); var dfaAttempt = Automaton<BDD>.Create(0, new int[] { 0,1 }, movesAttempt); var gr = NFAGrading.GetGrade(dfaSolution, dfaAttempt, al, solver, 4000, 10, FeedbackLevel.Hint); Console.WriteLine(gr.First); foreach (var f in gr.Second) Console.WriteLine(f.ToString()); gr = NFAGrading.GetGrade(dfaAttempt, dfaSolution, al, solver, 4000, 10, FeedbackLevel.Hint); Console.WriteLine(gr.First); foreach (var f in gr.Second) Console.WriteLine(f.ToString()); }
public void VladKlimkivDFAs() { var solver = new CharSetSolver(BitWidth.BV64); List<char> alph = new List<char> { 'a', 'b' }; HashSet<char> al = new HashSet<char>(alph); var a = solver.MkCharConstraint(false, 'a'); var b = solver.MkCharConstraint(false, 'b'); //PDLPred phi2 = new PDLIf(new PDLStartsWith("b"), new PDLEndsWith("b")); var movesSolution = new List<Move<BDD>>(); movesSolution.Add(new Move<BDD>(0, 1, a)); movesSolution.Add(new Move<BDD>(0, 2, b)); movesSolution.Add(new Move<BDD>(1, 0, a)); movesSolution.Add(new Move<BDD>(1, 3, b)); movesSolution.Add(new Move<BDD>(2, 3, a)); movesSolution.Add(new Move<BDD>(2, 5, b)); movesSolution.Add(new Move<BDD>(3, 2, a)); movesSolution.Add(new Move<BDD>(3, 4, b)); movesSolution.Add(new Move<BDD>(4, 5, a)); movesSolution.Add(new Move<BDD>(4, 1, b)); movesSolution.Add(new Move<BDD>(5, 4, a)); movesSolution.Add(new Move<BDD>(5, 1, b)); var dfaSolution = Automaton<BDD>.Create(0, new int[] { 2 }, movesSolution); var movesAttempt = new List<Move<BDD>>(); movesAttempt.Add(new Move<BDD>(0, 1, b)); movesAttempt.Add(new Move<BDD>(0, 2, a)); movesAttempt.Add(new Move<BDD>(1, 0, b)); movesAttempt.Add(new Move<BDD>(1, 3, a)); movesAttempt.Add(new Move<BDD>(2, 3, b)); movesAttempt.Add(new Move<BDD>(2, 5, a)); movesAttempt.Add(new Move<BDD>(3, 2, b)); movesAttempt.Add(new Move<BDD>(3, 4, a)); movesAttempt.Add(new Move<BDD>(4, 5, b)); movesAttempt.Add(new Move<BDD>(4, 1, a)); movesAttempt.Add(new Move<BDD>(5, 4, b)); movesAttempt.Add(new Move<BDD>(5, 1, a)); var dfaAttempt = Automaton<BDD>.Create(0, new int[] { 2 }, movesAttempt); var gr = DFAGrading.GetGrade(dfaSolution, dfaAttempt, al, solver, 1500, 10, FeedbackLevel.Hint, true, true, true); Console.WriteLine(gr.First); foreach (var f in gr.Second) Console.WriteLine(f.ToString()); gr = DFAGrading.GetGrade(dfaAttempt, dfaSolution, al, solver, 1500, 10, FeedbackLevel.Hint, true, true, true); Console.WriteLine(gr.First); foreach (var f in gr.Second) Console.WriteLine(f.ToString()); }
//check if delta(S,T,c) exists static bool MoveFromStoTContainsC(char c, int S, int T, Automaton<BDD> aut, CharSetSolver solver, out char witness) { var ccond = solver.MkCharConstraint(c); foreach (var move in aut.GetMovesFrom(S)) if (move.TargetState == T) { if (solver.IsSatisfiable(solver.MkAnd(move.Label, ccond))) { witness = c; return true; } else foreach (var w in solver.GenerateAllCharacters(move.Label, false)) { witness = w; return true; } } witness = c; return false; }
// returns the state reached from currState when reading c private static int GetNextState(int currState, char c, Automaton<BDD> dfa, CharSetSolver solver) { foreach (var move in dfa.GetNonepsilonMovesFrom(currState)) if (solver.IsSatisfiable(solver.MkAnd(move.Label, solver.MkCharConstraint(false, c)))) return move.TargetState; return -1; }
internal static void ComputeModels( string currStr, int currState, Automaton<BDD> dfa, List<int> finalStates, HashSet<char> alphabet, CharSetSolver solver, List<string> positive, List<string> negative) { if (currStr.Length >= 8) return; if (currState == -1 || !finalStates.Contains(currState)) negative.Add(currStr); else positive.Add(currStr); foreach (char ch in alphabet) { if (currState == -1) ComputeModels(currStr + ch, currState, dfa, finalStates, alphabet, solver, positive, negative); else { bool found = false; foreach (var move in dfa.GetMovesFrom(currState)) { if (solver.IsSatisfiable(solver.MkAnd(move.Label, solver.MkCharConstraint(false, ch)))) { found = true; ComputeModels(currStr + ch, move.TargetState, dfa, finalStates, alphabet, solver, positive, negative); break; } } if (!found) ComputeModels(currStr + ch, -1, dfa, finalStates, alphabet, solver, positive, negative); } } }
public static void RunPOPLTests() { //// all x1...xn. xi<xi+1 List<Pair<MSOFormula<BDD>, CharSetSolver>> phis = new List<Pair<MSOFormula<BDD>, CharSetSolver>>(); for (int to = 2; to < kpopl; to++) { var solver = new CharSetSolver(); MSOFormula<BDD> phi = new MSOTrue<BDD>(); for (int k = 1; k < to; k++) { var leq = new MSOLt<BDD>(new Variable("x" + (k - 1), true), new Variable("x" + k, true)); phi = new MSOAnd<BDD>(phi, leq); } for (int k = to - 1; k >= 0; k--) { phi = new MSOExists<BDD>(new Variable("x" + k, true), phi); } phis.Add(new Pair<MSOFormula<BDD>, CharSetSolver>(phi, solver)); } RunTest(new StreamWriter(@"popl14-1.csv"), phis); // all x1...xn. xi<xi+1 and a(xi) phis = new List<Pair<MSOFormula<BDD>, CharSetSolver>>(); for (int to = 2; to < kpopl; to++) { var solver = new CharSetSolver(BitWidth.BV64); MSOFormula<BDD> phi = new MSOTrue<BDD>(); for (int k = 1; k < to; k++) { var leq = new MSOLt<BDD>(new Variable("x" + (k - 1), true), new Variable("x" + k, true)); phi = new MSOAnd<BDD>(phi, leq); } for (int k = 0; k < to; k++) { var axk = new MSOPredicate<BDD>( solver.MkCharConstraint('a', false), new Variable("x" + k, true)); phi = new MSOAnd<BDD>(phi, axk); } for (int k = to - 1; k >= 0; k--) { phi = new MSOExists<BDD>(new Variable("x" + k, true), phi); } phis.Add(new Pair<MSOFormula<BDD>, CharSetSolver>(phi, solver)); } RunTest(new StreamWriter(@"popl14-2.csv"), phis); // all x1...xn. (xi<xi+1 and a(xi)) and ex y. c(y) phis = new List<Pair<MSOFormula<BDD>, CharSetSolver>>(); for (int to = 2; to < kpopl; to++) { var solver = new CharSetSolver(BitWidth.BV64); MSOFormula<BDD> phi = new MSOTrue<BDD>(); for (int k = 1; k < to; k++) { var leq = new MSOLt<BDD>(new Variable("x" + (k - 1), true), new Variable("x" + k, true)); phi = new MSOAnd<BDD>(phi, leq); } for (int k = 0; k < to; k++) { var axk = new MSOPredicate<BDD>(solver.MkCharConstraint('a', false), new Variable("x" + k, true)); phi = new MSOAnd<BDD>(phi, axk); } for (int k = to - 1; k >= 0; k--) { phi = new MSOExists<BDD>(new Variable("x" + k, true), phi); } var exycy = new MSOExists<BDD>(new Variable("y", true), new MSOPredicate<BDD>(solver.MkCharConstraint('c', false), new Variable("y", true))); phi = new MSOAnd<BDD>(phi, exycy); phis.Add(new Pair<MSOFormula<BDD>, CharSetSolver>(phi, solver)); } RunTest(new StreamWriter(@"popl14-3.csv"), phis); // all x1...xn. (xi<xi+1 and a(xi) \/ c(xi)) phis = new List<Pair<MSOFormula<BDD>, CharSetSolver>>(); for (int to = 2; to < kpopl; to++) { var solver = new CharSetSolver(BitWidth.BV64); MSOFormula<BDD> phi = new MSOTrue<BDD>(); for (int k = 1; k < to; k++) { var leq = new MSOLt<BDD>(new Variable("x" + (k - 1), true), new Variable("x" + k, true)); var axk = new MSOPredicate<BDD>(solver.MkCharConstraint('a', false), new Variable("x" + (k - 1), true)); var cxk = new MSOPredicate<BDD>(solver.MkCharConstraint('c', false), new Variable("x" + (k - 1), true)); var inter = new MSOOr<BDD>(new MSOAnd<BDD>(leq, axk), cxk); phi = new MSOAnd<BDD>(phi, inter); } for (int k = to - 1; k >= 0; k--) { phi = new MSOExists<BDD>(new Variable("x" + k, true), phi); } MSOFormula<BDD> exycy = new MSOExists<BDD>(new Variable("y", true), new MSOPredicate<BDD>(solver.MkCharConstraint('c', false), new Variable("y", true))); phi = new MSOAnd<BDD>(phi, exycy); phis.Add(new Pair<MSOFormula<BDD>, CharSetSolver>(phi, solver)); } RunTest(new StreamWriter(@"popl14-4.csv"), phis, 11, 11, 11); }
public DensityFeedback(FeedbackLevel level, HashSet<char> alphabet, Automaton<BDD> dfaGoal, Automaton<BDD> dfaAttempt, double utility, CharSetSolver solver) : base(level, alphabet, utility,solver) { BDD pred = solver.False; foreach (var el in alphabet) pred=solver.MkOr(pred,solver.MkCharConstraint(false,el)); var dfaAll = Automaton<BDD>.Create(0,new int[]{0},new Move<BDD>[]{new Move<BDD>(0,0,pred)}); this.type = FeedbackType.Density; this.positiveDifference = dfaGoal.Minus(dfaAttempt, solver).Determinize(solver).Minimize(solver); this.negativeDifference = dfaAttempt.Minus(dfaGoal, solver).Determinize(solver).Minimize(solver); this.symmetricDifference = dfaAll.Minus(dfaAll.Minus(positiveDifference,solver).Intersect(dfaAll.Minus(negativeDifference,solver),solver),solver).Determinize(solver).Minimize(solver); this.solver = solver; }
public static Automaton<BDD> parseForTest(string Automaton1, CharSetSolver solver) { var moves = new List<Move<BDD>>(); var finalStates = new List<int>(); int start = 0; XElement Automaton = XElement.Parse(Automaton1); XElement trans = Automaton.Element("transitionSet"); foreach (XElement child in trans.Elements()) { if (child.Name == "transition") { moves.Add(new Move<BDD>(Convert.ToInt32(child.Element("from").Value), Convert.ToInt32(child.Element("to").Value), solver.MkCharConstraint(false, Convert.ToChar(child.Element("read").Value)))); } } XElement acc = Automaton.Element("acceptingSet"); foreach (XElement child in acc.Elements()) { if (child.Name == "state") { finalStates.Add((int)child.Attribute("sid")); } } XElement states = Automaton.Element("initState"); foreach (XElement child in states.Elements()) { if (child.Name == "state") { start = (int)child.Attribute("sid"); } } return Automaton<BDD>.Create(start, finalStates, moves); }
//check if delta(S,T,c) exists static bool MoveFromStoTContainsC(char c, int S, int T, Automaton<BDD> aut, CharSetSolver solver) { var ccond = solver.MkCharConstraint(c); foreach(var move in aut.GetMovesFrom(S)) if (move.TargetState == T) if (solver.IsSatisfiable(solver.MkAnd(move.Label, ccond))) return true; return false; }
public static Pair<HashSet<char>, Automaton<BDD>> parseDFAFromJFLAP(string fileName, CharSetSolver solver) { HashSet<char> al = new HashSet<char>(); XElement Structure = XElement.Load(fileName); XElement MType = Structure.Element("type"); Debug.Assert(MType.Value == "fa"); XElement Automaton = Structure.Element("automaton"); var moves = new List<Move<BDD>>(); var finalStates = new List<int>(); int start = -1; foreach (XElement child in Automaton.Elements()) { if (child.Name == "state") // make start and/or add to final { foreach (XElement d in child.Elements()) { if (d.Name == "initial") start = (int)child.Attribute("id"); if (d.Name == "final") finalStates.Add((int)child.Attribute("id")); } } if (child.Name == "transition") { al.Add(Convert.ToChar(child.Element("read").Value)); moves.Add(new Move<BDD>(Convert.ToInt32(child.Element("from").Value), Convert.ToInt32(child.Element("to").Value), solver.MkCharConstraint(false, Convert.ToChar(child.Element("read").Value)))); } } Debug.Assert(start != -1); return new Pair<HashSet<char>, Automaton<BDD>>(al, Automaton<BDD>.Create(start, finalStates, moves)); }
/// <summary> /// Based on paper /// Order-n correction for regular langauges, http://dl.acm.org/citation.cfm?id=360995 /// </summary> /// <param name="str">input string</param> /// <param name="automaton">dfa for which you want to compute the distance</param> /// <param name="solver">character solver</param> /// <param name="bound">depth of search for max string insertion</param> /// <param name="distance">outputs the distance</param> /// <returns>the closest string to str in automaton</returns> public static string GetClosestElement(string str, Automaton<BDD> automaton, CharSetSolver solver, int bound, out int distance, bool checkDeterminism = true) { //bound = Math.Min(bound, str.Length); var input = str.ToCharArray(); var chars = new HashSet<char>(input); var maxl = input.Length+1; if(automaton.IsEmpty) throw new AutomataException("automaton must be nonempty"); if (checkDeterminism && !automaton.IsDeterministic) throw new AutomataException("automaton must be deterministic"); //Compute P(T,S) L(T,S,c) var lstates= automaton.States.ToList(); lstates.Sort(); var states = lstates.ToArray(); var stToInd = new Dictionary<int, int>(states.Length+1); for (int i = 0; i < states.Length; i++) stToInd[states[i]] = i; var Pold = new int[states.Length, states.Length]; var P1 = new bool[states.Length, states.Length]; //Records the transition relation var Pnew = new int[states.Length, states.Length]; var Lold=new Dictionary<char,bool[,]>(); var Lnew = new Dictionary<char, bool[ ,]>(); #region Initialize P L foreach (var c in chars) { Lold[c] = new bool[states.Length, states.Length]; Lnew[c] = new bool[states.Length, states.Length]; } foreach (var stT in automaton.States) { var T = stToInd[stT]; foreach (var stS in automaton.States) { var S = stToInd[stS]; if (T == S) { Pold[S, T] = 0; char wit; P1[S, T] = MoveFromStoT(stS, stT, automaton, solver, out wit); foreach (var c in chars) if (P1[S, T] && MoveFromStoTContainsC(c, stS, stT, automaton, solver)) Lold[c][S, T] = true; else Lold[c][S, T] = false; } else { char wit; if (MoveFromStoT(stS, stT, automaton, solver, out wit)) { Pold[S, T] = 1; P1[S, T] = true; foreach (var c in chars) if (MoveFromStoTContainsC(c, stS, stT, automaton, solver)) Lold[c][S, T] = true; else Lold[c][S, T] = false; } else { Pold[S, T] = int.MaxValue; P1[S, T] = false; foreach (var c in chars) Lold[c][S, T] = false; } } } } #endregion //solver.ShowGraph(automaton,"as"); //Inductive step for(int k=1;k<=bound;k++){ foreach (var stT in automaton.States) { var T = stToInd[stT]; foreach (var stS in automaton.States) { var S = stToInd[stS]; if (Pold[S, T] == int.MaxValue) { bool found=false; foreach (var move in automaton.GetMovesFrom(stS)) { var stk = move.TargetState; var K = stToInd[stk]; if (Pold[K, T] != int.MaxValue) if (P1[S, K]) { found = true; Pnew[S, T] = Pold[K, T] + 1; foreach (var c in chars) Lnew[c][S, T] = Lold[c][K, T] || solver.IsSatisfiable(solver.MkAnd(move.Label,solver.MkCharConstraint(c))); } } if (!found) { Pnew[S, T] = Pold[S, T]; foreach (var c in chars) Lnew[c][S, T] = Lold[c][S, T]; } } else { Pnew[S, T] = Pold[S, T]; foreach (var c in chars) Lnew[c][S, T] = Lold[c][S, T]; } } } Pold = Pnew; Pnew=new int[states.Length, states.Length]; foreach (var c in chars) Lold[c] = Lnew[c]; Lnew = new Dictionary<char, bool[,]>(); foreach (var c in chars) Lnew[c] = new bool[states.Length, states.Length]; } //Initialize table for value 0 Pair<int, int>[,] F = new Pair<int, int>[maxl, automaton.StateCount]; foreach (var st in automaton.States) { var T = stToInd[st]; if (st == automaton.InitialState) F[0, T] = new Pair<int, int>(0, -1); else F[0, T] = new Pair<int, int>(int.MaxValue, -1); } //solver.ShowGraph(automaton,"aa"); //Dynamic programming loop List<int> stateList = new List<int>(); for (int j = 1; j < maxl; j++) { var aj = input[j - 1]; foreach (var stT in automaton.States) { var T = stToInd[stT]; int min = int.MaxValue; int minSt = -1; foreach (var stS in automaton.States) { var S = stToInd[stS]; var pts = Pold[S, T]; if (pts != int.MaxValue) { var ltsc = Lold[aj][S, T] ? 1 : 0; int vts = pts == 0 ? 1 - ltsc : pts - ltsc; var fjm1t = F[j - 1, S]; int expr = fjm1t.First + vts; if (fjm1t.First == int.MaxValue || vts == int.MaxValue) expr = int.MaxValue; else if (expr <= min) { min = expr; minSt = S; if (min == 0) break; } } } F[j, T] = new Pair<int, int>(min, minSt); } } //Iteration over final states int minAcc = int.MaxValue; int minState = -1; foreach (var st in automaton.GetFinalStates()) { var S = stToInd[st]; if (F[input.Length, S].First < minAcc) { minAcc = F[input.Length, S].First; minState = F[input.Length, S].Second; minState = S; } } var minString =""; int curr = minState; int strindex = input.Length; while (strindex > 0) { var f = F[strindex, curr]; var aj = input[strindex-1]; var pts = Pold[f.Second,curr]; var ltsc = Lold[aj][f.Second,curr] ? 1 : 0; string vts = pts == 0 ? ((ltsc == 1)? aj.ToString():"") : ((ltsc == 1) ? ShortStringStoTwithC(aj, states[f.Second], states[curr], automaton, bound, solver) : ShortStringStoT(states[f.Second], states[curr], automaton, bound, solver)); minString = vts + minString; curr = f.Second; strindex--; } distance=minAcc; return minString; }
public void Grade2DFAs() { var solver = new CharSetSolver(BitWidth.BV64); List<char> alph = new List<char> { 'a', 'b' }; HashSet<char> al = new HashSet<char>(alph); PDLPred phi = new PDLAnd(new PDLIntGeq(new PDLIndicesOf("a"), 2), new PDLIntGeq(new PDLIndicesOf("b"), 2)); //PDLPred phi2 = new PDLIf(new PDLStartsWith("b"), new PDLEndsWith("b")); var dfaCorr = phi.GetDFA(al, solver); var a = solver.MkCharConstraint(false, 'a'); var b = solver.MkCharConstraint(false, 'b'); var moves = new List<Move<BDD>>(); moves.Add(new Move<BDD>(0, 1, a)); moves.Add(new Move<BDD>(0, 1, b)); moves.Add(new Move<BDD>(1, 2, a)); moves.Add(new Move<BDD>(1, 2, b)); moves.Add(new Move<BDD>(2, 3, a)); moves.Add(new Move<BDD>(2, 3, b)); moves.Add(new Move<BDD>(3,3, a)); moves.Add(new Move<BDD>(3, 3, b)); //moves.Add(new Move<BDD>(3, 4, a)); //moves.Add(new Move<BDD>(3, 2, b)); //moves.Add(new Move<BDD>(4, 4, a)); //moves.Add(new Move<BDD>(4, 5, b)); //moves.Add(new Move<BDD>(5, 6, a)); //moves.Add(new Move<BDD>(5, 4, b)); //moves.Add(new Move<BDD>(6, 6, a)); //moves.Add(new Move<BDD>(6, 6, b)); var dfa2 = Automaton<BDD>.Create(0, new int[] { 3 }, moves); //Assert.IsTrue(phi2.GetDFA(al,solver).IsEquivalentWith(dfa2,solver)); solver.SaveAsDot(dfaCorr, "aa", "corr"); solver.SaveAsDot(dfa2, "aa", "wrong"); //var v0 = DFADensity.GetDFADifferenceRatio(dfa1, dfa2, al, solver); //var v1 = PDLEditDistance.GetMinimalFormulaEditDistanceRatio(dfa1, dfa2, al, solver, timeout); //var v2 = DFAEditDistance.GetDFAOptimalEdit(dfa1, dfa2, al, solver, 4, new StringBuilder()); //Console.WriteLine("density ratio: {0}; pdl edit distance: {1}; dfa edit distance: {2}", v0, v1, v2); var gr = DFAGrading.GetGrade(dfaCorr, dfa2, al, solver, 2000, 10, FeedbackLevel.Hint, true, true, true); Console.WriteLine(gr.First); foreach (var f in gr.Second) Console.WriteLine(f.ToString()); }