Exemple #1
0
        private void GenerateBelievePssa()
        {
            _believeAffectedPredSet = new HashSet <Predicate>();

            foreach (var e in _believeEventArray)
            {
                _believeAffectedPredSet.UnionWith(e.AffectedPredicateSet);
            }

            BelievePartialSsa = CUDD.Constant(0);
            foreach (var e in _believeEventArray)
            {
                CUDDNode eventPssa = e.PartialSsa;
                CUDD.Ref(eventPssa);
                CUDD.Ref(e.Precondition);
                eventPssa = CUDD.Function.And(eventPssa, e.Precondition);

                foreach (var pred in _believeAffectedPredSet)
                {
                    if (!e.AffectedPredicateSet.Contains(pred))
                    {
                        CUDDNode prePredNode   = CUDD.Var(pred.PreviousCuddIndex);
                        CUDDNode sucPredNode   = CUDD.Var(pred.SuccessiveCuddIndex);
                        CUDDNode invariantNode = CUDD.Function.Equal(prePredNode, sucPredNode);
                        eventPssa = CUDD.Function.And(eventPssa, invariantNode);
                    }
                }

                BelievePartialSsa = CUDD.Function.Or(BelievePartialSsa, eventPssa);
            }
        }
Exemple #2
0
        private void GenerateGroundEffect(Dictionary <string, Ground <Predicate> > preGndPredDict)
        {
            CUDDVars oldVars = new CUDDVars();
            CUDDVars newVars = new CUDDVars();

            Dictionary <string, string> abstractParmMap = new Dictionary <string, string>();

            //Console.WriteLine("  Ground action constant list count:{0}", gndAction.ConstantList.Count);

            for (int i = 0; i < ConstantList.Count; i++)
            {
                string abstractParm = Container.VariableList[i].Item1;
                string gndParm      = ConstantList[i];
                abstractParmMap.Add(abstractParm, gndParm);
                //Console.WriteLine("    Parameter:{0}, constant:{1}", abstractParm, gndParm);
            }

            foreach (var pair in Container.AbstractPredicateDict)
            {
                oldVars.AddVar(CUDD.Var(pair.Value.CuddIndex));
                List <string> collection = new List <string>();
                foreach (var parm in pair.Value.ParameterList)
                {
                    collection.Add(abstractParmMap[parm]);
                }

                string             gndPredFullName = VariableContainer.GetFullName(pair.Value.Predicate.Name, collection);
                Ground <Predicate> gndPred         = preGndPredDict[gndPredFullName];
                newVars.AddVar(CUDD.Var(gndPred.CuddIndex));

                //Console.WriteLine("  old cuddIndex:{0}, new cuddIndex:{1}", pair.Value.CuddIndex, gndPred.CuddIndex);
            }

            foreach (var cEffect in Container.Effect)
            {
                CUDDNode abstractCondition = cEffect.Item1;
                CUDDNode gndCondition      = CUDD.Variable.SwapVariables(abstractCondition, oldVars, newVars);
                CUDD.Ref(gndCondition);

                var gndLiteralList      = new List <Tuple <Ground <Predicate>, bool> >();
                var abstractLiteralList = cEffect.Item2;
                foreach (var abstractLiteral in abstractLiteralList)
                {
                    List <string> collection = new List <string>();
                    foreach (var parm in abstractLiteral.Item1.ParameterList)
                    {
                        collection.Add(abstractParmMap[parm]);
                    }

                    string gndPredFullName = VariableContainer.GetFullName(abstractLiteral.Item1.Predicate.Name,
                                                                           collection);
                    Ground <Predicate> gndPred = preGndPredDict[gndPredFullName];
                    var gndLiteral             = new Tuple <Ground <Predicate>, bool>(gndPred, abstractLiteral.Item2);
                    gndLiteralList.Add(gndLiteral);
                }

                var gndCEffect = new Tuple <CUDDNode, List <Tuple <Ground <Predicate>, bool> > >(gndCondition, gndLiteralList);
                _effect.Add(gndCEffect);
            }
        }
Exemple #3
0
        public CUDDNode GetKnowledgeBase()
        {
            List <CUDDNode> literalNodes = new List <CUDDNode>();

            foreach (var gndPred in _predBooleanMap)
            {
                string   name  = gndPred.Key;
                int      index = _problem.GroundPredicateDict[name].CuddIndex;
                CUDDNode node;

                if (gndPred.Value)
                {
                    node = CUDD.Var(index);
                }
                else
                {
                    node = CUDD.Function.Not(CUDD.Var(index));
                }
                literalNodes.Add(node);
            }

            CUDDNode result = literalNodes[0];

            for (int i = 1; i < literalNodes.Count; i++)
            {
                CUDDNode literalNode = literalNodes[i];
                CUDDNode andNode     = CUDD.Function.And(result, literalNode);
                CUDD.Ref(andNode);
                CUDD.Deref(result);
                CUDD.Deref(literalNode);
                result = andNode;
            }
            return(result);
        }
Exemple #4
0
        private CUDDNode GetEffectNode()
        {
            _affectedPredicateSet = new HashSet <Predicate>();
            CUDDNode result = CUDD.ONE;

            CUDD.Ref(result);

            foreach (var cEffect in _condEffect)
            {
                var firstLiteral = cEffect.Item2[0];
                _affectedPredicateSet.Add(firstLiteral.Item1);
                CUDDNode predNode     = CUDD.Var(firstLiteral.Item1.SuccessiveCuddIndex);
                CUDDNode literalsNode = firstLiteral.Item2 ? predNode : CUDD.Function.Not(predNode);

                for (int i = 1; i < cEffect.Item2.Length; i++)
                {
                    var literal = cEffect.Item2[i];
                    _affectedPredicateSet.Add(literal.Item1);
                    predNode = CUDD.Var(literal.Item1.SuccessiveCuddIndex);
                    CUDDNode literalNode = literal.Item2 ? predNode : CUDD.Function.Not(predNode);
                    literalsNode = CUDD.Function.And(literalsNode, literalNode);
                }

                CUDD.Ref(cEffect.Item1);
                CUDDNode cEffectNode = CUDD.Function.Implies(cEffect.Item1, literalsNode);

                result = CUDD.Function.And(result, cEffectNode);
            }

            return(result);
        }
Exemple #5
0
        /// <summary>
        /// Add new not-global variable where row- and column- boolean variable are the same.
        /// This function is used to add BDD variables to encode event names
        /// </summary>
        /// <param name="name"></param>
        /// <param name="lower"></param>
        /// <param name="upper"></param>
        public void AddSingleCopyVar(string name, int lower, int upper)
        {
            varList.AddNewVariable(name, lower, upper);
            int numBits = varList.GetNumberOfBits(name);

            CUDDNode vr;
            CUDDVars rowVar = new CUDDVars();

            for (int j = 0; j < numBits; j++)
            {
                vr = CUDD.Var(numverOfBoolVars++);
                rowVar.AddVar(vr);
            }

            this.rowVars.Add(rowVar);
            this.colVars.Add(rowVar);

            this.AllRowVars.AddVars(rowVar);
            this.AllColVars.AddVars(rowVar);
            this.AllEventVars.AddVars(rowVar);

            // used for unchanged variable in transition.
            CUDDNode identity = (numBits == 0) ? CUDD.Constant(1) : CUDD.Matrix.Identity(rowVar, rowVar);

            this.varIdentities.Add(identity);

            //
            CUDDNode expressionDD = CUDD.MINUS_INFINITY; CUDD.Ref(expressionDD);

            for (int i = lower; i <= upper; i++)
            {
                expressionDD = CUDD.Matrix.SetVectorElement(expressionDD, rowVar, i - lower, i);
            }
            this.variableEncoding.Add(expressionDD);
        }
Exemple #6
0
        private CUDDNode GetPartialFrameNode()
        {
            CUDDNode result = CUDD.Constant(1);

            foreach (var predicate in _affectedPredicateSet)
            {
                CUDDNode frameCondition = CUDD.Constant(1);

                foreach (var cEffect in _condEffect)
                {
                    if (cEffect.Item2.Any(literal => literal.Item1.Equals(predicate)))
                    {
                        CUDD.Ref(cEffect.Item1);
                        CUDDNode negCondition = CUDD.Function.Not(cEffect.Item1);
                        frameCondition = CUDD.Function.And(frameCondition, negCondition);
                    }
                }

                CUDDNode prePredNode   = CUDD.Var(predicate.PreviousCuddIndex);
                CUDDNode sucPredNode   = CUDD.Var(predicate.SuccessiveCuddIndex);
                CUDDNode invariantNode = CUDD.Function.Equal(prePredNode, sucPredNode);

                CUDDNode frame = CUDD.Function.Implies(frameCondition, invariantNode);
                result = CUDD.Function.And(result, frame);
            }

            return(result);
        }
Exemple #7
0
        public IModel Encode()
        {
            //get variable info from ModulesFile
            varList = new VariableList(modules);

            //Collect all sync label of all modules
            synchs = modules.GetAllSynchs().ToList();

            AddVars();

            //Create Expression Encoder, use the same copy of varList, variableEncoding
            expressionEncoder = new ExpressionToBDD(varList, variableEncoding);

            EncodeSystemDef(modules.systemDef);

            // get rid of any nondet dd variables not needed
            if (modules.modelType == ModelType.MDP)
            {
                CUDDNode tmp = CUDD.GetSupport(trans);
                tmp = CUDD.Abstract.ThereExists(tmp, allRowVars);
                tmp = CUDD.Abstract.ThereExists(tmp, allColVars);

                CUDDVars ddv = new CUDDVars();
                while (!tmp.Equals(CUDD.ONE))
                {
                    ddv.AddVar(CUDD.Var(tmp.GetIndex()));
                    tmp = tmp.GetThen();
                }
                CUDD.Deref(tmp);
                allNondetVars.Deref();
                allNondetVars = ddv;
            }

            init = GetInitState();

            //
            CUDD.Deref(moduleRangeDDs, moduleIdentities, colVarRanges, syncVars, choiceVars);
            CUDD.Deref(moduleRowVars, moduleColVars, rowVars, colVars, new List <CUDDVars>()
            {
                globalRowVars, globalColVars, allSynchVars, allChoiceVars
            });

            IModel result;

            if (modules.modelType == ModelType.DTMC)
            {
                //
                allNondetVars.Deref();

                result = new ProbModel(trans, init, stateRewards, transRewards, allRowVars, allColVars, varList, allRowVarRanges,
                                       varIdentities, variableEncoding);
            }
            else
            {
                result = new NonDetModel(trans, init, stateRewards, transRewards, allRowVars, allColVars, allNondetVars,
                                         varList, allRowVarRanges, varIdentities, variableEncoding);
            }

            return(result);
        }
Exemple #8
0
        protected override CUDDNode GetCuddNode(PlanningParser.AtomicFormulaTermContext context)
        {
            ServerAbstractPredicate abstractPredicate = GetAbstractPredicate(context);
            int      index  = abstractPredicate.CuddIndex;
            CUDDNode result = CUDD.Var(index);

            return(result);
        }
        private static CUDDNode GetCuddNode(PlanningParser.TermEventFormContext context, IReadOnlyDictionary <string, Event> eventDict, StringDictionary assignment)
        {
            CUDDNode result;

            if (context.eventSymbol() != null)
            {
                string eventFullName = ConstContainer.GetFullName(context, assignment);
                if (eventDict.ContainsKey(eventFullName))
                {
                    Event e         = eventDict[eventFullName];
                    int   cuddIndex = e.CuddIndex;
                    result = CUDD.Var(cuddIndex);
                }
                else
                {
                    result = CUDD.ZERO;
                    CUDD.Ref(result);
                }
            }
            else
            {
                string firstTermString  = Globals.TermInterpreter.GetString(context.term(0), assignment);
                string secondTermString = Globals.TermInterpreter.GetString(context.term(1), assignment);
                if (context.EQ() != null)
                {
                    result = firstTermString == secondTermString ? CUDD.ONE : CUDD.ZERO;
                }
                else if (context.NEQ() != null)
                {
                    result = firstTermString != secondTermString ? CUDD.ONE : CUDD.ZERO;
                }
                else
                {
                    int firstValue  = int.Parse(firstTermString);
                    int secondValue = int.Parse(secondTermString);
                    if (context.LT() != null)
                    {
                        result = firstValue < secondValue ? CUDD.ONE : CUDD.ZERO;
                    }
                    else if (context.LEQ() != null)
                    {
                        result = firstValue <= secondValue ? CUDD.ONE : CUDD.ZERO;
                    }
                    else if (context.GT() != null)
                    {
                        result = firstValue > secondValue ? CUDD.ONE : CUDD.ZERO;
                    }
                    else
                    {
                        result = firstValue >= secondValue ? CUDD.ONE : CUDD.ZERO;
                    }
                }
                CUDD.Ref(result);
            }

            return(result);
        }
Exemple #10
0
        /// <summary>
        /// [ REFS: 'result', DEREFS: none ]
        /// </summary>
        /// <returns></returns>
        private CUDDNode GetKbNode()
        {
            //OK
            CUDDNode result = CUDD.Constant(1);

            foreach (var pair in _predBooleanMap)
            {
                string   name    = pair.Key;
                int      index   = _predicateDict[name].PreviousCuddIndex;
                CUDDNode literal = pair.Value ? CUDD.Var(index) : CUDD.Function.Not(CUDD.Var(index));
                result = CUDD.Function.And(result, literal);
            }
            return(result);
        }
Exemple #11
0
        private static CUDDNode GetCuddNode(this PlanningParser.TermAtomFormContext context,
                                            IReadOnlyDictionary <string, Predicate> predicateDict, StringDictionary assignment)
        {
            CUDDNode result;

            if (context.predicate() != null)
            {
                string    predicateFullName = ConstContainer.GetFullName(context, assignment);
                Predicate predicate         = predicateDict[predicateFullName];
                int       cuddIndex         = predicate.PreviousCuddIndex;
                result = CUDD.Var(cuddIndex);
            }
            else
            {
                string firstTermString  = Globals.TermInterpreter.GetString(context.term(0), assignment);
                string secondTermString = Globals.TermInterpreter.GetString(context.term(1), assignment);
                if (context.EQ() != null)
                {
                    result = firstTermString == secondTermString ? CUDD.ONE : CUDD.ZERO;
                }
                else if (context.NEQ() != null)
                {
                    result = firstTermString != secondTermString ? CUDD.ONE : CUDD.ZERO;
                }
                else
                {
                    int firstValue  = int.Parse(firstTermString);
                    int secondValue = int.Parse(secondTermString);
                    if (context.LT() != null)
                    {
                        result = firstValue < secondValue ? CUDD.ONE : CUDD.ZERO;
                    }
                    else if (context.LEQ() != null)
                    {
                        result = firstValue <= secondValue ? CUDD.ONE : CUDD.ZERO;
                    }
                    else if (context.GT() != null)
                    {
                        result = firstValue > secondValue ? CUDD.ONE : CUDD.ZERO;
                    }
                    else
                    {
                        result = firstValue >= secondValue ? CUDD.ONE : CUDD.ZERO;
                    }
                }
                CUDD.Ref(result);
            }

            return(result);
        }
Exemple #12
0
        /// <summary>
        /// Add new local variable
        /// </summary>
        /// <param name="name"></param>
        /// <param name="lower"></param>
        /// <param name="upper"></param>
        public void AddLocalVar(string name, int lower, int upper)
        {
            varList.AddNewVariable(name, lower, upper);
            int numBits = varList.GetNumberOfBits(name);

            CUDDNode vr, vc;
            CUDDVars rowVar = new CUDDVars();
            CUDDVars colVar = new CUDDVars();

            for (int j = 0; j < numBits; j++)
            {
                vr = CUDD.Var(numverOfBoolVars++);
                vc = CUDD.Var(numverOfBoolVars++);
                rowVar.AddVar(vr);
                colVar.AddVar(vc);
            }
            this.rowVars.Add(rowVar);
            this.colVars.Add(colVar);

            this.AllRowVars.AddVars(rowVar);
            this.AllRowVarsExceptSingleCopy.AddVars(rowVar);
            this.AllColVars.AddVars(colVar);

            // used for unchanged variable in transition.
            CUDDNode identity = CUDD.Constant(0);

            for (int i = lower; i <= upper; i++)
            {
                identity = CUDD.Matrix.SetMatrixElement(identity, rowVar, colVar, i - lower, i - lower, 1);
            }
            this.varIdentities.Add(identity);

            //
            CUDDNode expressionDD = CUDD.MINUS_INFINITY; CUDD.Ref(expressionDD);

            for (int i = lower; i <= upper; i++)
            {
                expressionDD = CUDD.Matrix.SetVectorElement(expressionDD, rowVar, i - lower, i);
            }
            this.variableEncoding.Add(expressionDD);

            //
            CUDD.Ref(identity);
            allRowVarRanges = CUDD.Function.And(allRowVarRanges, CUDD.Abstract.ThereExists(identity, colVar));

            CUDD.Ref(identity);
            colVarRanges.Add(CUDD.Abstract.ThereExists(identity, rowVar));
        }
Exemple #13
0
        private void UpdateKnowledge(EventModel eventModel)
        {
            //Console.WriteLine("Enter update knowledge");

            //CUDD.Ref(eventModel.KnowPrecondition);
            //Console.WriteLine("After ref know precondition");

            //CUDDNode knowledgeWithPre = CUDD.Function.And(Knowledge, eventModel.KnowPrecondition);
            CUDD.Ref(eventModel.KnowPartialSsa);
            //Console.WriteLine("After ref know partial ssa");

            CUDDNode knowledgeWithPssa = CUDD.Function.And(Knowledge, eventModel.KnowPartialSsa);

            //Console.WriteLine("After get knowledge with pre and partial ssa");

            if (eventModel.KnowAffectedPredSet.Count != 0)
            {
                CUDDVars oldVars = new CUDDVars();
                CUDDVars newVars = new CUDDVars();

                foreach (var predicate in eventModel.KnowAffectedPredSet)
                {
                    CUDDNode trueRestrictBy = CUDD.Var(predicate.PreviousCuddIndex);
                    CUDD.Ref(trueRestrictBy);
                    CUDD.Ref(knowledgeWithPssa);
                    CUDDNode trueNode        = CUDD.Function.Restrict(knowledgeWithPssa, trueRestrictBy);
                    CUDDNode falseRestrictBy = CUDD.Function.Not(trueRestrictBy);
                    CUDDNode falseNode       = CUDD.Function.Restrict(knowledgeWithPssa, falseRestrictBy);

                    knowledgeWithPssa = CUDD.Function.Or(trueNode, falseNode);

                    oldVars.AddVar(CUDD.Var(predicate.SuccessiveCuddIndex));
                    newVars.AddVar(CUDD.Var(predicate.PreviousCuddIndex));
                }

                Knowledge = CUDD.Variable.SwapVariables(knowledgeWithPssa, oldVars, newVars);

                oldVars.Deref();
                newVars.Deref();
            }
            else
            {
                Knowledge = knowledgeWithPssa;
            }
            //Console.WriteLine("Whether knowledge is equal to false: {0}", Knowledge.Equals(CUDD.ZERO));
            //Console.WriteLine("Finish!");
        }
Exemple #14
0
        public override void Execute()
        {
            List <Predicate> satifiedPredList = new List <Predicate>();

            foreach (var predicate in _predicateDict.Values)
            {
                if (predicate.Satisfy(_predicateName, _index, _scanArray))
                {
                    satifiedPredList.Add(predicate);
                }
            }

            CUDDNode orNode = CUDD.Constant(0);

            for (int i = 0; i < satifiedPredList.Count; i++)
            {
                CUDDNode predicate = CUDD.Var(satifiedPredList[i].PreviousCuddIndex);
                orNode = CUDD.Function.Or(orNode, predicate);
            }

            //ExclusiveAxiom = CUDD.Function.And(ExclusiveAxiom, orNode);

            CUDDNode exclusiveNode = CUDD.Constant(1);

            for (int i = 0; i < satifiedPredList.Count - 1; i++)
            {
                for (int j = i + 1; j < satifiedPredList.Count; j++)
                {
                    CUDDNode firstLiteral  = CUDD.Var(satifiedPredList[i].PreviousCuddIndex);
                    CUDDNode secondLiteral = CUDD.Var(satifiedPredList[j].PreviousCuddIndex);
                    CUDDNode bothNode      = CUDD.Function.And(firstLiteral, secondLiteral);
                    CUDDNode negBothNode   = CUDD.Function.Not(bothNode);
                    exclusiveNode = CUDD.Function.And(exclusiveNode, negBothNode);
                }
            }

            CUDDNode eachExclusiveAxiom = CUDD.Function.And(orNode, exclusiveNode);

            ExclusiveAxiom = CUDD.Function.And(ExclusiveAxiom, eachExclusiveAxiom);

            //Console.WriteLine("Enumerator's exclusive axiom:");
            //CUDD.Print.PrintMinterm(eachExclusiveAxiom);
            //Console.ReadLine();
        }
        public static Event[] ToEventCollection(this PlanningParser.GdEventContext context, IReadOnlyDictionary <string, Event> eventDict, StringDictionary assignment)
        {
            List <Event> eventList   = new List <Event>();
            CUDDNode     gdEventNode = GetCuddNode(context, eventDict, assignment);

            foreach (var e in eventDict.Values)
            {
                CUDDNode eventNode = CUDD.Var(e.CuddIndex);
                CUDD.Ref(gdEventNode);
                CUDDNode impliesNode = CUDD.Function.Implies(eventNode, gdEventNode);
                if (impliesNode.Equals(CUDD.ONE))
                {
                    eventList.Add(e);
                }
                CUDD.Deref(impliesNode);
            }
            CUDD.Deref(gdEventNode);

            //EventCollection result = new EventCollection(eventList);
            return(eventList.ToArray());
        }
Exemple #16
0
        private void GenerateGroundPrecondition(Dictionary <string, Ground <Predicate> > preGndPredDict)
        {
            CUDDVars oldVars = new CUDDVars();
            CUDDVars newVars = new CUDDVars();

            Dictionary <string, string> abstractParmMap = new Dictionary <string, string>();

            //Console.WriteLine("  Ground action constant list count:{0}", gndAction.ConstantList.Count);

            for (int i = 0; i < ConstantList.Count; i++)
            {
                string abstractParm = Container.VariableList[i].Item1;
                string gndParm      = ConstantList[i];
                abstractParmMap.Add(abstractParm, gndParm);
                //Console.WriteLine("    Parameter:{0}, constant:{1}", abstractParm, gndParm);
            }

            foreach (var pair in Container.AbstractPredicateDict)
            {
                oldVars.AddVar(CUDD.Var(pair.Value.CuddIndex));
                List <string> collection = new List <string>();
                foreach (var parm in pair.Value.ParameterList)
                {
                    collection.Add(abstractParmMap[parm]);
                }

                Ground <Predicate> gndPred = new Ground <Predicate>(pair.Value.Predicate, collection);
                gndPred = preGndPredDict[gndPred.ToString()];
                newVars.AddVar(CUDD.Var(gndPred.CuddIndex));
            }

            CUDDNode abstractPre = Container.Precondition;

            Precondition = CUDD.Variable.SwapVariables(abstractPre, oldVars, newVars);
            //Console.WriteLine("  Ground precondition:");
            //CUDD.Print.PrintMinterm(gndAction.Precondition);

            //CUDDNode abstractEff = gndAction.VariableContainer.Effect;
            //gndAction.VariableContainer.Effect = CUDD.Variable.SwapVariables(abstractEff, oldVars, newVars);
        }
Exemple #17
0
        private void GenerateKnowPssa()
        {
            if (MaxPlausibilityDegree == 0)
            {
                KnowPartialSsa       = BelievePartialSsa;
                _knowAffectedPredSet = _believeAffectedPredSet;
            }
            else
            {
                _knowAffectedPredSet = new HashSet <Predicate>();

                foreach (var e in _knowEventArray)
                {
                    _knowAffectedPredSet.UnionWith(e.AffectedPredicateSet);
                }

                KnowPartialSsa = CUDD.Constant(0);
                foreach (var e in _knowEventArray)
                {
                    CUDDNode eventPssa = e.PartialSsa;
                    CUDD.Ref(eventPssa);
                    CUDD.Ref(e.Precondition);
                    eventPssa = CUDD.Function.And(eventPssa, e.Precondition);

                    foreach (var pred in _knowAffectedPredSet)
                    {
                        if (!e.AffectedPredicateSet.Contains(pred))
                        {
                            CUDDNode prePredNode   = CUDD.Var(pred.PreviousCuddIndex);
                            CUDDNode sucPredNode   = CUDD.Var(pred.SuccessiveCuddIndex);
                            CUDDNode invariantNode = CUDD.Function.Equal(prePredNode, sucPredNode);
                            eventPssa = CUDD.Function.And(eventPssa, invariantNode);
                        }
                    }

                    KnowPartialSsa = CUDD.Function.Or(KnowPartialSsa, eventPssa);
                }
            }
        }
Exemple #18
0
        private void UpdateBelief(CUDDNode partialSsa, HashSet <Predicate> affectedPredSet)
        {
            //CUDD.Ref(precondition);
            //CUDDNode beliefWithPre = CUDD.Function.And(Belief, precondition);
            CUDD.Ref(partialSsa);
            CUDDNode beliefWithPssa = CUDD.Function.And(Belief, partialSsa);

            if (affectedPredSet.Count != 0)
            {
                CUDDVars oldVars = new CUDDVars();
                CUDDVars newVars = new CUDDVars();

                foreach (var predicate in affectedPredSet)
                {
                    CUDDNode trueRestrictBy = CUDD.Var(predicate.PreviousCuddIndex);
                    CUDD.Ref(trueRestrictBy);
                    CUDD.Ref(beliefWithPssa);
                    CUDDNode trueNode        = CUDD.Function.Restrict(beliefWithPssa, trueRestrictBy);
                    CUDDNode falseRestrictBy = CUDD.Function.Not(trueRestrictBy);
                    CUDDNode falseNode       = CUDD.Function.Restrict(beliefWithPssa, falseRestrictBy);
                    beliefWithPssa = CUDD.Function.Or(trueNode, falseNode);

                    oldVars.AddVar(CUDD.Var(predicate.SuccessiveCuddIndex));
                    newVars.AddVar(CUDD.Var(predicate.PreviousCuddIndex));
                }
                Belief = CUDD.Variable.SwapVariables(beliefWithPssa, oldVars, newVars);

                oldVars.Deref();
                newVars.Deref();
            }
            else
            {
                Belief = beliefWithPssa;
            }

            //Console.WriteLine("Whether belief is equal to false: {0}", Belief.Equals(CUDD.ZERO));
            //Console.WriteLine("Finish!");
        }
Exemple #19
0
        static void Test2()
        {
            //CUDDNode a, b, c;
            //CUDDVars vars;

            Console.WriteLine("\nTest program for CUDD\n====================");

            // initialise cudd
            CUDD.InitialiseCUDD(256, 256, 262144, 0.1);

            CUDDNode firstOne = CUDD.Constant(1);

            Console.WriteLine("initial one: {0}", CUDD.GetReference(firstOne));

            CUDDNode firstZero = CUDD.Constant(0);

            Console.WriteLine("initial zero: {0}", CUDD.GetReference(firstZero));

            CUDDNode x0 = CUDD.Var(0);
            CUDDNode x1 = CUDD.Var(1);

            Console.WriteLine("var({0}): {1}", 0, CUDD.GetReference(x0));
            Console.WriteLine("var({0}): {1}", 1, CUDD.GetReference(x1));
            CUDDNode andNode = CUDD.Function.And(x0, x1);

            //CUDD.Ref(andNode);
            Console.WriteLine("var({0}): {1}", 0, CUDD.GetReference(x0));
            Console.WriteLine("var({0}): {1}", 1, CUDD.GetReference(x1));
            Console.WriteLine("and node: {0}", CUDD.GetReference(andNode));
            //Console.WriteLine(CUDD.GetReference(andNode));

            //CUDDNode one = CUDD.Constant(1);
            //Console.WriteLine("initial one: {0}", CUDD.GetReference(one));

            //CUDDNode f, var, tmp;
            //f = CUDD.Constant(1);
            //Console.WriteLine("initial f: {0}", CUDD.GetReference(f));
            //CUDD.Ref(f);
            //Console.WriteLine("after ref f: {0}", CUDD.GetReference(f));

            //for (int i = 3; i >= 0; i--)
            //{
            //    Console.WriteLine();
            //    Console.WriteLine(i);
            //    var = CUDD.Var(i);
            //    Console.WriteLine("Before and var({0}): {1}", i, CUDD.GetReference(var));
            //    Console.WriteLine();

            //    tmp = CUDD.Function.And(CUDD.Function.Not(var), f);
            //    Console.WriteLine("After and var({0}): {1}", i, CUDD.GetReference(var));
            //    Console.WriteLine("After and f: {0}", CUDD.GetReference(f));
            //    Console.WriteLine("After and tmp: {0}", CUDD.GetReference(tmp));
            //    Console.WriteLine();

            //    CUDD.Ref(tmp);
            //    Console.WriteLine("After ref tmp: {0}", CUDD.GetReference(tmp));
            //    Console.WriteLine();

            //    CUDD.Deref(f);
            //    Console.WriteLine("After deref f: {0}", CUDD.GetReference(f));
            //    Console.WriteLine();

            //    f = tmp;
            //    Console.WriteLine("After assignment f: {0}", CUDD.GetReference(f));
            //    Console.WriteLine("After assignment tmp: {0}", CUDD.GetReference(tmp));

            //}



            //CUDDNode x0 = CUDD.Var(0);
            ////CUDDNode notx0 = CUDD.Function.Not(CUDD.Var(0));
            ////Console.WriteLine("notx0: {0}", CUDD.GetReference(notx0));
            //CUDDNode x1 = CUDD.Var(1);
            //CUDDNode x2 = CUDD.Var(2);
            //CUDDNode x3 = CUDD.Var(3);


            ////CUDD.Ref(x1);

            //Console.WriteLine("x0: {0}", CUDD.GetReference(x0));
            //Console.WriteLine("x1: {0}", CUDD.GetReference(x1));
            //Console.WriteLine("x2: {0}", CUDD.GetReference(x2));
            //Console.WriteLine("x3: {0}", CUDD.GetReference(x3));
            //Console.WriteLine();


            //CUDDNode andNode1 = CUDD.Function.And(x0, x1);
            //CUDD.Ref(andNode1);
            //Console.WriteLine("x0: {0}", CUDD.GetReference(x0));
            //Console.WriteLine("x1: {0}", CUDD.GetReference(x1));
            //Console.WriteLine("andNode1: {0}", CUDD.GetReference(andNode1));
            //Console.WriteLine();


            //CUDDNode andNode2 = CUDD.Function.And(x2, x3);
            //CUDD.Ref(andNode2);
            ////CUDD.Deref(orNode);
            //Console.WriteLine("x2: {0}", CUDD.GetReference(x2));
            //Console.WriteLine("x3: {0}", CUDD.GetReference(x3));
            //Console.WriteLine("andNode2: {0}", CUDD.GetReference(andNode2));
            //Console.WriteLine();


            //CUDDNode orNode = CUDD.Function.Or(andNode2, andNode1);
            //CUDD.Ref(orNode);
            //CUDD.Deref(andNode1);
            //CUDD.Deref(andNode2);


            //Console.WriteLine("x0: {0}", CUDD.GetReference(x0));
            //Console.WriteLine("x1: {0}", CUDD.GetReference(x1));
            //Console.WriteLine("x2: {0}", CUDD.GetReference(x2));
            //Console.WriteLine("x3: {0}", CUDD.GetReference(x3));
            //Console.WriteLine("andNode1: {0}", CUDD.GetReference(andNode1));
            //Console.WriteLine("andNode2: {0}", CUDD.GetReference(andNode2));
            //Console.WriteLine("orNode: {0}", CUDD.GetReference(orNode));



            //CUDD.Deref(orNode);
            //Console.WriteLine("andNode: {0}", CUDD.GetReference(andNode));
            //Console.WriteLine("orNode: {0}", CUDD.GetReference(orNode));
            //Console.WriteLine("x2: {0}", CUDD.GetReference(x2));

            //CUDD.Deref(orNode);
            //Console.WriteLine("andNode: {0}", CUDD.GetReference(andNode));
            //Console.WriteLine("orNode: {0}", CUDD.GetReference(orNode));
            //Console.WriteLine("x2: {0}", CUDD.GetReference(x2));

            //CUDD.Deref(orNode);
            //Console.WriteLine("andNode: {0}", CUDD.GetReference(andNode));
            //Console.WriteLine("orNode: {0}", CUDD.GetReference(orNode));
            //Console.WriteLine("x2: {0}", CUDD.GetReference(x2));



            //CUDDNode orNode = CUDD.Function.Or(x0, x1);

            //Console.WriteLine("x0: {0}", CUDD.GetReference(x0));
            //Console.WriteLine("x1: {0}", CUDD.GetReference(x1));
            //Console.WriteLine("orNode: {0}", CUDD.GetReference(orNode));

            //CUDDNode impliesNode = CUDD.Function.Implies(x0, x1);

            //Console.WriteLine("x0: {0}", CUDD.GetReference(x0));
            //Console.WriteLine("x1: {0}", CUDD.GetReference(x1));
            //Console.WriteLine("impliesNode: {0}", CUDD.GetReference(impliesNode));

            //CUDDNode f = CUDD.ZERO;
            //CUDDNode var, tmp;
            //CUDD.Ref(f);
            //int count = 1000;

            //CUDD.Var(count);
            //for (int i = count; i >= 0; i--)
            ////for (int i = 0; i <= count; i++)
            //{
            //    var = CUDD.Var(i);
            //    //if (i % 6 == 0)
            //    //{
            //        tmp = CUDD.Function.Or(var, f);
            //    //}
            //    //else
            //    //{
            //    //    tmp = CUDD.Function.And(var, f);
            //    //}
            //    CUDD.Ref(tmp);
            //    CUDD.Deref(f);

            //    f = tmp;
            //    if (i % 1000 == 0)
            //    {
            //        Console.WriteLine("Index: {0}", i);
            //        Console.WriteLine("Memory: {0} Kbytes", CUDD.ReadMemoryInUse() / 1024);
            //        //Console.WriteLine("Number of nodes: {0}", CUDD.GetNumNodes(f));
            //        Console.WriteLine();
            //        CUDD.Debug.DebugCheck();
            //    }

            //}

            //CUDD.Print.PrintMinterm(f);

            //CUDDNode x0 = CUDD.Var(0);
            //CUDDNode x1 = CUDD.Var(1);
            //CUDDNode x2 = CUDD.Var(2);

            //CUDDNode effect = CUDD.Function.Implies(x2, x1);
            //CUDDNode frame = CUDD.Function.Implies(CUDD.Function.Not(x2), CUDD.Function.Equal(x0, x1));
            //CUDDNode firstSsa = CUDD.Function.And(effect, frame);

            //CUDDNode secondSsa = CUDD.Function.Equal(x1, CUDD.Function.Or(x2, x0));

            //CUDD.Print.PrintMinterm(firstSsa);
            //CUDD.Print.PrintMinterm(secondSsa);


            //CUDDNode xor = CUDD.Function.Xor(x0, x1);
            //CUDD.Ref(xor);

            //CUDDNode and1 = CUDD.Function.And(x0, CUDD.Function.Not(x1));
            //CUDD.Ref(and1);
            //CUDDNode and2 = CUDD.Function.And(x0, x1);
            //CUDD.Ref(and2);
            //CUDDNode sum = CUDD.Function.Or(and1, and2);
            //CUDD.Ref(sum);
            //CUDD.Deref(and1);
            //CUDD.Deref(and2);

            //CUDDNode and = CUDD.Function.And(x0, CUDD.Function.Not(x0));
            //CUDD.Ref(and);

            //CUDDNode or = CUDD.Function.Or(x0, CUDD.Function.Not(x0));
            //CUDD.Ref(or);

            ////CUDDNode node = CUDD.Function.Equal(and, CUDD.ZERO);
            //CUDDNode node = CUDD.Function.Equal(or, CUDD.ZERO);

            //Console.WriteLine(node.GetValue());

            //CUDDNode xor2 = CUDD.Function.Or()

            //CUDDNode and1 = CUDD.Function.And(x0, CUDD.Function.Not(x1));
            //CUDD.Ref(and1);

            //CUDDNode and2 = CUDD.Function.And(CUDD.Function.Not(x0), x1);
            //CUDD.Ref(and2);

            //CUDDNode sum = CUDD.Function.Or(and1, and2);
            //CUDD.Ref(sum);

            //CUDD.Deref(and1);
            //CUDD.Deref(and2);

            //CUDDNode carry = CUDD.Function.And(x0, x1);
            //CUDD.Ref(carry);

            //int size = 4;
            //CUDDNode[] restrictBy = new CUDDNode[4];
            //CUDDNode[] testSum = new CUDDNode[4];
            //CUDDNode[] testCarry = new CUDDNode[4];

            //restrictBy[0] = CUDD.Function.And(CUDD.Function.Not(x0), CUDD.Function.Not(x1));
            //restrictBy[1] = CUDD.Function.And(CUDD.Function.Not(x0), x1);
            //restrictBy[2] = CUDD.Function.And(x0, CUDD.Function.Not(x1));
            //restrictBy[3] = CUDD.Function.And(x0, x1);

            //for (int i = 0; i < size; i++)
            //{
            //    CUDD.Ref(restrictBy[i]);

            //    testSum[i] = CUDD.Function.Restrict(sum, restrictBy[i]);
            //    testCarry[i] = CUDD.Function.Restrict(carry, restrictBy[i]);
            //    CUDD.Deref(restrictBy[i]);
            //}

            //CUDD.Print.PrintBDDTree(testSum[0]);

            //for (int i = 0; i < size; i++)
            //{
            //    CUDD.Deref(testSum[i]);
            //    CUDD.Deref(testSum[i]);
            //}
        }
Exemple #20
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);
        }
        private void AllocateDDVars()
        {
            CUDDNode v;
            int      bitsForChoices = 0;

            if (modules.modelType == ModelType.MDP)
            {
                //TODO: Different Prism
                //we will have choice in each label, if there are many choice in a certain guard, then we need choice vars.
                //Note that difference module has difference choice variable for each common lable
                //Therefore for each synchorize lable, we need to know what is the current number of bit used for that choice in that lable
                //Suppose label a in module 1 has 3 choices therefore we need 2 bits 0, 1 for module 1
                //Then in module 2, suppose lablel a has 4 choices, then we will need 2 bits 2, 3 for module 2.
                bitsForChoices = (int)Math.Ceiling(modules.GetNumberOfModules() * Math.Log(((double)modules.GetNumberOfCommands()) / modules.GetNumberOfModules(), 2));
            }
            // module variable (row/col) vars
            for (int i = 0; i < varList.GetNumberOfVar(); i++)
            {
                rowVars.Add(new CUDDVars());
                colVars.Add(new CUDDVars());
            }

            // now allocate variables

            //which label to choose
            if (modules.modelType == ModelType.MDP)
            {
                //0 for empty label
                //1... for other labels
                int numberOfBitForAllLabels = (int)Math.Ceiling(Math.Log(synchs.Count + 1, 2));
                // allocate vars))
                for (int i = 0; i < numberOfBitForAllLabels; i++)
                {
                    v = CUDD.Var(numverOfBoolVars++);
                    syncVars.Add(v);
                }
            }

            //For choices in one label
            if (modules.modelType == ModelType.MDP)
            {
                for (int i = 0; i < bitsForChoices; i++)
                {
                    v = CUDD.Var(numverOfBoolVars++);
                    choiceVars.Add(v);
                }
            }


            // allocate dd variables for module variables (i.e. rows/cols)
            // go through all vars in order (incl. global variables)
            // so overall ordering can be specified by ordering in the input file
            for (int i = 0; i < varList.GetNumberOfVar(); i++)
            {
                // get number of dd variables needed
                // (ceiling of log2 of range of variable)
                int numberOfBits = varList.GetNumberOfBits(i);
                // add pairs of variables (row/col))
                for (int j = 0; j < numberOfBits; j++)
                {
                    // new dd row variable
                    CUDDNode vr = CUDD.Var(numverOfBoolVars++);
                    // new dd col variable
                    CUDDNode vc = CUDD.Var(numverOfBoolVars++);
                    rowVars[i].AddVar(vr);
                    colVars[i].AddVar(vc);
                }
            }
        }