/// <summary> /// Returns a bag containing a single coverage point that is a pair of integers /// that are the hashcodes of the state and the action. /// </summary> static Bag<Term> DefaultCoveragePointProvider(IState state, CompoundTerm action) { return new Bag<Term>(new Pair<int, int>(state.GetHashCode(), action.GetHashCode()).AsTerm); }
private CompoundTerm ChooseAction(Sequence <Action> actions, IState iState) { Set <int> coveredActs; double minCov = 1.0; double maxv = 0.0; IState tState; Action maxAct = actions.Head; TransitionProperties tp; if (!cov.ContainsKey(iState)) { maxAct = actions.Choose(); cov[iState] = (1.0 / actions.Count); v[iState] = 1.0; // System.Console.WriteLine("inside if " + iState.GetHashCode() + " cov"+ cov[iState]); return(maxAct); } else if (cov.ContainsKey(iState) && cov[iState] < 1.0) { coveredActs = new Set <int>(this.findCoveredActs(iState)); Set <int> acts = new Set <int>(); foreach (Action a in actions) { if (!coveredActs.Contains(a.GetHashCode())) { System.Console.WriteLine(a.GetHashCode()); tState = modelProgram.GetTargetState(iState, a, null, out tp); if (!cov.ContainsKey(tState)) { minCov = 0; maxAct = a; } else if (cov[tState] <= minCov) { minCov = cov[tState]; maxAct = a; } } else { acts.Add(a.GetHashCode()); tState = modelProgram.GetTargetState(iState, a, null, out tp); if (v.ContainsKey(tState)) { maxv = Math.Max(maxv, v[tState]); } } } acts = acts.Add(maxAct.GetHashCode()); cov[iState] = (acts.Count / actions.Count); if (v.ContainsKey(iState)) { v[iState] = Math.Min(v[iState], (alpha * maxv)); } else { v[iState] = alpha * maxv; } //System.Console.WriteLine("inside <1.0 " + iState.GetHashCode() + " cov " + cov[iState]); return(maxAct); } else if (cov.ContainsKey(iState) && cov[iState] == 1.0) { foreach (Action a in actions) { tState = modelProgram.GetTargetState(iState, a, null, out tp); if (!cov.ContainsKey(tState)) { maxv = 1.0; maxAct = a; } else if (cov.ContainsKey(tState) && maxv <= v[tState]) { maxv = v[tState]; maxAct = a; } } if (v.ContainsKey(iState)) { v[iState] = Math.Min(v[iState], (alpha * maxv)); } else { v[iState] = alpha * maxv; } //System.Console.WriteLine("inside else " + iState.GetHashCode() + " " + v[iState]); return(maxAct); } else { //System.Console.WriteLine("Should not be reachable!! cov" + cov[iState]); return(maxAct); } }
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); }
private void Update(IState iState, CompoundTerm action) { int hash = iState.GetHashCode(); //System.Console.WriteLine("action taken "+ action.ToString()); if (!cps.ContainsKey(hash)) cps[hash] = new Set<int>(); cps[hash] = cps[hash].Add(action.GetHashCode()); /* foreach (Symbol s in this.ObservableActionSymbols) { System.Console.WriteLine(s); }*/ }
private void UpdateCoveragePoints(CompoundTerm action) { int hash = this.currState.GetHashCode(); //System.Console.WriteLine("action taken "+ action.ToString()); if (!cps.ContainsKey(hash)) cps[hash] = new Set<int>(); cps[hash] = cps[hash].Add(action.GetHashCode()); }