private State ApplyCompute(State s, string sName, List <Action> lActions, Domain d) { State sCurrent = s; Predicate pNew = new GroundedPredicate("new-" + sName); Predicate pDone = new GroundedPredicate("done-" + sName); int i = 0; while (!sCurrent.Contains(pNew.Negate()) || !sCurrent.Contains(pDone) || i < 10) { Action a1 = d.GroundActionByName(new string[] { "pre-" + sName, "" }, sCurrent.Predicates, false); Action a2 = d.GroundActionByName(new string[] { "compute-" + sName, "" }, sCurrent.Predicates, false); if (a1 != null && a2 != null) { sCurrent = sCurrent.Apply(a1); sCurrent = sCurrent.Apply(a2); lActions.Add(a1); lActions.Add(a2); } i++; } Action a = d.GroundActionByName(new string[] { "observe-new-" + sName + "-F", "" }, sCurrent.Predicates, false); sCurrent = sCurrent.Apply(a); lActions.Add(a); a = d.GroundActionByName(new string[] { "post-" + sName, "" }, sCurrent.Predicates, false); sCurrent = sCurrent.Apply(a); lActions.Add(a); return(sCurrent); }
private State ApplyAxiom(State s, List <Action> lActions, Domain d) { State sCurrent = s; Predicate pNew = new GroundedPredicate("new"); Predicate pDone = new GroundedPredicate("done"); while (!sCurrent.Contains(pNew.Negate()) || !sCurrent.Contains(pDone)) { Action a1 = d.GroundActionByName(new string[] { "pre-axiom", "" }, sCurrent.Predicates, false); Action a2 = d.GroundActionByName(new string[] { "axiom", "" }, sCurrent.Predicates, false); if (a1 != null && a2 != null) { sCurrent = sCurrent.Apply(a1); sCurrent = sCurrent.Apply(a2); lActions.Add(a1); lActions.Add(a2); } } Action a = d.GroundActionByName(new string[] { "observe-new-F", "" }, sCurrent.Predicates, false); sCurrent = sCurrent.Apply(a); lActions.Add(a); a = d.GroundActionByName(new string[] { "fixpoint", "" }, sCurrent.Predicates, false); sCurrent = sCurrent.Apply(a); lActions.Add(a); return(sCurrent); }
public List <Action> PlanII(State sStartState) { CompoundFormula fGoal = (CompoundFormula)m_pProblem.Goal; //List<Predicate> lGoal = new List<Predicate>(); //foreach (PredicateFormula vf in fGoal.Operands) // lGoal.Add(vf.Predicate); State sCurrent = null, sNext = null; List <State> lOpenList = new List <State>(); Dictionary <State, State> dParents = new Dictionary <State, State>(); Dictionary <State, Action> dGeneratingAction = new Dictionary <State, Action>(); Dictionary <State, double> dHeuristic = new Dictionary <State, double>(); Dictionary <State, double> dCost = new Dictionary <State, double>(); int cRepeated = 0; dCost[sStartState] = 0; sStartState.GroundAllActions(); dHeuristic[sStartState] = m_fHeuristic.h(sStartState); lOpenList.Add(sStartState); dParents[sStartState] = null; dGeneratingAction[sStartState] = null; m_cObservedStates = 1; int cHandled = 0; while (lOpenList.Count > 0) { sCurrent = GetMinimalStateBFS(lOpenList, dHeuristic, dCost); cHandled++; Debug.Write("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b" + cHandled + ", " + dHeuristic[sCurrent] + "," + lOpenList.Count); lOpenList.Remove(sCurrent); sCurrent.GroundAllActions(); foreach (Action a in sCurrent.AvailableActions) { sNext = sCurrent.Apply(a); if (sNext != null && !dParents.ContainsKey(sNext)) { m_cObservedStates++; dParents[sNext] = sCurrent; dGeneratingAction[sNext] = a; if (sNext.Contains(fGoal)) { return(CreatePlan(sNext, dParents, dGeneratingAction)); } else { dCost[sNext] = dCost[sCurrent] + 1; dHeuristic[sNext] = m_fHeuristic.h(sNext); lOpenList.Add(sNext); } } else if (sNext != null) { cRepeated++; } } } return(null); }
private bool ValidatePlan(State sStartState, List <Action> lPlan) { State sCurrent = sStartState; foreach (Action a in lPlan) { sCurrent = sCurrent.Apply(a); } bool bValid = sCurrent.Contains(m_pProblem.Goal); return(bValid); }
private int CostForAchieving(Predicate p, State s, Dictionary <Predicate, int> dCostForAchieving, List <Predicate> lObserved) { if (s.Contains(p)) { return(0); } if (dCostForAchieving.ContainsKey(p)) { return(dCostForAchieving[p]); } if (lObserved.Contains(p)) { return(int.MaxValue); } lObserved.Add(p); int iMin = int.MaxValue - 1; foreach (SortedSet <Predicate> sParents in m_dParents[p]) { int iMax = 0; int iSum = 0; foreach (Predicate pTag in sParents) { int iCostForPTag = CostForAchieving(pTag, s, dCostForAchieving, lObserved); if (iCostForPTag == int.MaxValue) { iSum = int.MaxValue - 1; break; } iSum += iCostForPTag; if (iCostForPTag > iMax) { iMax = iCostForPTag; } } //if (iMax < iMin) // iMin = iMax; if (iSum < iMin) { iMin = iSum; } } lObserved.Remove(p); //if (iMin < 0) // Debug.WriteLine("*"); dCostForAchieving[p] = iMin + 1; return(iMin + 1); }
private void AddEffects(Formula fEffects) { if (fEffects is PredicateFormula) { AddEffect(((PredicateFormula)fEffects).Predicate); } else { CompoundFormula cf = (CompoundFormula)fEffects; if (cf.Operator == "oneof" || cf.Operator == "or")//BUGBUG - should treat or differently { int iRandomIdx = RandomGenerator.Next(cf.Operands.Count); AddEffects(cf.Operands[iRandomIdx]); GroundedPredicate pChoice = new GroundedPredicate("Choice"); pChoice.AddConstant(new Constant("ActionIndex", "a" + (Time - 1)));//time - 1 because this is the action that generated the state, hence its index is i-1 pChoice.AddConstant(new Constant("ChoiceIndex", "c" + iRandomIdx)); State s = this; while (s != null) { s.m_lPredicates.Add(pChoice); s = s.m_sPredecessor; } } else if (cf.Operator == "and") { foreach (Formula f in cf.Operands) { if (f is PredicateFormula) { AddEffect(((PredicateFormula)f).Predicate); } else { AddEffects(f); } } } else if (cf.Operator == "when") { if (m_sPredecessor.Contains(cf.Operands[0])) { AddEffects(cf.Operands[1]); } } else { throw new NotImplementedException(); } } }
private int CostForAchieving(Predicate p, State s, Dictionary<Predicate, int> dCostForAchieving, List<Predicate> lObserved) { if (s.Contains(p)) return 0; if (dCostForAchieving.ContainsKey(p)) return dCostForAchieving[p]; if (lObserved.Contains(p)) return int.MaxValue; lObserved.Add(p); int iMin = int.MaxValue - 1; foreach(SortedSet<Predicate> sParents in m_dParents[p]) { int iMax = 0; int iSum = 0; foreach (Predicate pTag in sParents) { int iCostForPTag = CostForAchieving(pTag, s, dCostForAchieving, lObserved); if (iCostForPTag == int.MaxValue) { iSum = int.MaxValue - 1; break; } iSum += iCostForPTag; if (iCostForPTag > iMax) iMax = iCostForPTag; } //if (iMax < iMin) // iMin = iMax; if (iSum < iMin) iMin = iSum; } lObserved.Remove(p); //if (iMin < 0) // Debug.WriteLine("*"); dCostForAchieving[p] = iMin + 1; return iMin + 1; }
public bool IsGoalState(State s) { return(s.Contains(Goal)); }