/// <summary> /// Encode arithmetic expression whose returned value is integer /// </summary> /// <param name="model"></param> /// <returns></returns> public override ExpressionBDDEncoding TranslateIntExpToBDD(Model model) { if (IsBinaryExpression() && IsBinaryNumericalExpression()) { return(TranslateNumericalBinaryExpression(model)); } else if (IsBinaryExpression() && IsArrayExpression()) { return(TranslateArrayExpression(model)); } else if (!IsBinaryExpression() && this.Operator == NEGATIVE) { ExpressionBDDEncoding result = new ExpressionBDDEncoding(); ExpressionBDDEncoding operandBddEncoding = this.Argument1.TranslateIntExpToBDD(model); for (int i = 0; i < operandBddEncoding.Count(); i++) { result.GuardDDs.Add(operandBddEncoding.GuardDDs[i]); result.ExpressionDDs.Add(CUDD.Function.Minus(CUDD.Constant(0), operandBddEncoding.ExpressionDDs[i])); } return(result); } else { ExpressionBDDEncoding operandBddEncoding = this.Argument1.TranslateBoolExpToBDD(model); ExpressionBDDEncoding result = new ExpressionBDDEncoding(); for (int i = 0; i < operandBddEncoding.Count(); i++) { result.ExpressionDDs.Add(operandBddEncoding.GuardDDs[i]); result.GuardDDs.Add(CUDD.Constant(1)); } return(result); } }
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); }
public Model() { Model.tempVariableCount = 0; CUDD.InitialiseCUDD(2048 * 1024, Math.Pow(10, -15)); allRowVarRanges = CUDD.Constant(1); }
private void SortRanges() { // initialise raneg for whole system allRowVarRanges = CUDD.Constant(1); // variable ranges for (int i = 0; i < varList.GetNumberOfVar(); i++) { // obtain range dd by abstracting from identity matrix CUDD.Ref(varIdentities[i]); CUDDNode varRowRange = CUDD.Abstract.ThereExists(varIdentities[i], colVars[i]); // obtain range dd by abstracting from identity matrix CUDD.Ref(varIdentities[i]); colVarRanges.Add(CUDD.Abstract.ThereExists(varIdentities[i], rowVars[i])); // build up range for whole system as we go allRowVarRanges = CUDD.Function.And(allRowVarRanges, varRowRange); } // module ranges for (int i = 0; i < modules.GetNumberOfModules(); i++) { // obtain range dd by abstracting from identity matrix CUDD.Ref(moduleIdentities[i]); moduleRangeDDs.Add(CUDD.Abstract.ThereExists(moduleIdentities[i], moduleColVars[i])); } }
/// <summary> /// Only 1 expression of constant, guard ONE /// </summary> public override ExpressionBDDEncoding TranslateBoolExpToBDD(Model model) { ExpressionBDDEncoding result = new ExpressionBDDEncoding(); result.GuardDDs.Add(CUDD.Constant(1)); return(result); }
private void EncodeSimulationRel(BDDEncoder encoder, AutomataBDD processBDD, string stateVar) { int stateVarIndex = encoder.model.GetVarIndex(stateVar); CUDDVars rowStateVars = encoder.model.GetRowVars(stateVarIndex); CUDDVars colStateVars = encoder.model.GetColVars(stateVarIndex); CUDDNode result = CUDD.Constant(0); foreach (var pair in mapOldLoc2NewStates) { List <State> statesInLoc = pair.Value; for (int i = 0; i < statesInLoc.Count; i++) { for (int j = 0; j < statesInLoc.Count; j++) { if (ClockValuation.IsLUSimulated(mapNewState2Valuation[statesInLoc[i].ID], mapNewState2Valuation[statesInLoc[j].ID], pair.Key)) { CUDDNode simulated = CUDD.Matrix.SetVectorElement(CUDD.Constant(0), rowStateVars, encoder.stateIndexOfCurrentProcess[statesInLoc[i].ID], 1); CUDDNode simulating = CUDD.Matrix.SetVectorElement(CUDD.Constant(0), colStateVars, encoder.stateIndexOfCurrentProcess[statesInLoc[j].ID], 1); result = CUDD.Function.Or(result, CUDD.Function.And(simulated, simulating)); } } } } processBDD.SimulationRel = result; }
private void SortIdentities() { // variable identities for (int i = 0; i < varList.GetNumberOfVar(); i++) { // set each element of the identity matrix CUDDNode varIdentity = CUDD.Constant(0); int lower = varList.GetVarLow(i); int upper = varList.GetVarHigh(i); for (int j = lower; j <= upper; j++) { varIdentity = CUDD.Matrix.SetMatrixElement(varIdentity, rowVars[i], colVars[i], j - lower, j - lower, 1); } varIdentities.Add(varIdentity); } // module identities for (int i = 0; i < modules.GetNumberOfModules(); i++) { // product of identities for vars in module CUDDNode moduleIdentity = CUDD.Constant(1); for (int j = 0; j < varList.GetNumberOfVar(); j++) { if (varList.GetModuleIndex(j) == i) { CUDD.Ref(varIdentities[j]); moduleIdentity = CUDD.Function.And(moduleIdentity, varIdentities[j]); } } moduleIdentities.Add(moduleIdentity); } }
/// <summary> /// For each transition, check if the unchangedVariables is not updated in the transition /// then add the unchanged constraint to that transition /// [ REFS: 'result', DEREFS: 'transitions'] /// </summary> /// <param name="unchangedVariables">indexes of unchanged variables</param> /// <returns>New list of transitions</returns> public List <CUDDNode> AddVarUnchangedConstraint(List <CUDDNode> transitions, List <int> unchangedVariables) { if (unchangedVariables.Count == 0) { return(transitions); } else { List <CUDDNode> result = new List <CUDDNode>(); foreach (CUDDNode transition in transitions) { List <int> updatedVariableIndexes = this.GetColSupportedVars(transition); CUDDNode unchangedUpdates = CUDD.Constant(1); foreach (int k in unchangedVariables) { if (this.IsUnchangedVariable(k, updatedVariableIndexes)) { //if current variable is not updated CUDD.Ref(this.varIdentities[k]); unchangedUpdates = CUDD.Function.And(unchangedUpdates, this.varIdentities[k]); } } CUDDNode newTransition = CUDD.Function.And(transition, unchangedUpdates); // result.Add(newTransition); } // return(result); } }
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); } }
/// <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); }
/// <summary> /// Forward search, check destination is reachable from source /// [ REFS: '', DEREFS:] /// </summary> /// <param name="source"></param> /// <param name="destination"></param> /// <param name="transitions">transitions[0]: normal transitions, transitions[1]: priority transition</param> /// <returns></returns> public bool PathForward(CUDDNode source, CUDDNode destination, List <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 allReachableFromInit, currentReachableFromInit; CUDD.Ref(source, source); allReachableFromInit = currentReachableFromInit = source; CUDDNode commonNode = CUDD.Constant(0); do { //forward currentReachableFromInit = this.Successors(currentReachableFromInit, transitions); //Check 2 directions have intersection CUDD.Ref(destination, currentReachableFromInit); CUDD.Deref(commonNode); commonNode = CUDD.Function.And(destination, currentReachableFromInit); if (!commonNode.Equals(CUDD.ZERO)) { reachable = true; break; } //find fixpoint CUDD.Ref(currentReachableFromInit, allReachableFromInit); CUDDNode allReachabeFromInitTemp = CUDD.Function.Or(currentReachableFromInit, allReachableFromInit); if (allReachabeFromInitTemp.Equals(allReachableFromInit)) { //reachable = false; CUDD.Deref(allReachabeFromInitTemp); break; } else { currentReachableFromInit = CUDD.Function.Different(currentReachableFromInit, allReachableFromInit); allReachableFromInit = allReachabeFromInitTemp; } } while (true); CUDD.Deref(allReachableFromInit, currentReachableFromInit, commonNode); // return(reachable); }
/// <summary> /// return all states reachable from start /// P ◦ R+ /// [ REFS: 'result', DEREFS:start] /// </summary> /// <param name="start"></param> /// <param name="transitions">transitions[0]: normal transitions, transitions[1]: priority transition</param> /// <returns></returns> public CUDDNode SuccessorsPlus(CUDDNode start, List <List <CUDDNode> > transition) { CUDDNode allReachableStates = CUDD.Constant(0); CUDDNode currentStep = start; while (true) { currentStep = Successors(currentStep, transition); CUDD.Ref(allReachableStates, currentStep); CUDDNode allReachableTemp = CUDD.Function.Or(allReachableStates, currentStep); if (allReachableTemp.Equals(allReachableStates)) { CUDD.Deref(currentStep, allReachableTemp); return(allReachableStates); } else { CUDD.Deref(allReachableStates); allReachableStates = allReachableTemp; } } }
/// <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">transitions[0]: normal transitions, transitions[1]: priority transition</param> /// <returns></returns> public bool PathBackWard(CUDDNode source, CUDDNode destination, List <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 commonNode = CUDD.Constant(0); do { //backward currentReachableToGoal = this.Predecessors(currentReachableToGoal, transitions); //Check 2 directions have intersection CUDD.Ref(currentReachableToGoal, source); CUDD.Deref(commonNode); commonNode = CUDD.Function.And(currentReachableToGoal, source); if (!commonNode.Equals(CUDD.ZERO)) { reachable = true; break; } //find fixpoint CUDD.Ref(currentReachableToGoal, allReachabeToGoal); CUDDNode allReachabeToGoalTemp = CUDD.Function.Or(allReachabeToGoal, currentReachableToGoal); if (allReachabeToGoalTemp.Equals(allReachabeToGoal)) { reachable = false; CUDD.Deref(allReachabeToGoalTemp); break; } else { currentReachableToGoal = CUDD.Function.Different(currentReachableToGoal, allReachabeToGoal); allReachabeToGoal = allReachabeToGoalTemp; } } while (true); CUDD.Deref(allReachabeToGoal, currentReachableToGoal, commonNode); // return(reachable); }
public ExclusiveAxiomEnumerator(string predicateName, IDictionary <string, Predicate> predicateDict, IReadOnlyList <IList <string> > collection) : base(collection) { //Console.WriteLine("Enter ExclusiveAxiomEnumerator's constructor"); //Console.ReadLine(); _predicateName = predicateName; ExclusiveAxiom = CUDD.Constant(1); _predicateDict = predicateDict; }
private List <CUDDNode> EncodeTransitionBA(BA.Transition transition, Dictionary <string, Expression> declarationDatabase, string processVariableName) { // Expression transitionDD = Expression.EQ(new Variable(processVariableName), new IntConstant(this.stateIndexOfCurrentProcess[transition.FromState])); transitionDD = Expression.AND(transitionDD, new Assignment(processVariableName, new IntConstant(this.stateIndexOfCurrentProcess[transition.ToState]))); List <CUDDNode> transBDD = new List <CUDDNode> { CUDD.Constant(1) }; foreach (Proposition label in transition.labels) { List <CUDDNode> temp; if (!label.IsSigmal) { //label is a propostion if (declarationDatabase != null && declarationDatabase.ContainsKey(label.Label)) { Expression proposition = (label.Negated) ? Expression.NOT(declarationDatabase[label.Label]) : declarationDatabase[label.Label]; temp = EncodeProposition(transitionDD, proposition); } else { //label is an event //Because we mark event with update with # at the begininig //At this step, we don't know whether the event has update, therefore we check both state Expression eventExpression = Expression.OR( GetEventExpression(label.Label), GetEventExpression(Model.NAME_SEPERATOR + label.Label)); if (label.Negated) { eventExpression = Expression.NOT(eventExpression); } transitionDD = Expression.AND(transitionDD, eventExpression); temp = transitionDD.TranslateBoolExpToBDD(model).GuardDDs; } } else { temp = transitionDD.TranslateBoolExpToBDD(model).GuardDDs; } transBDD = CUDD.Function.And(transBDD, temp); } // return(transBDD); }
/// <summary> /// Forward search, check destination is reachable from source /// (S x (Tick + Trans)*) /// [ REFS: '', DEREFS:] /// </summary> /// <param name="source"></param> /// <param name="destination"></param> /// <param name="transitions"></param> /// <returns></returns> public bool PathForward3(CUDDNode source, CUDDNode destination, List <CUDDNode> transitions) { // bool reachable = false; CUDDNode allReachableFromInit, currentReachableFromInit; CUDD.Ref(source, source); allReachableFromInit = currentReachableFromInit = source; CUDDNode commonNode = CUDD.Constant(0); int numberOfLoop = 0; do { numberOfLoop++; Debug.Write(numberOfLoop + " "); currentReachableFromInit = Successors(currentReachableFromInit, transitions); //Check 2 directions have intersection CUDD.Ref(destination, currentReachableFromInit); CUDD.Deref(commonNode); commonNode = CUDD.Function.And(destination, currentReachableFromInit); if (!commonNode.Equals(CUDD.ZERO)) { reachable = true; break; } //find fixpoint CUDD.Ref(currentReachableFromInit, allReachableFromInit); CUDDNode allReachabeFromInitTemp = CUDD.Function.Or(currentReachableFromInit, allReachableFromInit); if (allReachabeFromInitTemp.Equals(allReachableFromInit)) { //reachable = false; CUDD.Deref(allReachabeFromInitTemp); break; } else { currentReachableFromInit = CUDD.Function.Different(currentReachableFromInit, allReachableFromInit); allReachableFromInit = allReachabeFromInitTemp; } } while (true); Debug.WriteLine("\nTA Path Forward 3: " + numberOfLoop + " loops."); CUDD.Deref(allReachableFromInit, currentReachableFromInit, commonNode); // return(reachable); }
public CUDDNode ComputeReachReward(CUDDNode b, int rewardStructIndex) { //Reference to b in the calling place and create a new copy CUDD.Ref(b, reach); b = CUDD.Function.And(b, reach); // CUDDNode inf, maybe, reward; if (b.Equals(CUDD.ZERO)) { CUDD.Ref(reach); inf = reach; maybe = CUDD.Constant(0); } else if (b.Equals(reach)) { inf = CUDD.Constant(0); maybe = CUDD.Constant(0); } else { CUDDNode no = ProbAlgo.Prob0(trans01, reach, allRowVars, allColVars, reach, b); CUDDNode prob1 = ProbAlgo.Prob1(trans01, reach, allRowVars, allColVars, reach, b, no); CUDD.Deref(no); CUDD.Ref(reach); inf = CUDD.Function.And(reach, CUDD.Function.Not(prob1)); CUDD.Ref(reach, inf, b); maybe = CUDD.Function.And(reach, CUDD.Function.Not(CUDD.Function.Or(inf, b))); } // print out yes/no/maybe Debug.WriteLine("goal = " + CUDD.GetNumMinterms(b, allRowVars.GetNumVars())); Debug.WriteLine("inf = " + CUDD.GetNumMinterms(inf, allRowVars.GetNumVars())); Debug.WriteLine("maybe = " + CUDD.GetNumMinterms(maybe, allRowVars.GetNumVars())); if (maybe.Equals(CUDD.ZERO)) { CUDD.Ref(inf); reward = CUDD.Function.ITE(inf, CUDD.PlusInfinity(), CUDD.Constant(0)); } else { reward = ProbAlgo.ProbReachReward(trans, stateRewards[rewardStructIndex], transRewards[rewardStructIndex], reach, allRowVars, allColVars, maybe, inf); } CUDD.Deref(inf, maybe, b); CUDD.Ref(start); return(CUDD.Function.Times(reward, start)); }
/// <summary> /// Encode all commands whose labels are the same as labelToBeEncoded /// </summary> /// <param name="moduleIndex"></param> /// <param name="module"></param> /// <param name="labelToBeEncoded"></param> private ComponentDDs EncodeModule(int moduleIndex, Module module, string labelToBeEncoded, int currentAvailStartBit) { //Store guard, and the final encoding of each command List <CUDDNode> guardDDs = new List <CUDDNode>(); List <CUDDNode> commandDDs = new List <CUDDNode>(); for (int i = 0; i < module.Commands.Count; i++) { Command command = module.Commands[i]; if (command.Synch == labelToBeEncoded) { CUDDNode guardDD = EncodeExpression(command.Guard); CUDD.Ref(allRowVarRanges); guardDD = CUDD.Function.And(guardDD, allRowVarRanges); if (guardDD.Equals(CUDD.ZERO)) { Debug.WriteLine("Warning: Guard " + command.Guard + " for command " + (i + 1) + " of module \"" + module.Name + "\" is never satisfied.\n"); guardDDs.Add(guardDD); commandDDs.Add(CUDD.Constant(0)); } else { CUDDNode updateDD = EncodeCommand(moduleIndex, command.Updates, guardDD); guardDDs.Add(guardDD); commandDDs.Add(updateDD); } } else { guardDDs.Add(CUDD.Constant(0)); commandDDs.Add(CUDD.Constant(0)); } } ComponentDDs result; if (modules.modelType == ModelType.DTMC) { result = CombineProbCommands(moduleIndex, guardDDs, commandDDs); } else if (modules.modelType == ModelType.MDP) { result = CombineNondetCommands(guardDDs, commandDDs, currentAvailStartBit); } else { throw new Exception("Unknown model type"); } return(result); }
/// <summary> /// Forward search, check destination is reachable from source /// Store all reachable states and check fix point /// At step t, find all reachable states after t time units /// Fix point: rechable(0, t) == rechable(0, t + 1) /// [ REFS: '', DEREFS:] /// </summary> /// <param name="source"></param> /// <param name="destination"></param> /// <param name="discreteTransitions"></param> /// <returns></returns> public bool PathForward1(CUDDNode source, CUDDNode destination, List <CUDDNode> discreteTransitions, List <CUDDNode> tickTransitions) { // bool reachable = false; CUDD.Ref(source); CUDDNode allReachableFromInit = SuccessorsStart(source, discreteTransitions); CUDD.Ref(allReachableFromInit); CUDDNode currentReachableFromInit = allReachableFromInit; CUDDNode commonNode = CUDD.Constant(0); do { Debug.WriteLine("Successors: " + numberOfBDDOperation++); currentReachableFromInit = Successors(currentReachableFromInit, tickTransitions); currentReachableFromInit = SuccessorsStart(currentReachableFromInit, discreteTransitions); //Check 2 directions have intersection CUDD.Ref(destination, currentReachableFromInit); CUDD.Deref(commonNode); commonNode = CUDD.Function.And(destination, currentReachableFromInit); if (!commonNode.Equals(CUDD.ZERO)) { reachable = true; break; } //find fixpoint CUDD.Ref(currentReachableFromInit, allReachableFromInit); CUDDNode allReachabeFromInitTemp = CUDD.Function.Or(currentReachableFromInit, allReachableFromInit); if (allReachabeFromInitTemp.Equals(allReachableFromInit)) { //reachable = false; CUDD.Deref(allReachabeFromInitTemp); break; } else { currentReachableFromInit = CUDD.Function.Different(currentReachableFromInit, allReachableFromInit); allReachableFromInit = allReachabeFromInitTemp; } } while (true); CUDD.Deref(allReachableFromInit, currentReachableFromInit, commonNode); // return(reachable); }
/// <summary> /// Forward search, check destination is reachable from source /// NOT store all reachable states, use some special condition to terminate /// At step t, find all reachable states after t time units /// Fix point when rechable(t1 + a, t1 + a) is a subset of rechable(t1, t1) /// Rabbit algorithm (S x Tick x Trans*)* /// [ REFS: '', DEREFS:] /// </summary> /// <param name="source"></param> /// <param name="destination"></param> /// <param name="discreteTransitions"></param> /// <returns></returns> public bool PathForward2(CUDDNode source, CUDDNode destination, List <CUDDNode> discreteTransitions, List <CUDDNode> tickTransitions) { // bool reachable = false; CUDD.Ref(source); CUDDNode currentReachableFromInit = SuccessorsStart(source, discreteTransitions); CUDDNode previousReachableFromInit = CUDD.Constant(0); int s, p; s = p = 0; CUDDNode commonNode = CUDD.Constant(0); int numberOfLoop = 0; while (!CUDD.IsSubSet(previousReachableFromInit, currentReachableFromInit)) { numberOfLoop++; Debug.Write(numberOfLoop + " "); if (p <= s / 2) { p = s; CUDD.Deref(previousReachableFromInit); CUDD.Ref(currentReachableFromInit); previousReachableFromInit = currentReachableFromInit; } currentReachableFromInit = Successors(currentReachableFromInit, tickTransitions); currentReachableFromInit = SuccessorsStart(currentReachableFromInit, discreteTransitions); s++; //Check 2 directions have intersection CUDD.Ref(destination, currentReachableFromInit); CUDD.Deref(commonNode); commonNode = CUDD.Function.And(destination, currentReachableFromInit); if (!commonNode.Equals(CUDD.ZERO)) { reachable = true; break; } } Debug.WriteLine("\nTA Path Forward 2: " + numberOfLoop + " loops."); CUDD.Deref(currentReachableFromInit, previousReachableFromInit, commonNode); // return(reachable); }
private void HandlePredicateDefine(PlanningParser.PredicateDefineContext context) { _exclusiveAxiom = CUDD.Constant(1); _predicateDict = new Dictionary <string, Predicate>(); foreach (var atomFormSkeleton in context.atomFormSkeleton()) { PredicateEnumerator enumerator = new PredicateEnumerator(atomFormSkeleton, _predicateDict, _currentCuddIndex); Algorithms.IterativeScanMixedRadix(enumerator); _currentCuddIndex = enumerator.CurrentCuddIndex; if (atomFormSkeleton.oneofDefine() != null) { Console.WriteLine("Predicate {0} have oneOf", atomFormSkeleton.predicate().GetText()); //Console.WriteLine("Hello!"); //Console.ReadLine(); string predicateName = atomFormSkeleton.predicate().GetText(); int oneOfVarsCount = atomFormSkeleton.oneofDefine().VAR().Count; string[] oneOfVarNameArray = new string[oneOfVarsCount]; var varNameList = atomFormSkeleton.listVariable().GetVariableNameList(); var collection = atomFormSkeleton.listVariable().GetCollection(); IList <string>[] notOneOfVarCollection = new IList <string> [varNameList.Count - oneOfVarsCount]; Parallel.For(0, oneOfVarsCount, i => oneOfVarNameArray[i] = atomFormSkeleton.oneofDefine().VAR(i).GetText()); for (int i = 0, k = 0; i < varNameList.Count; i++) { bool exists = oneOfVarNameArray.Any(s => s == varNameList[i]); if (!exists) { notOneOfVarCollection[k] = collection[i]; k++; if (k == notOneOfVarCollection.Length) { break; } } } ExclusiveAxiomEnumerator exclusiveAxiomEnumerator = new ExclusiveAxiomEnumerator(predicateName, _predicateDict, notOneOfVarCollection); Algorithms.IterativeScanMixedRadix(exclusiveAxiomEnumerator); _exclusiveAxiom = CUDD.Function.And(_exclusiveAxiom, exclusiveAxiomEnumerator.ExclusiveAxiom); } } //foreach (var predicate in _predicateDict.Values) //{ // Console.WriteLine("name: {0}, Previous index: {1}, successive index: {2}", predicate.FullName, predicate.PreviousCuddIndex, predicate.SuccessiveCuddIndex); //} }
/// <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); }
/// <summary> /// The result already contains the guard of this command. In other words, this is the encoding of the whole command, not just only update part. /// [ REFS: 'result', DEREFS: '' ] /// </summary> /// <param name="moduleIndex"></param> /// <param name="updates"></param> /// <param name="guardDD"></param> /// <returns></returns> private CUDDNode EncodeCommand(int moduleIndex, List <Update> updates, CUDDNode guardDD) { CUDDNode result = CUDD.Constant(0); foreach (var update in updates) { CUDD.Ref(guardDD); CUDDNode updateDD = EncodeUpdate(moduleIndex, update, guardDD); result = CUDD.Function.Plus(result, updateDD); } return(result); }
/// /// <summary> /// Use when the boolen constant is used as expression: a = true /// </summary> /// <param name="model"></param> /// <returns></returns> public override ExpressionBDDEncoding TranslateIntExpToBDD(Model model) { string channelName = Arguments[0].ToString(); string countChannelVariable = Model.GetCountVarChannel(channelName); string topChannelVariable = Model.GetTopVarChannel(channelName); switch (MethodName) { case Constants.cfull: Expression isFull = Expression.EQ(new Variable(countChannelVariable), new IntConstant(model.mapChannelToSize[channelName])); ExpressionBDDEncoding isFullTemp = isFull.TranslateBoolExpToBDD(model); isFullTemp.ExpressionDDs = new List <CUDDNode>(isFullTemp.GuardDDs); isFullTemp.GuardDDs = new List <CUDDNode>() { CUDD.Constant(1) }; return(isFullTemp); case Constants.cempty: Expression isEmpty = Expression.EQ(new Variable(countChannelVariable), new IntConstant(0)); ExpressionBDDEncoding isEmptyTemp = isEmpty.TranslateBoolExpToBDD(model); isEmptyTemp.ExpressionDDs = new List <CUDDNode>(isEmptyTemp.GuardDDs); isEmptyTemp.GuardDDs = new List <CUDDNode>() { CUDD.Constant(1) }; return(isEmptyTemp); case Constants.ccount: return(new Variable(countChannelVariable).TranslateIntExpToBDD(model)); case Constants.csize: return(new IntConstant(model.mapChannelToSize[channelName]).TranslateIntExpToBDD(model)); case Constants.cpeek: //(top_a - count_a) % L Expression popedElementPosition = new PrimitiveApplication(PrimitiveApplication.MOD, Expression.MINUS(new Variable(topChannelVariable), new Variable(countChannelVariable)), new IntConstant(model.mapChannelToSize[channelName])); //a[top_a - count_a % L][i] return(new PrimitiveApplication(PrimitiveApplication.ARRAY, new Variable(channelName), Expression.TIMES( popedElementPosition, new IntConstant(Model.MAX_MESSAGE_LENGTH)) ).TranslateIntExpToBDD(model)); } throw new Exception("Static call can not be encoded in BDD"); }
/// /// <summary> /// Use when the boolen constant is used as expression: a = true /// </summary> /// <param name="model"></param> /// <returns></returns> public override ExpressionBDDEncoding TranslateIntExpToBDD(Model model) { ExpressionBDDEncoding result = new ExpressionBDDEncoding(); result.GuardDDs.Add(CUDD.Constant(1)); if (Value) { result.ExpressionDDs.Add(CUDD.Constant(1)); } else { result.ExpressionDDs.Add(CUDD.Constant(0)); } return(result); }
/// <summary> /// Return variable expression /// </summary> public override ExpressionBDDEncoding TranslateIntExpToBDD(Model model) { ExpressionBDDEncoding result = new ExpressionBDDEncoding(); result.GuardDDs.Add(CUDD.Constant(1)); int variableIndex = model.GetVarIndex(this.expressionID); CUDDNode varDD = model.variableEncoding[variableIndex]; CUDD.Ref(varDD); result.ExpressionDDs.Add(varDD); return(result); }
/// <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)); }
/// <summary> /// Return strongly connected components /// Does not include Event variable /// this.transitionsNoEvents is the transtion from reachble states /// From Feasible algorithm in the article "LTL Symbolic MC" /// [ REFS: 'result', DEREFS:] /// </summary> /// <param name="model"></param> /// <param name="initState"></param> /// <param name="cycleMustPass">The cycle must pass all states in the cycleMustPass</param> /// <returns></returns> private CUDDNode SCCHull(Model model, CUDDNode initState, List <CUDDNode> cycleMustPass) { CUDDNode old = CUDD.Constant(0); CUDD.Ref(initState); CUDDNode New = model.SuccessorsStart(initState, this.transitionsNoEvents); //Limit transition to transition of reachable states CUDD.Ref(New, New); transitionsNoEvents[0] = CUDD.Function.And(transitionsNoEvents[0], New); transitionsNoEvents[1] = CUDD.Function.And(transitionsNoEvents[1], New); //Pruning step: remove states whose succeessors are not belong to that set while (!New.Equals(old)) { CUDD.Deref(old); CUDD.Ref(New); old = New; foreach (var justice in cycleMustPass) { //final state as justice requirement //new = (new and J) x R* CUDD.Ref(justice); New = model.SuccessorsStart(CUDD.Function.And(New, justice), this.transitionsNoEvents); } //while new is not comprised of the set of all successors of new states while (true) { CUDD.Ref(New, New); CUDDNode temp = CUDD.Function.And(New, model.Successors(New, this.transitionsNoEvents)); if (temp.Equals(New)) { CUDD.Deref(temp); break; } else { CUDD.Deref(New); New = temp; } } } return(New); }
/// <summary> /// [ REFS: 'result', DEREFS: 'guard' ] /// </summary> /// <param name="moduleIndex"></param> /// <param name="update"></param> /// <param name="synch"></param> /// <param name="guard"></param> /// <returns></returns> public CUDDNode EncodeUpdate(int moduleIndex, Update update, CUDDNode guard) { List <int> updatedVarIndexes = new List <int>(); CUDDNode result = CUDD.Constant(1); foreach (var assignment in update.Assignments) { int varIndex = varList.GetVarIndex(assignment.LeftHandSide); updatedVarIndexes.Add(varIndex); int low = varList.GetVarLow(varIndex); int high = varList.GetVarHigh(varIndex); CUDDNode leftDD = CUDD.Constant(0); for (int i = low; i <= high; i++) { leftDD = CUDD.Matrix.SetVectorElement(leftDD, colVars[varIndex], i - low, i); } CUDDNode rightDD = EncodeExpression(assignment.RightHandSide); CUDDNode commandDD = CUDD.Function.Equal(leftDD, rightDD); CUDD.Ref(guard); commandDD = CUDD.Function.And(commandDD, guard); //filter out of range configuration CUDD.Ref(colVarRanges[varIndex]); commandDD = CUDD.Function.And(commandDD, colVarRanges[varIndex]); CUDD.Ref(allRowVarRanges); commandDD = CUDD.Function.And(commandDD, allRowVarRanges); result = CUDD.Function.And(result, commandDD); } //Global variables and other local variables in the current module if not updated must be the same for (int i = 0; i < varList.GetNumberOfVar(); i++) { if ((varList.GetModuleIndex(i) == moduleIndex || varList.GetModuleIndex(i) == Modules.DefaultMainModuleIndex) && !updatedVarIndexes.Contains(i)) { CUDD.Ref(varIdentities[i]); result = CUDD.Function.And(result, varIdentities[i]); } } return(CUDD.Function.Times(result, CUDD.Constant((update.Probability as DoubleConstant).Value))); }
public CUDDNode ComputeCumulReward(int rewardStructIndex, int bound) { CUDDNode result = CUDD.Constant(0); if (bound > 0) { CUDDNode allRewards = ProbAlgo.ProbCumulReward(trans, stateRewards[rewardStructIndex], transRewards[rewardStructIndex], allRowVars, allColVars, bound); CUDD.Deref(result); CUDD.Ref(start); result = CUDD.Function.Times(allRewards, start); } // return(result); }