Exemplo n.º 1
0
 /// <summary>
 /// Remove boolean variables in ddv
 /// </summary>
 /// <param name="ddv"></param>
 public void RemoveVars(CUDDVars ddv)
 {
     foreach (CUDDNode var in ddv.vars)
     {
         vars.Remove(var);
     }
     isArrayAllocated = false;
 }
Exemplo n.º 2
0
 /// <summary>
 /// Or Abstract variables in vars, dd must be 0-1 ADD.
 /// result will not contain boolean variables in vars
 /// [ REFS: 'result', DEREFS: dd ]
 /// </summary>
 public static List<CUDDNode> ThereExists(List<CUDDNode> dds, CUDDVars vars)
 {
     List<CUDDNode> result = new List<CUDDNode>();
     foreach (CUDDNode dd in dds)
     {
         result.Add(ThereExists(dd, vars));
     }
     return result;
 }
Exemplo n.º 3
0
            /// <summary>
            /// Or Abstract variables in vars, dd must be 0-1 ADD.
            /// result will not contain boolean variables in vars
            /// [ REFS: 'result', DEREFS: dd ]
            /// </summary>
            public static List <CUDDNode> ThereExists(List <CUDDNode> dds, CUDDVars vars)
            {
                List <CUDDNode> result = new List <CUDDNode>();

                foreach (CUDDNode dd in dds)
                {
                    result.Add(ThereExists(dd, vars));
                }
                return(result);
            }
Exemplo n.º 4
0
            /// <summary>
            /// Swap variables (i.e. x_i -> y_i and y_i -> x_i) of a node.
            /// [ REFS: 'result', DEREFS: dds ]
            /// </summary>
            /// <param name="dds"></param>
            /// <param name="oldVars"></param>
            /// <param name="newVars"></param>
            /// <returns></returns>
            public static List <CUDDNode> SwapVariables(List <CUDDNode> dds, CUDDVars oldVars, CUDDVars newVars)
            {
                List <CUDDNode> result = new List <CUDDNode>();

                foreach (CUDDNode dd in dds)
                {
                    result.Add(CUDD.Variable.SwapVariables(dd, oldVars, newVars));
                }

                //
                return(result);
            }
Exemplo n.º 5
0
        public static CUDDNode NondetBoundedUntil(CUDDNode trans, CUDDNode nondetMask, CUDDVars allRowVars, CUDDVars allColVars, CUDDVars nondetVars, CUDDNode yes, CUDDNode maybe, int bound, bool min)
        {
            DateTime startTime = DateTime.Now;

            CUDDNode a, sol, tmp;

            CUDD.Ref(trans, maybe);
            a = CUDD.Function.Times(trans, maybe);

            CUDD.Ref(yes);
            sol = yes;

            for (int i = 0; i < bound; i++)
            {
                tmp = CUDD.Matrix.MatrixMultiplyVector(a, sol, allRowVars, allColVars);

                if (min)
                {
                    CUDD.Ref(nondetMask);
                    tmp = CUDD.Function.Maximum(tmp, nondetMask);

                    tmp = CUDD.Abstract.MinAbstract(tmp, nondetVars);
                }
                else
                {
                    tmp = CUDD.Abstract.MaxAbstract(tmp, nondetVars);
                }

                CUDD.Ref(yes);
                tmp = CUDD.Function.Maximum(tmp, yes);

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

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

            //
            CUDD.Deref(a);

            return sol;
        }
Exemplo n.º 6
0
        public static CUDDNode NondetInstReward(CUDDNode trans, CUDDNode stateReward, CUDDNode nondetMask, CUDDVars allRowVars, CUDDVars allColVars, CUDDVars nondetVars, int bound, bool min, CUDDNode init)
        {
            DateTime startTime = DateTime.Now;

            CUDDNode newNondetMask, sol, tmp;

            CUDD.Ref(nondetMask);
            newNondetMask = CUDD.Function.ITE(nondetMask, CUDD.PlusInfinity(), CUDD.Constant(0));

            CUDD.Ref(stateReward);
            sol = stateReward;

            for(int i = 0; i < bound; i++)
            {
                tmp = CUDD.Matrix.MatrixMultiplyVector(trans, sol, allRowVars, allColVars);

                if(min)
                {
                    CUDD.Ref(newNondetMask);
                    tmp = CUDD.Function.Maximum(tmp, newNondetMask);

                    tmp = CUDD.Abstract.MinAbstract(tmp, nondetVars);
                }
                else
                {
                    tmp = CUDD.Abstract.MaxAbstract(tmp, nondetVars);
                }

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

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

            //
            CUDD.Deref(newNondetMask);
            return sol;
        }
Exemplo n.º 7
0
 /// <summary>
 /// [ REFS: 'none', DEREFS: 'none' ]
 /// </summary>
 public static void PrintMatrix(CUDDNode dd, CUDDVars rVars, int numRowVars, CUDDVars cVars, int numColVars, int accuracy)
 {
     PlatformInvoke.DD_PrintMatrix(manager, dd.Ptr, rVars.GetArrayPointer(), numRowVars, cVars.GetArrayPointer(), numColVars, accuracy);
 }
Exemplo n.º 8
0
 /// <summary>
 /// [ REFS: 'none', DEREFS: 'none' ]
 /// </summary>
 public static void PrintVectorFiltered(CUDDNode dd, CUDDNode filter, CUDDVars vars, int numVars, int accuracy)
 {
     PlatformInvoke.DD_PrintVectorFiltered(manager, dd.Ptr, filter.Ptr, vars.GetArrayPointer(), numVars);
 }
Exemplo n.º 9
0
        /// <summary>
        /// Return Pmin(phi1 U phi2) = 1
        /// </summary>
        /// <param name="trans01"></param>
        /// <param name="reach"></param>
        /// <param name="nondetMask"></param>
        /// <param name="allRowVars"></param>
        /// <param name="allColVars"></param>
        /// <param name="nondetVar"></param>
        /// <param name="no"></param>
        /// <param name="b2"></param>
        /// <returns></returns>
        public static CUDDNode Prob1A(CUDDNode trans01, CUDDNode reach, CUDDNode nondetMask, CUDDVars allRowVars, CUDDVars allColVars, CUDDVars nondetVar, CUDDNode no, CUDDNode b2)
        {
            DateTime startTime = DateTime.Now;
            int numberOfIterations = 0;

            CUDD.Ref(reach, no);
            CUDDNode notNo = CUDD.Function.And(reach, CUDD.Function.Not(no));

            CUDD.Ref(b2, notNo);
            CUDDNode sol = CUDD.Function.Or(b2, notNo);

            while (true)
            {
                numberOfIterations++;

                CUDD.Ref(sol);
                CUDDNode tmp = CUDD.Variable.SwapVariables(sol, allRowVars, allColVars);

                CUDD.Ref(trans01);
                tmp = CUDD.Abstract.ForAll(CUDD.Function.Implies(trans01, tmp), allColVars);

                CUDD.Ref(nondetMask);
                tmp = CUDD.Function.Or(tmp, nondetMask);
                tmp = CUDD.Abstract.ForAll(tmp, nondetVar);

                CUDD.Ref(notNo);
                tmp = CUDD.Function.And(notNo, tmp);

                CUDD.Ref(b2);
                tmp = CUDD.Function.Or(b2, tmp);

                if (tmp.Equals(sol))
                {
                    CUDD.Deref(tmp);
                    break;
                }
                CUDD.Deref(sol);
                sol = tmp;
            }

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

            //
            CUDD.Deref(notNo);
            return sol;
        }
Exemplo n.º 10
0
 /// <summary>
 /// Universal Abstract (ie. product, *) variables in vars
 /// [ REFS: 'result', DEREFS: dd ]
 /// </summary>
 public static CUDDNode ForAll(CUDDNode dd, CUDDVars vars)
 {
     return(new CUDDNode(PlatformInvoke.DD_ForAll(manager, dd.Ptr, vars.GetArrayPointer(), vars.GetNumVars())));
 }
Exemplo n.º 11
0
        public static CUDDNode NondetUntil(CUDDNode trans, CUDDNode nondetMask, CUDDVars allRowVars, CUDDVars allColVars, CUDDVars nondetVars, CUDDNode yes, CUDDNode maybe, bool min)
        {
            DateTime startTime = DateTime.Now;
            int numberOfIterations = 0;

            CUDDNode a, sol, tmp;

            CUDD.Ref(trans, maybe);
            a = CUDD.Function.Times(trans, maybe);

            CUDD.Ref(yes);
            sol = yes;

            while (true)
            {
                numberOfIterations++;

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

                if (min)
                {
                    CUDD.Ref(nondetMask);
                    tmp = CUDD.Function.Maximum(tmp, nondetMask);

                    tmp = CUDD.Abstract.MinAbstract(tmp, nondetVars);
                }
                else
                {
                    tmp = CUDD.Abstract.MaxAbstract(tmp, nondetVars);
                }

                CUDD.Ref(yes);
                tmp = CUDD.Function.Maximum(tmp, yes);

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

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

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

            //
            CUDD.Deref(a);

            return sol;
        }
Exemplo n.º 12
0
        public static double[] Jacobi(double[][] A, double[] b, double[] init)
        {
            CUDD.InitialiseCUDD(2048 * 1024, Math.Pow(10, -15));

            if(!(A.Length == b.Length && b.Length == init.Length & A.Length > 0))
            {
                throw new Exception("Wrong format");
            }

            int rowSize = A.Length;
            int colSize = A[0].Length;

            CUDDVars rowVars = new CUDDVars();
            CUDDVars colVars = new CUDDVars();
            int numverOfBoolVars = 0;

            //Create bits for row index
            int numberOfBitForRow= (int) Math.Ceiling(Math.Log(rowSize, 2));
            for (int i = 0; i < numberOfBitForRow; i++)
            {
                CUDDNode v = CUDD.Var(numverOfBoolVars++);
                rowVars.AddVar(v);
            }

            //Create bits for col index
            int numberOfBitForCol= (int) Math.Ceiling(Math.Log(colSize, 2));
            for (int i = 0; i < numberOfBitForCol; i++)
            {
                CUDDNode v = CUDD.Var(numverOfBoolVars++);
                colVars.AddVar(v);
            }

            //Convert matrix to BDD
            CUDDNode matrixA = CUDD.Constant(0);

            for (int i = 0; i < A.Length; i++ )
            {
                for(int j = 0; j < A[i].Length; j++)
                {
                    if (A[i][j] != 0)
                    {
                        CUDD.Matrix.SetMatrixElement(matrixA, rowVars, colVars, i, j, A[i][j]);
                    }
                }
            }

            //Convert vector to BDD
            CUDDNode vectorB = CUDD.Constant(0);
            for(int i = 0; i < b.Length; i++)
            {
                if (b[i] != 0)
                {
                    CUDD.Matrix.SetVectorElement(vectorB, rowVars, i, b[i]);
                }
            }

            //Set reach state
            CUDDNode reach = CUDD.Constant(0);
            for(int i = 0; i < rowSize; i++)
            {
                CUDD.Matrix.SetVectorElement(reach, rowVars, i, 1);
            }

            CUDDNode vectorInit = CUDD.Constant(0);
            for(int i = 0; i < init.Length; i++)
            {
                if (init[i] != 0)
                {
                    CUDD.Matrix.SetVectorElement(vectorInit, rowVars, i, init[i]);
                }
            }

            CUDDNode result = Jacobi(matrixA, vectorB, vectorInit, reach, rowVars, colVars, 1);

            double[] solution = new double[colSize];

            for(int i = 0; i < colSize; i++)
            {
                solution[i] = CUDD.Matrix.GetVectorElement(result, rowVars, i);
            }

            return solution;
        }
Exemplo n.º 13
0
 /// <summary>
 /// Sum (ie. +) Abstract variables in vars
 /// [ REFS: 'result', DEREFS: dd ]
 /// </summary>
 public static CUDDNode SumAbstract(CUDDNode dd, CUDDVars vars)
 {
     return new CUDDNode(PlatformInvoke.DD_SumAbstract(manager, dd.Ptr, vars.GetArrayPointer(), vars.GetNumVars()));
 }
Exemplo n.º 14
0
 /// <summary>
 /// Add boolean variables from ddv
 /// </summary>
 /// <param name="ddv"></param>
 public void AddVars(CUDDVars ddv)
 {
     vars.AddRange(ddv.vars);
     isArrayAllocated = false;
 }
Exemplo n.º 15
0
 /// <summary>
 /// Return the first cube making the ADD not 0
 /// [ REFS: 'result', DEREFS: 'dd' ]
 /// </summary>
 public static CUDDNode RestrictToFirst(CUDDNode dd, CUDDVars vars)
 {
     return(new CUDDNode(PlatformInvoke.DD_RestrictToFirst(manager, dd.Ptr, vars.GetArrayPointer(), vars.GetNumVars())));
 }
Exemplo n.º 16
0
 /// <summary>
 /// [ REFS: 'none', DEREFS: 'none' ]
 /// </summary>
 public static void PrintMatrix(CUDDNode dd, CUDDVars rVars, int numRowVars, CUDDVars cVars, int numColVars, int accuracy)
 {
     PlatformInvoke.DD_PrintMatrix(manager, dd.Ptr, rVars.GetArrayPointer(), numRowVars, cVars.GetArrayPointer(), numColVars, accuracy);
 }
Exemplo n.º 17
0
 /// <summary>
 /// [ REFS: 'none', DEREFS: 'none' ]
 /// </summary>
 public static void PrintVectorFiltered(CUDDNode dd, CUDDNode filter, CUDDVars vars, int numVars, int accuracy)
 {
     PlatformInvoke.DD_PrintVectorFiltered(manager, dd.Ptr, filter.Ptr, vars.GetArrayPointer(), numVars);
 }
Exemplo n.º 18
0
 /// <summary>
 /// Print vector dd, suppos that all variables supporting dd belong to vars
 /// [ REFS: 'none', DEREFS: 'none' ]
 /// </summary>
 public static void PrintVector(CUDDNode dd, CUDDVars vars)
 {
     PlatformInvoke.DD_PrintVector(manager, dd.Ptr, vars.GetArrayPointer(), vars.GetNumVars(), LOW);
 }
Exemplo n.º 19
0
 /// <summary>
 /// Return (f and g) abstract vars
 /// [ REFS: 'result', DEREFS: f, g ]
 /// </summary>
 public static CUDDNode AndExists(CUDDNode f, CUDDNode g, CUDDVars vars)
 {
     return(new CUDDNode(PlatformInvoke.DD_AndExists(manager, f.Ptr, g.Ptr, vars.GetArrayPointer(), vars.GetNumVars())));
 }
Exemplo n.º 20
0
 /// <summary>
 /// Universal Abstract (ie. product, *) variables in vars, the same with ForAll
 /// [ REFS: 'result', DEREFS: dd ]
 /// </summary>
 public static CUDDNode ProductAbstract(CUDDNode dd, CUDDVars vars)
 {
     return(new CUDDNode(PlatformInvoke.DD_ProductAbstract(manager, dd.Ptr, vars.GetArrayPointer(), vars.GetNumVars())));
 }
Exemplo n.º 21
0
 /// <summary>
 /// Return (f and g) abstract vars
 /// [ REFS: 'result', DEREFS: f ]
 /// </summary>
 public static CUDDNode MaxAbstract(CUDDNode f, CUDDVars vars)
 {
     return(new CUDDNode(PlatformInvoke.DD_MaxAbstract(manager, f.Ptr, vars.GetArrayPointer(), vars.GetNumVars())));
 }
Exemplo n.º 22
0
 /// <summary>
 /// Return the int value corresponding the binary representation of the current minterm
 /// [ REFS: 'none', DEREFS: 'none' ]
 /// </summary>
 public static int MinTermToInt(CUDDNode dd, CUDDVars vars)
 {
     return PlatformInvoke.Cudd_MinTermToInt(manager, dd.Ptr, vars.GetArrayPointer(), vars.GetNumVars());
 }
Exemplo n.º 23
0
 /// <summary>
 /// Return (f and g) abstract vars
 /// [ REFS: 'result', DEREFS: f ]
 /// </summary>
 public static CUDDNode MinAbstract(CUDDNode f, CUDDVars vars)
 {
     return new CUDDNode(PlatformInvoke.DD_MinAbstract(manager, f.Ptr, vars.GetArrayPointer(), vars.GetNumVars()));
 }
Exemplo n.º 24
0
 /// <summary>
 /// Return the first cube making the ADD not 0
 /// [ REFS: 'result', DEREFS: 'dd' ]
 /// </summary>
 public static CUDDNode RestrictToFirst(CUDDNode dd, CUDDVars vars)
 {
     return new CUDDNode(PlatformInvoke.DD_RestrictToFirst(manager, dd.Ptr, vars.GetArrayPointer(), vars.GetNumVars()));
 }
Exemplo n.º 25
0
 /// <summary>
 /// Or Abstract variables in vars, dd must be 0-1 ADD.
 /// result will not contain boolean variables in vars
 /// [ REFS: 'result', DEREFS: dd ]
 /// </summary>
 public static CUDDNode ThereExists(CUDDNode dd, CUDDVars vars)
 {
     return new CUDDNode(PlatformInvoke.DD_ThereExists(manager, dd.Ptr, vars.GetArrayPointer(), vars.GetNumVars()));
 }
Exemplo n.º 26
0
 public static ODDNode BuildODD(CUDDNode reach, CUDDVars rowVars)
 {
     return new ODDNode(PlatformInvoke._Z9build_oddP9DdManagerP6DdNodePS2_i(manager, reach.Ptr, rowVars.GetArrayPointer(), rowVars.GetNumVars()));
 }
Exemplo n.º 27
0
 /// <summary>
 /// Remove boolean variables in ddv
 /// </summary>
 /// <param name="ddv"></param>
 public void RemoveVars(CUDDVars ddv)
 {
     foreach (CUDDNode var in ddv.vars)
     {
         vars.Remove(var);
     }
     isArrayAllocated = false;
 }
Exemplo n.º 28
0
 /// <summary>
 /// Add boolean variables from ddv
 /// </summary>
 /// <param name="ddv"></param>
 public void AddVars(CUDDVars ddv)
 {
     vars.AddRange(ddv.vars);
     isArrayAllocated = false;
 }
Exemplo n.º 29
0
        /// <summary>
        /// Return Pmax(b1 U b2) = 0
        /// </summary>
        /// <param name="trans01"></param>
        /// <param name="reach"></param>
        /// <param name="allRowVars"></param>
        /// <param name="allColVars"></param>
        /// <param name="nondetvars"></param>
        /// <param name="b1"></param>
        /// <param name="b2"></param>
        /// <returns></returns>
        public static CUDDNode Prob0A(CUDDNode trans01, CUDDNode reach, CUDDVars allRowVars, CUDDVars allColVars, CUDDVars nondetvars, CUDDNode b1, CUDDNode b2)
        {
            DateTime startTime = DateTime.Now;
            int numberOfIterations = 0;

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

            while (true)
            {
                numberOfIterations++;

                CUDD.Ref(sol, trans01);
                CUDDNode tmp = CUDD.Function.And(CUDD.Variable.SwapVariables(sol, allRowVars, allColVars), trans01);

                tmp = CUDD.Abstract.ThereExists(tmp, allColVars);

                tmp = CUDD.Abstract.ThereExists(tmp, nondetvars);

                CUDD.Ref(b1);
                tmp = CUDD.Function.And(b1, tmp);

                CUDD.Ref(b2);
                tmp = CUDD.Function.Or(b2, tmp);

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

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

            CUDD.Ref(reach);
            sol = CUDD.Function.And(reach, CUDD.Function.Not(sol));

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

            return sol;
        }
Exemplo n.º 30
0
        /// <summary>
        /// Solve the linear equation system Ax = b with Jacobi
        /// Sometimes we can use Jacobi to solve the recursive definition
        /// x = Ax + b. We will convert this to (I - A)x = b and use Jacobi to solve.
        /// Experiments show that Jacobi converges faster
        /// [ REFS: 'result', DEREFS: ]
        /// </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;
        }
Exemplo n.º 31
0
        /// <summary>
        /// return Pmax(b1 U b2) = 1
        /// </summary>
        /// <param name="trans01"></param>
        /// <param name="reach"></param>
        /// <param name="allRowVars"></param>
        /// <param name="allColVars"></param>
        /// <param name="nondetVars"></param>
        /// <param name="b1"></param>
        /// <param name="b2"></param>
        /// <param name="no"></param>
        /// <returns></returns>
        public static CUDDNode Prob1E(CUDDNode trans01, CUDDNode reach, CUDDVars allRowVars, CUDDVars allColVars, CUDDVars nondetVars, CUDDNode b1, CUDDNode b2, CUDDNode no)
        {
            DateTime startTime = DateTime.Now;
            int numberOfIterations = 0;

            CUDD.Ref(reach, no);
            CUDDNode u = CUDD.Function.And(reach, CUDD.Function.Not(no));

            bool uDone = false;
            while (!uDone)
            {

                CUDDNode v = CUDD.Constant(0);
                bool vDone = false;

                while (!vDone)
                {
                    numberOfIterations++;

                    CUDD.Ref(u);
                    CUDDNode tmp = CUDD.Variable.SwapVariables(u, allRowVars, allColVars);

                    CUDD.Ref(trans01);
                    tmp = CUDD.Abstract.ForAll(CUDD.Function.Implies(trans01, tmp), allColVars);

                    CUDD.Ref(v);
                    CUDDNode tmp2 = CUDD.Variable.SwapVariables(v, allRowVars, allColVars);

                    CUDD.Ref(trans01);
                    tmp2 = CUDD.Abstract.ThereExists(CUDD.Function.And(tmp2, trans01), allColVars);

                    tmp = CUDD.Function.And(tmp, tmp2);
                    tmp = CUDD.Abstract.ThereExists(tmp, nondetVars);

                    CUDD.Ref(b1);
                    tmp = CUDD.Function.And(b1, tmp);

                    CUDD.Ref(b2);
                    tmp = CUDD.Function.Or(b2, tmp);

                    if (tmp.Equals(v))
                    {
                        vDone = true;
                    }

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

                if (v == u)
                {
                    uDone = true;
                }

                CUDD.Deref(u);
                u = v;
            }

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

            return u;
        }
Exemplo n.º 32
0
        /// <summary>
        /// [ REFS: 'result', DEREFS: ]
        /// </summary>
        /// <param name="trans"></param>
        /// <param name="allRowVars"></param>
        /// <param name="allColVars"></param>
        /// <param name="yes"></param>
        /// <param name="maybe"></param>
        /// <param name="bound"></param>
        /// <returns></returns>
        public static CUDDNode ProbBoundedUntil(CUDDNode trans, CUDDVars allRowVars, CUDDVars allColVars, CUDDNode yes, CUDDNode maybe, int bound)
        {
            DateTime startTime = DateTime.Now;

            CUDDNode a, sol, tmp;

            CUDD.Ref(trans, maybe);
            a = CUDD.Function.Times(trans, maybe);

            CUDD.Ref(yes);
            sol = yes;

            for (int i = 1; i <= bound; i++)
            {
                tmp = CUDD.Matrix.MatrixMultiplyVector(a, sol, allRowVars, allColVars);

                CUDD.Ref(yes);
                tmp = CUDD.Function.Maximum(tmp, yes);

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

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

            //
            CUDD.Deref(a);

            return sol;
        }
Exemplo n.º 33
0
        public static CUDDNode NondetReachReward(CUDDNode trans, CUDDNode reach, CUDDNode stateReward, CUDDNode transReward, CUDDNode nondetMask, CUDDVars allRowVars, CUDDVars allColVars, CUDDVars nondetVars, CUDDNode infReward, CUDDNode maybeReward, bool min)
        {
            DateTime startTime = DateTime.Now;
            int numberOfIterations = 0;

            CUDDNode a, allReward, newNondetMask, sol, tmp;

            // filter out rows (goal states and infinity states) from matrix
            CUDD.Ref(trans, maybeReward);
            a = CUDD.Function.Times(trans, maybeReward);

            // also remove goal and infinity states from state rewards vector
            CUDD.Ref(stateReward, maybeReward);
            CUDDNode tempStateReward = CUDD.Function.Times(stateReward, maybeReward);

            // multiply transition rewards by transition probs and sum rows
            // (note also filters out unwanted states at the same time)
            CUDD.Ref(transReward, a);
            CUDDNode tempTransReward = CUDD.Function.Times(transReward, a);
            tempTransReward = CUDD.Abstract.SumAbstract(tempTransReward, allColVars);

            // combine state and transition rewards
            allReward = CUDD.Function.Plus(tempStateReward, tempTransReward);

            // need to change mask because rewards are not necessarily in the range 0..1
            CUDD.Ref(nondetMask);
            newNondetMask = CUDD.Function.ITE(nondetMask, CUDD.PlusInfinity(), CUDD.Constant(0));

            // initial solution is infinity in 'inf' states, zero elsewhere
            // note: ok to do this because cudd matrix-multiply (and other ops)
            // treat 0 * inf as 0, unlike in IEEE 754 rules
            CUDD.Ref(infReward);
            sol = CUDD.Function.ITE(infReward, CUDD.PlusInfinity(), CUDD.Constant(0));

            while(true)
            {
                numberOfIterations++;

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

                // add rewards
                CUDD.Ref(allReward);
                tmp = CUDD.Function.Plus(tmp, allReward);

                if(min)
                {
                    CUDD.Ref(newNondetMask);
                    tmp = CUDD.Function.Maximum(tmp, newNondetMask);
                    tmp = CUDD.Abstract.MinAbstract(tmp, nondetVars);
                }
                else
                {
                    tmp = CUDD.Abstract.MaxAbstract(tmp, nondetVars);
                }

                CUDD.Ref(infReward);
                tmp = CUDD.Function.ITE(infReward, CUDD.PlusInfinity(), tmp);

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

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

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

            //
            CUDD.Deref(a, allReward, newNondetMask);
            return sol;
        }
Exemplo n.º 34
0
        /// <summary>
        /// [ REFS: 'result', DEREFS: ]
        /// </summary>
        /// <param name="trans"></param>
        /// <param name="stateReward"></param>
        /// <param name="transReward"></param>
        /// <param name="allRowVars"></param>
        /// <param name="allColVars"></param>
        /// <param name="bound"></param>
        /// <returns></returns>
        public static CUDDNode ProbCumulReward(CUDDNode trans, CUDDNode stateReward, CUDDNode transReward, CUDDVars allRowVars, CUDDVars allColVars, int bound)
        {
            DateTime startTime = DateTime.Now;

            // multiply transition rewards by transition probs and sum rows
            // then combine state and transition rewards and put in a vector
            CUDD.Ref(transReward, trans);
            CUDDNode allReward = CUDD.Function.Times(transReward, trans);

            allReward = CUDD.Abstract.SumAbstract(allReward, allColVars);

            CUDD.Ref(stateReward);
            allReward = CUDD.Function.Plus(stateReward, allReward);

            // initial solution is zero
            CUDDNode sol = CUDD.Constant(0);

            for (int i = 0; i < bound; i++)
            {
                CUDDNode tmp = CUDD.Matrix.MatrixMultiplyVector(trans, sol, allRowVars, allColVars);

                // add in (combined state and transition) rewards
                CUDD.Ref(allReward);
                tmp = CUDD.Function.Plus(tmp, allReward);

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

            //
            CUDD.Deref(allReward);

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

            //
            return sol;
        }
Exemplo n.º 35
0
 /// <summary>
 /// Print vector dd, suppos that all variables supporting dd belong to vars
 /// [ REFS: 'none', DEREFS: 'none' ]
 /// </summary>
 public static void PrintVector(CUDDNode dd, CUDDVars vars)
 {
     PlatformInvoke.DD_PrintVector(manager, dd.Ptr, vars.GetArrayPointer(), vars.GetNumVars(), LOW);
 }
Exemplo n.º 36
0
        /// <summary>
        /// [ REFS: 'result', DEREFS: ]
        /// </summary>
        /// <param name="trans"></param>
        /// <param name="stateReward"></param>
        /// <param name="allRowVars"></param>
        /// <param name="allColVars"></param>
        /// <param name="bound"></param>
        /// <returns></returns>
        public static CUDDNode ProbInstReward(CUDDNode trans, CUDDNode stateReward, CUDDVars allRowVars, CUDDVars allColVars, int bound)
        {
            DateTime startTime = DateTime.Now;

            // initial solution is the state rewards
            CUDD.Ref(stateReward);
            CUDDNode sol = stateReward;

            for (int iters = 0; iters < bound; iters++)
            {
                CUDDNode tmp = CUDD.Matrix.MatrixMultiplyVector(trans, sol, allRowVars, allColVars);
                CUDD.Deref(sol);
                sol = tmp;
            }

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

            return sol;
        }
Exemplo n.º 37
0
 /// <summary>
 /// Swap variables (i.e. x_i -> y_i and y_i -> x_i) of a node.
 /// [ REFS: 'result', DEREFS: dd ]
 /// </summary>
 public static CUDDNode SwapVariables(CUDDNode dd, CUDDVars oldVars, CUDDVars newVars)
 {
     return(new CUDDNode(PlatformInvoke.DD_SwapVariables(manager, dd.Ptr, oldVars.GetArrayPointer(), newVars.GetArrayPointer(), oldVars.GetNumVars())));
 }
Exemplo n.º 38
0
        /// <summary>
        /// [ REFS: 'result', DEREFS: ]
        /// </summary>
        /// <param name="trans"></param>
        /// <param name="stateReward"></param>
        /// <param name="transReward"></param>
        /// <param name="reach"></param>
        /// <param name="allRowVars"></param>
        /// <param name="allColVars"></param>
        /// <param name="maybe"></param>
        /// <param name="inf"></param>
        /// <returns></returns>
        public static CUDDNode ProbReachReward(CUDDNode trans, CUDDNode stateReward, CUDDNode transReward, CUDDNode reach, CUDDVars allRowVars, CUDDVars allColVars, CUDDNode maybe, CUDDNode inf)
        {
            CUDD.Ref(trans, maybe);
            CUDDNode a = CUDD.Function.Times(trans, maybe);

            CUDD.Ref(stateReward, maybe);
            CUDDNode newStateReward = CUDD.Function.Times(stateReward, maybe);

            CUDD.Ref(transReward, a);
            CUDDNode newTransRewards = CUDD.Function.Times(transReward, a);
            newTransRewards = CUDD.Abstract.SumAbstract(newTransRewards, allColVars);

            CUDDNode allReward = CUDD.Function.Plus(newStateReward, newTransRewards);

            //
            CUDDNode tmp = CUDD.Matrix.Identity(allRowVars, allColVars);
            CUDD.Ref(reach);
            tmp = CUDD.Function.And(tmp, reach);
            a = CUDD.Function.Minus(tmp, a);

            CUDDNode sol = Jacobi(a, allReward, allReward, reach, allRowVars, allColVars, 1);

            // set reward for infinity states to infinity
            CUDD.Ref(inf);
            sol = CUDD.Function.ITE(inf, CUDD.PlusInfinity(), sol);

            //
            CUDD.Deref(a, allReward);
            return sol;
        }
Exemplo n.º 39
0
 /// <summary>
 /// Generates BDD for the function x = y
 /// where x, y are num_vars-bit numbers encoded by variables x_vars, y_vars
 /// [ REFS: 'result', DEREFS: 'none' ]
 /// </summary>
 public static CUDDNode VariablesEquals(CUDDVars xVars, CUDDVars yVars)
 {
     return(new CUDDNode(PlatformInvoke.DD_VariablesEquals(manager, xVars.GetArrayPointer(), yVars.GetArrayPointer(), xVars.GetNumVars())));
 }
Exemplo n.º 40
0
        /// <summary>
        /// [ REFS: 'result', DEREFS: ]
        /// </summary>
        /// <param name="trans"></param>
        /// <param name="reach"></param>
        /// <param name="allRowVars"></param>
        /// <param name="allColVars"></param>
        /// <param name="yes"></param>
        /// <param name="maybe"></param>
        /// <returns></returns>
        public static CUDDNode ProbUntil(CUDDNode trans, CUDDNode reach, CUDDVars allRowVars, CUDDVars allColVars, CUDDNode yes, CUDDNode maybe)
        {
            CUDD.Ref(trans, maybe);
            CUDDNode a = CUDD.Function.Times(trans, maybe);

            CUDDNode tmp = CUDD.Matrix.Identity(allRowVars, allColVars);
            CUDD.Ref(reach);
            tmp = CUDD.Function.And(tmp, reach);

            a = CUDD.Function.Minus(tmp, a);

            CUDD.Ref(yes);
            CUDDNode b = yes;

            CUDDNode sol = Jacobi(a, b, b, reach, allRowVars, allColVars, 1);

            CUDD.Deref(a, b);
            return sol;
        }
Exemplo n.º 41
0
 /// <summary>
 /// Return the int value corresponding the binary representation of the current minterm
 /// [ REFS: 'none', DEREFS: 'none' ]
 /// </summary>
 public static int MinTermToInt(CUDDNode dd, CUDDVars vars)
 {
     return(PlatformInvoke.Cudd_MinTermToInt(manager, dd.Ptr, vars.GetArrayPointer(), vars.GetNumVars()));
 }
Exemplo n.º 42
0
 public static ODDNode BuildODD(CUDDNode reach, CUDDVars rowVars)
 {
     return(new ODDNode(PlatformInvoke._Z9build_oddP9DdManagerP6DdNodePS2_i(manager, reach.Ptr, rowVars.GetArrayPointer(), rowVars.GetNumVars())));
 }