string GetModelName(IState state) { string name = this.defaultModelName; IExtendedState estate = state as IExtendedState; if (estate != null) { name = estate.ModelName; } return(Disambiguate(name)); }
void CreateVariableWiewers() { //view also the control mode of the root state if the product has more than one machine in it //the id of the composed state is -1 //if (leafStates.Count > 1) // termViewers.Add(TermViewer.Create("[Control Mode]", rootState.ControlMode), -1); for (int stateId = 0; stateId < this.leafStates.Count; stateId++) { IState leafState = this.leafStates[stateId]; //string modelName = this.modelNames[stateId]; //each state has a control mode, create a viewer for it if the controlMode is //not an empty sequence if (!leafState.ControlMode.ToCompactString().Equals("Sequence()")) { termViewers.Add(TermViewer.Create("[Control Mode]", leafState.ControlMode), stateId); } IExtendedState estate = leafState as IExtendedState; if (estate != null) { #region add the domain map viewer, skip this if there are no domains if (estate.DomainMap.Count > 0) { //StringBuilder sb = new StringBuilder(); Sequence <Term> args = Sequence <Term> .EmptySequence; foreach (Pair <Symbol, int> si in estate.DomainMap) { args = args.AddLast(new Literal(si.First.FullName)); args = args.AddLast(new Literal(si.Second)); } Symbol symb = Symbol.Parse("Map<String, Integer>"); Term domMap = new CompoundTerm(symb, args); termViewers.Add(TermViewer.Create("[Domain Map]", domMap), stateId); } #endregion //add a viewer for each state variable for (int j = 0; j < estate.LocationValuesCount; j++) { termViewers.Add(TermViewer.Create(estate.GetLocationName(j), estate.GetLocationValue(j)), stateId); } } } }
private CompoundTerm ChooseAction(Sequence <Action> actions, IState iState) { Action maxAct = actions.Head; int sState = iState.GetHashCode(); int tState; IExtendedState iestate = (IExtendedState)iState; int c = iestate.LocationValuesCount; foreach (Action a in actions) { foreach (string s in this.modelProgram.GetEnablingConditionDescriptions(iState, a, false)) { Term t = Term.Parse(s); Sequence <Term> vars = (t.Arguments[0].Arguments); Map <Variable, Term> subst = ConstructSubst(a, vars); System.Console.WriteLine(a.ToString() + sState + " enabled string " + t.Arguments[1].Substitute(subst)); } } /* * for (int i = 0; i < c; i++) * { * System.Console.WriteLine("name: "+iestate.GetLocationName(i) + " value : "+ * iestate.GetLocationValue(i) + " hash" + * iestate.GetLocationValue(i).GetHashCode()); * CompoundValue t = (CompoundValue)iestate.GetLocationValue(i); * foreach (CompoundValue t1 in t.FieldValues()) * { * System.Console.WriteLine(" field " + t1.ToString()); * } * } */ TransitionProperties tp; int sum = 0; Sequence <Pair <int, Action> > cumulActSum = Sequence <Pair <int, Action> > .EmptySequence; Set <int> coveredActs = findCoveredActs(sState, actions); if (!cov.ContainsKey(sState)) { cov[sState] = 0.0; } Set <Action> newStateActs = new Set <Action>(); Set <Action> newActs = new Set <Action>(); Set <Action> oldActs = new Set <Action>(actions.Head); foreach (Action a in actions) { tState = this.modelProgram.GetTargetState(iState, a, null, out tp).GetHashCode(); if (!v.ContainsKey(tState)) { newStateActs = newStateActs.Add(a); } else if (!coveredActs.Contains(a.GetHashCode())) { newActs = newActs.Add(a); } else { // one greedy approach /* * if (v.ContainsKey(tState) && v[tState] > maxv) * { * maxv = v[tState]; * oldActs = new Set<Action>(a); * } * else if (v.ContainsKey(tState) && v[tState] == maxv) * { * oldActs = oldActs.Add(a); * }*/ // probabilistic greedy approach if (v.ContainsKey(tState)) { sum = sum + (int)(v[tState] * Math.Pow(10.0, 9.0)); Pair <int, Action> np = new Pair <int, Action>(sum, a); cumulActSum = cumulActSum.AddLast(np); } } } if (!newStateActs.IsEmpty) { maxAct = newStateActs.Choose(); System.Console.WriteLine("new action in new state " + maxAct.ToString()); } else if (!newActs.IsEmpty) { maxAct = newActs.Choose(); System.Console.WriteLine("new action in old state " + maxAct.ToString()); } else { //maxAct = oldActs.Choose(); Random rndNumbers = new Random(); int rndNumber = rndNumbers.Next(sum); System.Console.WriteLine(sum + " " + rndNumber); foreach (Pair <int, Action> np in cumulActSum) { System.Console.WriteLine(np.First + " " + np.Second.ToString()); if (rndNumber <= np.First) { maxAct = np.Second; break; } maxAct = np.Second; } System.Console.WriteLine("old action in old state " + maxAct.ToString()); } coveredActs = coveredActs.Add(maxAct.GetHashCode()); cov[sState] = (double)coveredActs.Count / (double)actions.Count; return(maxAct); }