public void TestReduceInfEdAcUk() { var manager = new BDDManager(3); var n1 = manager.Create(2, manager.One, manager.Zero); var n2 = manager.Create(2, manager.One, manager.Zero); var n3 = manager.Create(1, n1, manager.Zero); var n4 = manager.Create(1, n2, n1); var n5 = manager.Create(0, n4, n3); var truth = BuildThruthTable(manager, n5); var res = manager.Reduce(n5); CheckThruthTable(truth, res); Assert.AreEqual(0, res.Index); Assert.AreEqual(1, res.Low.Index); Assert.AreEqual(2, res.Low.High.Index); Assert.AreEqual(2, res.High.Index); Assert.AreEqual(false, res.Low.Low.Value); Assert.AreEqual(false, res.Low.High.Low.Value); Assert.AreEqual(false, res.High.Low.Value); Assert.AreEqual(true, res.High.High.Value); Assert.AreEqual(true, res.Low.High.High.Value); }
public void TestSwapSimple2() { var manager = new BDDManager(3); var n2 = manager.Create(2, manager.One, manager.Zero); var n3 = manager.Create(1, manager.One, n2); var n4 = manager.Create(1, manager.Zero, manager.One); var root = manager.Create(0, n3, n4); var dict = new Dictionary <int, string> { { 0, "a" }, { 1, "b" }, { 2, "c" } }; var rdict = dict.ToDictionary((x) => x.Value, (x) => x.Key); var truth = BuildThruthTable(manager, root); Console.WriteLine(manager.ToDot(root, (x) => x.RefCount.ToString())); var res = manager.Swap(root, rdict["b"], rdict["c"]); CheckThruthTable(truth, res); Console.WriteLine(manager.ToDot(res, (x) => x.RefCount.ToString())); res = manager.Reduce(res); CheckThruthTable(truth, res); Console.WriteLine(manager.ToDot(res, (x) => x.RefCount.ToString())); }
public void TestDiamond() { var dict = new Dictionary <int, string> { { 0, "x0" }, { 1, "x1" }, { 2, "x2" }, { 3, "x3" }, { 4, "x4" }, { 5, "x5" } }; var rdict = dict.ToDictionary((x) => x.Value, (x) => x.Key); var manager = new BDDManager(6); manager.GetVariableString = (x) => x < 6 ? dict[x] : "sink"; var a1 = manager.Create(rdict["x0"], 0, 1); var a2 = manager.Create(rdict["x2"], 0, a1); var a3 = manager.Create(rdict["x2"], a1, 1); var a4 = manager.Create(rdict["x3"], a2, a3); var truth = BuildThruthTable(manager, a4); var res = manager.Sifting(a4); CheckThruthTable(truth, res); }
public void TestSwapNotAdjacentVariable() { var manager = new BDDManager(2); var n3 = manager.Create(1, manager.One, manager.Zero); var n4 = manager.Create(1, manager.Zero, manager.One); var root = manager.Create(0, n3, n4); var e = Assert.Catch(() => { var res = manager.Swap(root, 0, 2); }); Assert.IsInstanceOf(typeof(ArgumentException), e); StringAssert.Contains("not adjacents", e.Message); }
BDDNode Generate(int height, int max, int m2, BDDManager manager, Random r) { if (height > max) { var id = manager.CreateVariable(); var v = manager.Create(id, manager.Zero, manager.One); //Console.WriteLine(manager.ToDot(v, (x) => "["+x.Id+"] x" + x.Index + " (" + x.RefCount.ToString() + ")")); return(v); } else { var acc = GetAndOr(height, max, m2, manager, r); //Console.WriteLine(manager.ToDot(acc, (x) => "["+x.Id+"] x" + x.Index + " (" + x.RefCount.ToString() + ")")); for (int i = 0; i < m2; i++) { //if (r.NextDouble() > .5) // acc = manager.Or(acc, Generate(height + 1, max, m2)); //else acc = manager.And(acc, Generate(height + 1, max, m2, manager, r)); //Console.WriteLine(manager.ToDot(acc, (x) => "["+x.Id+"] x" + x.Index + " (" + x.RefCount.ToString() + ")")); } //Console.WriteLine(manager.ToDot(acc, (x) => "["+x.Id+"] x" + x.Index + " (" + x.RefCount.ToString() + ")")); //acc.RefCount++; //manager.GarbageCollect(); //acc.RefCount--; //Console.WriteLine(manager.ToDot(acc, (x) => "["+x.Id+"] x" + x.Index + " (" + x.RefCount.ToString() + ")")); //int v = manager.GetSize(acc); //acc = manager.Sifting(acc); //throw new Exception(); //Console.WriteLine (" Number of nodes: " + v + " -> " + manager.GetSize(acc)); return(acc); } }
public void TestReduceSimple() { var manager = new BDDManager(3); var n3 = manager.Create(2, manager.One, manager.One); var n4 = manager.Create(1, n3, manager.Zero); var n2 = manager.Create(1, n3, manager.Zero); var root = manager.Create(0, n2, n4); var truth = BuildThruthTable(manager, root); var res = manager.Reduce(root); CheckThruthTable(truth, res); Assert.AreEqual(1, res.Index); Assert.AreEqual(false, res.Low.Value); Assert.AreEqual(true, res.High.Value); }
public void TestSwapSimple2() { var manager = new BDDManager(3); var n2 = manager.Create(2, manager.One, manager.Zero); var n3 = manager.Create(1, manager.One, n2); var n4 = manager.Create(1, manager.Zero, manager.One); var root = manager.Create(0, n3, n4); var dict = new Dictionary <int, string> { { 0, "a" }, { 1, "b" }, { 2, "c" } }; var rdict = dict.ToDictionary((x) => x.Value, (x) => x.Key); var truth = BuildThruthTable(manager, root); var res = manager.Sifting(root); CheckThruthTable(truth, res); }
public static BDDNode CreateNodeBasedOnAutomata (string key, bool value, BDDManager manager, int i) { BDDNode nodeBasedOnAutomata; if (value) { nodeBasedOnAutomata = manager.Create(i, manager.One, manager.Zero); } else { nodeBasedOnAutomata = manager.Create(i, manager.Zero, manager.One); } //nodeBasedOnAutomata.OriginalValue = value; return(nodeBasedOnAutomata); }
public void TestSwapAsymetric() { var manager = new BDDManager(2); var n3 = manager.Create(1, manager.One, manager.Zero); var root = manager.Create(0, n3, manager.One); var dict = new Dictionary <int, string> { { 0, "a" }, { 1, "b" }, { 2, "c" } }; var truth = BuildThruthTable(manager, root); Console.WriteLine(manager.ToDot(root, (x) => x.RefCount.ToString())); var res = manager.Reduce(manager.Swap(root, 0, 1)); Console.WriteLine(manager.ToDot(res, (x) => x.RefCount.ToString())); CheckThruthTable(truth, res); }
public void TestSimpleITE() { var manager = new BDDManager(4); var b = manager.Create(1, manager.One, manager.Zero); var f = manager.Create(0, manager.One, b); var c = manager.Create(2, manager.One, manager.Zero); var g = manager.Create(0, c, manager.Zero); var d = manager.Create(3, manager.One, manager.Zero); var h = manager.Create(1, manager.One, d); var dict = new Dictionary <int, string> { { 0, "a" }, { 1, "b" }, { 2, "c" }, { 3, "d" } }; var res = manager.ITE(f, g, h); manager.Reduce(res); Assert.AreEqual(0, res.Index); Assert.AreEqual(2, res.High.Index); Assert.AreEqual(1, res.Low.Index); Assert.AreEqual(3, res.Low.Low.Index); Assert.AreEqual(true, res.High.High.Value); Assert.AreEqual(false, res.High.Low.Value); Assert.AreEqual(false, res.Low.High.Value); Assert.AreEqual(true, res.Low.Low.High.Value); Assert.AreEqual(false, res.Low.Low.Low.Value); }
public void TestSwapBug() { var dict = new Dictionary <int, string> { { 0, "x1" }, { 1, "x2" }, { 2, "x4" }, { 3, "x3" }, { 4, "x5" }, { 5, "x6" } }; var rdict = dict.ToDictionary((x) => x.Value, (x) => x.Key); var manager = new BDDManager(6); manager.GetVariableString = (x) => dict[x]; var n9 = manager.Create(rdict["x6"], 1, 0); var n8 = manager.Create(rdict["x5"], 0, n9); var n7 = manager.Create(rdict["x5"], n9, 0); var n6 = manager.Create(rdict["x3"], n8, n7); var n5 = manager.Create(rdict["x3"], 1, n7); var n4 = manager.Create(rdict["x4"], n5, n6); var root = n4; var truth = BuildThruthTable(manager, root); Console.WriteLine(manager.ToDot(root, (x) => x.RefCount.ToString())); var res = manager.Swap(root, rdict["x5"], rdict["x6"]); CheckThruthTable(truth, res); Console.WriteLine(manager.ToDot(res, (x) => x.RefCount.ToString())); }
public void TestToString() { var manager = new BDDManager(1); var c = manager.Create(0, manager.Zero, manager.One); var str = manager.One.ToString(); StringAssert.Contains("Identifier=1", str); StringAssert.Contains("Value=True", str); StringAssert.Contains("Low=null", str); StringAssert.Contains("High=null", str); str = manager.Zero.ToString(); StringAssert.Contains("Identifier=0", str); StringAssert.Contains("Value=False", str); StringAssert.Contains("Low=null", str); StringAssert.Contains("High=null", str); str = c.ToString(); StringAssert.Contains("Value=", str); StringAssert.Contains("Low=1", str); StringAssert.Contains("High=0", str); }
public void TestComplex() { var dict = new Dictionary <int, string> { { 0, "x1" }, { 1, "x3" }, { 2, "x5" }, { 3, "x2" }, { 4, "x4" }, { 5, "x6" } }; var rdict = dict.ToDictionary((x) => x.Value, (x) => x.Key); var manager = new BDDManager(6); manager.GetVariableString = (x) => x < 6 ? dict[x] : "sink"; var a13 = manager.Create(rdict["x6"], manager.One, manager.Zero); var a12 = manager.Create(rdict["x4"], manager.One, a13); var a11 = manager.Create(rdict["x4"], manager.One, manager.Zero); var a10 = manager.Create(rdict["x2"], manager.One, manager.Zero); var a9 = manager.Create(rdict["x2"], manager.One, a13); var a8 = manager.Create(rdict["x2"], manager.One, a11); var a7 = manager.Create(rdict["x2"], manager.One, a12); var a6 = manager.Create(rdict["x5"], a13, manager.Zero); var a5 = manager.Create(rdict["x5"], a12, a11); var a4 = manager.Create(rdict["x5"], a9, a10); var a3 = manager.Create(rdict["x5"], a7, a8); var a2 = manager.Create(rdict["x3"], a5, a6); var a1 = manager.Create(rdict["x3"], a3, a4); var a0 = manager.Create(rdict["x1"], a1, a2); var truth = BuildThruthTable(manager, a0); Console.WriteLine(manager.ToDot(a0, (x) => "x" + x.Index + " (" + x.RefCount.ToString() + ")")); var res = manager.Sifting(a0); Console.WriteLine(manager.ToDot(res, (x) => "x" + x.Index + " (" + x.RefCount.ToString() + ")")); CheckThruthTable(truth, res); Assert.AreEqual(8, manager.GetSize(res)); }
public void TestReduceInfUnibzIt() { var manager = new BDDManager(4); var n1 = manager.Create(3, manager.Zero, manager.Zero); var n2 = manager.Create(3, manager.One, manager.Zero); var n3 = manager.Create(3, manager.Zero, manager.Zero); var n4 = manager.Create(3, manager.One, manager.Zero); var n5 = manager.Create(3, manager.Zero, manager.Zero); var n6 = manager.Create(3, manager.One, manager.Zero); var n7 = manager.Create(3, manager.One, manager.One); var n8 = manager.Create(3, manager.One, manager.One); var n9 = manager.Create(2, n2, n1); var n10 = manager.Create(2, n4, n3); var n11 = manager.Create(2, n6, n5); var n12 = manager.Create(2, n8, n7); var n13 = manager.Create(1, n10, n9); var n14 = manager.Create(1, n12, n11); var n15 = manager.Create(0, n14, n13); var truth = BuildThruthTable(manager, n15); var res = manager.Reduce(n15); CheckThruthTable(truth, res); Assert.AreEqual(0, res.Index); Assert.AreEqual(2, res.Low.Index); Assert.AreEqual(3, res.Low.High.Index); Assert.AreEqual(1, res.High.Index); Assert.AreEqual(2, res.High.Low.Index); Assert.AreEqual(false, res.Low.Low.Value); Assert.AreEqual(false, res.Low.High.Low.Value); Assert.AreEqual(false, res.High.Low.Low.Value); Assert.AreEqual(true, res.High.High.Value); Assert.AreEqual(true, res.Low.High.High.Value); }
public void TestComplex() { var dict = new Dictionary <int, string> { { 0, "x1" }, { 1, "x3" }, { 2, "x5" }, { 3, "x2" }, { 4, "x4" }, { 5, "x6" } }; var rdict = dict.ToDictionary((x) => x.Value, (x) => x.Key); var manager = new BDDManager(6); manager.GetVariableString = (x) => x < 6 ? dict[x] : "sink"; var a13 = manager.Create(rdict["x6"], manager.One, manager.Zero); var a12 = manager.Create(rdict["x4"], manager.One, a13); var a11 = manager.Create(rdict["x4"], manager.One, manager.Zero); var a10 = manager.Create(rdict["x2"], manager.One, manager.Zero); var a9 = manager.Create(rdict["x2"], manager.One, a13); var a8 = manager.Create(rdict["x2"], manager.One, a11); var a7 = manager.Create(rdict["x2"], manager.One, a12); var a6 = manager.Create(rdict["x5"], a13, manager.Zero); var a5 = manager.Create(rdict["x5"], a12, a11); var a4 = manager.Create(rdict["x5"], a9, a10); var a3 = manager.Create(rdict["x5"], a7, a8); var a2 = manager.Create(rdict["x3"], a5, a6); var a1 = manager.Create(rdict["x3"], a3, a4); var a0 = manager.Create(rdict["x1"], a1, a2); Assert.AreEqual(3, manager.GetSize(a13)); Assert.AreEqual(4, manager.GetSize(a12)); Assert.AreEqual(3, manager.GetSize(a11)); Assert.AreEqual(3, manager.GetSize(a10)); Assert.AreEqual(4, manager.GetSize(a9)); Assert.AreEqual(4, manager.GetSize(a8)); Assert.AreEqual(5, manager.GetSize(a7)); Assert.AreEqual(4, manager.GetSize(a6)); Assert.AreEqual(6, manager.GetSize(a5)); Assert.AreEqual(6, manager.GetSize(a4)); Assert.AreEqual(8, manager.GetSize(a3)); Assert.AreEqual(8, manager.GetSize(a2)); Assert.AreEqual(12, manager.GetSize(a1)); Assert.AreEqual(16, manager.GetSize(a0)); var res = manager.Sifting(a0); Assert.AreEqual(8, manager.GetSize(res)); }
public BDDNode GetObstructionSet(Obstacle obstacle) { if (obstacleCache.ContainsKey(obstacle)) { return(obstacleCache[obstacle]); } BDDNode acc2 = null; foreach (var r in obstacle.Refinements()) { BDDNode acc = null; foreach (var c in r.SubObstacles()) { if (acc == null) { acc = GetObstructionSet(c); } else { var bDDNode = GetObstructionSet(c); acc = _manager.And(acc, bDDNode); } } foreach (var c in r.DomainHypotheses()) { if (acc == null) { acc = GetObstructionSet(c); } else { var bDDNode = GetObstructionSet(c); acc = _manager.And(acc, bDDNode); } } foreach (var c in r.DomainProperties()) { if (acc == null) { acc = GetObstructionSet(c); } else { var bDDNode = GetObstructionSet(c); acc = _manager.And(acc, bDDNode); } } if (acc2 == null) { acc2 = acc; } else { acc2 = _manager.Or(acc2, acc); } } // Leaf obstacle if (acc2 == null) { int idx; if (_mapping.ContainsKey(obstacle)) { idx = _mapping[obstacle]; } else { idx = _manager.CreateVariable(); _mapping.Add(obstacle, idx); _rmapping.Add(idx, obstacle); } acc2 = _manager.Create(idx, _manager.One, _manager.Zero); } obstacleCache.Add(obstacle, acc2); return(acc2); }
public static BDDNode CreateTree(List <Token> rpn, Dictionary <string, bool> variables, BDDManager manager)//возвращает бинарное дерево { int CountOfVariables = variables.Count; int count = Convert.ToInt32(Math.Pow(2, CountOfVariables - 1));//количесвто переменных в последнем ряду BDDNode[] arr = new BDDNode[count]; for (int i = 0; i < count; i++) { bool left; bool right; GetVariables(i * 2 + count * 2, variables); try { left = Calculate(rpn, variables); } catch { return(null); } GetVariables(i * 2 + 1 + count * 2, variables); try { right = Calculate(rpn, variables); } catch { return(null); } BDDNode lft; //Low BDDNode rght; //High if (left) { lft = manager.One; } else { lft = manager.Zero; } if (right) { rght = manager.One; } else { rght = manager.Zero; } arr[i] = manager.Create(CountOfVariables - 1, rght, lft); } if (CountOfVariables == 1) { return(arr[0]); } if (CountOfVariables == 2) { return(manager.Create(CountOfVariables - 2, arr[1], arr[0])); } int newcount = 0; List <BDDNode> list = new List <BDDNode>(); int c = 0; for (int i = 0; i < Convert.ToInt32(Math.Pow(2, CountOfVariables - 2)); i++)//предпоследняя линия переменных { list.Add(manager.Create(CountOfVariables - 2, arr[i * 2 + 1], arr[i * 2])); } if (CountOfVariables == 3) { return(manager.Create(0, list[1], list[0])); } c = list.Count(); int k = 0; for (int i = 0; i < CountOfVariables - 1; i++) { newcount = newcount + Convert.ToInt32(Math.Pow(2, i)); } int lvl = CountOfVariables - 3; while (lvl != 0) { for (int i = list.Count; i < newcount; i++) { if (i == Convert.ToInt32(Math.Pow(2, lvl)) + c) { k = c; lvl--; c = i;// first c=16, when variables.count=6 } list.Add(manager.Create(lvl, list[i - c + i % Convert.ToInt32(Math.Pow(2, lvl)) + 1 + k], list[i - c + i % Convert.ToInt32(Math.Pow(2, lvl)) + k])); } } return(list.Last()); }
public bool IsValidPath(GeneNode automata, List <GeneLink> booleanNetwok, Dictionary <string, List <int> > availableFunctions = null) { var nodeStore = new Dictionary <string, BDDNode>(); var letters = new List <string>(); var depth = automata.NodeLength; int z = 0; var manager = new BDDManager(50); automata.GetAllConditionLetters(letters); letters = letters.SelectMany(l => Enumerable.Range(0, depth).ToList().Select(n => Formater.FormatParameter(l, n))) .ToList(); int nodeNumber = 0; logger.Info(string.Join(",", letters)); var assignments = BDDLogicHelper.CreateDictBasedOnAutomata(automata); nodeStore["1"] = manager.One; nodeStore["0"] = manager.Zero; var assignmentsBDDNode = CreateAssignmentsBddNodes(assignments, manager, nodeStore, ref nodeNumber); BDDNode functionNodes = null; var functionsKeys = CreateFunctionsKeys(availableFunctions); functionsKeys.ToList().ForEach(f => { BDDNode currentNodeOr = null; List <BDDNode> currents = new List <BDDNode>(); f.Value.ForEach(d => { var c = manager.Create(nodeNumber++, 1, 0); nodeStore[d] = c; currents.Add(c); if (currentNodeOr == null) { currentNodeOr = c; } else { currentNodeOr = manager.Or(c, currentNodeOr); } }); currentNodeOr = manager.Equal(manager.One, currentNodeOr); if (functionNodes == null) { functionNodes = currentNodeOr; } else { functionNodes = manager.And(functionNodes, currentNodeOr); } }); var relations = CreateExpressionsFromBooleanNetwork(manager, booleanNetwok, availableFunctions, depth, nodeStore); relations = manager.And(relations, functionNodes); var root = manager.And(relations, assignmentsBDDNode); //// LOG PART LogToDotFormat(root, manager, nodeStore); //logger.Info("relations"); //logger.Info(manager.ToDot(relations, show_all: false)); //logger.Info("assignments"); //logger.Info(manager.ToDot(assignmentsBDDNode, show_all: false)); IEnumerable <KeyValuePair <string, bool> > truth = BuildThruthTable(manager, root, nodeNumber); assignments.ToList().ForEach(a => { var index = nodeStore[a.Key].Index; truth = truth.Where(d => d.Key[index] == (a.Value ? '1' : '0')); }); var pathes = truth.Where(a => a.Value).ToList(); LogValidPathes(pathes, nodeStore); return(pathes.Any()); // CheckThruthTable(truth, res); //return true; }
static void Main(string[] args) { var manager = new BDDManager(10); //var n3 = new BDDNode(2, manager.One, manager.One); //var n4 = new BDDNode(1, n3, manager.Zero); //var n2 = new BDDNode(1, n3, manager.Zero); //var n3 = manager.Create(2, manager.One, manager.One); //var n4 = manager.Create(1, n3, manager.Zero); //var n2 = manager.Create(1, n3, manager.Zero); //var root = manager.Create(0,n2, n4); //Console.WriteLine(n2); //Console.WriteLine(n3); //Console.WriteLine(n4); //Console.WriteLine(root); int index = 1; var p1_s = manager.Create(index++, manager.One, manager.Zero); var p1_k = manager.Create(index++, manager.One, manager.Zero); //var root = manager.Create(3, p1_s, p1_k); Console.WriteLine(p1_k); Console.WriteLine(p1_s); //var n2 = new BDDNode(1, p1_s, manager.Zero); var node = manager.Or(p1_k, p1_s); //node.Index = index++; var root = manager.Create(0, node, manager.Zero); Console.WriteLine(node); Console.WriteLine(root); //var reducedNode=manager.Reduce(root); //Console.WriteLine(reducedNode); var str = manager.ToDot(node); Console.WriteLine(str); //var root = new BDDNode(0, node, manager.Zero); //Console.WriteLine(root); //var node1 = manager.And(p1_k, p1_s); //var root1 = new BDDNode(0, node1, manager.Zero); // //var c = manager.Create(10, manager.Zero, manager.One); //string str = manager.Zero.ToString(); //string str1 = manager.One.ToString(); //Console.WriteLine(node1); //Console.WriteLine(node); //Console.WriteLine(node1); //Console.WriteLine(reucedBddNode); // -------------------------- //Program p = new Program(); //p.TestLarge01(30, 30); // Console.WriteLine(manager.ToDot(root)); //var reucedBddNode = manager.Reduce(node); //Console.WriteLine(manager.ToDot(reucedBddNode)); //Console.WriteLine(str); //Console.WriteLine(str1); //Console.WriteLine(root1); //Console.WriteLine(reucedBddNode); Console.Read(); //var root =new BDDNode(0, P1_1&000&000BI, P1_1&000&000CSL, P1_1&500&000BI, P1_1&500&000CSL, P1_10&000&000BI, P1_10&000&000CSL, P1_100&000BI, P1_100&000CSL, P1_2&000&000BI, P1_2&000&000CSL, P1_200&000BI, P1_200&000CSL, P1_25&000BI, P1_25&000CSL, P1_3&000&000BI, P1_3&000&000CSL, P1_300&000BI, P1_300&000CSL, P1_4&000&000BI, P1_4&000&000CSL, P1_5&000&000BI, P1_5&000&000CSL, P1_50&000BI, P1_50&000CSL, P1_500&000BI, P1_500&000CSL) // var P2_1&000&000BI = new BDDNode(2, false); //var P1_1 &000&000BI=new BDDNode(1,) }