private CompoundFormula AddKnowledgePredicatesToFormula(Formula f, HashSet <Predicate> lKnowPredicates) { CompoundFormula cf = new CompoundFormula("and"); HashSet <Predicate> lPredicates = new HashSet <Predicate>(); f.GetAllPredicates(lPredicates); foreach (Predicate p in lPredicates) { if (lKnowPredicates.Contains(p)) { KnowPredicate kp = new KnowPredicate(p); cf.AddOperand(new PredicateFormula(kp)); } } if (f is CompoundFormula && ((CompoundFormula)f).Operator == "and") { foreach (Formula fSub in ((CompoundFormula)f).Operands) { cf.AddOperand(fSub); } } else { cf.AddOperand(f); } return(cf); }
private void AddReasoningAction(HashSet <Predicate> lPreconditions, HashSet <Predicate> lEffects, Dictionary <List <Predicate>, List <Predicate> > dActions) { List <Predicate> lKnowPreconditions = new List <Predicate>(); foreach (GroundedPredicate p in lPreconditions) { KnowPredicate pKnow = new KnowPredicate(p); lKnowPreconditions.Add(pKnow); lKnowPreconditions.Add(p); } List <Predicate> lKnowEffects = new List <Predicate>(); foreach (GroundedPredicate p in lEffects) { KnowPredicate pKnow = new KnowPredicate(p); lKnowEffects.Add(pKnow); } if (dActions.ContainsKey(lKnowPreconditions)) { if (dActions.Comparer.Equals(lKnowEffects, dActions[lKnowPreconditions])) { return; } throw new NotImplementedException(); } dActions[lKnowPreconditions] = lKnowEffects; }
private Action CreateReasoningAction(Predicate pEffect, HashSet <Predicate> lPredicates) { KnowPredicate kpEffect = new KnowPredicate(pEffect); if (Predicates.Contains(kpEffect)) { return(null); } Action a = new KnowledgeAction("Reasoning_" + pEffect.ToString()); a.Preconditions = new CompoundFormula("and"); foreach (Predicate pOther in lPredicates) { if (pOther != pEffect) { KnowPredicate kp = new KnowPredicate(pOther); if (!Predicates.Contains(kp)) { return(null); } ((CompoundFormula)a.Preconditions).AddOperand(new PredicateFormula(kp)); } } CompoundFormula cfEffects = new CompoundFormula("and"); cfEffects.AddOperand(new PredicateFormula(kpEffect)); a.SetEffects(cfEffects); return(a); }
public KnowPredicate(KnowPredicate kp) : base(kp.Name) { Knowledge = kp.Knowledge; Value = kp.Value; Parametrized = kp.Parametrized; }
public override Predicate Negate() { /* * KnowPredicate kpNegate = new KnowPredicate(Knowledge, Value, Parametrized); * kpNegate.Negation = !Negation; * return kpNegate; */ KnowPredicate kpNegate = new KnowPredicate(this); kpNegate.Negation = !Negation; return(kpNegate); }
public override bool Equals(object obj) { if (obj is KnowPredicate) { KnowPredicate kp = (KnowPredicate)obj; if (Value == kp.Value && Negation == kp.Negation) { return(Knowledge.Equals(kp.Knowledge)); } } return(false); }
public override Predicate ToTag() { KnowPredicate ppNew = new KnowPredicate(this); if (Negation) { ppNew.Name = ppNew.Name + "-Remove"; } else { ppNew.Name = ppNew.Name + "-Add"; } ppNew.Negation = false; return(ppNew); }
private void WriteResoningAction(StreamWriter sw, List <Predicate> lPreconditions, List <Predicate> lEffects, int iNumber) { sw.WriteLine("(:action R" + iNumber); sw.Write(":precondition (and"); foreach (Predicate pPrecondition in lPreconditions) { if (pPrecondition is GroundedPredicate) { if (pPrecondition.Negation) { sw.Write(" (not"); } sw.Write(" (" + pPrecondition.Name); foreach (Constant c in ((GroundedPredicate)pPrecondition).Constants) { sw.Write(" " + c.Name); } sw.Write(")"); if (pPrecondition.Negation) { sw.Write(")"); } } else if (pPrecondition is KnowPredicate) { KnowPredicate kp = (KnowPredicate)pPrecondition; sw.Write(" (K" + kp.Knowledge.Name); foreach (Constant c in ((GroundedPredicate)kp.Knowledge).Constants) { sw.Write(" " + c.Name); } sw.Write(")"); } } sw.WriteLine(")"); sw.Write(":effect (and"); foreach (KnowPredicate pEffect in lEffects) { sw.Write(" (K" + pEffect.Knowledge.Name); foreach (Constant c in ((GroundedPredicate)pEffect.Knowledge).Constants) { sw.Write(" " + c.Name); } sw.Write(")"); } sw.WriteLine(")"); sw.WriteLine(")"); }
public override Formula Ground(Dictionary <string, Constant> dBindings) { if (Predicate is ParametrizedPredicate) { ParametrizedPredicate ppred = (ParametrizedPredicate)Predicate; GroundedPredicate gpred = ppred.Ground(dBindings); return(new PredicateFormula(gpred)); } if (Predicate is KnowPredicate) { KnowPredicate kp = (KnowPredicate)Predicate; GroundedPredicate gpred = kp.Ground(dBindings); return(new PredicateFormula(gpred)); } if (Predicate is KnowGivenPredicate) { throw new NotImplementedException(); } return(this); }
public override Predicate Negate() { /* KnowPredicate kpNegate = new KnowPredicate(Knowledge, Value, Parametrized); kpNegate.Negation = !Negation; return kpNegate; */ KnowPredicate kpNegate = new KnowPredicate(this); kpNegate.Negation = !Negation; return kpNegate; }
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; }
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 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; }
//C->L ==> KWC/t->KWL/t private CompoundFormula CreateTaggedKnowledgeWhetherGainConditions(CompoundFormula cfCondition, Domain d, List<string> lIncludedTags) { HashSet<Predicate> lPreconditions = new HashSet<Predicate>(); HashSet<Predicate> lEffects = new HashSet<Predicate>(); cfCondition.Operands[0].GetAllPredicates(lPreconditions); cfCondition.Operands[1].GetAllPredicates(lEffects); CompoundFormula cfWhen = new CompoundFormula("when"); CompoundFormula cfPreconditions = new CompoundFormula("and"); CompoundFormula cfEffects = new CompoundFormula("and"); foreach (Predicate p in lPreconditions) { if (p.Name == Domain.OPTION_PREDICATE) return null; Predicate pKGiven = null; if (d.AlwaysKnown(p) && d.AlwaysConstant(p)) { pKGiven = new KnowPredicate(p); cfPreconditions.AddOperand(pKGiven); } } foreach (string sKWTag in lIncludedTags) { CompoundFormula cfAnd = new CompoundFormula("and"); foreach (Predicate p in lPreconditions) { Predicate pKGiven = null; if (d.AlwaysKnown(p) && d.AlwaysConstant(p)) { continue; } else { if (!d.AlwaysKnown(p)) { pKGiven = p.GenerateKnowGiven(sKWTag, true); cfAnd.AddOperand(pKGiven); } pKGiven = p.GenerateGiven(sKWTag); cfAnd.AddOperand(pKGiven); } } if (cfAnd.Operands.Count > 0) { cfPreconditions.AddOperand(cfAnd); } foreach (Predicate p in lEffects) { Predicate pKEffect = p.GenerateKnowGiven(sKWTag, true); cfEffects.AddOperand(pKEffect); } } cfWhen.AddOperand(cfPreconditions.Simplify()); cfWhen.AddOperand(cfEffects.Simplify()); return cfWhen; }
//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; }
//C->L ==> KWC/t->KWL/t private CompoundFormula CreateTaggedKnowledgeWhetherGainConditions(CompoundFormula cfCondition, Domain d, IEnumerable<string> lTags, string sActionTag) { HashSet<Predicate> lPreconditions = new HashSet<Predicate>(); HashSet<Predicate> lEffects = new HashSet<Predicate>(); cfCondition.Operands[0].GetAllPredicates(lPreconditions); cfCondition.Operands[1].GetAllPredicates(lEffects); CompoundFormula cfAllConditions = new CompoundFormula("and"); foreach (string sKWTag in lTags) { CompoundFormula cfWhen = new CompoundFormula("when"); CompoundFormula cfPreconditions = new CompoundFormula("and"); CompoundFormula cfEffects = new CompoundFormula("and"); if (sKWTag != sActionTag) { Predicate pNotKWTag = Predicate.GenerateKNot(new Constant(Domain.TAG, sKWTag),new Constant(Domain.TAG, sActionTag)); cfPreconditions.AddOperand(pNotKWTag.Negate()); } foreach (Predicate p in lPreconditions) { if (p.Name == Domain.OPTION_PREDICATE) return null; Predicate pKGiven = null; if (d.AlwaysKnown(p) && d.AlwaysConstant(p)) { pKGiven = new KnowPredicate(p); cfPreconditions.AddOperand(pKGiven); } } foreach (string sTag in lTags) { CompoundFormula cfAnd = new CompoundFormula("and"); foreach (Predicate p in lPreconditions) { Predicate pKGiven = null; //if (d.AlwaysKnown(p) && d.AlwaysConstant(p)) if (d.AlwaysKnown(p)) { continue; } else { /* if (!d.AlwaysKnown(p)) { pKGiven = p.GenerateKnowGiven(sTag, true); cfAnd.AddOperand(pKGiven); } * */ pKGiven = p.GenerateGiven(sTag); cfAnd.AddOperand(pKGiven); } } if (cfAnd.Operands.Count > 0)//if there are no conditions then it is always true, and we don't need to care about whether the tag is consistent or not { if (sTag == sActionTag) { cfPreconditions.AddOperand(cfAnd); } else { CompoundFormula cfOr = new CompoundFormula("or"); Predicate pNotTag = Predicate.GenerateKNot(new Constant(Domain.TAG, sTag),new Constant(Domain.TAG, sActionTag)); cfOr.AddOperand(pNotTag); cfOr.AddOperand(cfAnd); cfPreconditions.AddOperand(cfOr); } } } foreach (Predicate p in lEffects) { //Predicate pKEffect = p.GenerateKnowGiven(sKWTag, true); Predicate pKEffect = p.GenerateGiven(sKWTag); cfEffects.AddOperand(pKEffect); } cfWhen.AddOperand(cfPreconditions.Simplify()); cfWhen.AddOperand(cfEffects.Simplify()); cfAllConditions.SimpleAddOperand(cfWhen); } return cfAllConditions; }
private CompoundFormula AddKnowledgePredicatesToFormula(Formula f, HashSet<Predicate> lKnowPredicates) { CompoundFormula cf = new CompoundFormula("and"); HashSet<Predicate> lPredicates = new HashSet<Predicate>(); f.GetAllPredicates(lPredicates); foreach (Predicate p in lPredicates) { if (lKnowPredicates.Contains(p)) { KnowPredicate kp = new KnowPredicate(p); cf.AddOperand(new PredicateFormula(kp)); } } if (f is CompoundFormula && ((CompoundFormula)f).Operator == "and") { foreach (Formula fSub in ((CompoundFormula)f).Operands) cf.AddOperand(fSub); } else cf.AddOperand(f); return cf; }
public override Predicate ToTag() { KnowPredicate ppNew = new KnowPredicate(this); if (Negation) ppNew.Name = ppNew.Name + "-Remove"; else ppNew.Name = ppNew.Name + "-Add"; ppNew.Negation = false; return ppNew; }
/* public List<Action> KnowCompilationSplitConditions(Dictionary<string, List<Predicate>> dTags, List<string> lAlwaysKnown, List<Predicate> lAdditionalPredicates) { List<Action> lActions = new List<Action>(); ParametrizedAction aNewState = new ParametrizedAction(Name + "-State"); ParametrizedAction aNewKnowledgeGain = new ParametrizedAction(Name + "-KnowledgeGain"); ParametrizedAction aNewKnowledgeLoss = new ParametrizedAction(Name + "-KnowledgeLoss"); ParametrizedPredicate ppInFirst = new ParametrizedPredicate("P1-" + Name); ParametrizedPredicate ppInSecond = new ParametrizedPredicate("P2-" + Name); GroundedPredicate gpNotInAction = new GroundedPredicate("NotInAction"); if (this is ParametrizedAction) { foreach (Parameter p in ((ParametrizedAction)this).Parameters) { aNewKnowledgeLoss.AddParameter(p); aNewKnowledgeGain.AddParameter(p); aNewState.AddParameter(p); ppInFirst.AddParameter(p); ppInSecond.AddParameter(p); } } List<CompoundFormula> lConditions = new List<CompoundFormula>(); List<Formula> lObligatory = new List<Formula>(); SplitEffects(lConditions, lObligatory); CompoundFormula cfPreconditions = new CompoundFormula("and"); List<Predicate> lKnowPreconditions = new List<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); aNewKnowledgeGain.Preconditions = cfPreconditions;//knowledge gain is the first action, so it will have all the preconditions if (Effects == null) throw new NotImplementedException(); List<Predicate> lKnowEffects = new List<Predicate>(); CompoundFormula cfStateEffects = new CompoundFormula("and"); CompoundFormula cfKnowledgeLossEffects = new CompoundFormula("and"); CompoundFormula cfKnowledgeGainEffects = new CompoundFormula("and"); foreach (Formula f in lObligatory) { f.GetAllPredicates(lKnowEffects); cfStateEffects.AddOperand(f); } foreach (Predicate p in lKnowEffects) { if (!lAlwaysKnown.Contains(p.Name)) { Predicate pKEffect = new KnowPredicate(p); cfKnowledgeGainEffects.AddOperand(pKEffect); pKEffect = new KnowPredicate(p.Negate()); cfKnowledgeGainEffects.AddOperand(pKEffect.Negate()); foreach (string sTag in dTags.Keys) { pKEffect = p.GenerateKnowGiven(sTag); cfKnowledgeGainEffects.AddOperand(pKEffect); pKEffect = p.Negate().GenerateKnowGiven(sTag); cfKnowledgeGainEffects.AddOperand(pKEffect.Negate()); } } } if (lConditions.Count > 0) { lAdditionalPredicates.Add(ppInFirst); lAdditionalPredicates.Add(ppInSecond); aNewKnowledgeGain.Preconditions = cfPreconditions; aNewKnowledgeLoss.Preconditions = new PredicateFormula(ppInFirst); aNewState.Preconditions = new PredicateFormula(ppInSecond); cfKnowledgeGainEffects.AddOperand(ppInFirst); cfKnowledgeGainEffects.AddOperand(gpNotInAction.Negate()); cfKnowledgeLossEffects.AddOperand(ppInSecond); cfKnowledgeLossEffects.AddOperand(ppInFirst.Negate()); cfStateEffects.AddOperand(ppInSecond.Negate()); cfStateEffects.AddOperand(gpNotInAction); foreach (CompoundFormula cfCondition in lConditions) { cfStateEffects.AddOperand(cfCondition); CompoundFormula cfK = CreateKnowledgeGainCondition(cfCondition, lAlwaysKnown, false); if (cfK != null) cfKnowledgeGainEffects.AddOperand(cfK); cfK = CreateKnowledgeLossCondition(cfCondition, lAlwaysKnown); if (cfK != null) cfKnowledgeLossEffects.AddOperand(cfK); foreach (string sTag in dTags.Keys) { cfK = CreateTaggedKnowledgeGainCondition(cfCondition, sTag, lAlwaysKnown, false); if (cfK != null) cfKnowledgeGainEffects.AddOperand(cfK); cfK = CreateTaggedKnowledgeLossCondition(cfCondition, sTag, lAlwaysKnown); if (cfK != null) cfKnowledgeLossEffects.AddOperand(cfK); } } aNewKnowledgeGain.Effects = cfKnowledgeGainEffects.Simplify(); aNewKnowledgeLoss.Effects = cfKnowledgeLossEffects.Simplify(); lActions.Add(aNewKnowledgeLoss); lActions.Add(aNewKnowledgeGain); } else { aNewState.Preconditions = cfPreconditions; } aNewState.Effects = cfStateEffects.Simplify(); lActions.Add(aNewState); if (Observe != null) { throw new NotImplementedException(); } return lActions; } */ public Action KnowCompilation(Dictionary<string, List<Predicate>> dTags, Domain d) { Action aNew = Clone(); aNew.Name = Name + "-K"; 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); foreach (Predicate p in lKnowPreconditions) { cfPreconditions.AddOperand(new KnowPredicate(p)); } aNew.Preconditions = cfPreconditions; } else aNew.Preconditions = null; 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);//BGUBGU - probably a bug here. Need to separate always known and the rest. } foreach (Predicate p in lKnowEffects) { Predicate pKEffect = new KnowPredicate(p); cfEffects.AddOperand(pKEffect); Predicate pKNegateEffect = new KnowPredicate(p.Negate()).Negate(); cfEffects.AddOperand(pKNegateEffect); /* why do we need all this? pKEffect = new KnowPredicate(p.Negate()); cfEffects.AddOperand(pKEffect.Negate()); foreach (string sTag in dTags.Keys) { pKEffect = p.GenerateGiven(sTag); cfEffects.AddOperand(pKEffect); pKEffect = p.Negate().GenerateGiven(sTag); cfEffects.AddOperand(pKEffect.Negate()); } */ } foreach (string sTag in dTags.Keys) { //e|s CompoundFormula cfKEffects = new CompoundFormula("and"); foreach (Predicate p in lKnowEffects) { Predicate pAdd = p.GenerateGiven(sTag); cfKEffects.AddOperand(pAdd); } cfEffects.SimpleAddOperand(cfKEffects); } foreach (CompoundFormula cfCondition in lConditions) { //cfEffects.AddOperand(cfCondition);//no longer valid? Perhaps needed if there are some "always known" conditions? CompoundFormula cfK = CreateKnowledgeGainCondition(cfCondition, d.m_lAlwaysKnown, false); if (cfK != null) cfEffects.AddOperand(cfK); cfK = CreateKnowledgeLossCondition(cfCondition, d.m_lAlwaysKnown, false); if (cfK != null) { if(cfK.Operator == "and" || cfK.Operands[0] is PredicateFormula || cfK.Operands[0] is CompoundFormula && (((CompoundFormula)cfK.Operands[0]).Operands.Count > 0)) cfEffects.SimpleAddOperand(cfK); } //cfK = CreateKnowledgeGainCondition(cfCondition, d.m_lAlwaysKnown, true); //if (cfK != null) // cfEffects.AddOperand(cfK); cfK = CreateKnowledgeLossCondition(cfCondition, d.m_lAlwaysKnown, true); if (cfK != null) { if (cfK.Operator == "and" || cfK.Operands[0] is PredicateFormula || cfK.Operands[0] is CompoundFormula && (((CompoundFormula)cfK.Operands[0]).Operands.Count > 0)) cfEffects.SimpleAddOperand(cfK); } foreach (string sTag in dTags.Keys) { cfK = CreateTaggedCondition(cfCondition, d, sTag); cfEffects.AddOperand(cfK); cfK = CreateTaggedKnowledgeWhetherGainCondition(cfCondition, d, sTag); if (cfK != null) { cfEffects.SimpleAddOperand(cfK); } cfK = CreateTaggedKnowledgeWhetherLossCondition(cfCondition, d, sTag); if (cfK != null) cfEffects.SimpleAddOperand(cfK); } } aNew.Effects = cfEffects.Simplify(); } if (Observe != null) { throw new NotImplementedException(); } return aNew; }
private Action CreateReasoningAction(Predicate pEffect, HashSet<Predicate> lPredicates) { KnowPredicate kpEffect = new KnowPredicate(pEffect); if (Predicates.Contains(kpEffect)) return null; Action a = new KnowledgeAction("Reasoning_" + pEffect.ToString()); a.Preconditions = new CompoundFormula("and"); foreach (Predicate pOther in lPredicates) { if (pOther != pEffect) { KnowPredicate kp = new KnowPredicate(pOther); if (!Predicates.Contains(kp)) return null; ((CompoundFormula)a.Preconditions).AddOperand(new PredicateFormula(kp)); } } CompoundFormula cfEffects = new CompoundFormula("and"); cfEffects.AddOperand(new PredicateFormula(kpEffect)); a.SetEffects(cfEffects); return a; }