//C->L ==> KC/t->KL/t private CompoundFormula CreateTaggedKnowledgeGainCondition(CompoundFormula cfCondition, string sTag, List<string> lAlwaysKnown, bool bNonDetEffect) { CompoundFormula cfWhen = new CompoundFormula("when"); HashSet<Predicate> lPreconditions = new HashSet<Predicate>(); HashSet<Predicate> lEffects = new HashSet<Predicate>(); cfCondition.Operands[0].GetAllPredicates(lPreconditions); cfCondition.Operands[1].GetAllPredicates(lEffects); Formula cfPreconditions = cfCondition.Operands[0].GenerateGiven(sTag, lAlwaysKnown); CompoundFormula cfEffects = new CompoundFormula("and"); foreach (Predicate p in lEffects) { if (lAlwaysKnown == null || !lAlwaysKnown.Contains(p.Name)) { Predicate pKEffect = p.GenerateKnowGiven(sTag); cfEffects.AddOperand(pKEffect); if (bNonDetEffect) { KnowPredicate pK = new KnowPredicate(p); cfEffects.AddOperand(pK.Negate()); } } } if (cfEffects.Operands.Count == 0) return null; cfWhen.AddOperand(cfPreconditions.Simplify()); cfWhen.AddOperand(cfEffects.Simplify()); return cfWhen; }
public Action KnowWhetherCompilation(Dictionary<string, List<Predicate>> dTags, Domain d) { Action aNew = Clone(); aNew.Name = Name + "-KW"; List<CompoundFormula> lConditions = new List<CompoundFormula>(); List<Formula> lObligatory = new List<Formula>(); SplitEffects(lConditions, lObligatory); CompoundFormula cfKWPreconditions = new CompoundFormula("and"); HashSet<Predicate> lKnowPreconditions = new HashSet<Predicate>(); if (Preconditions != null) { Preconditions.GetAllPredicates(lKnowPreconditions); foreach (Predicate p in lKnowPreconditions) { if(!d.AlwaysKnown(p)) cfKWPreconditions.AddOperand(new KnowWhetherPredicate(p)); if(d.AlwaysKnown(p) && d.AlwaysConstant(p)) cfKWPreconditions.AddOperand(new KnowPredicate(p)); } if (cfKWPreconditions.Operands.Count > 0) aNew.Preconditions = cfKWPreconditions; else aNew.Preconditions = null; } if (Effects != null) { HashSet<Predicate> lKnowEffects = new HashSet<Predicate>(); CompoundFormula cfEffects = new CompoundFormula("and"); CompoundFormula cfMandatoryEffects = new CompoundFormula("and"); foreach (Formula f in lObligatory) { f.GetAllPredicates(lKnowEffects); //cfEffects.AddOperand(f);//BGUBGU - probably a bug here. Need to separate always known and the rest. } if (lKnowEffects.Count > 0) { foreach (string sTag in dTags.Keys) { //K(preconditions|s)->K(effects|s) CompoundFormula cfKEffects = new CompoundFormula("and"); CompoundFormula cfKPreconditions = new CompoundFormula("and"); foreach (Predicate p in lKnowPreconditions) { if (d.AlwaysKnown(p) && d.AlwaysConstant(p)) continue; else cfKPreconditions.AddOperand(p.GenerateGiven(sTag)); } foreach (Predicate p in lKnowEffects) { Predicate pAdd = p.GenerateGiven(sTag); cfKEffects.AddOperand(pAdd); //Predicate pDelete = p.Negate().GenerateKnowGiven(sTag).Negate(); //cfKEffects.AddOperand(pDelete); } if (cfKPreconditions.Operands.Count > 0) { CompoundFormula cfCondition = new CompoundFormula("when"); cfCondition.AddOperand(cfKPreconditions); cfCondition.AddOperand(cfKEffects); cfEffects.AddOperand(cfCondition); } else cfEffects.AddOperand(cfKEffects); } } //forgetting: ~K~p foreach (Predicate p in lKnowEffects) { Predicate pKNotp = new KnowPredicate(p.Negate()); cfEffects.AddOperand(pKNotp.Negate()); } foreach (CompoundFormula cfCondition in lConditions) { CompoundFormula cfK = null, cfOr = null, cfAnd = null; //cfK = CreateKnowledgeGainCondition(cfCondition, d.m_lAlwaysKnown, false); //if (cfK != null) // cfEffects.AddOperand(cfK); cfK = CreateKnowledgeLossCondition(cfCondition, d.m_lAlwaysKnown, false); if (cfK != null) { cfOr = new CompoundFormula("or"); foreach (Predicate p in lKnowPreconditions) { Predicate pKNot = new KnowPredicate(p.Negate()); cfOr.AddOperand(pKNot.Negate()); } if (cfK.Operator == "when") { if (cfK.Operands[0] is CompoundFormula && ((CompoundFormula)cfK.Operands[0]).Operands.Count > 0) cfOr.AddOperand(cfK.Operands[0]); cfK.Operands[0] = cfOr.Simplify(); } else { CompoundFormula cfWhen = new CompoundFormula("when"); cfWhen.AddOperand(cfOr.Simplify()); cfWhen.AddOperand(cfK); cfK = cfWhen; } cfEffects.AddOperand(cfK); } //cfK = CreateKnowledgeGainCondition(cfCondition, d.m_lAlwaysKnown, true); //if (cfK != null) // cfEffects.AddOperand(cfK); cfK = CreateKnowledgeLossCondition(cfCondition, d.m_lAlwaysKnown, true); if (cfK != null) { cfOr = new CompoundFormula("or"); foreach (Predicate p in lKnowPreconditions) { Predicate pKNot = new KnowPredicate(p.Negate()); cfOr.AddOperand(pKNot.Negate()); } if (cfK.Operator == "when") { if (cfK.Operands[0] is PredicateFormula || ((CompoundFormula)cfK.Operands[0]).Operands.Count > 0) cfOr.AddOperand(cfK.Operands[0]); cfK.Operands[0] = cfOr.Simplify(); } else { CompoundFormula cfWhen = new CompoundFormula("when"); cfWhen.AddOperand(cfOr.Simplify()); cfWhen.AddOperand(cfK); cfK = cfWhen; } cfEffects.AddOperand(cfK); } foreach (string sTag in dTags.Keys) { cfK = CreateTaggedCondition(cfCondition, d, sTag); if (cfK != null) { cfAnd = new CompoundFormula("and"); foreach (Predicate p in lKnowPreconditions) { if (d.AlwaysKnown(p) && d.AlwaysConstant(p)) cfAnd.AddOperand(new KnowPredicate(p)); else cfAnd.AddOperand(p.GenerateGiven(sTag)); } if (cfK.Operator == "when") { cfAnd.AddOperand(cfK.Operands[0]); cfK.Operands[0] = cfAnd; cfEffects.AddOperand(cfK); } else throw new NotImplementedException(); } cfK = CreateTaggedKnowledgeWhetherGainCondition(cfCondition, d, sTag); if (cfK != null) { cfAnd = new CompoundFormula("and"); foreach (Predicate p in lKnowPreconditions) { if (d.AlwaysKnown(p) && d.AlwaysConstant(p)) cfAnd.AddOperand(new KnowPredicate(p)); else cfAnd.AddOperand(p.GenerateGiven(sTag)); } if (cfK.Operator == "when") { cfAnd.AddOperand(cfK.Operands[0]); cfK.Operands[0] = cfAnd; cfEffects.AddOperand(cfK); } else throw new NotImplementedException(); } cfK = CreateTaggedKnowledgeWhetherLossCondition(cfCondition, d, sTag); if (cfK != null) { cfOr = new CompoundFormula("or"); foreach (Predicate p in lKnowPreconditions) { Predicate pKNot = new KnowPredicate(p.Negate()); cfOr.AddOperand(pKNot.Negate()); } if (cfK.Operator == "when") { if (cfK.Operands[0] is PredicateFormula || ((CompoundFormula)cfK.Operands[0]).Operands.Count > 0) cfOr.AddOperand(cfK.Operands[0]); cfK.Operands[0] = cfOr.Simplify(); } else { CompoundFormula cfWhen = new CompoundFormula("when"); cfWhen.AddOperand(cfOr.Simplify()); cfWhen.AddOperand(cfK); cfK = cfWhen; } } } } aNew.Effects = cfEffects.Simplify(); } if (Observe != null) { throw new NotImplementedException(); } return aNew; }
public Action AddTaggedConditions(Dictionary<string, List<Predicate>> dTags, List<string> lAlwaysKnown) { Action aNew = Clone(); List<CompoundFormula> lConditions = new List<CompoundFormula>(); List<Formula> lObligatory = new List<Formula>(); SplitEffects(lConditions, lObligatory); CompoundFormula cfPreconditions = new CompoundFormula("and"); HashSet<Predicate> lKnowPreconditions = new HashSet<Predicate>(); if (Preconditions != null) { Preconditions.GetAllPredicates(lKnowPreconditions); cfPreconditions.AddOperand(Preconditions); foreach (Predicate p in lKnowPreconditions) if (!lAlwaysKnown.Contains(p.Name)) cfPreconditions.AddOperand(new PredicateFormula(new KnowPredicate(p))); if (SDRPlanner.SplitConditionalEffects) cfPreconditions.AddOperand(new GroundedPredicate("NotInAction")); aNew.Preconditions = cfPreconditions; } if (Effects != null) { HashSet<Predicate> lKnowEffects = new HashSet<Predicate>(); CompoundFormula cfEffects = new CompoundFormula("and"); foreach (Formula f in lObligatory) { f.GetAllPredicates(lKnowEffects); cfEffects.AddOperand(f); } foreach (Predicate p in lKnowEffects) { if (!lAlwaysKnown.Contains(p.Name)) { Predicate pKEffect = new KnowPredicate(p); cfEffects.AddOperand(pKEffect); pKEffect = new KnowPredicate(p.Negate()); cfEffects.AddOperand(pKEffect.Negate()); foreach (string sTag in dTags.Keys) { pKEffect = p.GenerateKnowGiven(sTag); cfEffects.AddOperand(pKEffect); pKEffect = p.Negate().GenerateKnowGiven(sTag); cfEffects.AddOperand(pKEffect.Negate()); } } } foreach (CompoundFormula cfCondition in lConditions) { cfEffects.AddOperand(cfCondition); CompoundFormula cfK = CreateKnowledgeGainCondition(cfCondition, lAlwaysKnown, false); if (cfK != null) cfEffects.AddOperand(cfK); cfK = CreateKnowledgeLossCondition(cfCondition, lAlwaysKnown); if (cfK != null) cfEffects.AddOperand(cfK); foreach (string sTag in dTags.Keys) { cfK = CreateTaggedKnowledgeGainCondition(cfCondition, sTag, lAlwaysKnown, false); if (cfK != null) cfEffects.AddOperand(cfK); cfK = CreateTaggedKnowledgeLossCondition(cfCondition, sTag, lAlwaysKnown); if (cfK != null) cfEffects.AddOperand(cfK); } } aNew.Effects = cfEffects; } if (Observe != null) { if (aNew.Effects == null) aNew.Effects = new CompoundFormula("and"); Predicate pObserve = ((PredicateFormula)Observe).Predicate; CompoundFormula cfWhen = new CompoundFormula("when"); cfWhen.AddOperand(pObserve); cfWhen.AddOperand(new KnowPredicate(pObserve)); ((CompoundFormula)aNew.Effects).AddOperand(cfWhen); cfWhen = new CompoundFormula("when"); cfWhen.AddOperand(pObserve.Negate()); cfWhen.AddOperand(new KnowPredicate(pObserve.Negate())); ((CompoundFormula)aNew.Effects).AddOperand(cfWhen); } return aNew; }
public List<Action> KnowCompilationSplitConditions(Dictionary<string, List<Predicate>> dTags, List<string> lAlwaysKnown, List<Predicate> lAdditionalPredicates) { List<Action> lActions = new List<Action>(); ParametrizedAction aNewAdd = new ParametrizedAction(Name + "-Add"); ParametrizedAction aNewRemove = new ParametrizedAction(Name + "-Remove"); ParametrizedAction aNewTranslateAdd = new ParametrizedAction(Name + "-TranslateAdd"); ParametrizedAction aNewTranslateRemove = new ParametrizedAction(Name + "-TranslateRemove"); ParametrizedPredicate ppInFirst = new ParametrizedPredicate("P1-" + Name); ParametrizedPredicate ppInSecond = new ParametrizedPredicate("P2-" + Name); ParametrizedPredicate ppInThird = new ParametrizedPredicate("P3-" + Name); GroundedPredicate gpNotInAction = new GroundedPredicate("NotInAction"); if (this is ParametrizedAction) { foreach (Parameter p in ((ParametrizedAction)this).Parameters) { aNewAdd.AddParameter(p); aNewRemove.AddParameter(p); aNewTranslateAdd.AddParameter(p); aNewTranslateRemove.AddParameter(p); ppInFirst.AddParameter(p); ppInSecond.AddParameter(p); ppInThird.AddParameter(p); } } List<CompoundFormula> lConditions = new List<CompoundFormula>(); List<Formula> lObligatory = new List<Formula>(); SplitEffects(lConditions, lObligatory); CompoundFormula cfPreconditions = new CompoundFormula("and"); HashSet<Predicate> lKnowPreconditions = new HashSet<Predicate>(); if (Preconditions != null) { Preconditions.GetAllPredicates(lKnowPreconditions); cfPreconditions.AddOperand(Preconditions); foreach (Predicate p in lKnowPreconditions) if (!lAlwaysKnown.Contains(p.Name)) cfPreconditions.AddOperand(new PredicateFormula(new KnowPredicate(p))); } cfPreconditions.AddOperand(gpNotInAction); if (Effects == null) throw new NotImplementedException(); HashSet<Predicate> lKnowEffects = new HashSet<Predicate>(); CompoundFormula cfAddEffects = new CompoundFormula("and"); CompoundFormula cfRemoveEffects = new CompoundFormula("and"); CompoundFormula cfTranslateAddEffects = new CompoundFormula("and"); CompoundFormula cfTranslateRemoveEffects = new CompoundFormula("and"); List<Predicate> lRequireTranslation = new List<Predicate>(); foreach (Formula f in lObligatory) { f.GetAllPredicates(lKnowEffects); cfAddEffects.AddOperand(f); //unconditional effects cannot conflict anyhow } foreach (Predicate p in lKnowEffects) { if (!lAlwaysKnown.Contains(p.Name)) { Predicate pKEffect = new KnowPredicate(p); cfAddEffects.AddOperand(pKEffect); pKEffect = new KnowPredicate(p.Negate()); cfRemoveEffects.AddOperand(pKEffect.Negate()); foreach (string sTag in dTags.Keys) { pKEffect = p.GenerateKnowGiven(sTag); cfAddEffects.AddOperand(pKEffect); pKEffect = p.Negate().GenerateKnowGiven(sTag); cfRemoveEffects.AddOperand(pKEffect.Negate()); } } } if (lConditions.Count > 0) { lAdditionalPredicates.Add(ppInFirst); lAdditionalPredicates.Add(ppInSecond); lAdditionalPredicates.Add(ppInThird); aNewRemove.Preconditions = cfPreconditions; cfRemoveEffects.AddOperand(ppInFirst); cfRemoveEffects.AddOperand(gpNotInAction.Negate()); aNewAdd.Preconditions = new PredicateFormula(ppInFirst); cfAddEffects.AddOperand(ppInSecond); cfAddEffects.AddOperand(ppInFirst.Negate()); aNewTranslateRemove.Preconditions = new PredicateFormula(ppInSecond); cfTranslateRemoveEffects.AddOperand(ppInSecond.Negate()); cfTranslateRemoveEffects.AddOperand(ppInThird); aNewTranslateAdd.Preconditions = new PredicateFormula(ppInThird); cfTranslateAddEffects.AddOperand(ppInThird.Negate()); cfTranslateAddEffects.AddOperand(gpNotInAction); Dictionary<Predicate, Predicate> dTaggedPredicates = new Dictionary<Predicate,Predicate>(); foreach (CompoundFormula cfCondition in lConditions) { CompoundFormula cfAddCondition, cfRemoveCondition; cfCondition.SplitAddRemove(dTaggedPredicates, out cfAddCondition, out cfRemoveCondition); if (cfAddCondition != null) cfAddEffects.AddOperand(cfAddCondition); if (cfRemoveCondition != null) cfRemoveEffects.AddOperand(cfRemoveCondition); CompoundFormula cfK = CreateKnowledgeGainCondition(cfCondition, lAlwaysKnown, false); if (cfK != null) { cfK.SplitAddRemove(dTaggedPredicates, out cfAddCondition, out cfRemoveCondition); if (cfAddCondition != null) cfAddEffects.AddOperand(cfAddCondition); if (cfRemoveCondition != null) cfRemoveEffects.AddOperand(cfRemoveCondition); } cfK = CreateKnowledgeLossCondition(cfCondition, lAlwaysKnown); if (cfK != null) { cfK.SplitAddRemove(dTaggedPredicates, out cfAddCondition, out cfRemoveCondition); if (cfAddCondition != null) cfAddEffects.AddOperand(cfAddCondition); if (cfRemoveCondition != null) cfRemoveEffects.AddOperand(cfRemoveCondition); } foreach (string sTag in dTags.Keys) { cfK = CreateTaggedKnowledgeGainCondition(cfCondition, sTag, lAlwaysKnown, false); if (cfK != null) { cfK.SplitAddRemove(dTaggedPredicates, out cfAddCondition, out cfRemoveCondition); if (cfAddCondition != null) cfAddEffects.AddOperand(cfAddCondition); if (cfRemoveCondition != null) cfRemoveEffects.AddOperand(cfRemoveCondition); } cfK = CreateTaggedKnowledgeLossCondition(cfCondition, sTag, lAlwaysKnown); if (cfK != null) { cfK.SplitAddRemove(dTaggedPredicates, out cfAddCondition, out cfRemoveCondition); if (cfAddCondition != null) cfAddEffects.AddOperand(cfAddCondition); if (cfRemoveCondition != null) cfRemoveEffects.AddOperand(cfRemoveCondition); } } } aNewAdd.Effects = cfAddEffects.Simplify(); aNewRemove.Effects = cfRemoveEffects.Simplify(); lActions.Add(aNewRemove); lActions.Add(aNewAdd); foreach (KeyValuePair<Predicate, Predicate> pair in dTaggedPredicates) { CompoundFormula cfWhen = new CompoundFormula("when"); CompoundFormula cfAnd = new CompoundFormula("and"); cfWhen.AddOperand(pair.Key); cfAnd.SimpleAddOperand(pair.Value); cfAnd.SimpleAddOperand(pair.Key.Negate()); cfWhen.SimpleAddOperand(cfAnd); if (pair.Value.Negation) cfTranslateRemoveEffects.AddOperand(cfWhen); else cfTranslateAddEffects.AddOperand(cfWhen); } aNewTranslateAdd.Effects = cfTranslateAddEffects; aNewTranslateRemove.Effects = cfTranslateRemoveEffects; lActions.Add(aNewTranslateRemove); lActions.Add(aNewTranslateAdd); } else { Action aK = AddTaggedConditions(dTags, lAlwaysKnown); lActions.Add(aK); } if (Observe != null) { throw new NotImplementedException(); } return lActions; }