Exemple #1
0
        /// <summary>
        /// Solve the linear equation system Ax = b with Jacobi
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <param name="init">initial solution</param>
        /// <param name="reach"></param>
        /// <param name="allRowVars"></param>
        /// <param name="allColVars"></param>
        /// <param name="omega">jor parameter</param>
        /// <returns></returns>
        public static CUDDNode Jacobi(CUDDNode a, CUDDNode b, CUDDNode init, CUDDNode reach, CUDDVars allRowVars, CUDDVars allColVars, double omega)
        {
            DateTime startTime = DateTime.Now;
            int numberOfIterations = 0;

            CUDDNode id = CUDD.Matrix.Identity(allRowVars, allColVars);

            CUDD.Ref(reach);
            id = CUDD.Function.And(id, reach);

            CUDD.Ref(id, a);
            CUDDNode diags = CUDD.Function.Times(id, a);
            diags = CUDD.Abstract.SumAbstract(diags, allColVars);

            //
            CUDD.Ref(id, a);
            CUDDNode newA = CUDD.Function.ITE(id, CUDD.Constant(0), a);

            newA = CUDD.Function.Times(CUDD.Constant(-1), newA);

            // divide a,b by diagonal
            CUDD.Ref(diags);
            newA = CUDD.Function.Divide(newA, diags);

            CUDD.Ref(b, diags);
            CUDDNode newB = CUDD.Function.Divide(b, diags);


            // print out some memory usage
            Debug.WriteLine("Iteration matrix MTBDD... [nodes = " + CUDD.GetNumNodes(newA) + "]");
            Debug.WriteLine("Diagonals MTBDD... [nodes = " + CUDD.GetNumNodes(diags) + "]");


            CUDD.Ref(init);
            CUDDNode sol = init;

            while(true)
            {
                numberOfIterations++;

                CUDDNode tmp = CUDD.Matrix.MatrixMultiplyVector(newA, sol, allRowVars, allColVars);

                CUDD.Ref(newB);
                tmp = CUDD.Function.Plus(tmp, newB);

                if(omega != 1)
                {
                    tmp = CUDD.Function.Times(tmp, CUDD.Constant(omega));

                    CUDD.Ref(sol);
                    tmp = CUDD.Function.Plus(tmp, CUDD.Function.Times(sol, CUDD.Constant(1 - omega)));
                }

                if(CUDD.IsEqual(tmp, sol))
                {
                    CUDD.Deref(tmp);
                    break;
                }

                CUDD.Deref(sol);
                sol = tmp;
            }

            CUDD.Deref(id, diags, newA, newB);

            DateTime endTime = DateTime.Now;
            double runningTime = (endTime - startTime).TotalSeconds;
            Debug.WriteLine("Jacobi: " + numberOfIterations + " iterations in " + runningTime + " seconds");

            return sol;
        }
Exemple #2
0
        /// <summary>
        /// Check whether destination is reachable from source
        /// [ REFS: '', DEREFS:]
        /// </summary>
        /// <param name="source"></param>
        /// <param name="destination"></param>
        /// <param name="transitions"></param>
        /// <returns></returns>
        public bool Path(CUDDNode source, CUDDNode destination, List <CUDDNode> transitions)
        {
            CUDD.Ref(source, destination);
            CUDDNode temp = CUDD.Function.And(source, destination);

            //In case source already satisfies destination
            if (!temp.Equals(CUDD.ZERO))
            {
                CUDD.Deref(temp);
                return(true);
            }

            //
            bool reachable = false;

            CUDDNode allReachabeToGoal, currentReachableToGoal;

            CUDD.Ref(destination, destination);
            allReachabeToGoal = currentReachableToGoal = destination;

            CUDDNode allReachableFromInit, currentReachableFromInit;

            CUDD.Ref(source, source);
            allReachableFromInit = currentReachableFromInit = source;

            int nextStep              = 0;
            int forwardStep           = 0;
            int numberOfForwardNodes  = 0;
            int backwardSteps         = 0;
            int numberOfBackwardNodes = 0;

            int numberOfLoop = 0;

            CUDDNode commonNode = CUDD.Constant(0);

            do
            {
                if (nextStep != GoForward)
                {
                    //backward
                    numberOfLoop++;
                    Debug.Write(numberOfLoop + " ");

                    //to find source state, now the currentReachableToGoal must be in column form (target state)
                    currentReachableToGoal = this.Predecessors(currentReachableToGoal, transitions);

                    //Check 2 directions have intersection
                    CUDD.Ref(currentReachableToGoal, currentReachableFromInit);
                    CUDD.Deref(commonNode);
                    commonNode = CUDD.Function.And(currentReachableToGoal, currentReachableFromInit);

                    if (!commonNode.Equals(CUDD.ZERO))
                    {
                        reachable = true;
                        break;
                    }

                    //find fixpoint
                    CUDD.Ref(currentReachableToGoal, allReachabeToGoal);
                    CUDDNode allReachabeToGoalTemp = CUDD.Function.Or(allReachabeToGoal, currentReachableToGoal);

                    backwardSteps++;
                    numberOfBackwardNodes = CUDD.GetNumNodes(allReachabeToGoalTemp);

                    if (allReachabeToGoalTemp.Equals(allReachabeToGoal))
                    {
                        reachable = false;
                        CUDD.Deref(allReachabeToGoalTemp);
                        break;
                    }
                    else
                    {
                        currentReachableToGoal = CUDD.Function.Different(currentReachableToGoal, allReachabeToGoal);
                        allReachabeToGoal      = allReachabeToGoalTemp;
                    }
                }

                if (nextStep != GoBackward)
                {
                    //forward
                    numberOfLoop++;
                    Debug.Write(numberOfLoop + " ");

                    currentReachableFromInit = this.Successors(currentReachableFromInit, transitions);

                    //Check 2 directions have intersection
                    CUDD.Ref(currentReachableToGoal, currentReachableFromInit);
                    CUDD.Deref(commonNode);
                    commonNode = CUDD.Function.And(currentReachableToGoal, currentReachableFromInit);

                    if (!commonNode.Equals(CUDD.ZERO))
                    {
                        reachable = true;
                        break;
                    }

                    //find fixpoint
                    CUDD.Ref(currentReachableFromInit, allReachableFromInit);
                    CUDDNode allReachabeFromInitTemp = CUDD.Function.Or(currentReachableFromInit, allReachableFromInit);

                    forwardStep++;
                    numberOfForwardNodes = CUDD.GetNumNodes(allReachabeFromInitTemp);

                    if (allReachabeFromInitTemp.Equals(allReachableFromInit))
                    {
                        reachable = false;
                        CUDD.Deref(allReachabeFromInitTemp);
                        break;
                    }
                    else
                    {
                        currentReachableFromInit = CUDD.Function.Different(currentReachableFromInit, allReachableFromInit);
                        allReachableFromInit     = allReachabeFromInitTemp;
                    }
                }

                nextStep = GetNextStep(forwardStep, numberOfForwardNodes, backwardSteps, numberOfBackwardNodes);
            } while (true);

            Debug.WriteLine("\nPath: " + numberOfLoop + " loops.");

            CUDD.Deref(allReachabeToGoal, currentReachableToGoal, allReachableFromInit, currentReachableFromInit, commonNode);

            //
            return(reachable);
        }
Exemple #3
0
        public void ShowInfo()
        {
            Console.WriteLine("Domain name: {0}", DomainName);
            Console.WriteLine(Domain.BarLine);

            Console.WriteLine("Problem Name: {0}", ProblemName);
            Console.WriteLine(Domain.BarLine);

            Globals.TermInterpreter.ShowInfo();

            Console.WriteLine("Predicates:");
            foreach (var pair in _predicateDict)
            {
                Console.WriteLine("  Name: {0}, PreCuddIndex: {1}, SucCuddIndex: {2}", pair.Key,
                                  pair.Value.PreviousCuddIndex, pair.Value.SuccessiveCuddIndex);
            }
            Console.WriteLine(Domain.BarLine);

            //Console.WriteLine("Initial state:");
            //foreach (var pred in TruePredSet)
            //{
            //    Console.WriteLine("  {0}", pred);
            //}

            List <string> eventNameArray = new List <string>
            {
                "leftFail(a1)",
                "rightSuc2(a2,-2)",
                "leftSuc2(a1,0)",
                "leftSuc1(a1)",
                "leftSuc1(a2)",
                "rightSuc1(a1)",
                "rightSuc1(a2)",
                "dropFail(a1)",
                "dropSuc(a1)",
                "pickSuc(a2)",
                "learn(a1,1,-1)",
                "learn(a1,1,0)"
            };

            Console.WriteLine("Events:");

            for (int i = 0; i < eventNameArray.Count; i++)
            {
                string eventName = eventNameArray[i];
                Console.WriteLine(eventName);
                Event e = _eventDict[eventName];
                Console.WriteLine("  Name: {0}", eventNameArray[i]);
                Console.WriteLine("  Cudd index: {0}", e.CuddIndex);

                Console.WriteLine("  Precondition:");
                Console.WriteLine("    Number of nodes: {0}", CUDD.GetNumNodes(e.Precondition));

                //Console.WriteLine("  CondEffect:");
                ////Console.WriteLine("  Count:{0}", e.CondEffect.Count);
                //for (int j = 0; j < e.CondEffect.Count; j++)
                //{
                //    Console.WriteLine("    Index: {0}", j);
                //    //Console.WriteLine("    Condition:");
                //    //CUDD.Print.PrintMinterm(e.CondEffect[j].Item1);
                //    Console.Write("    Literals: ");

                //    foreach (var literal in e.CondEffect[j].Item2)
                //    {
                //        string format = literal.Item2 ? "{0} " : "!{0} ";
                //        Console.Write(format, literal.Item1);
                //    }

                //    Console.WriteLine();
                //}

                Console.WriteLine("  Partial successor state axiom:");
                Console.WriteLine("    Number of nodes: {0}", CUDD.GetNumNodes(e.PartialSsa));
                Console.WriteLine();
            }
            Console.WriteLine(Domain.BarLine);

            Console.WriteLine("Actions:");

            foreach (var action in _actionDict.Values)
            {
                Console.WriteLine("  Name: {0}", action.FullName);
                Console.WriteLine("  Response:");
                foreach (var response in action.ResponseDict.Values)
                {
                    Console.WriteLine("    Name: {0}", response.FullName);
                    Console.Write("      Believe Event list: ");
                    foreach (var e in response.EventModel.BelieveEventList)
                    {
                        Console.Write("{0}  ", e.FullName);
                    }
                    Console.WriteLine();
                    Console.Write("      Know Event list: ");
                    foreach (var e in response.EventModel.KnowEventList)
                    {
                        Console.Write("{0}  ", e.FullName);
                    }
                    Console.WriteLine();
                }
                Console.WriteLine();
            }
            Console.WriteLine(Domain.BarLine);

            Console.WriteLine("Observations:");

            foreach (var observation in _obervationDict.Values)
            {
                Console.WriteLine("  Name: {0}", observation.FullName);
                Console.Write("      Believe Event list: ");
                foreach (var e in observation.EventModel.BelieveEventList)
                {
                    Console.Write("{0}  ", e.FullName);
                }
                Console.WriteLine();
                Console.Write("      Know Event list: ");
                foreach (var e in observation.EventModel.KnowEventList)
                {
                    Console.Write("{0}  ", e.FullName);
                }
                Console.WriteLine();
            }
            Console.WriteLine(Domain.BarLine);
        }
Exemple #4
0
        /// <summary>
        /// Return the path from source to destination. If reachable, return true and path contains the path from source to destination
        /// [ REFS: '', DEREFS:]
        /// </summary>
        /// <param name="source"></param>
        /// <param name="destination"></param>
        /// <param name="transitions"></param>
        /// <param name="model"></param>
        /// <param name="path">not include the init state</param>
        /// <param name="isEmptyPathAllowed">if false, then the path must be not empty though the source satisfies the destination</param>
        /// <returns></returns>
        public bool Path(CUDDNode source, CUDDNode destination, List <CUDDNode> transitions, List <CUDDNode> path, bool isEmptyPathAllowed)
        {
            if (isEmptyPathAllowed)
            {
                CUDD.Ref(source, destination);
                CUDDNode temp = CUDD.Function.And(source, destination);

                //In case source already satisfies destination
                if (!temp.Equals(CUDD.ZERO))
                {
                    CUDD.Deref(temp);
                    return(true);
                }
            }

            //
            bool            reachable = false;
            List <CUDDNode> backtrackingReachability = new List <CUDDNode>();

            List <CUDDNode> forwardReachability = new List <CUDDNode>();

            CUDDNode allReachabeToGoal, currentReachableToGoal;

            CUDD.Ref(destination, destination);
            allReachabeToGoal = currentReachableToGoal = destination;

            CUDDNode allReachableFromInit, currentReachableFromInit;

            CUDD.Ref(source, source);
            allReachableFromInit = currentReachableFromInit = source;

            int nextStep              = 0;
            int forwardStep           = 0;
            int numberOfForwardNodes  = 0;
            int backwardSteps         = 0;
            int numberOfBackwardNodes = 0;

            int numberOfLoop = 0;

            CUDDNode commonNode = CUDD.Constant(0);

            do
            {
                if (nextStep != GoForward)
                {
                    //Backward
                    numberOfLoop++;
                    Debug.Write(numberOfLoop + " ");

                    //to find source state, now the currentReachableToGoal must be in column form (target state)
                    currentReachableToGoal = this.SwapRowColVars(currentReachableToGoal);

                    CUDD.Ref(transitions);
                    currentReachableToGoal = CUDD.Function.And(currentReachableToGoal, transitions);

                    CUDD.Ref(currentReachableToGoal);
                    backtrackingReachability.Add(currentReachableToGoal);

                    //get source state, but in row-form
                    currentReachableToGoal = CUDD.Abstract.ThereExists(currentReachableToGoal, AllColVars);

                    //Check 2 directions have intersection
                    CUDD.Ref(currentReachableToGoal, currentReachableFromInit);
                    CUDD.Deref(commonNode);
                    commonNode = CUDD.Function.And(currentReachableToGoal, currentReachableFromInit);

                    if (!commonNode.Equals(CUDD.ZERO))
                    {
                        reachable = true;
                        break;
                    }

                    //find fixpoint
                    CUDD.Ref(currentReachableToGoal, allReachabeToGoal);
                    CUDDNode allReachabeToGoalTemp = CUDD.Function.Or(allReachabeToGoal, currentReachableToGoal);

                    backwardSteps++;
                    numberOfBackwardNodes = CUDD.GetNumNodes(allReachabeToGoalTemp);

                    if (allReachabeToGoalTemp.Equals(allReachabeToGoal))
                    {
                        reachable = false;
                        CUDD.Deref(allReachabeToGoalTemp);
                        break;
                    }
                    else
                    {
                        currentReachableToGoal = CUDD.Function.Different(currentReachableToGoal, allReachabeToGoal);
                        allReachabeToGoal      = allReachabeToGoalTemp;
                    }
                }

                if (nextStep != GoBackward)
                {
                    //forward
                    numberOfLoop++;
                    Debug.Write(numberOfLoop + " ");

                    CUDD.Ref(transitions);
                    currentReachableFromInit = CUDD.Function.And(currentReachableFromInit, transitions);

                    //Must reference because current value of currentReachableDD belonging to backtrackingReachability
                    CUDD.Ref(currentReachableFromInit);
                    forwardReachability.Add(currentReachableFromInit);

                    currentReachableFromInit = this.SwapRowColVars(currentReachableFromInit);

                    //get target state, in row-form
                    currentReachableFromInit = CUDD.Abstract.ThereExists(currentReachableFromInit, AllColVars);

                    //Check 2 directions have intersection
                    CUDD.Ref(currentReachableToGoal, currentReachableFromInit);
                    CUDD.Deref(commonNode);
                    commonNode = CUDD.Function.And(currentReachableToGoal, currentReachableFromInit);

                    if (!commonNode.Equals(CUDD.ZERO))
                    {
                        reachable = true;
                        break;
                    }

                    //find fixpoint
                    CUDD.Ref(currentReachableFromInit, allReachableFromInit);
                    CUDDNode allReachabeFromInitTemp = CUDD.Function.Or(currentReachableFromInit, allReachableFromInit);

                    forwardStep++;
                    numberOfForwardNodes = CUDD.GetNumNodes(allReachabeFromInitTemp);

                    if (allReachabeFromInitTemp.Equals(allReachableFromInit))
                    {
                        reachable = false;
                        CUDD.Deref(allReachabeFromInitTemp);
                        break;
                    }
                    else
                    {
                        currentReachableFromInit = CUDD.Function.Different(currentReachableFromInit, allReachableFromInit);
                        allReachableFromInit     = allReachabeFromInitTemp;
                    }
                }

                nextStep = GetNextStep(forwardStep, numberOfForwardNodes, backwardSteps, numberOfBackwardNodes);
            } while (true);

            Debug.WriteLine("\nPath: " + numberOfLoop + " loops.");

            if (!reachable)
            {
                CUDD.Deref(currentReachableToGoal, allReachabeToGoal, currentReachableFromInit, allReachableFromInit, commonNode);
                CUDD.Deref(backtrackingReachability);
                CUDD.Deref(forwardReachability);

                //
                backtrackingReachability.Clear();
                forwardReachability.Clear();
            }
            else
            {
                //
                CUDD.Deref(currentReachableToGoal, allReachabeToGoal, currentReachableFromInit, allReachableFromInit);

                //in column form
                commonNode = this.SwapRowColVars(commonNode);
                for (int i = forwardReachability.Count - 1; i >= 0; i--)
                {
                    //Kill commonNode
                    CUDDNode correctTransition = CUDD.Function.And(forwardReachability[i], commonNode);

                    CUDD.Ref(correctTransition);
                    backtrackingReachability.Add(correctTransition);


                    //in column form
                    commonNode = CUDD.Abstract.ThereExists(correctTransition, AllColVars);
                    commonNode = this.SwapRowColVars(commonNode);
                }

                CUDD.Deref(commonNode);

                //backtrackingReachability contains transitions from source to destination
                if (backtrackingReachability.Count > 0)
                {
                    CUDDNode currentStateDD = source;
                    CUDD.Ref(currentStateDD);

                    for (int i = backtrackingReachability.Count - 1; i >= 0; i--)
                    {
                        //find the intersection
                        currentStateDD = CUDD.Function.And(backtrackingReachability[i], currentStateDD);

                        //remove all boolean variables in the row-form, it is now set of all reachable backward state
                        currentStateDD = CUDD.Abstract.ThereExists(currentStateDD, AllRowVars);

                        currentStateDD = CUDD.RestrictToFirst(currentStateDD, this.AllColVars);
                        currentStateDD = CUDD.Abstract.ThereExists(currentStateDD, this.GetAllEventVars());

                        //swap to row-variable form
                        currentStateDD = this.SwapRowColVars(currentStateDD);

                        CUDD.Ref(currentStateDD);
                        path.Add(currentStateDD);
                    }

                    //
                    CUDD.Deref(currentStateDD);
                }
            }
            //
            return(reachable);
        }