public override TAction DecideAction(MDPPerception <TState> perception) { if (!(utilityFunction.HasUtilityFor(perception.GetState()))) { // if // perceptionState // is // new utilityFunction.SetUtility(perception.GetState(), perception.GetReward()); MDP.SetReward(perception.GetState(), perception.GetReward()); } if (!(PreviousState == null)) { stateCount.IncrementFor(PreviousState); utilityFunction = this.UpdateUtilityFunction(1.0); } if (MDP.IsTerminalState(CurrentState)) { PreviousState = null; PreviousAction = null; //TODO: make sure that 0 is appropriate value for what used to be null in java previousReward = 0; } else { PreviousState = CurrentState; PreviousAction = policy.GetAction(CurrentState); previousReward = CurrentReward; } return(PreviousAction); }
public void MdpPostActivityFeed() { Dictionary<string, MySpaceUser> users = TestConstants.GetMySpaceUsers(); MdpApplication application = TestConstants.GetApplication(); MDP mdp = new MDP(application.key, application.secret); string oAuthToken = TestConstants.API_ACCESS_TOKEN; long myspaceId = users["tom"].id; Dictionary<string, string> parameters = new Dictionary<string, string>(); parameters.Add("eventTitle", HttpUtility.UrlEncode("Hello")); parameters.Add("eventDate", HttpUtility.UrlEncode(DateTime.Now.ToString("MMMM d"))); parameters.Add("eventLoc", HttpUtility.UrlEncode("My House")); parameters.Add("eventId", HttpUtility.UrlEncode("12")); try { PostActivityFeedResult result = mdp.PostActivityFeed(oAuthToken, "CreateEvent", parameters, "en-US", "json"); Console.WriteLine("Consumer Key: {0}", application.key); Console.WriteLine("OAuthToken: {0}", oAuthToken); Console.WriteLine("Result: {0} ", result.PostActivityId); } catch (WebException ex) { Console.Error.WriteLine(Util.HandleWebException(ex)); Assert.Fail(); } }
public PassiveTDAgent(MDP <TState, TAction> mdp, MDPPolicy <TState, TAction> policy) : base(mdp.EmptyMdp()) { this.policy = policy; this.utilityFunction = new MDPUtilityFunction <TState>(); this.stateCount = new FrequencyCounter <TState>(); }
public PassiveADPAgent(MDP <STATE_TYPE, ACTION_TYPE> mdp, MDPPolicy <STATE_TYPE, ACTION_TYPE> policy) : base(mdp.emptyMdp()) { this.policy = policy; this.utilityFunction = new MDPUtilityFunction <STATE_TYPE>(); this.nsa = new Dictionary <Pair <STATE_TYPE, ACTION_TYPE>, Double>(); this.nsasdash = new Dictionary <MDPTransition <STATE_TYPE, ACTION_TYPE>, Double>(); }
public PassiveADPAgent(MDP <TState, TAction> mdp, MDPPolicy <TState, TAction> policy) : base(mdp.EmptyMdp()) { this.policy = policy; this.utilityFunction = new MDPUtilityFunction <TState>(); this.nsa = new Dictionary <Pair <TState, TAction>, double>(); this.nsasdash = new Dictionary <MDPTransition <TState, TAction>, double>(); }
private void EXT_State_Wait(INPUT input) { if (input.Type == InputType.TAG) { TAG tag = input.Record as TAG; if ((MyScr < 11) && (UrScr < 11) && (Rcv == 2)) //Condition { //Transition Action Srv += 1; if (Srv == 2) { Rcv = 0; } //Next State State_Attack(tag.Now); } else if ((MyScr < 11) && (UrScr < 11) && (Srv == 2)) //Condition { //Transition Action Rcv += 1; if (Rcv == 2) { Srv = 0; } //Next State State_Defense(tag.Now); } else if ((MyScr == 11) || (UrScr == 11)) //Condition { //Transition Action Send_MSR("Over"); //Next State State_Gameover(tag.Now); } else { WriteError("No matching condition.", "EXT_State_Wait()"); } } else if (input.Type == InputType.MDP) { MDP mdp = input.Record as MDP; if (mdp.Msg.Equals("Quit")) { //Next State State_Quit(mdp.Now); } else if (mdp.Msg.Equals("OVER")) { //Next State State_OVER(mdp.Now); } else { WriteError("No matching message.", "EXT_State_Wait()"); } } }
public override TAction DecideAction(MDPPerception <TState> perception) { if (!(utilityFunction.HasUtilityFor(perception.GetState()))) { // if // perceptionState // is // new utilityFunction.SetUtility(perception.GetState(), perception .GetReward()); MDP.SetReward(perception.GetState(), perception.GetReward()); } if (!(PreviousState == null)) { if (nsa.ContainsKey(new Pair <TState, TAction>( PreviousState, PreviousAction))) { nsa[new Pair <TState, TAction>(PreviousState, PreviousAction)] += 1; } else { nsa[new Pair <TState, TAction>(PreviousState, PreviousAction)] = 1.0; } if (nsasdash.ContainsKey(new MDPTransition <TState, TAction>(PreviousState, PreviousAction, CurrentState))) { nsasdash[new MDPTransition <TState, TAction>(PreviousState, PreviousAction, CurrentState)] += 1; } else { nsasdash[new MDPTransition <TState, TAction>(PreviousState, PreviousAction, CurrentState)] = 1.0; } foreach (MDPTransition <TState, TAction> transition in nsasdash.Keys) { if (nsasdash[transition] != 0.0) { double newValue = nsasdash[transition] / nsa[new Pair <TState, TAction>( transition.GetInitialState(), transition.GetAction())]; MDP.SetTransitionProbability(transition, newValue); } } IList <MDPTransition <TState, TAction> > validTransitions = MDP .GetTransitionsWith(PreviousState, policy.GetAction(PreviousState)); utilityFunction = this.ValueDetermination(validTransitions, 1); } if (MDP.IsTerminalState(CurrentState)) { PreviousState = null; PreviousAction = null; } else { PreviousState = CurrentState; PreviousAction = policy.GetAction(CurrentState); } return(PreviousAction); }
public PassiveADPAgent(IMap <S, A> fixedPolicy, ISet <S> states, S initialState, IActionsFunction <S, A> actionsFunction, IPolicyEvaluation <S, A> policyEvaluation) { this.pi.AddAll(fixedPolicy); this.mdp = new MDP <S, A>(states, initialState, actionsFunction, new TransitionProbabilityFunctionImpl(P), new RewardFunctionImpl(R)); this.policyEvaluation = policyEvaluation; }
static void Main(string[] args) { var grid = Grid.Construct(6, 5, new Obstacle[] { new Obstacle(2, 2), new Obstacle(4, 4) }); MDP mdp = new MDP(grid); grid = mdp.ValueIteration(); //Grid.show_grid(grid); }
protected void btnConfirmer_Click(object sender, EventArgs e) { // Reset everything Courriel1.DefaultControl(); Courriel2.DefaultControl(); MDP.DefaultControl(); MDP2.DefaultControl(); // Check for errors bool[] arrError = new bool[] { InvalidateIfEmpty(Courriel1, lblErrorCourriel1, "Le courriel doit être présent") || CheckFormatCourriel(Courriel1, lblErrorCourriel1) || CheckMatch(Courriel1, Courriel2, lblErrorCourriel1, lblErrorCourriel2, "Les courriels ne sont pas identiques"), InvalidateIfEmpty(Courriel2, lblErrorCourriel2, "La confirmation de courriel doit être présente") || CheckFormatCourriel(Courriel2, lblErrorCourriel2), InvalidateIfEmpty(MDP, lblErrorMDP, "Le mot de passe doit être présent") || CheckMatch(MDP, MDP2, lblErrorMDP, lblErrorMDP2, "Les mots de passes ne sont pas identiques"), InvalidateIfEmpty(MDP2, lblErrorMDP2, "La confirmation de mot de passe doit être présente") }; if (!arrError.Contains(true)) { PPClients clients = new PPClients(); PPVendeurs vendeurs = new PPVendeurs(); PPGestionnaires gestionnaires = new PPGestionnaires(); if (clients.Values.Any(x => x.AdresseEmail == Courriel1.Text) || vendeurs.Values.Any(x => x.AdresseEmail == Courriel1.Text) || gestionnaires.Values.Any(x => x.Email == Courriel1.Text)) { Courriel1.Invalidate(); lblErrorCourriel1.Text = "Ce courriel est déjà utilisé par un autre utilisateur"; } else { Client newClient = new Client(null) { NoClient = clients.NextId(), AdresseEmail = Courriel1.Text, MotDePasse = MDP.Text, DateCreation = DateTime.Now, NbConnexions = 0, Pays = "Canada" }; clients.Add(newClient); Response.Redirect("~/Pages/Inscription.aspx?Reussite=true"); } } }
private TAction ActionMaximizingLearningFunction() { TAction maxAct = null; double maxValue = double.NegativeInfinity; foreach (TAction action in MDP.GetAllActions()) { double qValue = qTable.GetQValue(CurrentState, action); var lfv = this.LearningFunction(qValue); if (lfv > maxValue) { maxValue = lfv; maxAct = action; } } return(maxAct); }
public void setUp() { fourByThree = MDPFactory.createFourByThreeMDP(); policy = new MDPPolicy<CellWorldPosition, String>(); policy.setAction(new CellWorldPosition(1, 1), CellWorld.UP); policy.setAction(new CellWorldPosition(1, 2), CellWorld.LEFT); policy.setAction(new CellWorldPosition(1, 3), CellWorld.LEFT); policy.setAction(new CellWorldPosition(1, 4), CellWorld.LEFT); policy.setAction(new CellWorldPosition(2, 1), CellWorld.UP); policy.setAction(new CellWorldPosition(2, 3), CellWorld.UP); policy.setAction(new CellWorldPosition(3, 1), CellWorld.RIGHT); policy.setAction(new CellWorldPosition(3, 2), CellWorld.RIGHT); policy.setAction(new CellWorldPosition(3, 3), CellWorld.RIGHT); }
public void setUp() { fourByThree = MDPFactory.createFourByThreeMDP(); policy = new MDPPolicy <CellWorldPosition, String>(); policy.setAction(new CellWorldPosition(1, 1), CellWorld.UP); policy.setAction(new CellWorldPosition(1, 2), CellWorld.LEFT); policy.setAction(new CellWorldPosition(1, 3), CellWorld.LEFT); policy.setAction(new CellWorldPosition(1, 4), CellWorld.LEFT); policy.setAction(new CellWorldPosition(2, 1), CellWorld.UP); policy.setAction(new CellWorldPosition(2, 3), CellWorld.UP); policy.setAction(new CellWorldPosition(3, 1), CellWorld.RIGHT); policy.setAction(new CellWorldPosition(3, 2), CellWorld.RIGHT); policy.setAction(new CellWorldPosition(3, 3), CellWorld.RIGHT); }
private void EXT_State_Attack(INPUT input) { if (input.Type == InputType.TAG) { TAG tag = input.Record as TAG; if (U <= PB) //Condition { //Transition Action Send_MSR("BallB"); //Next State State_Defense(tag.Now); } else if (true) //Condition { //Transition Action UrScr++; Send_MSR("OutB"); //Next State State_Wait(tag.Now); } else { WriteError("No matching condition.", "EXT_State_Attack()"); } } else if (input.Type == InputType.MDP) { MDP mdp = input.Record as MDP; if (mdp.Msg.Equals("Quit")) { //Next State State_Quit(mdp.Now); } else if (mdp.Msg.Equals("OVER")) { //Next State State_OVER(mdp.Now); } else { WriteError("No matching message.", "EXT_State_Attack()"); } } }
/// <summary> /// Run the verification and get the result. /// </summary> /// <returns></returns> public override void RunVerification() { if (ConstraintType == QueryConstraintType.NONE) { base.RunVerification(); return; } MDP mdp = BuildMDP(); //GetTransitionRelation(); //MDP mdp = BuildDTMCSSZ(); //GetTransitionRelation(); if (!CancelRequested) { switch (ConstraintType) { case QueryConstraintType.PROB: Min = mdp.MinProbability(VerificationOutput); mdp.ResetNonTargetState(); Max = mdp.MaxProbability(VerificationOutput); break; case QueryConstraintType.PMAX: Max = mdp.MaxProbability(VerificationOutput); break; case QueryConstraintType.PMIN: Min = mdp.MinProbability(VerificationOutput); break; } if (Min == 1) { this.VerificationOutput.VerificationResult = VerificationResultType.VALID; } else if (Max == 0) { this.VerificationOutput.VerificationResult = VerificationResultType.INVALID; } else { this.VerificationOutput.VerificationResult = VerificationResultType.WITHPROBABILITY; } } }
public override void RunVerification() { if (QueryConstraintType == QueryConstraintType.NONE) { base.RunVerification(); } else { MDP mdp = BuildMDP(); if (!CancelRequested) { switch (QueryConstraintType) { case QueryConstraintType.PROB: Max = 1 - mdp.MinProbability(VerificationOutput); mdp.ResetNonTargetState(); Min = 1 - mdp.MaxProbability(VerificationOutput); break; case QueryConstraintType.PMAX: Max = 1 - mdp.MinProbability(VerificationOutput); break; case QueryConstraintType.PMIN: Min = 1 - mdp.MaxProbability(VerificationOutput); break; } if (Min == 1) { VerificationOutput.VerificationResult = VerificationResultType.VALID; } else if (Max == 0) { VerificationOutput.VerificationResult = VerificationResultType.INVALID; } else { VerificationOutput.VerificationResult = VerificationResultType.WITHPROBABILITY; } } } }
private MDPUtilityFunction <TState> ValueDetermination( IList <MDPTransition <TState, TAction> > validTransitions, double gamma) { MDPUtilityFunction <TState> uf = utilityFunction.Copy(); double additional = 0.0; if (validTransitions.Count > 0) { TState initState = validTransitions[0].GetInitialState(); double reward = MDP.GetRewardFor(initState); additional = validTransitions.Sum( transition => MDP.GetTransitionProbability(transition) * this.utilityFunction.GetUtility(transition.GetDestinationState())); uf.SetUtility(initState, reward + (gamma * additional)); } return(uf); }
private void EXT_State_Defense(INPUT input) { if (input.Type == InputType.MDP) { MDP mdp = input.Record as MDP; if (mdp.Msg.Equals("BallA")) { //Next State State_Attack(mdp.Now); } else if (mdp.Msg.Equals("OutA")) { //Transition Action MyScr++; //Next State State_Wait(mdp.Now); } else if (mdp.Msg.Equals("Quit")) { //Next State State_Quit(mdp.Now); } else if (mdp.Msg.Equals("OVER")) { //Next State State_OVER(mdp.Now); } else { WriteError("No matching message.", "EXT_State_Defense()"); } } else { WriteError("No matching input.", "EXT_State_Defense()"); } }
private void EXT_State_Watch(INPUT input) { if (input.Type == InputType.TAG) { TAG tag = input.Record as TAG; if (true) //Condition { //Transition Action Send_MSR("Quit"); //Next State State_End(tag.Now); //Next State } } else if (input.Type == InputType.MDP) { MDP mdp = input.Record as MDP; if (mdp.Msg.Equals("Over")) { if (true) //Condition { //Transition Action //Next State State_End(mdp.Now); //Next State } } else if (mdp.Msg.Equals("OVER")) { //Transition Action //Next State State_OVER(mdp.Now); //Next State } else { WriteError("No matching message.", "EXT_State_Watch()"); } } }
protected MDP BuildMDP() { Stack<KeyValuePair<MDPConfiguration, MDPState>> working = new Stack<KeyValuePair<MDPConfiguration, MDPState>>(1024); string initID = InitialStep.GetID(); MDPState init = new MDPState(initID); working.Push(new KeyValuePair<MDPConfiguration, MDPState>(InitialStep as MDPConfiguration, init)); MDP mdp = new MDP(init, Ultility.Ultility.DEFAULT_PRECISION, Ultility.Ultility.MAX_DIFFERENCE); //if initial state is target ExpressionValue initV = EvaluatorDenotational.Evaluate(ReachableStateCondition, InitialStep.GlobalEnv); if ((initV as BoolConstant).Value) { mdp.AddTargetStates(init); VerificationOutput.NoOfStates = 1; return mdp; } //if initial state is target do { if (CancelRequested) { VerificationOutput.NoOfStates = mdp.States.Count; return mdp; } KeyValuePair<MDPConfiguration, MDPState> current = working.Pop(); IEnumerable<MDPConfiguration> list = current.Key.MakeOneMoveLocal(); VerificationOutput.Transitions += list.Count(); int currentDistriIndex = -1; Distribution newDis = new Distribution(Constants.TAU); //for (int i = 0; i < list.Count; i++) foreach (var step in list) { //MDPConfiguration step = list[i]; string stepID = step.GetID(); MDPState nextState; if (!mdp.States.TryGetValue(stepID, out nextState)) { nextState = new MDPState(stepID); mdp.AddState(nextState); ExpressionValue v = EvaluatorDenotational.Evaluate(ReachableStateCondition, step.GlobalEnv); if ((v as BoolConstant).Value) { mdp.AddTargetStates(nextState); } else { working.Push(new KeyValuePair<MDPConfiguration, MDPState>(step, nextState)); } } if (step.DisIndex == -1) { if (currentDistriIndex >= 0) { current.Value.AddDistribution(newDis); newDis = new Distribution(Constants.TAU); } Distribution newTrivialDis = new Distribution(step.Event); newTrivialDis.AddProbStatePair(1, nextState); current.Value.AddDistribution(newTrivialDis); } else if (currentDistriIndex != -1 && step.DisIndex != currentDistriIndex) { current.Value.AddDistribution(newDis); newDis = new Distribution(Constants.TAU); newDis.AddProbStatePair(step.Probability, nextState); } else { newDis.AddProbStatePair(step.Probability, nextState); } currentDistriIndex = step.DisIndex; } if (currentDistriIndex >= 0) { current.Value.AddDistribution(newDis); } } while (working.Count > 0); VerificationOutput.NoOfStates = mdp.States.Count; //mdp.BackUpTargetStates(); return mdp; }
//protected QueryConstraintType ConstraintType; //protected DRA DRA; //public DRA PositiveDRA; //public DRA NegativeDRA; //public BuchiAutomata PositiveBA; //protected double Min = -1; //protected double Max = -1; //protected MDP mdp; //protected Dictionary<string, int> MDPState2DRAStateMapping; //private bool HasDeadLock; ////private DefinitionRef Process; //public const string DUMMY_INIT = "dummy"; //private bool AlmostFair = false; protected void BuildMD_ImprovedTarjan() { //int counter = 0; MDPState dummyInit = new MDPState(DUMMY_INIT); mdp = new MDP(dummyInit, Ultility.Ultility.DEFAULT_PRECISION, Ultility.Ultility.MAX_DIFFERENCE); List<PCSPEventDRAPairMEC> initials = GetInitialPairsEMC(InitialStep as MDPConfiguration); Stack<KeyValuePair<PCSPEventDRAPairMEC, MDPState>> working = new Stack<KeyValuePair<PCSPEventDRAPairMEC, MDPState>>(1024); //Stack<PCSPEventDRAPairMEC> stepStack = new Stack<PCSPEventDRAPairMEC>(1024); List<PCSPEventDRAPairMEC> stepStack = new List<PCSPEventDRAPairMEC>(1024); MDPState2DRAStateMapping = new Dictionary<string, int>(Common.Classes.Ultility.Ultility.MC_INITIAL_SIZE); //HashSet<string> MDPStateMapping = new HashSet<string>(); //build the MDP while identifying the SCCs List<List<string>> SCCs = new List<List<string>>(64); for (int z = 1; z <= initials.Count; z++) { PCSPEventDRAPairMEC initState = initials[z - 1]; string stringID = initState.ID; MDPState newinit = new MDPState(stringID); mdp.AddState(newinit); dummyInit.AddDistribution(new Distribution(stringID, newinit)); stepStack.Add(initState); //newinit.AddDistribution(new Distribution(Constants.TAU, newinit)); working.Push(new KeyValuePair<PCSPEventDRAPairMEC, MDPState>(initState, newinit)); } Dictionary<string, int> preorder = new Dictionary<string, int>(Common.Classes.Ultility.Ultility.MC_INITIAL_SIZE); Dictionary<string, int> lowlink = new Dictionary<string, int>(Common.Classes.Ultility.Ultility.MC_INITIAL_SIZE); Dictionary<string, int> distrRecord = new Dictionary<string, int>(); int preorderCounter = 0; Dictionary<string, List<PCSPEventDRAPairMEC>> ExpendedNode = new Dictionary<string, List<PCSPEventDRAPairMEC>>(); do { if (CancelRequested) { this.VerificationOutput.NoOfStates = this.VerificationOutput.NoOfStates + mdp.States.Count; return; } KeyValuePair<PCSPEventDRAPairMEC, MDPState> pair = working.Peek(); MDPConfiguration evt = pair.Key.configuration; int DRAState = pair.Key.state; string currentID = pair.Key.ID; List<Distribution> outgoing = pair.Value.Distributions; if (!preorder.ContainsKey(currentID)) { preorder.Add(currentID, preorderCounter); distrRecord.Add(currentID, 0); //stepStack.Add(new PCSPEventDRAPairMEC(currentID + "separate" + 0)); preorderCounter++; } bool done = true; if (ExpendedNode.ContainsKey(currentID)) { List<PCSPEventDRAPairMEC> list = ExpendedNode[currentID]; //int counter = 0; if (list.Count > 0) { int k = list.Count - 1; //for (int k = list.Count - 1; k >= 0; k--) PCSPEventDRAPairMEC step = list[k]; if (step == null) { stepStack.Add(new PCSPEventDRAPairMEC(currentID + "separate" + (distrRecord[currentID]++))); list.RemoveAt(k); continue; } else { string stepID = step.ID; if (!preorder.ContainsKey(stepID)) { //if (done) //{ working.Push(new KeyValuePair<PCSPEventDRAPairMEC, MDPState>(step, mdp.States[stepID])); stepStack.Add(step); done = false; list.RemoveAt(k); //} } else { //stepStack.Add(step.ID); stepStack.Add(step); list.RemoveAt(k); done = false; } } } //else //{ // stepStack.Add(new PCSPEventDRAPairMEC(currentID + "end")); //} } else { int currentDistriIndex = -1; Distribution newDis = new Distribution(Constants.TAU); IEnumerable<MDPConfiguration> steps = evt.MakeOneMoveLocal(); //int counter = 0; //NOTE: here we play a trick for deadlock case: if a deadlock exist in the MDP, we will make a //self loop transition to remove the deadlock. Deadlock is meaningless in MDP. if (evt.IsDeadLock) { List<MDPConfiguration> stepsList = new List<MDPConfiguration>(steps); stepsList.Add(CreateSelfLoopStep(evt)); steps = stepsList; HasDeadLock = true; } List<PCSPEventDRAPairMEC> product = NextMEC(steps.ToArray(), DRAState); this.VerificationOutput.Transitions += product.Count; for (int k = product.Count - 1; k >= 0; k--) { PCSPEventDRAPairMEC step = product[k]; string tmp = step.ID; //int nextIndex = VisitedWithID.Count; MDPState nextState; if (mdp.States.TryGetValue(tmp, out nextState)) { if (!preorder.ContainsKey(tmp)) { if (done) { working.Push(new KeyValuePair<PCSPEventDRAPairMEC, MDPState>(step, nextState)); //stepStack.Add(step.ID); stepStack.Add(step); product.RemoveAt(k); done = false; } else { product[k] = step; } } else { if (done) { product.RemoveAt(k); done = false; } } } else { nextState = new MDPState(tmp); mdp.States.Add(tmp, nextState); if (done) { working.Push(new KeyValuePair<PCSPEventDRAPairMEC, MDPState>(step, nextState)); //stepStack.Add(step.ID); stepStack.Add(step); done = false; product.RemoveAt(k); } else { product[k] = step; } } MDPConfiguration pstep = step.configuration; if (pstep.DisIndex == -1) { if (currentDistriIndex >= 0) { pair.Value.AddDistribution(newDis); product.Insert(k+1, null);//separator newDis = new Distribution(Constants.TAU); } Distribution newTrivialDis = new Distribution(pstep.Event, nextState); pair.Value.AddDistribution(newTrivialDis); if (k != 0) { product.Insert(k, null);//separator } } else if (currentDistriIndex != -1 && pstep.DisIndex != currentDistriIndex) { pair.Value.AddDistribution(newDis); product.Insert(k+1, null);//separator newDis = new Distribution(Constants.TAU); newDis.AddProbStatePair(pstep.Probability, nextState); } else { newDis.AddProbStatePair(pstep.Probability, nextState); } currentDistriIndex = pstep.DisIndex; } if (currentDistriIndex >= 0) { pair.Value.AddDistribution(newDis); } ExpendedNode.Add(currentID, product); } if (done) { int lowlinkV = preorder[currentID]; int preorderV = preorder[currentID]; bool selfLoop = false; //List<bool> temp = new List<bool>(outgoing.Count); int length = outgoing.Count; for (int i = 0; i < length; i++)//Distribution list in outgoing) { //temp[i] = true; Distribution list = outgoing[i];//note the order of distribution is reversed int templowlinkV = int.MaxValue; //bool tempUpdate = false; foreach (KeyValuePair<double, MDPState> state in list.States) { string w = state.Value.ID; if (w == currentID && list.States.Count == 1) { selfLoop = true; } if (!MDPState2DRAStateMapping.ContainsKey(w)) { //tempUpdate = true; if (preorder[w] > preorderV) { templowlinkV = Math.Min(templowlinkV, lowlink[w]); //lowlinkV = Math.Min(lowlinkV, lowlink[w]); } else { templowlinkV = Math.Min(templowlinkV, preorder[w]); //lowlinkV = Math.Min(lowlinkV, preorder[w]); } } else { templowlinkV = int.MaxValue; break; } } //if (tempUpdate) //{ //} if (templowlinkV == int.MaxValue) { //int index = stepStack.IndexOf(currentID + "separate" + i); //for (int index = stepStack.IndexOf(currentID + "separate" + (i)); index < stepStack.Count; ) //{ // if (stepStack[index] != currentID + "separate" + (i+1).ToString() && stepStack[index] != currentID + "end") // { // stepStack.RemoveAt(index); // } // else // { // break; // } //} bool remove = false; List<int> toRemove = new List<int>(); for (int index = stepStack.Count - 1; index >= 0; index--) { //counter++; if (stepStack[index].ID == currentID + "separate" + i) { remove = true; } if (remove) { if (stepStack[index].ID != currentID + "separate" + (i - 1) && stepStack[index].ID != currentID) { stepStack.RemoveAt(index); } else { break; } } else { if (stepStack[index].ID == currentID) { foreach(var ind in toRemove) { stepStack.RemoveAt(ind); } break; } toRemove.Add(index); } } } else { lowlinkV = Math.Min(templowlinkV, lowlinkV); } } lowlink[currentID] = lowlinkV; working.Pop(); if (lowlinkV == preorderV) { List<string> SCC = new List<string>(1024); SCC.Add(currentID); MDPState2DRAStateMapping.Add(currentID, DRAState); //while (stepStack.Count > 0 && preorder[stepStack[stepStack.Count - 1]] > preorderV) while(stepStack[stepStack.Count - 1].ID != currentID) { PCSPEventDRAPairMEC s = stepStack[stepStack.Count - 1]; string sID = s.ID; stepStack.RemoveAt(stepStack.Count - 1); if (!sID.Contains("separate") && !SCC.Contains(sID)) { SCC.Add(sID); MDPState2DRAStateMapping.Add(sID, s.state); //MDPState2DRAStateMapping.Add(sID, ); } } stepStack.RemoveAt(stepStack.Count - 1); if (SCC.Count > 1 || selfLoop) //evt.IsDeadLock || { SCCs.Add(SCC); } } //else //{ // stepStack.Push(pair.Key); //} } } while (working.Count > 0); Debug.WriteLine(mdp.States.Count); List<string> EndComponents = new List<string>(SCCs.Count); int count = DRA.acceptance().size(); int helper = 0; foreach (List<string> scc in SCCs) { //for debug //List<MDPState> debug = new List<MDPState>(); //List<int> drastates = new List<int>(); //foreach(string state in scc) //{ // debug.Add(mdp.States[state]); // drastates.Add(MDPState2DRAStateMapping[state]); //} //for debug //if(sentence.Count == 0) //{ //} for (int index = 0; index < count; index++) { List<string> newSCC = new List<string>(); List<string> targets = new List<string>(); if (AlmostFair) { //bool bottom = true; ////int SCCcount = scc.Count; ////note that as long as one SCC(might not be a real MEC) has a U state, the whole SCC cannot be targets. ////RemoveNonECStates(scc, targets); //foreach (string i in scc) //{ // int draState = MDPState2DRAStateMapping[i]; // if (bottom) // { // if (DRA.acceptance().isStateInAcceptance_U(index, draState)) // { // bottom = false; // } // } // newSCC.Add(i); // if (DRA.acceptance().isStateInAcceptance_L(index, draState)) // { // targets.Add(i); // } //} //if (bottom) //{ // if (!BottomECStates(newSCC, targets)) // { // if (newSCC.Count > 0) // { // GroupMEC(newSCC); // } // } // else // { // Common.Classes.Ultility.Ultility.AddList(EndComponents, scc); // } //} //else //{ // RemoveNonECStates(newSCC); // if (newSCC.Count > 0) // { // GroupMEC(newSCC); // } //} } else { foreach (string i in scc) { int draState = MDPState2DRAStateMapping[i]; if (!DRA.acceptance().isStateInAcceptance_U(index, draState)) { newSCC.Add(i); //note add by ssz if (DRA.acceptance().isStateInAcceptance_L(index, draState)) { targets.Add(i); } //note add by ssz } } if (targets.Count > 0) { RemoveNonECStates(newSCC, targets); } if (targets.Count > 0) { //List<string> endComponent = TarjanModelChecking(index, newSCC); Debug.WriteLine(helper++); //Common.Classes.Ultility.Ultility.AddList(EndComponents, endComponent); Common.Classes.Ultility.Ultility.AddList(EndComponents, scc);//note here newSCC changed to scc } else { if (scc.Count > 1) { GroupMEC(scc); } } } } } foreach (string s in EndComponents) { mdp.AddTargetStates(mdp.States[s]); } Debug.WriteLine(mdp.States.Count); VerificationOutput.NoOfStates = VerificationOutput.NoOfStates + mdp.States.Count; }
public void setUp() { fourByThreeMDP = MDPFactory.createFourByThreeMDP(); }
private MDP BuildMDP() { DeterministicAutomata specAutomaton = BuildDeterministicAutomata(InitSpecStep); Stack<Tuple> working = new Stack<Tuple>(1024); DeterministicFAState currentSpec = specAutomaton.InitialState; MDPState init = new MDPState(InitialStep.GetID() + Constants.SEPARATOR + currentSpec.GetID()); working.Push(new Tuple(InitialStep as MDPConfiguration, currentSpec, init)); MDP mdp = new MDP(init, Ultility.Ultility.DEFAULT_PRECISION, Ultility.Ultility.MAX_DIFFERENCE); do { if (CancelRequested) { VerificationOutput.NoOfStates = mdp.States.Count; return mdp; } // KeyValuePair<KeyValuePair<MDPConfiguration, DeterministicFAState>, MDPState> Tuple current = working.Pop(); if (current.SpecState == null) { mdp.AddTargetStates(current.MDPState); } else { IEnumerable<MDPConfiguration> list = current.ImplState.MakeOneMoveLocal(); VerificationOutput.Transitions += list.Count(); int currentDistriIndex = -1; Distribution newDis = new Distribution(Constants.TAU); //for (int i = 0; i < list.Count; i++) foreach (var step in list) { //MDPConfiguration step = list[i]; DeterministicFAState nextSpec = current.SpecState; if (step.Event != Constants.TAU) { nextSpec = current.SpecState.Next(step.Event); } //System.Diagnostics.Debug.Assert(nextSpec != null, "NextSpec is null!!"); //string stepID = step.GetID() + Constants.SEPARATOR + nextSpec.GetID().ToString(); string stepID = step.GetID() + Constants.SEPARATOR + (nextSpec == null ? "" : nextSpec.GetID().ToString()); MDPState nextState; if (!mdp.States.TryGetValue(stepID, out nextState)) { nextState = new MDPState(stepID); mdp.AddState(nextState); working.Push(new Tuple(step, nextSpec, nextState)); } if (step.DisIndex == -1) { if (currentDistriIndex >= 0) { current.MDPState.AddDistribution(newDis); newDis = new Distribution(Constants.TAU); } Distribution newTrivialDis = new Distribution(step.Event, nextState); current.MDPState.AddDistribution(newTrivialDis); } else if (currentDistriIndex != -1 && step.DisIndex != currentDistriIndex) { current.MDPState.AddDistribution(newDis); newDis = new Distribution(Constants.TAU); newDis.AddProbStatePair(step.Probability, nextState); } else { newDis.AddProbStatePair(step.Probability, nextState); } currentDistriIndex = step.DisIndex; } if (currentDistriIndex >= 0) { current.MDPState.AddDistribution(newDis); } } } while (working.Count > 0); VerificationOutput.NoOfStates = mdp.States.Count; //mdp.BackUpTargetStates(); return mdp; }
/// <summary> /// Run the verification and get the result. /// </summary> /// <returns></returns> public override void RunVerification() { if (ConstraintType == QueryConstraintType.NONE) { base.RunVerification(); return; } if (IsSafety) { RunVerificationSafety(); return; } if (!IsNegateLiveness) { RunVerificationNegate(); return; } if (SelectedEngineName == Constants.ENGINE_MDP_SEARCH) { switch (ConstraintType) { case QueryConstraintType.PROB: DRA = NegativeDRA; BuildMDP(); //note this function is just used to calculate the maximal probability. Min = 1 - mdp.MaxProbability(VerificationOutput); DRA = PositiveDRA; BuildMDP(); Max = mdp.MaxProbability(VerificationOutput); break; case QueryConstraintType.PMAX: DRA = PositiveDRA; BuildMDP(); Max = mdp.MaxProbability(VerificationOutput); break; case QueryConstraintType.PMIN: if (AlmostFair) //note add by ssz { DRA = PositiveDRA; BuildMDP(); Min = mdp.MinProbability(VerificationOutput); } else { DRA = NegativeDRA; BuildMDP(); Min = 1 - mdp.MaxProbability(VerificationOutput); } break; } } else if (SelectedEngineName == Constants.ENGINE_MDP_MEC_SEARCH) { switch (ConstraintType) { case QueryConstraintType.PROB: DRA = NegativeDRA; BuildMD_ImprovedTarjan(); //note this function is just used to calculate the maximal probability. Min = 1 - mdp.MaxProbability(VerificationOutput); DRA = PositiveDRA; BuildMD_ImprovedTarjan(); Max = mdp.MaxProbability(VerificationOutput); break; case QueryConstraintType.PMAX: DRA = PositiveDRA; //BuildMDP(); BuildMD_ImprovedTarjan(); Max = mdp.MaxProbability(VerificationOutput); break; case QueryConstraintType.PMIN: if (AlmostFair) //note add by ssz { DRA = PositiveDRA; BuildMD_ImprovedTarjan(); Min = mdp.MinProbability(VerificationOutput); } else { DRA = NegativeDRA; BuildMD_ImprovedTarjan(); Min = 1 - mdp.MaxProbability(VerificationOutput); } break; } } else if (SelectedEngineName == Constants.ENGINE_MDP_SIM_SEARCH) { switch (ConstraintType) { case QueryConstraintType.PROB: DRA = NegativeDRA; BuildMDP(); //note this function is just used to calculate the maximal probability. mdp = mdp.ComputeGCPP(VerificationOutput); Min = 1 - mdp.MaxProbability(VerificationOutput); DRA = PositiveDRA; BuildMDP(); mdp = mdp.ComputeGCPP(VerificationOutput); Max = mdp.MaxProbability(VerificationOutput); break; case QueryConstraintType.PMAX: DRA = PositiveDRA; BuildMDP(); mdp = mdp.ComputeGCPP(VerificationOutput); Max = mdp.MaxProbability(VerificationOutput); break; case QueryConstraintType.PMIN: if (AlmostFair) //note add by ssz { DRA = PositiveDRA; BuildMDP(); mdp = mdp.ComputeGCPP(VerificationOutput); Min = mdp.MinProbability(VerificationOutput); } else { DRA = NegativeDRA; BuildMDP(); mdp = mdp.ComputeGCPP(VerificationOutput); Min = 1 - mdp.MaxProbability(VerificationOutput); } break; } } if (Min == 1) { VerificationOutput.VerificationResult = VerificationResultType.VALID; } else if (Max == 0) { VerificationOutput.VerificationResult = VerificationResultType.INVALID; } else { VerificationOutput.VerificationResult = VerificationResultType.WITHPROBABILITY; } }
/// <summary> /// This method builds a MDP, while identifying SCCs. /// </summary> protected void BuildMDP_OldTarjan() { MDPState dummyInit = new MDPState(DUMMY_INIT); mdp = new MDP(dummyInit, Ultility.Ultility.DEFAULT_PRECISION, Ultility.Ultility.MAX_DIFFERENCE); List<PCSPEventDRAPair> initials = GetInitialPairs(InitialStep as MDPConfiguration); Stack<KeyValuePair<PCSPEventDRAPair, MDPState>> working = new Stack<KeyValuePair<PCSPEventDRAPair, MDPState>>(1024); Stack<PCSPEventDRAPair> stepStack = new Stack<PCSPEventDRAPair>(1024); MDPState2DRAStateMapping = new Dictionary<string, int>(Common.Classes.Ultility.Ultility.MC_INITIAL_SIZE); //build the MDP while identifying the SCCs List<List<string>> SCCs = new List<List<string>>(64); for (int z = 1; z <= initials.Count; z++) { PCSPEventDRAPair initState = initials[z - 1]; string stringID = initState.GetCompressedState(); MDPState newinit = new MDPState(stringID); mdp.AddState(newinit); dummyInit.AddDistribution(new Distribution(stringID, newinit)); //newinit.AddDistribution(new Distribution(Constants.TAU, newinit)); working.Push(new KeyValuePair<PCSPEventDRAPair, MDPState>(initState, newinit)); } Dictionary<string, int> preorder = new Dictionary<string, int>(Common.Classes.Ultility.Ultility.MC_INITIAL_SIZE); Dictionary<string, int> lowlink = new Dictionary<string, int>(Common.Classes.Ultility.Ultility.MC_INITIAL_SIZE); int preorderCounter = 0; Dictionary<string, List<PCSPEventDRAPair>> ExpendedNode = new Dictionary<string, List<PCSPEventDRAPair>>(); do { if (CancelRequested) { this.VerificationOutput.NoOfStates = this.VerificationOutput.NoOfStates + mdp.States.Count; return; } KeyValuePair<PCSPEventDRAPair, MDPState> pair = working.Peek(); MDPConfiguration evt = pair.Key.configuration; int DRAState = pair.Key.state; string currentID = pair.Key.GetCompressedState(); List<Distribution> outgoing = pair.Value.Distributions; if (!preorder.ContainsKey(currentID)) { preorder.Add(currentID, preorderCounter); preorderCounter++; } bool done = true; if (ExpendedNode.ContainsKey(currentID)) { List<PCSPEventDRAPair> list = ExpendedNode[currentID]; if (list.Count > 0) { for (int k = list.Count - 1; k >= 0; k--) { PCSPEventDRAPair step = list[k]; string stepID = step.GetCompressedState(); if (!preorder.ContainsKey(stepID)) { if (done) { working.Push(new KeyValuePair<PCSPEventDRAPair, MDPState>(step, mdp.States[stepID])); done = false; list.RemoveAt(k); } } else { list.RemoveAt(k); } } } } else { int currentDistriIndex = -1; Distribution newDis = new Distribution(Constants.TAU); MDPConfiguration[] steps = evt.MakeOneMoveLocal().ToArray(); //NOTE: here we play a trick for deadlock case: if a deadlock exist in the MDP, we will make a //self loop transition to remove the deadlock. Deadlock is meaningless in MDP. if(evt.IsDeadLock) { List<MDPConfiguration> stepsList = new List<MDPConfiguration>(steps); stepsList.Add(CreateSelfLoopStep(evt)); steps = stepsList.ToArray(); HasDeadLock = true; } List<PCSPEventDRAPair> product = Next(steps, DRAState); this.VerificationOutput.Transitions += product.Count; for (int k = product.Count - 1; k >= 0; k--) { PCSPEventDRAPair step = product[k]; string tmp = step.GetCompressedState(); //int nextIndex = VisitedWithID.Count; MDPState nextState; if (mdp.States.TryGetValue(tmp, out nextState)) { if (!preorder.ContainsKey(tmp)) { if (done) { working.Push(new KeyValuePair<PCSPEventDRAPair, MDPState>(step, nextState)); done = false; product.RemoveAt(k); } else { product[k] = step; } } else { product.RemoveAt(k); } } else { nextState = new MDPState(tmp); mdp.States.Add(tmp, nextState); if (done) { working.Push(new KeyValuePair<PCSPEventDRAPair, MDPState>(step, nextState)); done = false; product.RemoveAt(k); } else { product[k] = step; } } MDPConfiguration pstep = step.configuration; if (pstep.DisIndex == -1) { if (currentDistriIndex >= 0) { pair.Value.AddDistribution(newDis); newDis = new Distribution(Constants.TAU); } //note here changed by ssz //int draState = MDPState2DRAStateMapping[pair.Value.ID]; if(nextState == pair.Value) { for (int index = 0; index < DRA.acceptance().size(); index++) { if (DRA.acceptance().isStateInAcceptance_L(index, DRAState)) { mdp.AddTargetStates(pair.Value); } } } else { Distribution newTrivialDis = new Distribution(pstep.Event, nextState); pair.Value.AddDistribution(newTrivialDis); } //note here changed by ssz } else if (currentDistriIndex != -1 && pstep.DisIndex != currentDistriIndex) { pair.Value.AddDistribution(newDis); newDis = new Distribution(Constants.TAU); newDis.AddProbStatePair(pstep.Probability, nextState); } else { newDis.AddProbStatePair(pstep.Probability, nextState); } currentDistriIndex = pstep.DisIndex; } if (currentDistriIndex >= 0) { pair.Value.AddDistribution(newDis); } ExpendedNode.Add(currentID, product); } if (done) { int lowlinkV = preorder[currentID]; int preorderV = preorder[currentID]; bool selfLoop = false; foreach (Distribution list in outgoing) { foreach (KeyValuePair<double, MDPState> state in list.States) { string w = state.Value.ID; if (w == currentID) { selfLoop = true; } if (!MDPState2DRAStateMapping.ContainsKey(w)) { if (preorder[w] > preorderV) { lowlinkV = Math.Min(lowlinkV, lowlink[w]); } else { lowlinkV = Math.Min(lowlinkV, preorder[w]); } } } } lowlink[currentID] = lowlinkV; working.Pop(); if (lowlinkV == preorderV) { List<string> SCC = new List<string>(1024); SCC.Add(currentID); MDPState2DRAStateMapping.Add(currentID, DRAState); while (stepStack.Count > 0 && preorder[stepStack.Peek().GetCompressedState()] > preorderV) { PCSPEventDRAPair s = stepStack.Pop(); string sID = s.GetCompressedState(); SCC.Add(sID); MDPState2DRAStateMapping.Add(sID, s.state); } if (SCC.Count > 1 || selfLoop) //evt.IsDeadLock || { SCCs.Add(SCC); } } else { stepStack.Push(pair.Key); } } } while (working.Count > 0); List<string> EndComponents = new List<string>(SCCs.Count); int count = DRA.acceptance().size(); int helper = 0; foreach (List<string> scc in SCCs) { //for debug //List<MDPState> debug = new List<MDPState>(); //List<int> drastates = new List<int>(); //foreach(string state in scc) //{ // debug.Add(mdp.States[state]); // drastates.Add(MDPState2DRAStateMapping[state]); //} //for debug for (int index = 0; index < count; index++) { List<string> newSCC = new List<string>(); List<string> targets = new List<string>(); if (AlmostFair) { bool bottom = true; //int SCCcount = scc.Count; //note that as long as one SCC(might not be a real MEC) has a U state, the whole SCC cannot be targets. //RemoveNonECStates(scc, targets); foreach (string i in scc) { int draState = MDPState2DRAStateMapping[i]; if (bottom) { if (DRA.acceptance().isStateInAcceptance_U(index, draState)) { bottom = false; } } newSCC.Add(i); if (DRA.acceptance().isStateInAcceptance_L(index, draState)) { targets.Add(i); } } if(bottom) { if (!BottomECStates(newSCC, targets)) { if (newSCC.Count > 0) { GroupMEC(newSCC); } } else { Common.Classes.Ultility.Ultility.AddList(EndComponents, scc); } } else { RemoveNonECStates(newSCC); if(newSCC.Count>0) { GroupMEC(newSCC); } } } else { foreach (string i in scc) { int draState = MDPState2DRAStateMapping[i]; if (!DRA.acceptance().isStateInAcceptance_U(index, draState)) { newSCC.Add(i); //note add by ssz if (DRA.acceptance().isStateInAcceptance_L(index, draState)) { targets.Add(i); } //note add by ssz } } //if (AlmostFair) //{ // RemoveNonECStates(newSCC, targets); // if (newSCC.Count > 0) // { // GroupMEC(newSCC); // } // if (newSCC.Count > 0) // { // } //} //else //{ if (targets.Count > 0) { RemoveNonECStates(newSCC, targets); } else { continue; } if (targets.Count > 0) { //List<string> endComponent = TarjanModelChecking(index, newSCC); Debug.WriteLine(helper++); //Common.Classes.Ultility.Ultility.AddList(EndComponents, endComponent); Common.Classes.Ultility.Ultility.AddList(EndComponents, newSCC); } //} } } } foreach (string s in EndComponents) { mdp.AddTargetStates(mdp.States[s]); } VerificationOutput.NoOfStates = VerificationOutput.NoOfStates + mdp.States.Count; //mdp.BackUpTargetStates(); }
private MDP BuildMDP() { Stack <KeyValuePair <MDPConfiguration, MDPState> > working = new Stack <KeyValuePair <MDPConfiguration, MDPState> >(1024); string initID = InitialStep.GetID(); MDPState init = new MDPState(initID); working.Push(new KeyValuePair <MDPConfiguration, MDPState>(InitialStep as MDPConfiguration, init)); MDP mdp = new MDP(init, Ultility.Ultility.DEFAULT_PRECISION, Ultility.Ultility.MAX_DIFFERENCE); do { if (CancelRequested) { VerificationOutput.NoOfStates = mdp.States.Count; return(mdp); } KeyValuePair <MDPConfiguration, MDPState> current = working.Pop(); IEnumerable <MDPConfiguration> list = current.Key.MakeOneMoveLocal(); VerificationOutput.Transitions += list.Count(); //check if it is one of the target state //if (list.Length == 0) if (current.Key.IsDeadLock) { if (isNotTerminationTesting || current.Key.Event != Constants.TERMINATION) { mdp.AddTargetStates(current.Value); } } else { int currentDistriIndex = -1; Distribution newDis = new Distribution(Constants.TAU); //for (int i = 0; i < list.Length; i++) foreach (var step in list) { //MDPConfiguration step = list[i]; string stepID = step.GetID(); MDPState nextState; if (!mdp.States.TryGetValue(stepID, out nextState)) { nextState = new MDPState(stepID); mdp.AddState(nextState); working.Push(new KeyValuePair <MDPConfiguration, MDPState>(step, nextState)); } if (step.DisIndex == -1) { if (currentDistriIndex >= 0) { current.Value.AddDistribution(newDis); newDis = new Distribution(Constants.TAU); } Distribution newTrivialDis = new Distribution(step.Event); newTrivialDis.AddProbStatePair(1, nextState); current.Value.AddDistribution(newTrivialDis); } else if (currentDistriIndex != -1 && step.DisIndex != currentDistriIndex) { current.Value.AddDistribution(newDis); newDis = new Distribution(Constants.TAU); newDis.AddProbStatePair(step.Probability, nextState); } else { newDis.AddProbStatePair(step.Probability, nextState); } currentDistriIndex = step.DisIndex; } if (currentDistriIndex >= 0) { current.Value.AddDistribution(newDis); } } } while (working.Count > 0); VerificationOutput.NoOfStates = mdp.States.Count; //mdp.BackUpTargetStates(); return(mdp); }
/// <summary> /// Run the verification and get the result. /// </summary> /// <returns></returns> public override void RunVerification() { if (ConstraintType == QueryConstraintType.NONE) { base.RunVerification(); return; } MDP mdp = null; if (SelectedEngineName == Constants.ENGINE_MDP_SEARCH) { mdp = BuildMDP(); //GetTransitionRelation(); } else if (SelectedEngineName == Constants.ENGINE_MDP_ANTICHAIN_SEARCH) { mdp = BuildMDPSubset(); //mdp = BuildMDPAntiChain_L(); //mdp = BuildMDPAntiChain_S(); } // if (!CancelRequested) { if (SelectedEngineName == Constants.ENGINE_MDP_SEARCH) { switch (ConstraintType) { case QueryConstraintType.PROB: Min = 1 - mdp.MaxProbability(VerificationOutput); mdp.ResetNonTargetState(); Max = 1 - mdp.MinProbability(VerificationOutput); break; case QueryConstraintType.PMAX: Max = 1 - mdp.MinProbability(VerificationOutput); break; case QueryConstraintType.PMIN: Min = 1 - mdp.MaxProbability(VerificationOutput); break; } } else if (SelectedEngineName == Constants.ENGINE_MDP_ANTICHAIN_SEARCH) { switch (ConstraintType) { case QueryConstraintType.PROB: Min = 1 - mdp.MaxProbability(VerificationOutput, true); mdp.ResetNonTargetState(); Max = 1 - mdp.MinProbability(VerificationOutput, true); break; case QueryConstraintType.PMAX: Max = 1 - mdp.MinProbability(VerificationOutput, true); break; case QueryConstraintType.PMIN: Min = 1 - mdp.MaxProbability(VerificationOutput, true); break; } } if (Min == 1) { VerificationOutput.VerificationResult = VerificationResultType.VALID; } else if (Max == 0) { VerificationOutput.VerificationResult = VerificationResultType.INVALID; } else { VerificationOutput.VerificationResult = VerificationResultType.WITHPROBABILITY; } } }
private MDP BuildMDPSubset() { DeterministicAutomata_Subset specAutomaton = BuildDeterministicAutomata_Subset(InitSpecStep); Stack <TupleSub> working = new Stack <TupleSub>(1024); DeterministicFAState_Subset InitialSpec = specAutomaton.InitialState; Dictionary <string, Dictionary <int, MDPState> > visited = new Dictionary <string, Dictionary <int, MDPState> >(); int specID = InitialSpec.GetID(); string impID = InitialStep.GetID(); MDPState init = new MDPState(impID + Constants.SEPARATOR + specID); working.Push(new TupleSub(InitialStep as MDPConfiguration, InitialSpec, init)); Dictionary <int, MDPState> initialDic = new Dictionary <int, MDPState>(); initialDic.Add(specID, init); visited.Add(impID, initialDic); MDP mdp = new MDP(init, Ultility.Ultility.DEFAULT_PRECISION, Ultility.Ultility.MAX_DIFFERENCE); do { if (CancelRequested) { VerificationOutput.NoOfStates = mdp.States.Count; return(mdp); } TupleSub current = working.Pop(); if (current.SpecState == null) { mdp.AddTargetStates(current.MDPState); } else { IEnumerable <MDPConfiguration> list = current.ImplState.MakeOneMoveLocal(); VerificationOutput.Transitions += list.Count(); int currentDistriIndex = -1; Distribution newDis = new Distribution(Constants.TAU); //for (int i = 0; i < list.Count; i++) foreach (var step in list) { //MDPConfiguration step = list[i]; DeterministicFAState_Subset nextSpec = current.SpecState; if (step.Event != Constants.TAU) { nextSpec = current.SpecState.Next(step.Event); } //System.Diagnostics.Debug.Assert(nextSpec != null, "NextSpec is null!!"); //string stepID = step.GetID() + Constants.SEPARATOR + nextSpec.GetID().ToString(); impID = step.GetID(); string stepID = impID + Constants.SEPARATOR + (nextSpec == null ? "" : nextSpec.GetID().ToString()); MDPState nextState; if (!mdp.States.TryGetValue(stepID, out nextState)) { nextState = new MDPState(stepID); //mdp.AddState(nextState); //KeyValuePair<MDPConfiguration, DeterministicFAState_Subset> newPair = // new KeyValuePair<MDPConfiguration, DeterministicFAState_Subset>(step, nextSpec); working.Push(new TupleSub(step, nextSpec, nextState)); if (nextSpec != null) { specID = nextSpec.GetID(); //DeterministicFAState_Subset DFAstate = nextSpec; Dictionary <int, MDPState> mapping = null; if (visited.TryGetValue(impID, out mapping)) { //int Spec = nextSpec.GetID(); mapping.Add(specID, nextState); foreach (int spec in mapping.Keys) { if (specAutomaton.States[spec].Sub.Contains(nextSpec)) { mapping[spec].Sub.Add(nextState); } else if (nextSpec.Sub.Contains(specAutomaton.States[spec])) { nextState.Sub.Add(mapping[spec]); } } //foreach (var x in visited) //{ // if (x.Key.Key.GetID() == newPair.Key.GetID() && // x.SpecState.Sub.Contains(newPair.Value)) // { // x.Value.Sub.Add(nextState); // } // else if (x.Key.Key.GetID() == newPair.Key.GetID() && // newPair.Value.Sub.Contains(x.SpecState)) // { // nextState.Sub.Add(x.Value); // } //} //visited.Add(newPair, nextState); } else { Dictionary <int, MDPState> newDicDic = new Dictionary <int, MDPState>(); newDicDic.Add(specID, nextState); visited.Add(impID, newDicDic); } } mdp.AddState(nextState); } if (step.DisIndex == -1) { if (currentDistriIndex >= 0) { current.MDPState.AddDistribution(newDis); newDis = new Distribution(Constants.TAU); } Distribution newTrivialDis = new Distribution(step.Event, nextState); current.MDPState.AddDistribution(newTrivialDis); } else if (currentDistriIndex != -1 && step.DisIndex != currentDistriIndex) { current.MDPState.AddDistribution(newDis); newDis = new Distribution(Constants.TAU); newDis.AddProbStatePair(step.Probability, nextState); } else { newDis.AddProbStatePair(step.Probability, nextState); } currentDistriIndex = step.DisIndex; } if (currentDistriIndex >= 0) { current.MDPState.AddDistribution(newDis); } } } while (working.Count > 0); VerificationOutput.NoOfStates = mdp.States.Count; //mdp.BackUpTargetStates(); return(mdp); }
private MDP BuildMDP() { DeterministicAutomata specAutomaton = BuildDeterministicAutomata(InitSpecStep); Stack <Tuple> working = new Stack <Tuple>(1024); DeterministicFAState currentSpec = specAutomaton.InitialState; MDPState init = new MDPState(InitialStep.GetID() + Constants.SEPARATOR + currentSpec.GetID()); working.Push(new Tuple(InitialStep as MDPConfiguration, currentSpec, init)); MDP mdp = new MDP(init, Ultility.Ultility.DEFAULT_PRECISION, Ultility.Ultility.MAX_DIFFERENCE); do { if (CancelRequested) { VerificationOutput.NoOfStates = mdp.States.Count; return(mdp); } // KeyValuePair<KeyValuePair<MDPConfiguration, DeterministicFAState>, MDPState> Tuple current = working.Pop(); if (current.SpecState == null) { mdp.AddTargetStates(current.MDPState); } else { IEnumerable <MDPConfiguration> list = current.ImplState.MakeOneMoveLocal(); VerificationOutput.Transitions += list.Count(); int currentDistriIndex = -1; Distribution newDis = new Distribution(Constants.TAU); //for (int i = 0; i < list.Count; i++) foreach (var step in list) { //MDPConfiguration step = list[i]; DeterministicFAState nextSpec = current.SpecState; if (step.Event != Constants.TAU) { nextSpec = current.SpecState.Next(step.Event); } //System.Diagnostics.Debug.Assert(nextSpec != null, "NextSpec is null!!"); //string stepID = step.GetID() + Constants.SEPARATOR + nextSpec.GetID().ToString(); string stepID = step.GetID() + Constants.SEPARATOR + (nextSpec == null ? "" : nextSpec.GetID().ToString()); MDPState nextState; if (!mdp.States.TryGetValue(stepID, out nextState)) { nextState = new MDPState(stepID); mdp.AddState(nextState); working.Push(new Tuple(step, nextSpec, nextState)); } if (step.DisIndex == -1) { if (currentDistriIndex >= 0) { current.MDPState.AddDistribution(newDis); newDis = new Distribution(Constants.TAU); } Distribution newTrivialDis = new Distribution(step.Event, nextState); current.MDPState.AddDistribution(newTrivialDis); } else if (currentDistriIndex != -1 && step.DisIndex != currentDistriIndex) { current.MDPState.AddDistribution(newDis); newDis = new Distribution(Constants.TAU); newDis.AddProbStatePair(step.Probability, nextState); } else { newDis.AddProbStatePair(step.Probability, nextState); } currentDistriIndex = step.DisIndex; } if (currentDistriIndex >= 0) { current.MDPState.AddDistribution(newDis); } } } while (working.Count > 0); VerificationOutput.NoOfStates = mdp.States.Count; //mdp.BackUpTargetStates(); return(mdp); }
protected void btnConfirmer_Click(object sender, EventArgs e) { // Reset everything Courriel1.DefaultControl(); Courriel2.DefaultControl(); MDP.DefaultControl(); MDP2.DefaultControl(); tbNomAffaires.DefaultControl(); tbPrenom.DefaultControl(); tbNom.DefaultControl(); tbRue.DefaultControl(); tbVille.DefaultControl(); tbCodePostal.DefaultControl(); tbTelephone.DefaultControl(); tbTelephone2.DefaultControl(); tbPoidsMax.DefaultControl(); tbPrixLivGratuite.DefaultControl(); int poidsMax = -1; decimal prixLivGratuit = -1; // Check for errors bool[] arrError = new bool[] { // Courriel Courriel1.InvalidateIfEmpty(lblErrorCourriel1, "Le courriel doit être présent") || Courriel1.CheckFormatCourriel(lblErrorCourriel1) || Courriel1.CheckMatch(Courriel2, lblErrorCourriel1, lblErrorCourriel2, "Les courriels ne sont pas identiques"), Courriel2.InvalidateIfEmpty(lblErrorCourriel2, "La confirmation de courriel doit être présente") || Courriel2.CheckFormatCourriel(lblErrorCourriel2), // Mot de passe MDP.InvalidateIfEmpty(lblErrorMDP, "Le mot de passe doit être présent") || MDP.CheckMatch(MDP2, lblErrorMDP, lblErrorMDP2, "Les mots de passes ne sont pas identiques"), MDP2.InvalidateIfEmpty(lblErrorMDP2, "La confirmation de mot de passe doit être présente"), // Nom d'affaires tbNomAffaires.InvalidateIfEmpty(lblErrorNomAffaires, "Le nom d'affaires doit être présent"), tbPrenom.InvalidateIfEmpty(lblErrorPrenom, "Le prénom doit être présent") || tbPrenom.CheckFormatNomPrenom(lblErrorPrenom), tbNom.InvalidateIfEmpty(lblErrorNom, "Le nom doit être présent") || tbNom.CheckFormatNomPrenom(lblErrorNom), tbRue.InvalidateIfEmpty(lblErrorRue, "Les informations sur la rue (No Civique et Rue)") || tbRue.CheckFormatNomPrenom(lblErrorRue), tbVille.InvalidateIfEmpty(lblErrorVille, "Le nom de la ville doit être présent") || tbVille.CheckFormatNomPrenom(lblErrorVille), tbCodePostal.InvalidateIfEmpty(lblErrorCodePostal, "Le code postal doit être présent") || tbCodePostal.CheckContains(lblErrorCodePostal, "Le code postal doit être entré au complet", "_"), tbTelephone.InvalidateIfEmpty(lblErrorTelephone, "Le numéro de téléphone doit être présent") || tbTelephone.CheckContains(lblErrorTelephone, "Le numéro de téléphone doit être entré au complet", "_"), tbTelephone2.CheckContains(lblErrorTelephone2, "Le numéro de téléphone doit être entré au complet", "_"), // Poids Max tbPoidsMax.InvalidateIfEmpty(lblErrorPoidsMax, "Le poids maximal pour une livraison doit être présent") || tbPoidsMax.CheckInt(lblErrorPoidsMax, "Le poids maximal doit être un nomber entier", out poidsMax) || tbPoidsMax.CheckIntOver0(lblErrorPoidsMax, poidsMax), // Poids livraison gratuite //tbPrixLivGratuite.InvalidateIfEmpty(lblErrorPoidsLivGratuit, "Le poids auquel une livraison devient gratuite doit être présent") tbPrixLivGratuite.Text != "" && ( tbPrixLivGratuite.CheckDecimal(lblErrorPoidsLivGratuit, "Le prix auquel une livraison devient gratuite doit être un nombre décimal", out prixLivGratuit) || tbPrixLivGratuite.CheckDecimalOver0(lblErrorPoidsLivGratuit, prixLivGratuit) ) }; if (!arrError.Contains(true)) { PPVendeurs vendeurs = new PPVendeurs(); PPClients clients = new PPClients(); PPGestionnaires gestionnaires = new PPGestionnaires(); bool errorsDB = false; if (vendeurs.Values.Any(x => x.AdresseEmail == Courriel1.Text)) { Courriel1.Invalidate(); lblErrorCourriel1.Text = "Ce courriel est déjà utilisé par un vendeur"; errorsDB = true; } else if (clients.Values.Any(x => x.AdresseEmail == Courriel1.Text)) { Courriel1.Invalidate(); lblErrorCourriel1.Text = "Ce courriel est déjà utilisé par un client"; errorsDB = true; } else if (gestionnaires.Values.Any(x => x.Email == Courriel1.Text)) { Courriel1.Invalidate(); lblErrorCourriel1.Text = "Ce courriel est déjà utilisé par un gestionnaire"; errorsDB = true; } if (vendeurs.Values.Any(x => x.NomAffaires == tbNomAffaires.Text)) { tbNomAffaires.Invalidate(); lblErrorNomAffaires.Text = "Ce nom d'affaires est déjà utilisé par un autre vendeur"; errorsDB = true; } if (!errorsDB) { Vendeur newVendeur = new Vendeur(null) { NoVendeur = vendeurs.NextId(), NomAffaires = tbNomAffaires.Text, Nom = tbNom.Text, Prenom = tbPrenom.Text, Rue = tbRue.Text, Ville = tbVille.Text, Province = ddlProvince.SelectedValue, CodePostal = tbCodePostal.Text.ToUpper(), Pays = tbPays.Text, Tel1 = tbTelephone.Text, Tel2 = tbTelephone2.Text.ToStringEmptyNull(), AdresseEmail = Courriel1.Text, MotDePasse = MDP.Text, PoidsMaxLivraison = poidsMax, LivraisonGratuite = prixLivGratuit <= 0 ? null : (decimal?)prixLivGratuit, Taxes = rbTaxesOui.Checked, DateCreation = DateTime.Now, Statut = 0 }; vendeurs.Add(newVendeur); Response.Redirect("~/Pages/InscriptionVendeur.aspx?Reussite=true"); } } }
private MDP BuildMDP() { Stack<KeyValuePair<MDPConfiguration, MDPState>> working = new Stack<KeyValuePair<MDPConfiguration, MDPState>>(1024); string initID = InitialStep.GetID(); MDPState init = new MDPState(initID); working.Push(new KeyValuePair<MDPConfiguration, MDPState>(InitialStep as MDPConfiguration, init)); MDP mdp = new MDP(init, Ultility.Ultility.DEFAULT_PRECISION, Ultility.Ultility.MAX_DIFFERENCE); do { if (CancelRequested) { VerificationOutput.NoOfStates = mdp.States.Count; return mdp; } KeyValuePair<MDPConfiguration, MDPState> current = working.Pop(); IEnumerable<MDPConfiguration> list = current.Key.MakeOneMoveLocal(); VerificationOutput.Transitions += list.Count(); //check if it is one of the target state //if (list.Length == 0) if(current.Key.IsDeadLock) { if (isNotTerminationTesting || current.Key.Event != Constants.TERMINATION) { mdp.AddTargetStates(current.Value); } } else { int currentDistriIndex = -1; Distribution newDis = new Distribution(Constants.TAU); //for (int i = 0; i < list.Length; i++) foreach (var step in list) { //MDPConfiguration step = list[i]; string stepID = step.GetID(); MDPState nextState; if (!mdp.States.TryGetValue(stepID, out nextState)) { nextState = new MDPState(stepID); mdp.AddState(nextState); working.Push(new KeyValuePair<MDPConfiguration, MDPState>(step, nextState)); } if (step.DisIndex == -1) { if (currentDistriIndex >= 0) { current.Value.AddDistribution(newDis); newDis = new Distribution(Constants.TAU); } Distribution newTrivialDis = new Distribution(step.Event); newTrivialDis.AddProbStatePair(1, nextState); current.Value.AddDistribution(newTrivialDis); } else if (currentDistriIndex != -1 && step.DisIndex != currentDistriIndex) { current.Value.AddDistribution(newDis); newDis = new Distribution(Constants.TAU); newDis.AddProbStatePair(step.Probability, nextState); } else { newDis.AddProbStatePair(step.Probability, nextState); } currentDistriIndex = step.DisIndex; } if (currentDistriIndex >= 0) { current.Value.AddDistribution(newDis); } } } while (working.Count > 0); VerificationOutput.NoOfStates = mdp.States.Count; //mdp.BackUpTargetStates(); return mdp; }
public MDPAgent(MDP <STATE_TYPE, ACTION_TYPE> mdp) { this.mdp = mdp; this.currentState = mdp.getInitialState(); this.currentReward = mdp.getRewardFor(currentState); }
private void BuildMDPSafety() { Stack <KeyValuePair <EventBAPairSafetyPCSP, MDPState> > working = new Stack <KeyValuePair <EventBAPairSafetyPCSP, MDPState> >(1024); EventBAPairSafetyPCSP initialstep = EventBAPairSafetyPCSP.GetInitialPairs(BA, InitialStep as MDPConfiguration); string initID = initialstep.GetID(); MDPState init = new MDPState(initID); working.Push(new KeyValuePair <EventBAPairSafetyPCSP, MDPState>(initialstep, init)); mdp = new MDP(init, Ultility.Ultility.DEFAULT_PRECISION, Ultility.Ultility.MAX_DIFFERENCE); do { if (CancelRequested) { VerificationOutput.NoOfStates = mdp.States.Count; } KeyValuePair <EventBAPairSafetyPCSP, MDPState> current = working.Pop(); if (current.Key.BAStates.Count == 0) { mdp.AddTargetStates(current.Value); } else { MDPConfiguration[] steps = current.Key.Config.MakeOneMoveLocal().ToArray(); this.VerificationOutput.Transitions += steps.Length; List <EventBAPairSafetyPCSP> products = EventBAPairSafetyPCSP.Next(BA, steps, current.Key.BAStates); int currentDistriIndex = -1; Distribution newDis = new Distribution(Constants.TAU); foreach (EventBAPairSafetyPCSP eventBaPairSafetyPcsp in products) { string stepID = eventBaPairSafetyPcsp.GetID(); MDPState nextState; if (!mdp.States.TryGetValue(stepID, out nextState)) { nextState = new MDPState(stepID); mdp.AddState(nextState); working.Push(new KeyValuePair <EventBAPairSafetyPCSP, MDPState>(eventBaPairSafetyPcsp, nextState)); } if (eventBaPairSafetyPcsp.Config.DisIndex == -1) { if (currentDistriIndex >= 0) { current.Value.AddDistribution(newDis); newDis = new Distribution(Constants.TAU); } Distribution newTrivialDis = new Distribution(eventBaPairSafetyPcsp.Config.Event); newTrivialDis.AddProbStatePair(1, nextState); current.Value.AddDistribution(newTrivialDis); } else if (currentDistriIndex != -1 && eventBaPairSafetyPcsp.Config.DisIndex != currentDistriIndex) { current.Value.AddDistribution(newDis); newDis = new Distribution(Constants.TAU); newDis.AddProbStatePair(eventBaPairSafetyPcsp.Config.Probability, nextState); } else { newDis.AddProbStatePair(eventBaPairSafetyPcsp.Config.Probability, nextState); } currentDistriIndex = eventBaPairSafetyPcsp.Config.DisIndex; } if (currentDistriIndex >= 0) { current.Value.AddDistribution(newDis); } } } while (working.Count > 0); VerificationOutput.NoOfStates = mdp.States.Count; //mdp.BackUpTargetStates(); }
private void BuildMDPSafety() { Stack<KeyValuePair<EventBAPairSafetyPCSP, MDPState>> working = new Stack<KeyValuePair<EventBAPairSafetyPCSP, MDPState>>(1024); EventBAPairSafetyPCSP initialstep = EventBAPairSafetyPCSP.GetInitialPairs(BA, InitialStep as MDPConfiguration); string initID = initialstep.GetID(); MDPState init = new MDPState(initID); working.Push(new KeyValuePair<EventBAPairSafetyPCSP, MDPState>(initialstep, init)); mdp = new MDP(init, Ultility.Ultility.DEFAULT_PRECISION, Ultility.Ultility.MAX_DIFFERENCE); do { if (CancelRequested) { VerificationOutput.NoOfStates = mdp.States.Count; } KeyValuePair<EventBAPairSafetyPCSP, MDPState> current = working.Pop(); if (current.Key.BAStates.Count == 0) { mdp.AddTargetStates(current.Value); } else { MDPConfiguration[] steps = current.Key.Config.MakeOneMoveLocal().ToArray(); this.VerificationOutput.Transitions += steps.Length; List<EventBAPairSafetyPCSP> products = EventBAPairSafetyPCSP.Next(BA, steps, current.Key.BAStates); int currentDistriIndex = -1; Distribution newDis = new Distribution(Constants.TAU); foreach (EventBAPairSafetyPCSP eventBaPairSafetyPcsp in products) { string stepID = eventBaPairSafetyPcsp.GetID(); MDPState nextState; if (!mdp.States.TryGetValue(stepID, out nextState)) { nextState = new MDPState(stepID); mdp.AddState(nextState); working.Push(new KeyValuePair<EventBAPairSafetyPCSP, MDPState>(eventBaPairSafetyPcsp, nextState)); } if (eventBaPairSafetyPcsp.Config.DisIndex == -1) { if (currentDistriIndex >= 0) { current.Value.AddDistribution(newDis); newDis = new Distribution(Constants.TAU); } Distribution newTrivialDis = new Distribution(eventBaPairSafetyPcsp.Config.Event); newTrivialDis.AddProbStatePair(1, nextState); current.Value.AddDistribution(newTrivialDis); } else if (currentDistriIndex != -1 && eventBaPairSafetyPcsp.Config.DisIndex != currentDistriIndex) { current.Value.AddDistribution(newDis); newDis = new Distribution(Constants.TAU); newDis.AddProbStatePair(eventBaPairSafetyPcsp.Config.Probability, nextState); } else { newDis.AddProbStatePair(eventBaPairSafetyPcsp.Config.Probability, nextState); } currentDistriIndex = eventBaPairSafetyPcsp.Config.DisIndex; } if (currentDistriIndex >= 0) { current.Value.AddDistribution(newDis); } } } while (working.Count > 0); VerificationOutput.NoOfStates = mdp.States.Count; //mdp.BackUpTargetStates(); }
private MDP BuildMDPAntiChain_S() { DeterministicAutomata_Subset specAutomaton = BuildDeterministicAutomata_Subset(InitSpecStep); Stack <TupleSub> working = new Stack <TupleSub>(1024); DeterministicFAState_Subset InitialSpec = specAutomaton.InitialState; Dictionary <string, HashSet <DeterministicFAState_Subset> > antichain = new Dictionary <string, HashSet <DeterministicFAState_Subset> >(); string impID = InitialStep.GetID(); int specID = InitialSpec.GetID(); MDPState init = new MDPState(impID + Constants.SEPARATOR + specID); working.Push(new TupleSub(InitialStep as MDPConfiguration, InitialSpec, init)); MDP mdp = new MDP(init, Ultility.Ultility.DEFAULT_PRECISION, Ultility.Ultility.MAX_DIFFERENCE); HashSet <DeterministicFAState_Subset> Specs = new HashSet <DeterministicFAState_Subset>(); Specs.Add(InitialSpec); antichain.Add(impID, Specs); do { if (CancelRequested) { VerificationOutput.NoOfStates = mdp.States.Count; return(mdp); } TupleSub current = working.Pop(); if (current.SpecState == null) { mdp.AddTargetStates(current.MDPState); } else { IEnumerable <MDPConfiguration> list = current.ImplState.MakeOneMoveLocal(); VerificationOutput.Transitions += list.Count(); int currentDistriIndex = -1; Distribution newDis = new Distribution(Constants.TAU); //for (int i = 0; i < list.Count; i++) foreach (MDPConfiguration step in list) { //MDPConfiguration step = list[i]; DeterministicFAState_Subset nextSpec = current.SpecState; if (step.Event != Constants.TAU) { nextSpec = current.SpecState.Next(step.Event); } //System.Diagnostics.Debug.Assert(nextSpec != null, "NextSpec is null!!"); //string stepID = step.GetID() + Constants.SEPARATOR + nextSpec.GetID().ToString(); impID = step.GetID(); string stepID = impID + Constants.SEPARATOR + (nextSpec == null ? "" : nextSpec.GetID().ToString()); MDPState nextState; if (!mdp.States.TryGetValue(stepID, out nextState)) { nextState = new MDPState(stepID); //mdp.AddState(nextState); //KeyValuePair<MDPConfiguration, DeterministicFAState_Subset> newPair = // new KeyValuePair<MDPConfiguration, DeterministicFAState_Subset>(step, nextSpec); //working.Push(new TupleSub(step, nextSpec, nextState)); bool addstate = true; if (nextSpec != null) { //specID = nextSpec.GetID(); //DeterministicFAState_Subset DFAstate = nextSpec; HashSet <DeterministicFAState_Subset> specStates; //Dictionary<int, MDPState> mapping = null; if (antichain.TryGetValue(impID, out specStates)) { HashSet <DeterministicFAState_Subset> toRemove = new HashSet <DeterministicFAState_Subset>(); //int Spec = nextSpec.GetID(); bool chainIncrease = true; foreach (DeterministicFAState_Subset specState in specStates) { //DeterministicFAState_Subset specState = specStates[j]; if (specState.Sub.Contains(nextSpec)) { toRemove.Add(specState); nextSpec.Sub.Add(specState); chainIncrease = false; } if (nextSpec.Sub.Contains(specState)) { nextSpec = specState; addstate = false; chainIncrease = false; break; } } if (toRemove.Count > 0) { foreach (var specState in toRemove) { specStates.Remove(specState); } specStates.Add(nextSpec); // toRemove; } else if (chainIncrease) { specStates.Add(nextSpec); } } else { HashSet <DeterministicFAState_Subset> newSpecs = new HashSet <DeterministicFAState_Subset>(); newSpecs.Add(nextSpec); antichain.Add(impID, newSpecs); } } if (addstate) { mdp.AddState(nextState); working.Push(new TupleSub(step, nextSpec, nextState)); } } if (step.DisIndex == -1) { if (currentDistriIndex >= 0) { current.MDPState.AddDistribution(newDis); newDis = new Distribution(Constants.TAU); } Distribution newTrivialDis = new Distribution(step.Event, nextState); current.MDPState.AddDistribution(newTrivialDis); } else if (currentDistriIndex != -1 && step.DisIndex != currentDistriIndex) { current.MDPState.AddDistribution(newDis); newDis = new Distribution(Constants.TAU); newDis.AddProbStatePair(step.Probability, nextState); } else { newDis.AddProbStatePair(step.Probability, nextState); } currentDistriIndex = step.DisIndex; } if (currentDistriIndex >= 0) { current.MDPState.AddDistribution(newDis); } } } while (working.Count > 0); VerificationOutput.NoOfStates = mdp.States.Count; //mdp.BackUpTargetStates(); return(mdp); }
public MDPAgent(MDP <TState, TAction> mdp) { this.MDP = mdp; this.CurrentState = mdp.GetInitialState(); this.CurrentReward = mdp.GetRewardFor(this.CurrentState); }
protected void b_grabar_Click(object sender, EventArgs e) { String Sql, SqlCertif, SqlCertifMdp; SqlConnection SqlConn = fn.ConnectionSql(); String MDP, Cortesia, Local; //Se verifica que exista detalle para la session actual Sql = "select count(ncertificado) from tblCertifDet where Puesto='" + Session["idPuesto"] + "' and idPais='" + Session["idPais"] + "' and ncertificado='" + l_n_certificado.Text + "' and session='" + Session["session"] + "'"; Double cert_det = fn.EjecutarScalarDouble(Sql, SqlConn); Double total = fn.TotalCertificado(l_n_certificado.Text, Session["idPuesto"].ToString(), Session["idPais"].ToString(), SqlConn); if (Convert.ToDouble(tb_subtotalg.Text) == Math.Round(total, 2) && cert_det == Convert.ToDouble(l_servicios.Text) && cert_det > 0 && ddl_cliente.SelectedValue.Trim() != "" && ddl_cuarentena.SelectedValue.Trim() != "" && tb_fecha_tratamiento.Text.Trim().Length != 0) { //Se graba en la tblCertifMDP if (cb_cliente_contado.Checked == true) { MDP = "0"; } else { MDP = "1"; } if (cb_cortesia.Checked == true) { Cortesia = "1"; } else { Cortesia = "0"; } if (cb_local.Checked == true) { Local = "1"; } else { Local = "0"; } if (Convert.ToDouble(tb_recargo.Text) > 0) { Sql = "insert into tblCertifDet (Puesto,Ncertificado,Servicio,Cantidad,US,local,subtotal,DB,idPais) values ('" + Session["idPuesto"] + "','" + l_n_certificado.Text + "','"; Sql = Sql + fn.ValIni(Session["idPais"].ToString(), "CuentaRecargo", SqlConn) + "',1," + tb_recargo.Text + "," + tb_recargo.Text + "," + tb_recargo.Text + ",0,'" + Session["idPais"] + "')"; fn.EjecutarNonQuery(Sql, SqlConn); } SqlCertifMdp = "insert into tblCertifMDP (Puesto, NCertificado, MDP, Cantidad, Abono, SaldoNuevo, IDAbono, NEnd, idPais) values ('" + Session["idPuesto"] + "','" + l_n_certificado.Text + "',"; SqlCertifMdp = SqlCertifMdp + MDP.ToString() + "," + tb_total.Text + ",0," + tb_total.Text + ",0,0,'" + Session["idPais"] + "')"; //Se graba en la tblCertif String nplaca; if (tb_nplaca.Text.Length == 0) { nplaca = ""; } else { nplaca = tb_nplaca.Text; } String observacion; if (tb_observaciones.Text.Length == 0) { observacion = ""; } else { observacion = tb_observaciones.Text; } String fecha_trat = tb_fecha_tratamiento.Text + " " + DateTime.Now.ToLongTimeString(); String fecha = tb_fecha.Text + " " + DateTime.Now.ToLongTimeString(); SqlCertif = "insert into tblCertif (Puesto,Ncertificado,Fecha,Cambio,Cortesia,Local,Total,TotalString,Observacion,Responsable,Anulado,Remesado,Replicado,NEnd,TipoCertificado,ClienteExtra,Cliente,Placa,FechaTrat,Cuarentena,idPais) values ('"; SqlCertif = SqlCertif + Session["idPuesto"] + "','" + l_n_certificado.Text + "','" + fecha.ToString() + "'," + tb_cambio.Text + "," + Cortesia.ToString() + "," + Local + "," + tb_total.Text + ",'" + l_numlet.Text + "','" + observacion.ToString() + "','"; SqlCertif = SqlCertif + Session["login"] + "',0,0,0,1,'ASPERSIÓN TERRESTRE','" + tb_cliente.Text + "','" + ddl_cliente.SelectedValue + "','" + nplaca.ToString() + "','" + fecha_trat.ToString() + "','" + ddl_cuarentena.SelectedValue + "','" + Session["idPais"] + "')"; bool error = false; try { fn.EjecutarNonQuery(SqlCertif, SqlConn); fn.EjecutarNonQuery(SqlCertifMdp, SqlConn); } catch { error = true; } finally { if (error) { Response.Redirect("Cert_Aspersion_Terrestre.aspx?error=ok&ncertif=" + l_n_certificado.Text); } else { if (fn.Verificar_Certificado(l_n_certificado.Text, Session["idPuesto"].ToString(), Session["idPais"].ToString(), SqlConn)) { fn.Incrementar_Correlativo(Session["idPais"].ToString(), Session["idPuesto"].ToString(), "NCertificado", SqlConn); //Se registra en auditoria fn.Auditoria(Session["login"].ToString(), "Agregar Certificado", l_n_certificado.Text, Session["idPais"].ToString(), SqlConn); SqlConn.Close(); Response.Redirect("Cert_Aspersion_Terrestre.aspx?save=ok&ncertif=" + l_n_certificado.Text); } else { Response.Redirect("Cert_Aspersion_Terrestre.aspx?error=ok&ncertif=" + l_n_certificado.Text); } } } } else { if (cert_det == 0 || cert_det != Convert.ToDouble(l_servicios.Text) || Convert.ToDouble(tb_subtotalg.Text) != Math.Round(total, 2)) { Response.Redirect("Cert_Aspersion_Terrestre.aspx?error=ok"); } fn.Message(Page, "Verificar! Hay campos que son obligatorios como el CLIENTE, FECHA TRATAMIENTO y ENCARGADO DE CUARENTENA"); } }
public void MySpaceIdGetMySpaceInfo() { MDP mdp = new MDP(TestConstants.API_MS_CONSUMER_KEY, TestConstants.API_MS_CONSUMER_SECRET); PersonInfo person = mdp.GetMySpaceInfo(TestConstants.API_ACCESS_TOKEN, TestConstants.API_ACCESS_TOKEN_SECRET); Console.WriteLine(person.ToJson()); }
private MDP BuildMDPAntiChain_S() { DeterministicAutomata_Subset specAutomaton = BuildDeterministicAutomata_Subset(InitSpecStep); Stack<TupleSub> working = new Stack<TupleSub>(1024); DeterministicFAState_Subset InitialSpec = specAutomaton.InitialState; Dictionary<string, HashSet<DeterministicFAState_Subset>> antichain = new Dictionary<string, HashSet<DeterministicFAState_Subset>>(); string impID = InitialStep.GetID(); int specID = InitialSpec.GetID(); MDPState init = new MDPState(impID + Constants.SEPARATOR + specID); working.Push(new TupleSub(InitialStep as MDPConfiguration, InitialSpec, init)); MDP mdp = new MDP(init, Ultility.Ultility.DEFAULT_PRECISION, Ultility.Ultility.MAX_DIFFERENCE); HashSet<DeterministicFAState_Subset> Specs = new HashSet<DeterministicFAState_Subset>(); Specs.Add(InitialSpec); antichain.Add(impID, Specs); do { if (CancelRequested) { VerificationOutput.NoOfStates = mdp.States.Count; return mdp; } TupleSub current = working.Pop(); if (current.SpecState == null) { mdp.AddTargetStates(current.MDPState); } else { IEnumerable<MDPConfiguration> list = current.ImplState.MakeOneMoveLocal(); VerificationOutput.Transitions += list.Count(); int currentDistriIndex = -1; Distribution newDis = new Distribution(Constants.TAU); //for (int i = 0; i < list.Count; i++) foreach (MDPConfiguration step in list) { //MDPConfiguration step = list[i]; DeterministicFAState_Subset nextSpec = current.SpecState; if (step.Event != Constants.TAU) { nextSpec = current.SpecState.Next(step.Event); } //System.Diagnostics.Debug.Assert(nextSpec != null, "NextSpec is null!!"); //string stepID = step.GetID() + Constants.SEPARATOR + nextSpec.GetID().ToString(); impID = step.GetID(); string stepID = impID + Constants.SEPARATOR + (nextSpec == null ? "" : nextSpec.GetID().ToString()); MDPState nextState; if (!mdp.States.TryGetValue(stepID, out nextState)) { nextState = new MDPState(stepID); //mdp.AddState(nextState); //KeyValuePair<MDPConfiguration, DeterministicFAState_Subset> newPair = // new KeyValuePair<MDPConfiguration, DeterministicFAState_Subset>(step, nextSpec); //working.Push(new TupleSub(step, nextSpec, nextState)); bool addstate = true; if (nextSpec != null) { //specID = nextSpec.GetID(); //DeterministicFAState_Subset DFAstate = nextSpec; HashSet<DeterministicFAState_Subset> specStates; //Dictionary<int, MDPState> mapping = null; if (antichain.TryGetValue(impID, out specStates)) { HashSet<DeterministicFAState_Subset> toRemove = new HashSet<DeterministicFAState_Subset>(); //int Spec = nextSpec.GetID(); bool chainIncrease = true; foreach (DeterministicFAState_Subset specState in specStates) { //DeterministicFAState_Subset specState = specStates[j]; if (specState.Sub.Contains(nextSpec)) { toRemove.Add(specState); nextSpec.Sub.Add(specState); chainIncrease = false; } if (nextSpec.Sub.Contains(specState)) { nextSpec = specState; addstate = false; chainIncrease = false; break; } } if (toRemove.Count > 0) { foreach (var specState in toRemove) { specStates.Remove(specState); } specStates.Add(nextSpec); // toRemove; } else if (chainIncrease) { specStates.Add(nextSpec); } } else { HashSet<DeterministicFAState_Subset> newSpecs = new HashSet<DeterministicFAState_Subset>(); newSpecs.Add(nextSpec); antichain.Add(impID, newSpecs); } } if (addstate) { mdp.AddState(nextState); working.Push(new TupleSub(step, nextSpec, nextState)); } } if (step.DisIndex == -1) { if (currentDistriIndex >= 0) { current.MDPState.AddDistribution(newDis); newDis = new Distribution(Constants.TAU); } Distribution newTrivialDis = new Distribution(step.Event, nextState); current.MDPState.AddDistribution(newTrivialDis); } else if (currentDistriIndex != -1 && step.DisIndex != currentDistriIndex) { current.MDPState.AddDistribution(newDis); newDis = new Distribution(Constants.TAU); newDis.AddProbStatePair(step.Probability, nextState); } else { newDis.AddProbStatePair(step.Probability, nextState); } currentDistriIndex = step.DisIndex; } if (currentDistriIndex >= 0) { current.MDPState.AddDistribution(newDis); } } } while (working.Count > 0); VerificationOutput.NoOfStates = mdp.States.Count; //mdp.BackUpTargetStates(); return mdp; }
private bool StartingTrial() { return((PreviousAction == null) && (PreviousState == null) && (previousReward == double.NegativeInfinity) && (CurrentState.Equals(MDP.GetInitialState()))); }
//protected QueryConstraintType ConstraintType; //protected DRA DRA; //public DRA PositiveDRA; //public DRA NegativeDRA; //public BuchiAutomata PositiveBA; //protected double Min = -1; //protected double Max = -1; //protected MDP mdp; //protected Dictionary<string, int> MDPState2DRAStateMapping; //private bool HasDeadLock; ////private DefinitionRef Process; //public const string DUMMY_INIT = "dummy"; //private bool AlmostFair = false; protected void BuildMD_ImprovedTarjan() { //int counter = 0; MDPState dummyInit = new MDPState(DUMMY_INIT); mdp = new MDP(dummyInit, Ultility.Ultility.DEFAULT_PRECISION, Ultility.Ultility.MAX_DIFFERENCE); List <PCSPEventDRAPairMEC> initials = GetInitialPairsEMC(InitialStep as MDPConfiguration); Stack <KeyValuePair <PCSPEventDRAPairMEC, MDPState> > working = new Stack <KeyValuePair <PCSPEventDRAPairMEC, MDPState> >(1024); //Stack<PCSPEventDRAPairMEC> stepStack = new Stack<PCSPEventDRAPairMEC>(1024); List <PCSPEventDRAPairMEC> stepStack = new List <PCSPEventDRAPairMEC>(1024); MDPState2DRAStateMapping = new Dictionary <string, int>(Common.Classes.Ultility.Ultility.MC_INITIAL_SIZE); //HashSet<string> MDPStateMapping = new HashSet<string>(); //build the MDP while identifying the SCCs List <List <string> > SCCs = new List <List <string> >(64); for (int z = 1; z <= initials.Count; z++) { PCSPEventDRAPairMEC initState = initials[z - 1]; string stringID = initState.ID; MDPState newinit = new MDPState(stringID); mdp.AddState(newinit); dummyInit.AddDistribution(new Distribution(stringID, newinit)); stepStack.Add(initState); //newinit.AddDistribution(new Distribution(Constants.TAU, newinit)); working.Push(new KeyValuePair <PCSPEventDRAPairMEC, MDPState>(initState, newinit)); } Dictionary <string, int> preorder = new Dictionary <string, int>(Common.Classes.Ultility.Ultility.MC_INITIAL_SIZE); Dictionary <string, int> lowlink = new Dictionary <string, int>(Common.Classes.Ultility.Ultility.MC_INITIAL_SIZE); Dictionary <string, int> distrRecord = new Dictionary <string, int>(); int preorderCounter = 0; Dictionary <string, List <PCSPEventDRAPairMEC> > ExpendedNode = new Dictionary <string, List <PCSPEventDRAPairMEC> >(); do { if (CancelRequested) { this.VerificationOutput.NoOfStates = this.VerificationOutput.NoOfStates + mdp.States.Count; return; } KeyValuePair <PCSPEventDRAPairMEC, MDPState> pair = working.Peek(); MDPConfiguration evt = pair.Key.configuration; int DRAState = pair.Key.state; string currentID = pair.Key.ID; List <Distribution> outgoing = pair.Value.Distributions; if (!preorder.ContainsKey(currentID)) { preorder.Add(currentID, preorderCounter); distrRecord.Add(currentID, 0); //stepStack.Add(new PCSPEventDRAPairMEC(currentID + "separate" + 0)); preorderCounter++; } bool done = true; if (ExpendedNode.ContainsKey(currentID)) { List <PCSPEventDRAPairMEC> list = ExpendedNode[currentID]; //int counter = 0; if (list.Count > 0) { int k = list.Count - 1; //for (int k = list.Count - 1; k >= 0; k--) PCSPEventDRAPairMEC step = list[k]; if (step == null) { stepStack.Add(new PCSPEventDRAPairMEC(currentID + "separate" + (distrRecord[currentID]++))); list.RemoveAt(k); continue; } else { string stepID = step.ID; if (!preorder.ContainsKey(stepID)) { //if (done) //{ working.Push(new KeyValuePair <PCSPEventDRAPairMEC, MDPState>(step, mdp.States[stepID])); stepStack.Add(step); done = false; list.RemoveAt(k); //} } else { //stepStack.Add(step.ID); stepStack.Add(step); list.RemoveAt(k); done = false; } } } //else //{ // stepStack.Add(new PCSPEventDRAPairMEC(currentID + "end")); //} } else { int currentDistriIndex = -1; Distribution newDis = new Distribution(Constants.TAU); IEnumerable <MDPConfiguration> steps = evt.MakeOneMoveLocal(); //int counter = 0; //NOTE: here we play a trick for deadlock case: if a deadlock exist in the MDP, we will make a //self loop transition to remove the deadlock. Deadlock is meaningless in MDP. if (evt.IsDeadLock) { List <MDPConfiguration> stepsList = new List <MDPConfiguration>(steps); stepsList.Add(CreateSelfLoopStep(evt)); steps = stepsList; HasDeadLock = true; } List <PCSPEventDRAPairMEC> product = NextMEC(steps.ToArray(), DRAState); this.VerificationOutput.Transitions += product.Count; for (int k = product.Count - 1; k >= 0; k--) { PCSPEventDRAPairMEC step = product[k]; string tmp = step.ID; //int nextIndex = VisitedWithID.Count; MDPState nextState; if (mdp.States.TryGetValue(tmp, out nextState)) { if (!preorder.ContainsKey(tmp)) { if (done) { working.Push(new KeyValuePair <PCSPEventDRAPairMEC, MDPState>(step, nextState)); //stepStack.Add(step.ID); stepStack.Add(step); product.RemoveAt(k); done = false; } else { product[k] = step; } } else { if (done) { product.RemoveAt(k); done = false; } } } else { nextState = new MDPState(tmp); mdp.States.Add(tmp, nextState); if (done) { working.Push(new KeyValuePair <PCSPEventDRAPairMEC, MDPState>(step, nextState)); //stepStack.Add(step.ID); stepStack.Add(step); done = false; product.RemoveAt(k); } else { product[k] = step; } } MDPConfiguration pstep = step.configuration; if (pstep.DisIndex == -1) { if (currentDistriIndex >= 0) { pair.Value.AddDistribution(newDis); product.Insert(k + 1, null);//separator newDis = new Distribution(Constants.TAU); } Distribution newTrivialDis = new Distribution(pstep.Event, nextState); pair.Value.AddDistribution(newTrivialDis); if (k != 0) { product.Insert(k, null);//separator } } else if (currentDistriIndex != -1 && pstep.DisIndex != currentDistriIndex) { pair.Value.AddDistribution(newDis); product.Insert(k + 1, null);//separator newDis = new Distribution(Constants.TAU); newDis.AddProbStatePair(pstep.Probability, nextState); } else { newDis.AddProbStatePair(pstep.Probability, nextState); } currentDistriIndex = pstep.DisIndex; } if (currentDistriIndex >= 0) { pair.Value.AddDistribution(newDis); } ExpendedNode.Add(currentID, product); } if (done) { int lowlinkV = preorder[currentID]; int preorderV = preorder[currentID]; bool selfLoop = false; //List<bool> temp = new List<bool>(outgoing.Count); int length = outgoing.Count; for (int i = 0; i < length; i++)//Distribution list in outgoing) { //temp[i] = true; Distribution list = outgoing[i];//note the order of distribution is reversed int templowlinkV = int.MaxValue; //bool tempUpdate = false; foreach (KeyValuePair <double, MDPState> state in list.States) { string w = state.Value.ID; if (w == currentID && list.States.Count == 1) { selfLoop = true; } if (!MDPState2DRAStateMapping.ContainsKey(w)) { //tempUpdate = true; if (preorder[w] > preorderV) { templowlinkV = Math.Min(templowlinkV, lowlink[w]); //lowlinkV = Math.Min(lowlinkV, lowlink[w]); } else { templowlinkV = Math.Min(templowlinkV, preorder[w]); //lowlinkV = Math.Min(lowlinkV, preorder[w]); } } else { templowlinkV = int.MaxValue; break; } } //if (tempUpdate) //{ //} if (templowlinkV == int.MaxValue) { //int index = stepStack.IndexOf(currentID + "separate" + i); //for (int index = stepStack.IndexOf(currentID + "separate" + (i)); index < stepStack.Count; ) //{ // if (stepStack[index] != currentID + "separate" + (i+1).ToString() && stepStack[index] != currentID + "end") // { // stepStack.RemoveAt(index); // } // else // { // break; // } //} bool remove = false; List <int> toRemove = new List <int>(); for (int index = stepStack.Count - 1; index >= 0; index--) { //counter++; if (stepStack[index].ID == currentID + "separate" + i) { remove = true; } if (remove) { if (stepStack[index].ID != currentID + "separate" + (i - 1) && stepStack[index].ID != currentID) { stepStack.RemoveAt(index); } else { break; } } else { if (stepStack[index].ID == currentID) { foreach (var ind in toRemove) { stepStack.RemoveAt(ind); } break; } toRemove.Add(index); } } } else { lowlinkV = Math.Min(templowlinkV, lowlinkV); } } lowlink[currentID] = lowlinkV; working.Pop(); if (lowlinkV == preorderV) { List <string> SCC = new List <string>(1024); SCC.Add(currentID); MDPState2DRAStateMapping.Add(currentID, DRAState); //while (stepStack.Count > 0 && preorder[stepStack[stepStack.Count - 1]] > preorderV) while (stepStack[stepStack.Count - 1].ID != currentID) { PCSPEventDRAPairMEC s = stepStack[stepStack.Count - 1]; string sID = s.ID; stepStack.RemoveAt(stepStack.Count - 1); if (!sID.Contains("separate") && !SCC.Contains(sID)) { SCC.Add(sID); MDPState2DRAStateMapping.Add(sID, s.state); //MDPState2DRAStateMapping.Add(sID, ); } } stepStack.RemoveAt(stepStack.Count - 1); if (SCC.Count > 1 || selfLoop) //evt.IsDeadLock || { SCCs.Add(SCC); } } //else //{ // stepStack.Push(pair.Key); //} } } while (working.Count > 0); Debug.WriteLine(mdp.States.Count); List <string> EndComponents = new List <string>(SCCs.Count); int count = DRA.acceptance().size(); int helper = 0; foreach (List <string> scc in SCCs) { //for debug //List<MDPState> debug = new List<MDPState>(); //List<int> drastates = new List<int>(); //foreach(string state in scc) //{ // debug.Add(mdp.States[state]); // drastates.Add(MDPState2DRAStateMapping[state]); //} //for debug //if(sentence.Count == 0) //{ //} for (int index = 0; index < count; index++) { List <string> newSCC = new List <string>(); List <string> targets = new List <string>(); if (AlmostFair) { //bool bottom = true; ////int SCCcount = scc.Count; ////note that as long as one SCC(might not be a real MEC) has a U state, the whole SCC cannot be targets. ////RemoveNonECStates(scc, targets); //foreach (string i in scc) //{ // int draState = MDPState2DRAStateMapping[i]; // if (bottom) // { // if (DRA.acceptance().isStateInAcceptance_U(index, draState)) // { // bottom = false; // } // } // newSCC.Add(i); // if (DRA.acceptance().isStateInAcceptance_L(index, draState)) // { // targets.Add(i); // } //} //if (bottom) //{ // if (!BottomECStates(newSCC, targets)) // { // if (newSCC.Count > 0) // { // GroupMEC(newSCC); // } // } // else // { // Common.Classes.Ultility.Ultility.AddList(EndComponents, scc); // } //} //else //{ // RemoveNonECStates(newSCC); // if (newSCC.Count > 0) // { // GroupMEC(newSCC); // } //} } else { foreach (string i in scc) { int draState = MDPState2DRAStateMapping[i]; if (!DRA.acceptance().isStateInAcceptance_U(index, draState)) { newSCC.Add(i); //note add by ssz if (DRA.acceptance().isStateInAcceptance_L(index, draState)) { targets.Add(i); } //note add by ssz } } if (targets.Count > 0) { RemoveNonECStates(newSCC, targets); } if (targets.Count > 0) { //List<string> endComponent = TarjanModelChecking(index, newSCC); Debug.WriteLine(helper++); //Common.Classes.Ultility.Ultility.AddList(EndComponents, endComponent); Common.Classes.Ultility.Ultility.AddList(EndComponents, scc);//note here newSCC changed to scc } else { if (scc.Count > 1) { GroupMEC(scc); } } } } } foreach (string s in EndComponents) { mdp.AddTargetStates(mdp.States[s]); } Debug.WriteLine(mdp.States.Count); VerificationOutput.NoOfStates = VerificationOutput.NoOfStates + mdp.States.Count; }
private MDP BuildMDPSubset() { DeterministicAutomata_Subset specAutomaton = BuildDeterministicAutomata_Subset(InitSpecStep); Stack<TupleSub> working = new Stack<TupleSub>(1024); DeterministicFAState_Subset InitialSpec = specAutomaton.InitialState; Dictionary<string, Dictionary<int, MDPState>> visited = new Dictionary<string, Dictionary<int, MDPState>>(); int specID = InitialSpec.GetID(); string impID = InitialStep.GetID(); MDPState init = new MDPState(impID + Constants.SEPARATOR + specID); working.Push(new TupleSub(InitialStep as MDPConfiguration, InitialSpec, init)); Dictionary<int, MDPState> initialDic = new Dictionary<int, MDPState>(); initialDic.Add(specID, init); visited.Add(impID, initialDic); MDP mdp = new MDP(init, Ultility.Ultility.DEFAULT_PRECISION, Ultility.Ultility.MAX_DIFFERENCE); do { if (CancelRequested) { VerificationOutput.NoOfStates = mdp.States.Count; return mdp; } TupleSub current = working.Pop(); if (current.SpecState == null) { mdp.AddTargetStates(current.MDPState); } else { IEnumerable<MDPConfiguration> list = current.ImplState.MakeOneMoveLocal(); VerificationOutput.Transitions += list.Count(); int currentDistriIndex = -1; Distribution newDis = new Distribution(Constants.TAU); //for (int i = 0; i < list.Count; i++) foreach (var step in list) { //MDPConfiguration step = list[i]; DeterministicFAState_Subset nextSpec = current.SpecState; if (step.Event != Constants.TAU) { nextSpec = current.SpecState.Next(step.Event); } //System.Diagnostics.Debug.Assert(nextSpec != null, "NextSpec is null!!"); //string stepID = step.GetID() + Constants.SEPARATOR + nextSpec.GetID().ToString(); impID = step.GetID(); string stepID = impID + Constants.SEPARATOR + (nextSpec == null ? "" : nextSpec.GetID().ToString()); MDPState nextState; if (!mdp.States.TryGetValue(stepID, out nextState)) { nextState = new MDPState(stepID); //mdp.AddState(nextState); //KeyValuePair<MDPConfiguration, DeterministicFAState_Subset> newPair = // new KeyValuePair<MDPConfiguration, DeterministicFAState_Subset>(step, nextSpec); working.Push(new TupleSub(step, nextSpec, nextState)); if (nextSpec != null) { specID = nextSpec.GetID(); //DeterministicFAState_Subset DFAstate = nextSpec; Dictionary<int, MDPState> mapping = null; if (visited.TryGetValue(impID, out mapping)) { //int Spec = nextSpec.GetID(); mapping.Add(specID, nextState); foreach (int spec in mapping.Keys) { if (specAutomaton.States[spec].Sub.Contains(nextSpec)) { mapping[spec].Sub.Add(nextState); } else if (nextSpec.Sub.Contains(specAutomaton.States[spec])) { nextState.Sub.Add(mapping[spec]); } } //foreach (var x in visited) //{ // if (x.Key.Key.GetID() == newPair.Key.GetID() && // x.SpecState.Sub.Contains(newPair.Value)) // { // x.Value.Sub.Add(nextState); // } // else if (x.Key.Key.GetID() == newPair.Key.GetID() && // newPair.Value.Sub.Contains(x.SpecState)) // { // nextState.Sub.Add(x.Value); // } //} //visited.Add(newPair, nextState); } else { Dictionary<int, MDPState> newDicDic = new Dictionary<int, MDPState>(); newDicDic.Add(specID, nextState); visited.Add(impID, newDicDic); } } mdp.AddState(nextState); } if (step.DisIndex == -1) { if (currentDistriIndex >= 0) { current.MDPState.AddDistribution(newDis); newDis = new Distribution(Constants.TAU); } Distribution newTrivialDis = new Distribution(step.Event, nextState); current.MDPState.AddDistribution(newTrivialDis); } else if (currentDistriIndex != -1 && step.DisIndex != currentDistriIndex) { current.MDPState.AddDistribution(newDis); newDis = new Distribution(Constants.TAU); newDis.AddProbStatePair(step.Probability, nextState); } else { newDis.AddProbStatePair(step.Probability, nextState); } currentDistriIndex = step.DisIndex; } if (currentDistriIndex >= 0) { current.MDPState.AddDistribution(newDis); } } } while (working.Count > 0); VerificationOutput.NoOfStates = mdp.States.Count; //mdp.BackUpTargetStates(); return mdp; }