Esempio n. 1
0
        BDDNode Generate(int height, int max, int m2)
        {
            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);
                //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));

                    //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);
            }
        }
Esempio n. 2
0
        private BDDNode CreateExpressionsFromBooleanNetwork(
            BDDManager manager,
            List <GeneLink> booleanNetwok,
            Dictionary <string, List <int> > availableFunctions,
            int depth,
            Dictionary <string, BDDNode> nodeStore)
        {
            var toDictionary = booleanNetwok.GroupBy(a => a.To);

            BDDNode seq = null;

            for (int i = 0; i < depth - 1; i++)
            {
                var ass = CreateFunctionApplication(manager, availableFunctions, toDictionary, i, nodeStore);

                if (seq == null)
                {
                    seq = ass;
                }
                else
                {
                    seq = manager.And(seq, ass);
                }
            }

            return(seq);
        }
Esempio n. 3
0
 private BDDNode GetAndOr(int height, int max, int m2, BDDManager manager, Random r)
 {
     if (r.NextDouble() > .5)
     {
         return(manager.Or(Generate(height + 1, max, m2, manager, r), Generate(height + 1, max, m2, manager, r)));
     }
     else
     {
         return(manager.And(Generate(height + 1, max, m2, manager, r), Generate(height + 1, max, m2, manager, r)));
     }
 }
Esempio n. 4
0
        private BDDNode AppyToAll(List <GeneLink> froms, int i,
                                  bool value, string func)
        {
            BDDNode app = null;

            foreach (var f in froms)
            {
                var formatParameter = Formater.FormatParameter(f.From, i);

                var param = _nodeStore[formatParameter];

                var node1 =
                    BDDSharpSolver.CreateNodeBasedOnAutomata(
                        formatParameter, true, _manager,
                        param.Index);

                if (func == OR)
                {
                    if (app == null)
                    {
                        app = node1;
                    }
                    else
                    {
                        app = _manager.Or(app, node1);
                    }
                }
                else
                if (func == AND)
                {
                    if (app == null)
                    {
                        app = node1;
                    }
                    else
                    {
                        app = _manager.And(app, node1);
                    }
                }
            }

            if (!value)
            {
                app = _manager.Not(app);
            }

            return(app);
        }
Esempio n. 5
0
        private BDDNode CreateAssignmentsBddNodes(Dictionary <string, bool> assignments,
                                                  BDDManager manager, Dictionary <string, BDDNode> nodeStore, ref int i)
        {
            BDDNode assignmentsBDDNode = null;

            foreach (var a in assignments.ToList())
            {
                BDDNode bddNode = CreateNodeBasedOnAutomata(a.Key, a.Value, manager, i);
                i++;

                nodeStore[a.Key] = bddNode;
                if (assignmentsBDDNode == null)
                {
                    assignmentsBDDNode = bddNode;
                }
                else
                {
                    assignmentsBDDNode = manager.And(assignmentsBDDNode, bddNode);
                }
            }

            return(assignmentsBDDNode);
        }
        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);
        }
Esempio n. 7
0
        private BDDNode CreateFunctionApplication(
            BDDManager manager,
            Dictionary <string, List <int> > availableFunctions,
            IEnumerable <IGrouping <string, GeneLink> > toDictionary,
            int i,
            Dictionary <string, BDDNode> nodeStore)
        {
            BDDNode res = null;


            toDictionary.ToList().ForEach(ff =>
            {
                BDDNode ass = null;
                var froms   = ff.Where(a => !a.IsOptional).ToList();

                // can be null!!
                var to = Formater.FormatParameter(ff.Key, i + 1);

                if (availableFunctions == null || !availableFunctions.ContainsKey(ff.Key))
                {
                    var from1 = Formater.FormatParameter(ff.FirstOrDefault().From, i);
                    //TODO: NOT!
                    res = manager.Equal(nodeStore[from1], nodeStore[to]);
                }
                else // real functions
                {
                    var availableFunc        = availableFunctions[ff.Key];
                    var funcAssignmentHelper =
                        new BddNodeFuncAssignmentHelper(manager, nodeStore);
                    BDDNode rightSide = null;

                    var leftSide =
                        BDDSharpSolver.CreateNodeBasedOnAutomata(
                            to, true, manager,
                            nodeStore[to].Index);

                    availableFunc.ForEach(f =>
                    {
                        ass =
                            funcAssignmentHelper.CreateFuncAssignment(to, froms, i, f);

                        ass = manager.And(nodeStore[CreateFunctionKey(f, ff.Key)], ass);

                        if (rightSide == null)
                        {
                            rightSide = ass;
                        }
                        else
                        {
                            rightSide = manager.Or(rightSide, ass);
                        }
                    });

                    logger.Info("Right Side");
                    LogToDotFormat(rightSide, manager, nodeStore);

                    res = manager.Equal(leftSide, rightSide);
                }
            });

            return(res);
        }
Esempio n. 8
0
        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;
        }
Esempio n. 9
0
        public override BDDNode And(BDDNode a, BDDNode b)
        {
            var bddNode = _manager.And(a, b);

            return(bddNode);
        }