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); }
//for SDR public static Predicate GenerateKNot(Argument pTag) { ParametrizedPredicate pp = new ParametrizedPredicate("KNot"); pp.AddParameter(pTag); return(pp); }
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); }
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 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; }
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; }
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 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 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> 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; }