public RegressionHeuristic(Domain d, Formula fGoal, bool bMax) { m_dDomain = d; m_bMax = bMax; m_lGoal = new List<Predicate>(fGoal.GetAllPredicates()); //ComputeFullRegression(); }
public bool AddOperand(Formula f) { if (Operands.Count > 0) throw new NotImplementedException(); SimpleAddOperand(f); return false; }
public HSPHeuristic(Domain d, Formula fGoal, bool bMax) { m_dDomain = d; m_bMax = bMax; m_lGoal = new List<Predicate>(fGoal.GetAllPredicates()); //foreach (PredicateFormula vf in ((CompoundFormula)fGoal).Operands) // m_lGoal.Add(vf.Predicate); //ComputeAllEffectsPreconditions(); }
private CompoundFormula AddKnowledgePredicatesToFormula(Formula f, List<Predicate> lKnowPredicates) { CompoundFormula cf = new CompoundFormula("and"); List<Predicate> lPredicates = GetAllPredicates(f); foreach (Predicate p in lPredicates) { if (lKnowPredicates.Contains(p)) cf.AddOperand(new PredicateFormula(new KnowPredicate(p))); } cf.AddOperand(f); return cf; }
private Dictionary<Predicate, List<Action>> GetLandmarkAchievers(Formula fLandmark, HashSet<Predicate> lAllReachable, List<Action> lPotentialLandmarkAchievers) { Dictionary<Predicate, List<Action>> dActions = new Dictionary<Predicate, List<Action>>(); HashSet<Predicate> lLandmark = fLandmark.GetAllPredicates(); foreach (Predicate p in lLandmark) { dActions[p] = new List<Action>(); foreach (Action a in lPotentialLandmarkAchievers) { if (a.Preconditions.IsTrue(lAllReachable)) { Formula fEffects = a.GetApplicableEffects(lAllReachable, false); HashSet<Predicate> lEffects = fEffects.GetAllPredicates(); if (lEffects.Contains(p)) { dActions[p].Add(a); } } } } return dActions; }
private void FilterActions(List<Action> lGrounded, Formula fCurrent, List<Action> lFiltered, List<Action> lLandmarkAchievers) { HashSet<Predicate> lLandmark = fCurrent.GetAllPredicates(); foreach (Action a in lGrounded) { HashSet<Predicate> lConditionalEffects = new HashSet<Predicate>(), lNonConditionalEffects = new HashSet<Predicate>(); a.Effects.GetAllEffectPredicates(lConditionalEffects, lNonConditionalEffects); bool bAchieveLandmark = false; foreach (Predicate p in lLandmark) { if (lConditionalEffects.Contains(p) || lNonConditionalEffects.Contains(p)) { bAchieveLandmark = true; break; } } if (bAchieveLandmark) lLandmarkAchievers.Add(a); else lFiltered.Add(a); } }
//returns true if operand already exists public bool AddOperand(Formula f) { m_sCachedToString = null; if (f == null) return false; if (Size > 20 || f.Size > 20) { SimpleAddOperand(f); return false; } if (Operator == "when") { if (Operands.Count < 2) { Operands.Add(f); Size += f.Size; } else throw new NotImplementedException(); return false; } if (Operands.Contains(f)) return true; if (f is ParametrizedFormula)//for universal quantifiers, assuming for now that they were not added before { Operands.Add(f); Size += f.Size; return false; } if (f is ProbabilisticFormula) { Operands.Add(f); Size += f.Size; return false; } if (f is PredicateFormula) { Predicate p = ((PredicateFormula)f).Predicate; if (p.Name == Domain.TRUE_PREDICATE) if (Operator == "and") return false; if (p.Name == Domain.FALSE_PREDICATE) if (Operator == "or") return false; } if( f is CompoundFormula && ((CompoundFormula)f).Operator == Operator) { bool bContainsAll = true; foreach (Formula fOperand in ((CompoundFormula)f).Operands) { if (!AddOperand(fOperand)) bContainsAll = false; else Size += fOperand.Size; } return bContainsAll; } if (f is CompoundFormula) { CompoundFormula cf = (CompoundFormula)f; if (cf.Operands.Count == 1) { Size += cf.Operands[0].Size; return AddOperand(cf.Operands[0]); } } Simplified = false; if (f is CompoundFormula && ((CompoundFormula)f).Operator == "oneof") { Size += f.Size; Operands.Add(f);//don't know how to negate oneof for now return false; } else { if (Operator != "oneof" && Operands.Contains(f.Negate())) { if (Operator == "and") Operands.Add(new PredicateFormula(new GroundedPredicate(Domain.FALSE_PREDICATE))); else if (Operator == "or") Operands.Add(new PredicateFormula(new GroundedPredicate(Domain.TRUE_PREDICATE))); else throw new NotImplementedException(); Operands.Remove(f.Negate()); return true; } else { Size += f.Size; Operands.Add(f); return false; } } return true; }
private CompoundFormula InsertGiven(Formula fGiven, out bool bInserted) { CompoundFormula cfNew = new CompoundFormula(Operator); bInserted = false; if (Operator == "when") { CompoundFormula cfAnd = new CompoundFormula("and"); cfAnd.AddOperand(Operands[0]); cfAnd.AddOperand(fGiven); cfNew.AddOperand(cfAnd); cfNew.AddOperand(Operands[1]); bInserted = true; } else if (Operator == "and") { foreach (Formula f in Operands) { if (f is PredicateFormula) cfNew.AddOperand(f); else { CompoundFormula cf = (CompoundFormula)f; cfNew.AddOperand(cf.InsertGiven(fGiven, out bInserted)); } } } else if (Operator == "not") { return this;//when cannot be inside a negation (I think) } else throw new NotImplementedException(); return cfNew; }
/* public void SimpleAddOperand(Formula f) { Operands.Add(f); } */ public void SimpleAddOperand(Formula f) { m_sCachedToString = null; if (f == null) return; if (f is ParametrizedFormula)//for universal quantifiers, assuming for now that they were not added before { Size += f.Size; Operands.Add(f); return; } if (f is CompoundFormula && ((CompoundFormula)f).Operator == Operator) { bool bContainsAll = true; foreach (Formula fOperand in ((CompoundFormula)f).Operands) { Size += fOperand.Size; SimpleAddOperand(fOperand); } return; } if (f is CompoundFormula) { CompoundFormula cf = (CompoundFormula)f; if (cf.Operands.Count == 1) { Size += cf.Operands[0].Size; AddOperand(cf.Operands[0]); return; } } Size += f.Size; Simplified = false; Operands.Add(f); }
public abstract void addObservation(Formula s);
public Formula RegressObservation(Formula f) { /* There is no point in adding the observation, because it was already regressed CompoundFormula fWithObservation = new CompoundFormula("and"); fWithObservation.AddOperand(f); if (GeneratingObservation != null) fWithObservation.AddOperand(GeneratingObservation); Formula fReduced = fWithObservation.Reduce(Observed); */ Formula fReduced = f.Reduce(Observed); Formula fToRegress = fReduced; if (fToRegress is CompoundFormula) { bool bChanged = false; //fToRegress = ((CompoundFormula)fToRegress).RemoveNestedConjunction(out bChanged).Simplify(); } if (fToRegress.IsTrue(null)) return fToRegress; if (fToRegress.IsFalse(null)) Debug.Assert(false); if (GeneratingAction.HasConditionalEffects) { Formula fRegressed = fToRegress.Regress(GeneratingAction, Observed); return fRegressed; } else return fToRegress; }
private void AddHidden(Formula f) { if (f is PredicateFormula) AddHidden(((PredicateFormula)f).Predicate); else { CompoundFormula cf = (CompoundFormula)f; foreach (Formula fSub in cf.Operands) AddHidden(fSub); } }
private void AddEffects(Formula fEffects) { if (fEffects is PredicateFormula) { AddEffect(((PredicateFormula)fEffects).Predicate); } else { CompoundFormula cf = (CompoundFormula)fEffects; if (cf.Operator == "oneof") { foreach (Formula f in cf.Operands) AddHidden(f); } else if (cf.Operator != "and") throw new NotImplementedException(); else { foreach (Formula f in cf.Operands) { if (f is PredicateFormula) { AddEffect(((PredicateFormula)f).Predicate); } else AddEffects(f); } } } }
//public string GetObservation(BeliefState bs) //{ // string obs = ""; // foreach (Predicate p in bs.Observed) // if ((p.Name.Contains("free")) && (p.Negation == false)) obs = obs + p.Name + "\r\n"; // return obs; // // //} public override void addObservation(Formula s) { obs = obs + s; }
public override Formula Replace(Formula fOrg, Formula fNew) { if (Equals(fOrg)) return fNew; return this; }
//used to regress goal or precondition public bool RegressCondition(Formula f) { PartiallySpecifiedState pssCurrent = this; Formula fCurrent = f.Negate(); while (pssCurrent != null) { Formula fReduced = fCurrent.Reduce(pssCurrent.Observed); if (fReduced.IsTrue(null)) return false; if (fReduced.IsFalse(null)) return true; if (pssCurrent.Predecessor != null) { if (pssCurrent.GeneratingAction.HasConditionalEffects) { Formula fRegressed = fReduced.Regress(pssCurrent.GeneratingAction); fCurrent = fRegressed; } } pssCurrent = pssCurrent.Predecessor; } return !m_bsInitialBelief.ConsistentWith(fCurrent); }
private PartiallySpecifiedState Apply(Action aOrg, out Formula fObserve, bool bPropogateOnly) { //Debug.WriteLine("Executing " + a.Name); fObserve = null; if (aOrg is ParametrizedAction) return null; DateTime dtStart = DateTime.Now; Action a = aOrg.ApplyObserved(m_lObserved); //no need to check pre during propogation - they were already confirmed the first time if (!bPropogateOnly && a.Preconditions != null && !IsApplicable(a)) return null; a.ComputeRegressions(); tsPre += DateTime.Now - dtStart; dtStart = DateTime.Now; State sNew = null; if (!bPropogateOnly && UnderlyingEnvironmentState != null) sNew = UnderlyingEnvironmentState.Apply(a); CompoundFormula cfAndChoices = null; if (!bPropogateOnly && a.ContainsNonDeterministicEffect) { a = a.RemoveNonDeterminism(Time, out cfAndChoices); } PartiallySpecifiedState bsNew = new PartiallySpecifiedState(this, a); if (sNew != null) { bsNew.UnderlyingEnvironmentState = sNew; if (!bPropogateOnly && bsNew.Time != sNew.Time) Debug.WriteLine("BUGBUG"); } if (a.Effects != null) { if (a.HasConditionalEffects) { List<CompoundFormula> lApplicableConditions = ApplyKnown(a.GetConditions()); bsNew.ApplyKnowledgeLoss(lApplicableConditions); HashSet<Predicate> lAddEffects = new HashSet<Predicate>(), lRemoveEffects = new HashSet<Predicate>(); a.GetApplicableEffects(m_lObserved,lAddEffects, lRemoveEffects, true); //first removing then adding foreach (Predicate p in lRemoveEffects) bsNew.AddEffect(p); foreach (Predicate p in lAddEffects) bsNew.AddEffect(p); //bsNew.UpdateHidden(a, m_lObserved); bsNew.UpdateHidden(); } else { bsNew.AddEffects(a.Effects); } } //if(m_sPredecessor != null)//the first one holds all knowns, to avoid propogation from the initial belief // RemoveDuplicateObserved(bsNew.m_lObserved);//if p is true at t+1 and p is true at t, there is no point in maintaining the copy at t tsEffects += DateTime.Now - dtStart; dtStart = DateTime.Now; if (!bPropogateOnly && a.Observe != null) { //first applying the action (effects) and then observing fObserve = bsNew.UnderlyingEnvironmentState.Observe(a.Observe); bsNew.GeneratingObservation = fObserve; bsNew.AddObserved(fObserve); /* if (ReviseInitialBelief(fObserve)) bsNew.PropogateObservedPredicates(); * */ HashSet<int> hsModified = m_bsInitialBelief.ReviseInitialBelief(fObserve, this); if (hsModified.Count > 0) { if (!SDRPlanner.OptimizeMemoryConsumption) bsNew.PropogateObservedPredicates(); } } tsObs += DateTime.Now - dtStart; if (bsNew != null && cfAndChoices != null) m_bsInitialBelief.AddInitialStateFormula(cfAndChoices); if (!bPropogateOnly && bsNew.Time != sNew.Time) Debug.WriteLine("BUGBUG"); return bsNew; }
private bool ConsistentWith(Formula fOriginal, bool bCheckingActionPreconditions) { PartiallySpecifiedState pssCurrent = this; Formula fCurrent = fOriginal; Formula fReduced = null; int cRegressions = 0; while (pssCurrent.m_sPredecessor != null) { fReduced = fCurrent.Reduce(pssCurrent.Observed); if (fReduced.IsTrue(null)) return true; if (fReduced.IsFalse(null)) return false; Formula fToRegress = fReduced; if (fToRegress is CompoundFormula) { //bool bChanged = false; //fToRegress = ((CompoundFormula)fToRegress).RemoveNestedConjunction(out bChanged); } if (fToRegress.IsTrue(pssCurrent.Observed)) return true; if (fToRegress.IsFalse(pssCurrent.Observed)) return false; Formula fRegressed = fToRegress.Regress(pssCurrent.GeneratingAction, pssCurrent.Observed); //Formula fRegressed = fToRegress.Regress(GeneratingAction); cRegressions++; fCurrent = fRegressed; pssCurrent = pssCurrent.m_sPredecessor; } fReduced = fCurrent.Reduce(pssCurrent.Observed); if (fReduced.IsTrue(null)) return true; if (fReduced.IsFalse(null)) return false; return m_bsInitialBelief.ConsistentWith(fReduced); //m_bsInitialBelief.ApplyReasoning(); }
public HashSet<Predicate> AddObserved(Formula f) { HashSet<Predicate> hsNew = new HashSet<Predicate>(); if (f is PredicateFormula) { Predicate p = ((PredicateFormula)f).Predicate; if (AddObserved(p)) hsNew.Add(p); } else { CompoundFormula cf = (CompoundFormula)f; if (cf.Operator == "and") foreach (Formula fSub in cf.Operands) hsNew.UnionWith(AddObserved(fSub)); else { //do nothing here - not adding formulas currently, only certainties //throw new NotImplementedException(); } } return hsNew; }
public override Formula Replace(Formula fOrg, Formula fNew) { if (this.Equals(fOrg)) return fNew; CompoundFormula fReplaced = new CompoundFormula(Operator); foreach (Formula f in Operands) fReplaced.AddOperand(f.Replace(fOrg, fNew)); return fReplaced; }
public PartiallySpecifiedState Apply(string sActionName, out Formula fObserve) { fObserve = null; Action a = Problem.Domain.GroundActionByName(sActionName.Split(' ')); if (a == null) return null; PartiallySpecifiedState pssNext = Apply(a, out fObserve); return pssNext; }
private bool ConditionDeletesPrecondition(out Formula fRest) { //BUGBUG: simplified and not full implementation fRest = null; if (Operator != "when") return false; Formula fPre = Operands[0]; Formula fDelete = fPre.Negate().Simplify(); bool bFound = false; CompoundFormula cfRest = new CompoundFormula("and"); if (Operands[1] is CompoundFormula) { foreach (Formula f in ((CompoundFormula)Operands[1]).Operands) { if (f.Equals(fDelete)) bFound = true; else cfRest.AddOperand(f); } } else { PredicateFormula f = (PredicateFormula)Operands[1]; if (f.Equals(fDelete)) bFound = true; else cfRest.AddOperand(f); } if (!bFound) return false; if (cfRest.Operands.Count == 1) fRest = cfRest.Operands[0]; else fRest = cfRest; return true; }
public PartiallySpecifiedState Apply(Action a, out Formula fObserve) { return Apply(a, out fObserve, false); }
private bool IsSimpleAnd(Formula f) { if (f is PredicateFormula) return true; if (f is ProbabilisticFormula) return false; CompoundFormula cf = (CompoundFormula)f; if (cf.Operator != "and") return false; foreach (Formula fSub in cf.Operands) if (!(fSub is PredicateFormula)) return false; return true; }
public void ApplyOffline(Action a, out Formula fObserve, out PartiallySpecifiedState psTrueState, out PartiallySpecifiedState psFalseState) { psTrueState = null; psFalseState = null; fObserve = null; a = a.ApplyObserved(m_lObserved); //for removing all generaly known items from the computations. Formula fPreconditions = a.Preconditions; if (fPreconditions != null && !IsApplicable(a)) return; PartiallySpecifiedState bsNew = new PartiallySpecifiedState(this, a); ChildCount = 1; if (a.Effects != null) { if (a.HasConditionalEffects) { List<CompoundFormula> lApplicableConditions = ApplyKnown(a.GetConditions()); bsNew.ApplyKnowledgeLoss(lApplicableConditions); HashSet<Predicate> lAddEffects = new HashSet<Predicate>(), lRemoveEffects = new HashSet<Predicate>(); a.GetApplicableEffects(m_lObserved, lAddEffects, lRemoveEffects, true); //first removing then adding foreach (Predicate p in lRemoveEffects) bsNew.AddEffect(p); foreach (Predicate p in lAddEffects) bsNew.AddEffect(p); //bsNew.UpdateHidden(a, m_lObserved); bsNew.UpdateHidden(); } else { bsNew.AddEffects(a.Effects); } } if (a.Observe != null) { PartiallySpecifiedState bsTrue = bsNew.Clone(); PartiallySpecifiedState bsFalse = bsNew.Clone(); bsTrue.GeneratingObservation = a.Observe; bsFalse.GeneratingObservation = a.Observe.Negate(); ChildCount = 0; if (ConsistentWith(bsTrue.GeneratingObservation, false)) { HashSet<int> hsModifiedTrue = bsTrue.m_bsInitialBelief.ReviseInitialBelief(bsTrue.GeneratingObservation, bsTrue); if (hsModifiedTrue.Count > 0) { bsTrue.PropogateObservedPredicates(); } bsTrue.AddObserved(a.Observe); psTrueState = bsTrue; ChildCount++; } else psTrueState = null; if (ConsistentWith(bsFalse.GeneratingObservation, false)) { HashSet<int> hsModifiedFalse = bsFalse.m_bsInitialBelief.ReviseInitialBelief(bsFalse.GeneratingObservation, bsFalse); if (hsModifiedFalse.Count > 0) { bsFalse.PropogateObservedPredicates(); } bsFalse.AddObserved(a.Observe.Negate()); psFalseState = bsFalse; ChildCount++; } else psFalseState = null; } else psTrueState = bsNew; }
public abstract Formula Replace(Formula fOrg, Formula fNew);
public void ApplyOffline(string sActionName, out Action a, out Formula fObserve, out PartiallySpecifiedState psTrueState, out PartiallySpecifiedState psFalseState) { psTrueState = null; psFalseState = null; fObserve = null; a = Problem.Domain.GroundActionByName(sActionName.Split(' ')); if (a == null || a is ParametrizedAction) return; ApplyOffline(a, out fObserve, out psTrueState, out psFalseState); }
private void FilterActions(List<Action> lGrounded, Formula fCurrent, List<Action> lFiltered, List<Action> lLandmarkAchievers, Dictionary<Predicate, List<Action>> dPreconditionToAction, Dictionary<Predicate, List<Action>> dEffectToAction, HashSet<Predicate> lInitialState) { HashSet<Predicate> lLandmark = fCurrent.GetAllPredicates(); HashSet<string> hsPreNames = new HashSet<string>(); HashSet<string> hsEffectNames = new HashSet<string>(); foreach (Predicate p in lLandmark) { if (!lInitialState.Contains(p)) { if (dPreconditionToAction.ContainsKey(p)) { foreach (Action a in dPreconditionToAction[p]) hsPreNames.Add(a.Name); } } if (dEffectToAction.ContainsKey(p)) { foreach (Action a in dEffectToAction[p]) hsEffectNames.Add(a.Name); } } foreach (Action a in lGrounded) { //if (a.Name.Contains("p3-1") && a.Name.Contains("smell")) // Console.WriteLine("*"); if (hsEffectNames.Contains(a.Name)) lLandmarkAchievers.Add(a); else if(!hsPreNames.Contains(a.Name)) lFiltered.Add(a); } }
public bool Contains(Formula f) { return f.ContainedIn(m_lObserved, true); }
private List<Formula> RegressLandmark(Formula fCurrentLandmark, Dictionary<Predicate, List<Action>> dLandmarkAchievers, HashSet<Predicate> lInitialState) { //when landmark is (or p1 p2), and p1 requires (or q1 q2) (or r1 r2) and p2 requires p3 and r3, then the result should be (or q1 q2 q3) (or r1 r2 r3) List<Formula> lFormulas = new List<Formula>(); Dictionary<string, CompoundFormula> dAll = null; int cPredicatesToAchieve = 0; foreach (GroundedPredicate pLandmark in dLandmarkAchievers.Keys) { List<Action> lActions = dLandmarkAchievers[pLandmark]; Dictionary<string, CompoundFormula> dRegressedFormulas = new Dictionary<string, CompoundFormula>(); RegressLandmark(pLandmark, dLandmarkAchievers[pLandmark], lInitialState, dRegressedFormulas); string sLandmarkName = GetNameAndTag(pLandmark); if (dAll == null) dAll = dRegressedFormulas; else { Dictionary<string, CompoundFormula> dNew = new Dictionary<string, CompoundFormula>(); foreach (string sKey in dAll.Keys) { if (dRegressedFormulas.ContainsKey(sKey)) { CompoundFormula cfOr = new CompoundFormula("or"); cfOr.AddOperand(dAll[sKey]); cfOr.AddOperand(dRegressedFormulas[sKey]); dNew[sKey] = cfOr; } } dAll = dNew; } } foreach (string sPreType in dAll.Keys) { Formula fSimplified = dAll[sPreType].Simplify(); //4 options here: //fSimplified is a single predicate //fSimplified is a simple disjunction //fSimplified is a simple conjunction //fSimplified is a disjunction of simple conjunctions if (fSimplified is PredicateFormula) lFormulas.Add(fSimplified); else { CompoundFormula cf = (CompoundFormula)fSimplified; if (cf.Operator == "and") { foreach (PredicateFormula pf in cf.Operands) lFormulas.Add(pf); } else lFormulas.Add(RemoveConjunctions(cf)); } } return lFormulas; }