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 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); }
private int WriteReasoningAxioms(StreamWriter sw, CompoundFormula cfHidden, int cActions) { HashSet <Predicate> lPredicates = new HashSet <Predicate>(); cfHidden.GetAllPredicates(lPredicates); Dictionary <HashSet <Predicate>, HashSet <Predicate> > dActions = new Dictionary <HashSet <Predicate>, HashSet <Predicate> >(); if (cfHidden.IsSimpleFormula()) { SimpleAddReasoningActions(cfHidden, lPredicates, dActions, false); } else { throw new NotImplementedException(); } foreach (KeyValuePair <HashSet <Predicate>, HashSet <Predicate> > p in dActions) { if (p.Value.Count == 1) { KnowPredicate kp = (KnowPredicate)p.Value.First(); GroundedPredicate pOrg = (GroundedPredicate)kp.Knowledge; //GroundedPredicate gpNotK = new GroundedPredicate(pOrg); //gpNotK.Name = "NotK" + gpNotK.Name; //p.Key.Add(gpNotK); } WriteResoningAxiom(sw, p.Key, p.Value, cActions); cActions++; } return(cActions); }
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; }
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 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 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 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 KnowPredicate(KnowPredicate kp) : base(kp.Name) { Knowledge = kp.Knowledge; Value = kp.Value; Parametrized = kp.Parametrized; }
public MemoryStream WriteKnowledgeProblem(HashSet <Predicate> lObserved, HashSet <Predicate> lAllValues) { MemoryStream msProblem = new MemoryStream(); StreamWriter sw = new StreamWriter(msProblem); sw.WriteLine("(define (problem K" + Name + ")"); sw.WriteLine("(:domain K" + Domain.Name + ")"); sw.WriteLine(";;" + SDRPlanner.Translation); sw.WriteLine("(:init"); //ff doesn't like the and (and"); foreach (GroundedPredicate gp in lObserved) { if (gp.Name == "Choice") { continue; } sw.WriteLine(gp); if (!Domain.AlwaysKnown(gp)) { Predicate kp = new KnowPredicate(gp); sw.WriteLine(kp); } } HashSet <Predicate> lHidden = new HashSet <Predicate>(lAllValues.Except(lObserved)); foreach (GroundedPredicate gp in lHidden) { sw.WriteLine(gp); } sw.WriteLine(")"); HashSet <Predicate> lGoalPredicates = Goal.GetAllPredicates(); CompoundFormula cfGoal = new CompoundFormula("and"); foreach (Predicate p in lGoalPredicates) { if (Domain.AlwaysKnown(p)) { cfGoal.AddOperand(p); } else { cfGoal.AddOperand(new KnowPredicate(p)); } } CompoundFormula cfAnd = new CompoundFormula(cfGoal); sw.WriteLine("(:goal " + cfAnd.Simplify() + ")"); //sw.WriteLine("))"); sw.WriteLine(")"); sw.Flush(); return(msProblem); }
public MemoryStream WriteKnowledgeProblem(HashSet <Predicate> lObserved, HashSet <Predicate> lHidden, int cMinMishaps, int cMishaps) { MemoryStream msProblem = new MemoryStream(); StreamWriter sw = new StreamWriter(msProblem); sw.WriteLine("(define (problem K" + Name + ")"); sw.WriteLine("(:domain K" + Domain.Name + ")"); sw.WriteLine(";;" + SDRPlanner.Translation); sw.WriteLine("(:init"); //ff doesn't like the and (and"); string sKP = "", sP = ""; foreach (GroundedPredicate gp in lObserved) { if (gp.Name == "Choice") { continue; } if (Domain.AlwaysKnown(gp)) { sw.WriteLine(gp); } if (!Domain.AlwaysKnown(gp)) { Predicate kp = new KnowPredicate(gp); sw.WriteLine(kp); } } foreach (GroundedPredicate gp in lHidden) { //GroundedPredicate kp = new GroundedPredicate(gp); //kp.Name = "NotK" + kp.Name; //sw.WriteLine(kp); } if (cMinMishaps > cMishaps) { sw.WriteLine("(MishapCount m" + cMishaps + ")"); } sw.WriteLine(")"); HashSet <Predicate> lGoalPredicates = Goal.GetAllPredicates(); CompoundFormula cfGoal = new CompoundFormula("and"); foreach (Predicate p in lGoalPredicates) { if (Domain.AlwaysKnown(p)) { cfGoal.AddOperand(p); } else { cfGoal.AddOperand(new KnowPredicate(p)); } } CompoundFormula cfAnd = new CompoundFormula(cfGoal); if (cMinMishaps > cMishaps && SDRPlanner.Translation != SDRPlanner.Translations.Conformant) { GroundedPredicate gp = new GroundedPredicate("MishapCount"); gp.AddConstant(new Constant("mishaps", "m" + cMinMishaps)); cfAnd.AddOperand(gp); } sw.WriteLine("(:goal " + cfAnd.Simplify() + ")"); //sw.WriteLine("))"); sw.WriteLine(")"); sw.Flush(); return(msProblem); }