//for SDR public static Predicate GenerateKNot(Argument pTag) { ParametrizedPredicate pp = new ParametrizedPredicate("KNot"); pp.AddParameter(pTag); return(pp); }
public override Predicate GenerateKnowGiven(string sTag, bool bKnowWhether) { if (bKnowWhether) { throw new NotImplementedException("There should no longer be any Know Whether prediate"); } ParametrizedPredicate pKGiven = null; if (bKnowWhether) { pKGiven = new ParametrizedPredicate("KWGiven" + Name); } else { pKGiven = new ParametrizedPredicate("KGiven" + Name); } foreach (Argument a in Parameters) { pKGiven.AddParameter(a); } pKGiven.AddParameter(new Parameter(Domain.TAG, sTag)); if (!bKnowWhether) { if (Negation) { pKGiven.AddParameter(new Parameter(Domain.VALUE, Domain.FALSE_VALUE)); } else { pKGiven.AddParameter(new Parameter(Domain.VALUE, Domain.TRUE_VALUE)); } } return(pKGiven); }
private bool MatchParametersToPredicateDeclaration(ParametrizedPredicate pp, Domain d) { foreach (Predicate pDefinition in d.Predicates) { if (pDefinition.Name == pp.Name) { if (pDefinition is ParametrizedPredicate) { ParametrizedPredicate ppDefinition = (ParametrizedPredicate)pDefinition; if (pp.Parameters.Count() != ppDefinition.Parameters.Count()) { return(false); } for (int i = 0; i < pp.Parameters.Count(); i++) { if (ppDefinition.Parameters.ElementAt(i).Type == "") { ppDefinition.Parameters.ElementAt(i).Type = pp.Parameters.ElementAt(i).Type; } else if (ppDefinition.Parameters.ElementAt(i).Type != pp.Parameters.ElementAt(i).Type) { return(false); } } return(true); } } } return(false); }
public override bool Equals(object obj) { if (obj is ParametrizedPredicate) { ParametrizedPredicate pp = (ParametrizedPredicate)obj; if (pp.Name != Name) { return(false); } if (m_lParameters.Count != pp.m_lParameters.Count) { return(false); } for (int iParameter = 0; iParameter < m_lParameters.Count; iParameter++) { if (!m_lParameters[iParameter].Equals(pp.m_lParameters[iParameter])) { return(false); } } return(Negation == pp.Negation); } return(false); }
public Dictionary <string, Constant> Bind(ParametrizedPredicate p) { if (Name != p.Name) { return(null); } if (((List <Constant>)Constants).Count != ((List <Argument>)p.Parameters).Count) { return(null); } Dictionary <string, Constant> dBindings = new Dictionary <string, Constant>(); for (int i = 0; i < Constants.Count; i++) { Argument arg = p.Parameters.ElementAt(i); if (arg is Constant) { if (!Constants[i].Equals(arg)) { return(null); } } if (arg is Parameter) { dBindings[arg.Name] = Constants[i]; } } return(dBindings); }
public override Predicate Negate() { ParametrizedPredicate pNegate = new ParametrizedPredicate(Name); pNegate.Negation = !Negation; pNegate.m_lParameters = new List <Argument>(m_lParameters); return(pNegate); }
public override Predicate GenerateGiven(string sTag) { ParametrizedPredicate pGiven = new ParametrizedPredicate("Given" + Name); foreach (Argument a in Parameters) { pGiven.AddParameter(a); } pGiven.AddParameter(new Parameter(Domain.TAG, sTag)); if (Negation) return pGiven.Negate(); return pGiven; }
public Predicate ReadFunctionExpression(CompoundExpression exp, Dictionary <string, string> dParameterNameToType, Domain d) { Constant c = null; string sName = exp.SubExpressions[0].ToString(); if (exp.Type == "=") { string sParam1 = exp.SubExpressions[0].ToString(); string sParam2 = exp.SubExpressions[1].ToString(); if (!dParameterNameToType.ContainsKey(sParam1)) { throw new ArgumentException("First argument of = must be a parameter"); } ParametrizedPredicate pp = new ParametrizedPredicate("="); pp.AddParameter(new Parameter(dParameterNameToType[sParam1], sParam1)); if (dParameterNameToType.ContainsKey(sParam2)) { pp.AddParameter(new Parameter(dParameterNameToType[sParam2], sParam2)); } else { pp.AddParameter(new Constant(d.ConstantNameToType[sParam2], sParam2)); } return(pp); } GroundedPredicate p = new GroundedPredicate(exp.Type); double dValue = 0.0; if (d.Functions.Contains(sName)) { c = new Constant("Function", sName); } else { throw new ArgumentException("First argument of increase or decrease must be a function"); } p.AddConstant(c); sName = exp.SubExpressions[1].ToString(); if (double.TryParse(sName, out dValue)) { c = new Constant("Number", sName); } else { throw new ArgumentException("Second argument of increase or decrease must be a number"); } p.AddConstant(c); return(p); }
public Predicate PartiallyGround(Dictionary <string, Constant> dBindings) { GroundedPredicate gpred = new GroundedPredicate(Name); ParametrizedPredicate ppred = new ParametrizedPredicate(Name); gpred.Negation = Negation; ppred.Negation = Negation; bool bAllGrounded = true; foreach (Argument a in Parameters) { if (a is Parameter) { if (!dBindings.ContainsKey(a.Name)) { ppred.AddParameter(a); bAllGrounded = false; } else { ppred.AddParameter(dBindings[a.Name]); gpred.AddConstant(dBindings[a.Name]); } } else { gpred.AddConstant((Constant)a); ppred.AddParameter(a); } } if (bAllGrounded) { if (gpred.Name == "=") { bool bSame = gpred.Constants[0].Equals(gpred.Constants[1]); if (bSame && !Negation || !bSame && Negation) { return(new GroundedPredicate(Domain.TRUE_PREDICATE)); } else { return(new GroundedPredicate(Domain.FALSE_PREDICATE)); } } return(gpred); } else { return(ppred); } }
public override Predicate GenerateGiven(string sTag) { ParametrizedPredicate pGiven = new ParametrizedPredicate("Given" + Name); foreach (Argument a in Parameters) { pGiven.AddParameter(a); } pGiven.AddParameter(new Parameter(Domain.TAG, sTag)); if (Negation) { return(pGiven.Negate()); } return(pGiven); }
public override Predicate ToTag() { ParametrizedPredicate ppNew = new ParametrizedPredicate(this); if (Negation) { ppNew.Name = ppNew.Name + "-Remove"; } else { ppNew.Name = ppNew.Name + "-Add"; } ppNew.Negation = false; return(ppNew); }
public override Formula PartiallyGround(Dictionary <string, Constant> dBindings) { if (Predicate is ParametrizedPredicate) { ParametrizedPredicate ppred = (ParametrizedPredicate)Predicate; Predicate pGrounded = ppred.PartiallyGround(dBindings); return(new PredicateFormula(pGrounded)); } if (Predicate is KnowPredicate) { throw new NotImplementedException(); } if (Predicate is KnowGivenPredicate) { throw new NotImplementedException(); } return(this); }
public Dictionary<string, Constant> Bind(ParametrizedPredicate p) { if (Name != p.Name) return null; if (((List<Constant>)Constants).Count != ((List<Argument>)p.Parameters).Count) return null; Dictionary<string, Constant> dBindings = new Dictionary<string, Constant>(); for (int i = 0; i < Constants.Count; i++) { Argument arg = p.Parameters.ElementAt(i); if (arg is Constant) { if (!Constants[i].Equals(arg)) return null; } if (arg is Parameter) dBindings[arg.Name] = Constants[i]; } return dBindings; }
private Predicate ReadPredicate(CompoundExpression exp, Domain d) { ParametrizedPredicate pp = new ParametrizedPredicate(exp.Type); int iExpression = 0; Parameter p = null; string sName = ""; List <Parameter> lUntypedParameters = new List <Parameter>(); for (iExpression = 0; iExpression < exp.SubExpressions.Count; iExpression++) { sName = exp.SubExpressions[iExpression].ToString(); if (sName == "-") { string sType = exp.SubExpressions[iExpression + 1].ToString(); foreach (Parameter pUntyped in lUntypedParameters) { pUntyped.Type = sType; } lUntypedParameters.Clear(); iExpression++;//skip the "-" and the type } else { p = new Parameter("", sName); lUntypedParameters.Add(p); pp.AddParameter(p); } } if (d.Types.Count == 1) { foreach (Parameter pUntyped in lUntypedParameters) { pUntyped.Type = d.Types[0]; } } return(pp); }
private Formula ReadPredicate(CompoundExpression exp, Dictionary<string, string> dParameterNameToType, bool bParametrized, Domain d) { Predicate p = null; int iExpression = 0; string sName = ""; if (bParametrized) p = new ParametrizedPredicate(exp.Type); else p = new GroundedPredicate(exp.Type); bool bAllConstants = true; for (iExpression = 0; iExpression < exp.SubExpressions.Count; iExpression++) { sName = exp.SubExpressions[iExpression].ToString(); if (bParametrized) { Argument a = null; if (sName.StartsWith("?")) { a = new Parameter(dParameterNameToType[sName], sName); bAllConstants = false; } else { a = new Constant(d.ConstantNameToType[sName], sName); } ((ParametrizedPredicate)p).AddParameter(a); } else { try { Constant c = new Constant(d.ConstantNameToType[sName], sName); ((GroundedPredicate)p).AddConstant(c); } catch (Exception e) { Console.WriteLine(); } } } if (bParametrized) if (!MatchParametersToPredicateDeclaration((ParametrizedPredicate)p, d)) throw new NotImplementedException(); if (bParametrized && bAllConstants) { GroundedPredicate gp = new GroundedPredicate(p.Name); foreach (Constant c in ((ParametrizedPredicate)p).Parameters) gp.AddConstant(c); p = gp; } PredicateFormula vf = new PredicateFormula(p); return vf; }
public ParametrizedPredicate(ParametrizedPredicate pp) : base(pp.Name) { m_lParameters = new List <Argument>(pp.m_lParameters); Parameterized = pp.Parameterized; }
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; }
private Formula ReadPredicate(CompoundExpression exp, Dictionary <string, string> dParameterNameToType, bool bParametrized, Domain d) { Predicate p = null; int iExpression = 0; string sName = ""; if (bParametrized) { p = new ParametrizedPredicate(exp.Type); } else { p = new GroundedPredicate(exp.Type); } bool bAllConstants = true; for (iExpression = 0; iExpression < exp.SubExpressions.Count; iExpression++) { sName = exp.SubExpressions[iExpression].ToString(); if (bParametrized) { Argument a = null; if (sName.StartsWith("?")) { a = new Parameter(dParameterNameToType[sName], sName); bAllConstants = false; } else { a = new Constant(d.ConstantNameToType[sName], sName); } ((ParametrizedPredicate)p).AddParameter(a); } else { try { Constant c = new Constant(d.ConstantNameToType[sName], sName); ((GroundedPredicate)p).AddConstant(c); } catch (Exception e) { Console.WriteLine(); } } } if (bParametrized) { if (!MatchParametersToPredicateDeclaration((ParametrizedPredicate)p, d)) { throw new NotImplementedException(); } } if (bParametrized && bAllConstants) { GroundedPredicate gp = new GroundedPredicate(p.Name); foreach (Constant c in ((ParametrizedPredicate)p).Parameters) { gp.AddConstant(c); } p = gp; } PredicateFormula vf = new PredicateFormula(p); return(vf); }
public List<Action> KnowWhetherTagCompilationSplitConditions(Dictionary<string, List<Predicate>> dTags, Domain d, List<string> lIncludedTags, List<string> lExcludedTags, List<Predicate> lAdditionalPredicates) { string sName = Name + "-KW"; foreach (string sTag in lIncludedTags) sName += "-" + sTag; ParametrizedAction aNewState = new ParametrizedAction(sName + "-State"); ParametrizedAction aNewKnowledgeGain = new ParametrizedAction(sName + "-KnowledgeGain"); ParametrizedAction aNewKnowledgeLoss = new ParametrizedAction(sName + "-KnowledgeLoss"); ParametrizedPredicate ppInFirst = new ParametrizedPredicate("P1-" + sName); ParametrizedPredicate ppInSecond = new ParametrizedPredicate("P2-" + sName); 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"); Formula cfKWPreconditions = GetKnowWhetherPreconditions(dTags, d, lIncludedTags, lExcludedTags); cfPreconditions.AddOperand(cfKWPreconditions); //knowledge loss is the first action, so it will have all the preconditions cfPreconditions.AddOperand(gpNotInAction); if (Effects == null) throw new NotImplementedException(); HashSet<Predicate> lKnowEffects = new HashSet<Predicate>(); CompoundFormula cfStateEffects = new CompoundFormula("and"); CompoundFormula cfKnowledgeLossEffects = new CompoundFormula("and"); CompoundFormula cfKnowledgeGainEffects = new CompoundFormula("and"); //CompoundFormula cfMandatoryEffects = new CompoundFormula("and"); foreach (Formula f in lObligatory) { f.GetAllPredicates(lKnowEffects); } if (lKnowEffects.Count > 0) { foreach (string sTag in lIncludedTags) { //~KNot t|?t -> effects|t CompoundFormula cfKEffects = new CompoundFormula("and"); foreach (Predicate p in lKnowEffects) { Predicate pAdd = p.GenerateGiven(sTag); cfKEffects.AddOperand(pAdd); if (!d.AlwaysKnown(p)) { pAdd = p.GenerateKnowGiven(sTag, true); cfKEffects.AddOperand(pAdd); } } cfStateEffects.SimpleAddOperand(cfKEffects); } } List<Action> lActions = new List<Action>(); if (lConditions.Count > 0) { lAdditionalPredicates.Add(ppInFirst); lAdditionalPredicates.Add(ppInSecond); aNewKnowledgeLoss.Preconditions = cfPreconditions; aNewKnowledgeGain.Preconditions = new PredicateFormula(ppInFirst); aNewState.Preconditions = new PredicateFormula(ppInSecond); cfKnowledgeLossEffects.AddOperand(ppInFirst); cfKnowledgeLossEffects.AddOperand(gpNotInAction.Negate()); cfKnowledgeGainEffects.AddOperand(ppInSecond); cfKnowledgeGainEffects.AddOperand(ppInFirst.Negate()); cfStateEffects.AddOperand(ppInSecond.Negate()); cfStateEffects.AddOperand(gpNotInAction); foreach (CompoundFormula cfCondition in lConditions) { CompoundFormula cfK = null, cfAnd = null; HashSet<Predicate> lConditionEffects = cfCondition.Operands[1].GetAllPredicates(); cfAnd = new CompoundFormula("and"); foreach (string sTag in lIncludedTags) { cfK = CreateTaggedCondition(cfCondition, d, sTag); if (cfK != null) { cfStateEffects.SimpleAddOperand(cfK); } } cfK = CreateTaggedKnowledgeWhetherGainConditions(cfCondition, d, lIncludedTags); if (cfK != null) { cfKnowledgeGainEffects.SimpleAddOperand(cfK); } cfK = CreateTaggedKnowledgeWhetherLossCondition(cfCondition, d, lIncludedTags); if (cfK != null && cfK.Operands.Count > 0) { cfKnowledgeLossEffects.SimpleAddOperand(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 List<Action> SplitConditions(List<Predicate> lAdditionalPredicates) { List<Action> lActions = new List<Action>(); ParametrizedAction aNewAdd = new ParametrizedAction(Name + "-Add"); ParametrizedAction aNewRemove = new ParametrizedAction(Name + "-Remove"); ParametrizedAction aNewTranslateRemove = new ParametrizedAction(Name + "-TranslateRemove"); ParametrizedAction aNewTranslateAdd = new ParametrizedAction(Name + "-TranslateAdd"); 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"); cfPreconditions.AddOperand(Preconditions); 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 } 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); } 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 throw new NotImplementedException(); if (Observe != null) { throw new NotImplementedException(); } return lActions; }
public override Predicate GenerateKnowGiven(string sTag, bool bKnowWhether) { if (bKnowWhether) throw new NotImplementedException("There should no longer be any Know Whether prediate"); ParametrizedPredicate pKGiven = null; if (bKnowWhether) pKGiven = new ParametrizedPredicate("KWGiven" + Name); else pKGiven = new ParametrizedPredicate("KGiven" + Name); foreach (Argument a in Parameters) { pKGiven.AddParameter(a); } pKGiven.AddParameter(new Parameter(Domain.TAG, sTag)); if (!bKnowWhether) { if (Negation) pKGiven.AddParameter(new Parameter(Domain.VALUE, Domain.FALSE_VALUE)); else pKGiven.AddParameter(new Parameter(Domain.VALUE, Domain.TRUE_VALUE)); } return pKGiven; }
public override Predicate ToTag() { ParametrizedPredicate ppNew = new ParametrizedPredicate(this); if (Negation) ppNew.Name = ppNew.Name + "-Remove"; else ppNew.Name = ppNew.Name + "-Add"; ppNew.Negation = false; return ppNew; }
public ParametrizedPredicate(ParametrizedPredicate pp) : base(pp.Name) { m_lParameters = new List<Argument>(pp.m_lParameters); Parameterized = pp.Parameterized; }
public Predicate PartiallyGround(Dictionary<string, Constant> dBindings) { GroundedPredicate gpred = new GroundedPredicate(Name); ParametrizedPredicate ppred = new ParametrizedPredicate(Name); gpred.Negation = Negation; ppred.Negation = Negation; bool bAllGrounded = true; foreach (Argument a in Parameters) { if (a is Parameter) { if (!dBindings.ContainsKey(a.Name)) { ppred.AddParameter(a); bAllGrounded = false; } else { ppred.AddParameter(dBindings[a.Name]); gpred.AddConstant(dBindings[a.Name]); } } else { gpred.AddConstant((Constant)a); ppred.AddParameter(a); } } if (bAllGrounded) { if (gpred.Name == "=") { bool bSame = gpred.Constants[0].Equals(gpred.Constants[1]); if (bSame && !Negation || !bSame && Negation) return new GroundedPredicate(Domain.TRUE_PREDICATE); else return new GroundedPredicate(Domain.FALSE_PREDICATE); } return gpred; } else return ppred; }
public override Predicate Negate() { ParametrizedPredicate pNegate = new ParametrizedPredicate(Name); pNegate.Negation = !Negation; pNegate.m_lParameters = new List<Argument>(m_lParameters); return pNegate; }
public Predicate ReadFunctionExpression(CompoundExpression exp, Dictionary<string, string> dParameterNameToType, Domain d) { Constant c = null; string sName = exp.SubExpressions[0].ToString(); if (exp.Type == "=") { string sParam1 = exp.SubExpressions[0].ToString(); string sParam2 = exp.SubExpressions[1].ToString(); if (!dParameterNameToType.ContainsKey(sParam1)) throw new ArgumentException("First argument of = must be a parameter"); ParametrizedPredicate pp = new ParametrizedPredicate("="); pp.AddParameter(new Parameter(dParameterNameToType[sParam1], sParam1)); if (dParameterNameToType.ContainsKey(sParam2)) pp.AddParameter(new Parameter(dParameterNameToType[sParam2], sParam2)); else pp.AddParameter(new Constant(d.ConstantNameToType[sParam2], sParam2)); return pp; } GroundedPredicate p = new GroundedPredicate(exp.Type); double dValue = 0.0; if (d.Functions.Contains(sName)) c = new Constant("Function", sName); else throw new ArgumentException("First argument of increase or decrease must be a function"); p.AddConstant(c); sName = exp.SubExpressions[1].ToString(); if (double.TryParse(sName, out dValue)) c = new Constant("Number", sName); else throw new ArgumentException("Second argument of increase or decrease must be a number"); p.AddConstant(c); return p; }
private Predicate ReadPredicate(CompoundExpression exp, Domain d) { ParametrizedPredicate pp = new ParametrizedPredicate(exp.Type); int iExpression = 0; Parameter p = null; string sName = ""; List<Parameter> lUntypedParameters = new List<Parameter>(); for (iExpression = 0; iExpression < exp.SubExpressions.Count; iExpression++) { sName = exp.SubExpressions[iExpression].ToString(); if (sName == "-") { string sType = exp.SubExpressions[iExpression + 1].ToString(); foreach (Parameter pUntyped in lUntypedParameters) pUntyped.Type = sType; lUntypedParameters.Clear(); iExpression++;//skip the "-" and the type } else { p = new Parameter("", sName); lUntypedParameters.Add(p); pp.AddParameter(p); } } if (d.Types.Count == 1) { foreach (Parameter pUntyped in lUntypedParameters) pUntyped.Type = d.Types[0]; } return pp; }
private bool MatchParametersToPredicateDeclaration(ParametrizedPredicate pp, Domain d) { foreach (Predicate pDefinition in d.Predicates) { if (pDefinition.Name == pp.Name) { if (pDefinition is ParametrizedPredicate) { ParametrizedPredicate ppDefinition = (ParametrizedPredicate)pDefinition; if (pp.Parameters.Count() != ppDefinition.Parameters.Count()) return false; for (int i = 0; i < pp.Parameters.Count(); i++) { if (ppDefinition.Parameters.ElementAt(i).Type == "") ppDefinition.Parameters.ElementAt(i).Type = pp.Parameters.ElementAt(i).Type; else if (ppDefinition.Parameters.ElementAt(i).Type != pp.Parameters.ElementAt(i).Type) return false; } return true; } } } return false; }