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> /// return all states reachable from start and Start /// P ◦ R* /// [ REFS: 'result', DEREFS:start] /// </summary> /// <param name="start"></param> /// <param name="transition"></param> /// <returns></returns> public CUDDNode PredecessorsStart(CUDDNode start, List <CUDDNode> transition) { CUDD.Ref(start); CUDDNode allReachableStates = start; CUDDNode currentStep = start; int numberOfLoop = 0; while (true) { numberOfLoop++; Debug.Write(numberOfLoop + " "); currentStep = Predecessors(currentStep, transition); CUDD.Ref(allReachableStates, currentStep); CUDDNode allReachableTemp = CUDD.Function.Or(allReachableStates, currentStep); if (allReachableTemp.Equals(allReachableStates)) { CUDD.Deref(currentStep, allReachableTemp); Debug.WriteLine("\nPredecessor* : " + numberOfLoop + " loops."); return(allReachableStates); } else { currentStep = CUDD.Function.Different(currentStep, allReachableStates); allReachableStates = allReachableTemp; } } }
/// <summary> /// Return transition where source states are in states /// [ REFS: 'result', DEREFS:states] /// </summary> /// <param name="states">source state of transition</param> /// <param name="transitions">transitions[0]: normal transitions, transitions[1]: priority transition</param> /// <returns></returns> private CUDDNode TransitionsBySourceStates(CUDDNode states, List <List <CUDDNode> > transitions) { CUDDNode successors; if (transitions.Count > 1) { CUDD.Ref(states); CUDD.Ref(transitions[1]); CUDDNode temp = CUDD.Function.And(states, transitions[1]); if (!temp.Equals(CUDD.ZERO)) { CUDD.Deref(states); successors = temp; } else { CUDD.Deref(temp); CUDD.Ref(transitions[0]); successors = CUDD.Function.And(states, transitions[0]); } } else { CUDD.Ref(transitions[0]); successors = CUDD.Function.And(states, transitions[0]); } return(successors); }
public void Update(Observation observation) { CUDD.Ref(observation.Precondition); Knowledge = CUDD.Function.And(Knowledge, observation.Precondition); CUDD.Ref(observation.Precondition); CUDD.Ref(Belief); CUDDNode negPrecondition = CUDD.Function.Not(observation.Precondition); CUDDNode impliesNode = CUDD.Function.Implies(Belief, negPrecondition); if (!impliesNode.Equals(CUDD.ONE)) { CUDD.Ref(observation.Precondition); Belief = CUDD.Function.And(Belief, observation.Precondition); } else { CUDD.Deref(Belief); Belief = Knowledge; CUDD.Ref(Belief); } CUDD.Deref(impliesNode); Update(observation.EventModel); }
public override void Execute() { CUDDNode precondition = _context.emptyOrPreGD().ToPrecondition(_predicateDict, _assignment); if (!precondition.Equals(CUDD.ZERO)) { Observation observation = new Observation(_context, precondition, _eventDict, _scanArray, _assignment); _observationDict.Add(observation.FullName, observation); foreach (var pair in _agentDict) { int startIndex = observation.FullName.IndexOf('('); int startIndexAddOne = observation.FullName.IndexOf(pair.Key); //Console.WriteLine("observation name: {0}, agent name: {1}, isContains: {2}", observation.FullName, pair.Key, startIndex == startIndexAddOne - 1); //Console.ReadLine(); if (startIndex == startIndexAddOne - 1) { pair.Value.AddObservation(observation); break; } } } else { CUDD.Deref(precondition); } }
/// <summary> /// [ REFS: '', DEREFS: 'processes.transitionBDD'] /// </summary> /// <param name="processes"></param> /// <param name="model"></param> /// <param name="result"></param> private static void SyncTerminateTrans(List <AutomataBDD> processes, Model model, AutomataBDD result) { CUDDNode syncTerminateTrans = GetTerminationTransEncoding(model); foreach (var process in processes) { List <CUDDNode> allTransitions = new List <CUDDNode>(); allTransitions.AddRange(process.transitionBDD); allTransitions.AddRange(process.priorityTransitionsBDD); if (syncTerminateTrans.Equals(CUDD.ZERO)) { CUDD.Deref(allTransitions); } else { syncTerminateTrans = CUDD.Function.And(syncTerminateTrans, allTransitions); } } if (syncTerminateTrans != CUDD.ZERO) { result.transitionBDD.Add(syncTerminateTrans); } else { CUDD.Deref(syncTerminateTrans); } }
/// <summary> /// return all states reachable from start and Start /// P ◦ R* /// [ REFS: 'result', DEREFS:start] /// </summary> /// <param name="start"></param> /// <param name="transition"></param> /// <returns></returns> public CUDDNode SuccessorsStart(CUDDNode start, List <CUDDNode> transition) { CUDD.Ref(start); CUDDNode allReachableStates = start; 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 { currentStep = CUDD.Function.Different(currentStep, allReachableStates); allReachableStates = allReachableTemp; } } }
/// <summary> /// Return only guards for complete boolean expression, no expression /// Use this when we want to encode a single assignment or the assignment does not depend other assignments. /// </summary> public override ExpressionBDDEncoding TranslateBoolExpToBDD(Model model) { ExpressionBDDEncoding result = new ExpressionBDDEncoding(); ExpressionBDDEncoding variableBddEncoding = new VariablePrime(this.LeftHandSide).TranslateIntExpToBDD(model); ExpressionBDDEncoding valueBddEncoding = this.RightHandSide.TranslateIntExpToBDD(model); for (int i = 0; i < variableBddEncoding.Count(); i++) { for (int j = 0; j < valueBddEncoding.Count(); j++) { CUDD.Ref(variableBddEncoding.GuardDDs[i], valueBddEncoding.GuardDDs[j]); CUDDNode guardDD = CUDD.Function.And(variableBddEncoding.GuardDDs[i], valueBddEncoding.GuardDDs[j]); if (guardDD.Equals(CUDD.ZERO)) { CUDD.Deref(guardDD); continue; } CUDD.Ref(variableBddEncoding.ExpressionDDs[i], valueBddEncoding.ExpressionDDs[j]); CUDDNode assignmentDD = CUDD.Function.Equal(variableBddEncoding.ExpressionDDs[i], valueBddEncoding.ExpressionDDs[j]); guardDD = CUDD.Function.And(guardDD, assignmentDD); result.AddNodeToGuard(guardDD); } } //remove unused expression variableBddEncoding.DeRef(); valueBddEncoding.DeRef(); return(result); }
/// <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); }
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); }
/// <summary> /// return all states reachable to 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 PredecessorsPlus(CUDDNode start, List <List <CUDDNode> > transition) { CUDDNode allReachableStates = CUDD.Constant(0); CUDDNode currentStep = start; while (true) { currentStep = Predecessors(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); }
/// <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); }
/// <summary> /// [ REFS: 'none', DEREFS: 'dd if failed to add' ] /// </summary> public void AddNodeToGuard(CUDDNode dd) { if (!dd.Equals(CUDD.ZERO)) { this.GuardDDs.Add(dd); } else { CUDD.Deref(dd); } }
/// <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> /// 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); }
private void Update(EventModel eventModel) { CUDD.Ref(Belief); CUDD.Ref(eventModel.BelievePrecondition); CUDDNode impliesNode = CUDD.Function.Implies(Belief, CUDD.Function.Not(eventModel.BelievePrecondition)); if (!impliesNode.Equals(CUDD.ONE)) { UpdateBelief(eventModel.BelievePartialSsa, eventModel.BelieveAffectedPredSet); UpdateKnowledge(eventModel); return; } CUDD.Ref(Belief); CUDD.Ref(eventModel.KnowPrecondition); impliesNode = CUDD.Function.Implies(Belief, CUDD.Function.Not(eventModel.KnowPrecondition)); if (!impliesNode.Equals(CUDD.ONE)) { UpdateBelief(eventModel.KnowPartialSsa, eventModel.KnowAffectedPredSet); UpdateKnowledge(eventModel); return; } CUDD.Deref(Belief); CUDD.Ref(Knowledge); CUDD.Ref(eventModel.BelievePrecondition); impliesNode = CUDD.Function.Implies(Knowledge, CUDD.Function.Not(eventModel.BelievePrecondition)); if (!impliesNode.Equals(CUDD.ONE)) { Belief = Knowledge; CUDD.Ref(Belief); UpdateBelief(eventModel.BelievePartialSsa, eventModel.BelieveAffectedPredSet); UpdateKnowledge(eventModel); return; } UpdateKnowledge(eventModel); Belief = Knowledge; CUDD.Ref(Belief); }
/// <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 static CUDDNode RecursiveScanMixedRaio(PlanningParser.GdContext context, IReadOnlyDictionary <string, Predicate> predicateDict, IReadOnlyList <string> variableNameList, IReadOnlyList <IList <string> > collection, StringDictionary assignment, int currentLevel = 0, bool isForall = true) { CUDDNode result; if (currentLevel != variableNameList.Count) { string variableName = variableNameList[currentLevel]; result = isForall ? CUDD.ONE : CUDD.ZERO; CUDD.Ref(result); CUDDNode equalNode = isForall ? CUDD.ZERO : CUDD.ONE; Func <CUDDNode, CUDDNode, CUDDNode> boolFunc; if (isForall) { boolFunc = CUDD.Function.And; } else { boolFunc = CUDD.Function.Or; } foreach (string value in collection[currentLevel]) { assignment[variableName] = value; CUDDNode gdNode = RecursiveScanMixedRaio(context, predicateDict, variableNameList, collection, assignment, currentLevel + 1, isForall); if (gdNode.Equals(equalNode)) { CUDD.Deref(result); result = equalNode; CUDD.Ref(result); break; } result = boolFunc(result, gdNode); } } else { result = GetCuddNode(context, predicateDict, assignment); } return(result); }
/// <summary> /// Return state having probability 1 of b1 U b2 /// [ REFS: 'result', DEREFS: ] /// </summary> /// <param name="trans01"></param> /// <param name="reach"></param> /// <param name="allRowVars"></param> /// <param name="allColVars"></param> /// <param name="b1"></param> /// <param name="b2"></param> /// <param name="no"></param> /// <returns></returns> public static CUDDNode Prob1(CUDDNode trans01, CUDDNode reach, CUDDVars allRowVars, CUDDVars allColVars, CUDDNode b1, CUDDNode b2, CUDDNode no) { DateTime startTime = DateTime.Now; int numberOfIterations = 0; CUDD.Ref(no); CUDDNode sol = no; while (true) { numberOfIterations++; CUDD.Ref(sol); CUDDNode tmp = CUDD.Variable.SwapVariables(sol, allRowVars, allColVars); CUDD.Ref(trans01); tmp = CUDD.Function.And(tmp, trans01); tmp = CUDD.Abstract.ThereExists(tmp, allColVars); CUDD.Ref(b1); tmp = CUDD.Function.And(b1, tmp); CUDD.Ref(b2); tmp = CUDD.Function.And(CUDD.Function.Not(b2), tmp); CUDD.Ref(no); tmp = CUDD.Function.Or(no, 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("Prob1: " + numberOfIterations + " iterations in " + runningTime + " seconds"); return(sol); }
/// <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); }
public override void Execute() { CUDDNode precondition = _context.emptyOrPreGD().ToPrecondition(_predicateDict, _assignment); if (!precondition.Equals(CUDD.ZERO)) { Event e = new Event(_context, precondition, _predicateDict, _scanArray, _assignment, CurrentCuddIndex); _eventDict.Add(e.FullName, e); CurrentCuddIndex++; } else { CUDD.Deref(precondition); } }
/// <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); }
private List <Tuple <Predicate, bool> > GenerateLiteralList(CUDDNode node, Event e) { //OK List <Tuple <Predicate, bool> > result = new List <Tuple <Predicate, bool> >(); foreach (var cEffect in e.CondEffect) { CUDD.Ref(node); CUDD.Ref(cEffect.Item1); CUDDNode impliesNode = CUDD.Function.Implies(node, cEffect.Item1); if (impliesNode.Equals(CUDD.ONE)) { result.AddRange(cEffect.Item2); } CUDD.Deref(impliesNode); } return(result); }
/// <summary> /// Combine all the encoding of probabilistic commands /// [ REFS: 'result', DEREFS: 'guardDDs, commandDDs' ] /// </summary> /// <param name="trans"></param> /// <returns></returns> private ComponentDDs CombineProbCommands(int moduleIndex, List <CUDDNode> guardDDs, List <CUDDNode> commandDDs) { ComponentDDs result = new ComponentDDs(); CUDDNode trans = CUDD.Constant(0); CUDDNode cover = CUDD.Constant(0); for (int i = 0; i < guardDDs.Count; i++) { if (guardDDs[i].Equals(CUDD.ZERO)) { continue; } //Check whether the guard is unsatisfiable CUDD.Ref(guardDDs[i], cover); CUDDNode temp = CUDD.Function.And(guardDDs[i], cover); if (!temp.Equals(CUDD.ZERO)) { Debug.WriteLine("Warning: Guard for command " + (i + 1) + " of module \"" + modules.AllModules[moduleIndex].Name + "\" overlaps with previous commands."); CUDD.Deref(temp); } // CUDD.Ref(guardDDs[i]); cover = CUDD.Function.Or(cover, guardDDs[i]); CUDD.Ref(commandDDs[i]); trans = CUDD.Function.Plus(trans, commandDDs[i]); } CUDD.Deref(guardDDs, commandDDs); // store result result.guards = cover; result.trans = trans; result.min = 0; result.max = 0; return(result); }
/// <summary> /// Generate fair counter example /// [ REFS: '', DEREFS:''] /// </summary> /// <param name="finalState"></param> /// <param name="automataBDD"></param> /// <param name="model"></param> private void BuildFairCounterExample(CUDDNode finalState, AutomataBDD automataBDD, Model model) { CUDD.CUDD.Ref(this.transitionsNoEvents); CUDD.CUDD.Ref(this.scc, this.scc); List <CUDDNode> transInSCCNoEvent = CUDD.CUDD.Function.And(CUDD.CUDD.Function.And(this.transitionsNoEvents, this.scc), model.SwapRowColVars(this.scc)); CUDD.CUDD.Ref(automataBDD.transitionBDD); CUDD.CUDD.Ref(this.scc, this.scc); List <CUDDNode> transInSCCWithEvent = CUDD.CUDD.Function.And(CUDD.CUDD.Function.And(automataBDD.transitionBDD, this.scc), model.SwapRowColVars(this.scc)); // List <CUDDNode> path = new List <CUDDNode>(); CUDD.CUDD.Ref(period[0], finalState); CUDDNode temp1 = CUDD.CUDD.Function.And(period[0], finalState); if (temp1.Equals(CUDD.CUDD.ZERO)) { CUDD.CUDD.Ref(this.scc, finalState); CUDDNode acceptanceStateInCyle = CUDD.CUDD.Function.And(this.scc, finalState); model.Path(period[0], acceptanceStateInCyle, transInSCCWithEvent, path, true, true); period.AddRange(path); CUDD.CUDD.Deref(acceptanceStateInCyle); } // CUDD.CUDD.Deref(temp1); // BuildFairPeriod(transInSCCWithEvent, model); bool isEmptyPathAllowed = (period.Count == 1 ? false : true); model.Path(period[period.Count - 1], period[0], transInSCCNoEvent, path, isEmptyPathAllowed, false); period.AddRange(path); CUDD.CUDD.Deref(transInSCCNoEvent); CUDD.CUDD.Deref(transInSCCWithEvent); //Remove dummy value CUDD.CUDD.Deref(period[0]); period.RemoveAt(0); }
/// <summary> /// return colomn variables used in the dd /// [ REFS: '', DEREFS: '' ] /// </summary> public List <int> GetColSupportedVars(CUDDNode dd) { List <int> result = new List <int>(); //Get changed variables CUDDNode supportedVariable = CUDD.GetSupport(dd); for (int k = 0; k < this.colVars.Count; k++) { CUDD.Ref(supportedVariable); CUDDNode temp = CUDD.Abstract.ThereExists(supportedVariable, this.colVars[k]); if (!supportedVariable.Equals(temp)) { result.Add(k); } CUDD.Deref(temp); } CUDD.Deref(supportedVariable); return(result); }
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()); }
public Event GetActualEvent(Response response) { Event result = null; IReadOnlyList <Event> eventList = response.EventModel.KnowEventList; foreach (var e in eventList) { CUDD.Ref(CurrentCuddNode); CUDD.Ref(e.Precondition); CUDDNode impliesNode = CUDD.Function.Implies(CurrentCuddNode, e.Precondition); if (impliesNode.Equals(CUDD.ONE)) { CUDD.Deref(impliesNode); result = e; break; } CUDD.Deref(impliesNode); } return(result); }
public static List <Tuple <CUDDNode, Tuple <Predicate, bool>[]> > GetCondEffectList( this PlanningParser.CEffectContext context, CUDDNode currentConditionNode, IReadOnlyDictionary <string, Predicate> predicateDict, StringDictionary assignment) { List <Tuple <CUDDNode, Tuple <Predicate, bool>[]> > result; if (context.FORALL() != null) { CEffectEnumerator enumerator = new CEffectEnumerator(context, currentConditionNode, predicateDict, assignment); Algorithms.IterativeScanMixedRadix(enumerator); result = enumerator.CondEffectList; } else if (context.WHEN() != null) { result = new List <Tuple <CUDDNode, Tuple <Predicate, bool>[]> >(); CUDD.Ref(currentConditionNode); CUDDNode gdNode = context.gd().GetCuddNode(predicateDict, assignment); CUDDNode conditionNode = CUDD.Function.And(currentConditionNode, gdNode); if (!conditionNode.Equals(CUDD.ZERO)) { var literaCollection = context.condEffect().GetLiteralArray(predicateDict, assignment); var condEffect = new Tuple <CUDDNode, Tuple <Predicate, bool>[]>(conditionNode, literaCollection); result.Add(condEffect); } else { CUDD.Deref(conditionNode); } } else { result = new List <Tuple <CUDDNode, Tuple <Predicate, bool>[]> >(); var literals = context.condEffect().GetLiteralArray(predicateDict, assignment); var condEffect = new Tuple <CUDDNode, Tuple <Predicate, bool>[]>(currentConditionNode, literals); result.Add(condEffect); } return(result); }