public bool IsUnNone(int iX, int iY) { if (Belief.Observed == null) { return(false); } //Predicate p=new Predicate("cv"); foreach (Formula f in Belief.Problem.Hidden) { GroundedPredicate gp = new GroundedPredicate("opened"); gp.AddConstant(new Constant("pos", "p" + iX + "-" + iY)); //if(f.Contains if (f.Contains(gp)) { return(true); } //{ // string sPos = gp.Constants[0].Name; // sPos = sPos.Replace("p", ""); // string[] asPos = sPos.Split('-'); // if ((iX == int.Parse(asPos[0])) && (iY == int.Parse(asPos[1]))) return true; //} } return(false); }
private State ApplyCompute(State s, string sName, List <Action> lActions, Domain d) { State sCurrent = s; Predicate pNew = new GroundedPredicate("new-" + sName); Predicate pDone = new GroundedPredicate("done-" + sName); int i = 0; while (!sCurrent.Contains(pNew.Negate()) || !sCurrent.Contains(pDone) || i < 10) { Action a1 = d.GroundActionByName(new string[] { "pre-" + sName, "" }, sCurrent.Predicates, false); Action a2 = d.GroundActionByName(new string[] { "compute-" + sName, "" }, sCurrent.Predicates, false); if (a1 != null && a2 != null) { sCurrent = sCurrent.Apply(a1); sCurrent = sCurrent.Apply(a2); lActions.Add(a1); lActions.Add(a2); } i++; } Action a = d.GroundActionByName(new string[] { "observe-new-" + sName + "-F", "" }, sCurrent.Predicates, false); sCurrent = sCurrent.Apply(a); lActions.Add(a); a = d.GroundActionByName(new string[] { "post-" + sName, "" }, sCurrent.Predicates, false); sCurrent = sCurrent.Apply(a); lActions.Add(a); return(sCurrent); }
public GroundedPredicate Ground(Dictionary <string, Constant> dBindings) { GroundedPredicate gp = new GroundedPredicate("K" + Knowledge.Name); if (Knowledge is ParametrizedPredicate) { foreach (Argument a in ((ParametrizedPredicate)Knowledge).Parameters) { if (a is Parameter) { if (dBindings.ContainsKey(a.Name)) { gp.AddConstant(dBindings[a.Name]); } else { throw new NotImplementedException(); } } else { gp.AddConstant((Constant)a); } } } else { foreach (Constant c in ((GroundedPredicate)Knowledge).Constants) { gp.AddConstant(c); } } return(gp); }
private State ApplyAxiom(State s, List <Action> lActions, Domain d) { State sCurrent = s; Predicate pNew = new GroundedPredicate("new"); Predicate pDone = new GroundedPredicate("done"); while (!sCurrent.Contains(pNew.Negate()) || !sCurrent.Contains(pDone)) { Action a1 = d.GroundActionByName(new string[] { "pre-axiom", "" }, sCurrent.Predicates, false); Action a2 = d.GroundActionByName(new string[] { "axiom", "" }, sCurrent.Predicates, false); if (a1 != null && a2 != null) { sCurrent = sCurrent.Apply(a1); sCurrent = sCurrent.Apply(a2); lActions.Add(a1); lActions.Add(a2); } } Action a = d.GroundActionByName(new string[] { "observe-new-F", "" }, sCurrent.Predicates, false); sCurrent = sCurrent.Apply(a); lActions.Add(a); a = d.GroundActionByName(new string[] { "fixpoint", "" }, sCurrent.Predicates, false); sCurrent = sCurrent.Apply(a); lActions.Add(a); return(sCurrent); }
public GroundedPredicate Ground(Dictionary<string, Constant> dBindings) { GroundedPredicate gp = new GroundedPredicate("K" + Knowledge.Name); if (Knowledge is ParametrizedPredicate) { foreach (Argument a in ((ParametrizedPredicate)Knowledge).Parameters) { if (a is Parameter) { if (dBindings.ContainsKey(a.Name)) gp.AddConstant(dBindings[a.Name]); else throw new NotImplementedException(); } else gp.AddConstant((Constant)a); } } else { foreach (Constant c in ((GroundedPredicate)Knowledge).Constants) { gp.AddConstant(c); } } return gp; }
public override Predicate GenerateKnowGiven(string sTag, bool bKnowWhether) { GroundedPredicate pKGiven = null; if (bKnowWhether) { pKGiven = new GroundedPredicate("KWGiven" + Name); } else { pKGiven = new GroundedPredicate("KGiven" + Name); } foreach (Constant c in Constants) { pKGiven.AddConstant(c); } pKGiven.AddConstant(new Constant(Domain.TAG, sTag)); if (!bKnowWhether) { if (Negation) { pKGiven.AddConstant(new Constant(Domain.VALUE, Domain.FALSE_VALUE)); } else { pKGiven.AddConstant(new Constant(Domain.VALUE, Domain.TRUE_VALUE)); } } return(pKGiven); }
public override int Similarity(Predicate p) { if (Negation != p.Negation) { //if (Name != p.Name || Negation != p.Negation) return(0); } if (p is GroundedPredicate) { GroundedPredicate gpGrounded = (GroundedPredicate)p; int iSimilarity = 0; if (Name == p.Name) { for (int i = 0; i < Constants.Count; i++) { if (Constants[i].Equals(gpGrounded.Constants[i])) { iSimilarity++; } } } else { foreach (Constant c in Constants) { if (gpGrounded.Constants.Contains(c)) { iSimilarity++; } } } return(iSimilarity); } return(0); }
public override bool ConsistentWith(Predicate p) { if (Name != p.Name) { return(true); //irrelvant predicate - no contradiction } if (p is ParametrizedPredicate) { //TODO throw new NotImplementedException(); } GroundedPredicate gp = (GroundedPredicate)p; if (((List <Constant>)Constants).Count != ((List <Constant>)gp.Constants).Count) { return(true); } for (int i = 0; i < Constants.Count; i++) { if (!gp.Constants[i].Equals(Constants[i])) { return(true);//irrelvant predicate - no contradiction } } return(Negation == p.Negation); }
private static GroundedPredicate GetPredicate(string sName, int iX, int iY) { GroundedPredicate gpSafe = new GroundedPredicate(sName); gpSafe.AddConstant(new Constant("pos", "p" + iX + "-" + iY)); return(gpSafe); }
public void AddHidden(CompoundFormula cf) { Domain.AddHidden(cf); HashSet <Predicate> hs = cf.GetAllPredicates(); foreach (GroundedPredicate gp in hs) { m_lInitiallyUnknown.Add(gp.Canonical()); GroundedPredicate gpCanonical = (GroundedPredicate)gp.Canonical(); if (!m_dRelevantPredicates.ContainsKey(gpCanonical)) { m_dRelevantPredicates[gpCanonical] = new HashSet <GroundedPredicate>(); } foreach (GroundedPredicate gpOther in hs) { GroundedPredicate gpOtherCanonical = (GroundedPredicate)gpOther.Canonical(); if (gpOtherCanonical != gpCanonical) { m_dRelevantPredicates[gpCanonical].Add(gpOtherCanonical); } } } m_lHidden.Add(cf); }
private static GroundedPredicate GetSafe(int iX, int iY) { GroundedPredicate gpSafe = new GroundedPredicate("safe"); gpSafe.AddConstant(new Constant("pos", "p-" + iX)); gpSafe.AddConstant(new Constant("pos", "p-" + iY)); return(gpSafe); }
public GroundedPredicate(string sName) : base(sName) { //if (sName == Domain.FALSE_PREDICATE) // Debug.WriteLine("Initialized a false predicate"); m_gpNegation = null; Constants = new List <Constant>(); }
public bool IsRelevantFor(GroundedPredicate gp, GroundedPredicate gpRelevant) { if (!m_dRelevantPredicates.ContainsKey((GroundedPredicate)gp.Canonical())) { return(false); } return(m_dRelevantPredicates[gp].Contains((GroundedPredicate)gpRelevant.Canonical())); }
public HashSet <GroundedPredicate> GetRelevantPredicates(GroundedPredicate gp) { if (m_dRelevantPredicates.ContainsKey(gp)) { return(m_dRelevantPredicates[gp]); } return(new HashSet <GroundedPredicate>()); }
public GroundedPredicate(string sName) : base(sName) { //if (sName == Domain.FALSE_PREDICATE) // Debug.WriteLine("Initialized a false predicate"); m_gpNegation = null; Constants = new List<Constant>(); }
public RegressedPredicate(GroundedPredicate pCurrent, Predicate pNext, int iChoice) : base(pCurrent) { Choice = iChoice; if (pNext is RegressedPredicate) Next = (RegressedPredicate)pNext; else Next = null; }
private string GetNameAndTag(GroundedPredicate p) { string sName = p.Name; if (sName.StartsWith("Given")) { sName = sName + "." + p.Constants.Last().Name; } return(sName); }
public override Predicate Negate() { if (m_gpNegation == null) { m_gpNegation = new GroundedPredicate(this); m_gpNegation.Negation = !Negation; m_gpNegation.m_gpNegation = this; } return(m_gpNegation); }
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 RegressedPredicate(GroundedPredicate pCurrent, Predicate pNext, int iChoice) : base(pCurrent) { Choice = iChoice; if (pNext is RegressedPredicate) { Next = (RegressedPredicate)pNext; } else { Next = null; } }
public bool IsLocationBlocked(int iX, int iY) { GroundedPredicate gp = new GroundedPredicate("at"); gp.AddConstant(new Constant("pos", "p" + iX + "-" + iY)); Action aChecking = Domain.GetAction("checking"); if (aChecking.Effects.Contains(gp)) { return(false); } return(true); }
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 Formula Reduce(IEnumerable <Predicate> lKnown) { Predicate pReduced = Predicate; if (lKnown.Contains(Predicate)) { pReduced = new GroundedPredicate(Domain.TRUE_PREDICATE); } if (lKnown.Contains(Predicate.Negate())) { pReduced = new GroundedPredicate(Domain.FALSE_PREDICATE); } return(new PredicateFormula(pReduced)); }
public static List <string> BattleshipHeuristicII(PartiallySpecifiedState pssCurrent, Domain d) { List <string> lActions = new List <string>(); if (lPlaces == null) { lPlaces = new List <int>(); for (int iX = 0; iX < Size; iX++) { for (int iY = 0; iY < Size; iY++) { lPlaces.Add(iX * 1000 + iY); } } for (int i = 0; i < lPlaces.Count; i++) { int iRandom = RandomGenerator.Next(lPlaces.Count); int iAux = lPlaces[iRandom]; lPlaces[iRandom] = lPlaces[i]; lPlaces[i] = iAux; } iCurrent = 0; } bool bUnknown = false; int iSkipped = 0; while (!bUnknown) { int iChosen = lPlaces[iCurrent]; iCurrent++; int iChosenX = iChosen / 1000; int iChosenY = iChosen % 1000; GroundedPredicate gp = GetPredicate("water-at", iChosenX, iChosenY); if (!pssCurrent.Observed.Contains(gp)) { bUnknown = true; lActions.Add("shoot p-" + iChosenX + " p-" + iChosenY); } else { iSkipped++; } } return(lActions); }
private void AddEffects(Formula fEffects) { if (fEffects is PredicateFormula) { AddEffect(((PredicateFormula)fEffects).Predicate); } else { CompoundFormula cf = (CompoundFormula)fEffects; if (cf.Operator == "oneof" || cf.Operator == "or")//BUGBUG - should treat or differently { int iRandomIdx = RandomGenerator.Next(cf.Operands.Count); AddEffects(cf.Operands[iRandomIdx]); GroundedPredicate pChoice = new GroundedPredicate("Choice"); pChoice.AddConstant(new Constant("ActionIndex", "a" + (Time - 1)));//time - 1 because this is the action that generated the state, hence its index is i-1 pChoice.AddConstant(new Constant("ChoiceIndex", "c" + iRandomIdx)); State s = this; while (s != null) { s.m_lPredicates.Add(pChoice); s = s.m_sPredecessor; } } else if (cf.Operator == "and") { foreach (Formula f in cf.Operands) { if (f is PredicateFormula) { AddEffect(((PredicateFormula)f).Predicate); } else { AddEffects(f); } } } else if (cf.Operator == "when") { if (m_sPredecessor.Contains(cf.Operands[0])) { AddEffects(cf.Operands[1]); } } else { throw new NotImplementedException(); } } }
public bool IsPossibleLocation(int iX, int iY) { GroundedPredicate gp = new GroundedPredicate("at"); gp.AddConstant(new Constant("pos", "p" + iX + "-" + iY)); foreach (CompoundFormula cf in Belief.Hidden) { if (cf.Contains(gp)) { return(true); } } return(false); }
public override bool IsTrue(IEnumerable <Predicate> lKnown, bool bContainsNegations) { if (Predicate.Name == Domain.TRUE_PREDICATE) { return(true); } if (Predicate.Name == Domain.FALSE_PREDICATE) { return(false); } if (Predicate.Name == "=" && Predicate is GroundedPredicate) { GroundedPredicate gp = (GroundedPredicate)Predicate; bool bIsSame = gp.Constants[0].Equals(gp.Constants[1]); if (gp.Negation) { return(!bIsSame); } return(bIsSame); } if (lKnown != null) { if (bContainsNegations) { return(lKnown.Contains(Predicate)); } else { Predicate pCheck = Predicate; if (Predicate.Negation) { pCheck = Predicate.Negate(); } bool bContained = lKnown.Contains(pCheck); if (!bContained && Predicate.Negation) { return(true); } if (bContained && !Predicate.Negation) { return(true); } return(false); } } return(false); }
public override Predicate GenerateGiven(string sTag) { GroundedPredicate pGiven = new GroundedPredicate("Given" + Name); foreach (Constant c in Constants) { pGiven.AddConstant(c); } pGiven.AddConstant(new Constant(Domain.TAG, sTag)); if (Negation) { return(pGiven.Negate()); } return(pGiven); }
private GroundedPredicate ReadGroundedPredicate(CompoundExpression exp, Domain d) { GroundedPredicate gp = new GroundedPredicate(exp.Type); int iExpression = 0; Constant c = null; string sName = ""; for (iExpression = 0; iExpression < exp.SubExpressions.Count; iExpression++) { sName = exp.SubExpressions[iExpression].ToString(); c = d.GetConstant(sName); gp.AddConstant(c); } return(gp); }
public override Predicate ToTag() { GroundedPredicate gpNew = new GroundedPredicate(this); if (Negation) { gpNew.Name = gpNew.Name + "-Remove"; } else { gpNew.Name = gpNew.Name + "-Add"; } gpNew.Negation = false; return(gpNew); }
public KnowGivenPredicate(GroundedPredicate p, bool bValue, string sTag, bool bKnowWhether) : base((bKnowWhether ? "KWGiven" : "KGiven") + p.Name) { KnowWhether = bKnowWhether; Predicate = p; Tag = sTag; Value = bValue; Constants = new List<Constant>(p.Constants); Constants.Add(new Constant(Domain.TAG, Tag)); if (!bKnowWhether) { if (Value) Constants.Add(new Constant(Domain.VALUE, Domain.TRUE_VALUE)); else Constants.Add(new Constant(Domain.VALUE, Domain.FALSE_VALUE)); } }
private void AddEffect(Predicate pEffect) { if (pEffect.Name == Domain.FALSE_PREDICATE) { Debug.WriteLine("BUGBUG"); } if (Problem.Domain.IsFunctionExpression(pEffect.Name)) { GroundedPredicate gpIncreaseDecrease = (GroundedPredicate)pEffect; double dPreviousValue = m_sPredecessor.FunctionValues[gpIncreaseDecrease.Constants[0].Name]; double dDiff = double.Parse(gpIncreaseDecrease.Constants[1].Name); double dNewValue = double.NaN; if (gpIncreaseDecrease.Name.ToLower() == "increase") { dNewValue = dPreviousValue + dDiff; } else if (gpIncreaseDecrease.Name.ToLower() == "decrease") { dNewValue = dPreviousValue + dDiff; } else { throw new NotImplementedException(); } FunctionValues[gpIncreaseDecrease.Constants[0].Name] = dNewValue; } else if (!m_lPredicates.Contains(pEffect)) { Predicate pNegateEffect = pEffect.Negate(); if (m_lPredicates.Contains(pNegateEffect)) { //Debug.WriteLine("Removing " + pNegateEffect); m_lPredicates.Remove(pNegateEffect); } /* * if (!pEffect.Negation) * { * //Debug.WriteLine("Adding " + pEffect); * m_lPredicates.Add(pEffect); * } * */ m_lPredicates.Add(pEffect);//we are maintaining complete state information } }
//for MPSR public static Predicate GenerateKNot(Constant cTag1, Constant cTag2) { GroundedPredicate gp = new GroundedPredicate("KNot"); int iTag1 = int.Parse(cTag1.Name.Substring(3)); int iTag2 = int.Parse(cTag2.Name.Substring(3)); if (iTag1 < iTag2) { gp.AddConstant(cTag1); gp.AddConstant(cTag2); } else { gp.AddConstant(cTag2); gp.AddConstant(cTag1); } return(gp); }
public Dictionary <string, Constant> Match(GroundedPredicate pOther, Dictionary <string, Constant> dBindings) { if (pOther.Name != Name) { return(null); } if (pOther.Negation != Negation) { return(null); } if (pOther.Constants.Count != m_lParameters.Count) { return(null); } int i = 0; Dictionary <string, Constant> dNewBindings = new Dictionary <string, Constant>(); for (i = 0; i < pOther.Constants.Count; i++) { Argument a = m_lParameters[i]; if (a is Constant) { if (pOther.Constants[i].Name != a.Name) { return(null); } } else if (a is Parameter) { if (dBindings.ContainsKey(a.Name)) { if (!pOther.Constants[i].Equals(dBindings[a.Name])) { return(null); } } else { dNewBindings[a.Name] = pOther.Constants[i]; } } } return(dNewBindings); }
public Dictionary<string, Constant> Match(GroundedPredicate pOther, Dictionary<string, Constant> dBindings) { if (pOther.Name != Name) return null; if (pOther.Negation != Negation) return null; if (pOther.Constants.Count != m_lParameters.Count) return null; int i = 0; Dictionary<string, Constant> dNewBindings = new Dictionary<string, Constant>(); for (i = 0; i < pOther.Constants.Count; i++) { Argument a = m_lParameters[i]; if (a is Constant) { if (pOther.Constants[i].Name != a.Name) return null; } else if (a is Parameter) { if (dBindings.ContainsKey(a.Name)) { if (!pOther.Constants[i].Equals(dBindings[a.Name])) return null; } else dNewBindings[a.Name] = pOther.Constants[i]; } } return dNewBindings; }
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 bool IsGoal(int iX, int iY) { //iX = -1; //iY = -1; if (Belief.Observed == null) return false; GroundedPredicate gp = new GroundedPredicate("at"); gp.AddConstant(new Constant("pos", "p" + iX + "-" + iY)); //if(f.Contains if (Belief.Problem.Goal.Contains(gp)) return true; return false; }
private bool ConsistentWith(HashSet<GroundedPredicate> hsObservations, GroundedPredicate gpReasoned) { HashSet<Predicate> hsLearned = new HashSet<Predicate>(hsObservations); if (!Hidden.Contains(gpReasoned.Canonical())) if (Observed.Contains(gpReasoned.Negate())) return false; List<CompoundFormula> lHidden = new List<CompoundFormula>(m_bsInitialBelief.Hidden); bool bDone = false; while (!bDone) { bDone = true; for (int i = 0; i < lHidden.Count; i++) { CompoundFormula cf = lHidden[i]; if (cf != null) { Formula fReduced = cf.Reduce(hsLearned); if (fReduced.IsFalse(null)) return false; if (fReduced.IsTrue(null)) { lHidden[i] = null; continue; } if (fReduced is PredicateFormula) { Predicate p = ((PredicateFormula)fReduced).Predicate; if (gpReasoned.Equals(p.Negate())) return false; if (hsLearned.Add(p)) bDone = false; lHidden[i] = null; } else { CompoundFormula cfReduced = (CompoundFormula)fReduced; if (cfReduced.IsSimpleConjunction()) { HashSet<Predicate> hsPredicates = cfReduced.GetAllPredicates(); foreach (Predicate p in hsPredicates) { if (gpReasoned.Equals(p.Negate())) return false; if (hsLearned.Add(p)) bDone = false; } lHidden[i] = null; } else lHidden[i] = cfReduced; } } } } bool bLearned = hsLearned.Contains(gpReasoned); return bLearned; }
public override Predicate GenerateGiven(string sTag) { GroundedPredicate pGiven = new GroundedPredicate("Given" + Name); foreach (Constant c in Constants) pGiven.AddConstant(c); pGiven.AddConstant(new Constant(Domain.TAG, sTag)); if (Negation) return pGiven.Negate(); return pGiven; }
public override Predicate GenerateKnowGiven(string sTag, bool bKnowWhether) { GroundedPredicate pKGiven = null; if (bKnowWhether) pKGiven = new GroundedPredicate("KWGiven" + Name); else pKGiven = new GroundedPredicate("KGiven" + Name); foreach (Constant c in Constants) pKGiven.AddConstant(c); pKGiven.AddConstant(new Constant(Domain.TAG, sTag)); if (!bKnowWhether) { if (Negation) pKGiven.AddConstant(new Constant(Domain.VALUE, Domain.FALSE_VALUE)); else pKGiven.AddConstant(new Constant(Domain.VALUE, Domain.TRUE_VALUE)); } return pKGiven; }
private static GroundedPredicate GetPredicate(string sName, int iX, int iY) { GroundedPredicate gpSafe = new GroundedPredicate(sName); gpSafe.AddConstant(new Constant("pos", "p" + iX + "-" + iY)); return gpSafe; }
public GroundedPredicate Ground(Dictionary<string, Constant> dBindings) { GroundedPredicate gpred = new GroundedPredicate(Name); gpred.Negation = Negation; foreach (Argument a in Parameters) { if (a is Parameter) { if (!dBindings.ContainsKey(a.Name)) return null; gpred.AddConstant(dBindings[a.Name]); } else gpred.AddConstant((Constant)a); } return gpred; }
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 GroundedPredicate ReadGroundedPredicate(CompoundExpression exp, Domain d) { GroundedPredicate gp = new GroundedPredicate(exp.Type); int iExpression = 0; Constant c = null; string sName = ""; for (iExpression = 0; iExpression < exp.SubExpressions.Count; iExpression++) { sName = exp.SubExpressions[iExpression].ToString(); c = d.GetConstant(sName); gp.AddConstant(c); } return gp; }
private string GetNameAndTag(GroundedPredicate p) { string sName = p.Name; if (sName.StartsWith("Given")) sName = sName + "." + p.Constants.Last().Name; return sName; }
public override Predicate Negate() { if (m_gpNegation == null) { m_gpNegation = new GroundedPredicate(this); m_gpNegation.Negation = !Negation; m_gpNegation.m_gpNegation = this; } return m_gpNegation; }
private static GroundedPredicate GetSafe(int iX, int iY) { GroundedPredicate gpSafe = new GroundedPredicate("safe"); gpSafe.AddConstant(new Constant("pos", "p-" + iX)); gpSafe.AddConstant(new Constant("pos", "p-" + iY)); return gpSafe; }
public bool IsUnNone(int iX, int iY) { if (Belief.Observed == null) return false; //Predicate p=new Predicate("cv"); foreach (Formula f in Belief.Problem.Hidden) { GroundedPredicate gp = new GroundedPredicate("opened"); gp.AddConstant(new Constant("pos", "p" + iX + "-" + iY)); //if(f.Contains if (f.Contains(gp)) return true; //{ // string sPos = gp.Constants[0].Name; // sPos = sPos.Replace("p", ""); // string[] asPos = sPos.Split('-'); // if ((iX == int.Parse(asPos[0])) && (iY == int.Parse(asPos[1]))) return true; //} } return false; }
public static List<string> MasterMindHeuristic(PartiallySpecifiedState pssCurrent, Domain domain) { List<Constant> lColors = new List<Constant>(); List<Constant> lPegs = new List<Constant>(); List<Constant> lValues = new List<Constant>(); foreach (Constant c in domain.Constants) { if (c.Type.ToLower() == "color") lColors.Add(c); if (c.Type.ToLower() == "peg") lPegs.Add(c); if (c.Type.ToLower() == "value") lValues.Add(c); } //DateTime dtStart = DateTime.Now; //BeliefState bs = pssCurrent.m_bsInitialBelief; //State s = bs.ChooseState(true); //List<Predicate> l = bs.RunSatSolver(); //Console.WriteLine((DateTime.Now - dtStart).TotalSeconds); Permute(lColors); List<Constant> lGuessColors = new List<Constant>(); for (int iPeg = 0; iPeg < lPegs.Count; iPeg++) { /* //foreach (GroundedPredicate gp in s.Predicates) foreach (GroundedPredicate gp in l) { if (gp.ToString().StartsWith("(on p" + iPeg) && gp.Negation == false) { lGuessColors.Add(gp.Constants[1]); } } */ foreach (Constant cColor in lColors) { GroundedPredicate gp = new GroundedPredicate("on"); gp.AddConstant(lPegs[iPeg]); gp.AddConstant(cColor); if (!pssCurrent.Observed.Contains(gp.Negate())) { lGuessColors.Add(cColor); break; } } lColors.Remove(lGuessColors.Last()); } if (lGuessColors.Count == lPegs.Count) { List<string> lActions = new List<string>(); string sGuess = "guess-all"; foreach (Constant c in lGuessColors) sGuess += " " + c.Name; lActions.Add(sGuess); lActions.Add("evaluate-guess"); foreach (Constant cValue in lValues) { lActions.Add("observe-LocationHits " + cValue.Name); lActions.Add("observe-ColorHits " + cValue.Name); } return lActions; } return null; }
private State ApplyAxiom(State s, List<Action> lActions, Domain d) { State sCurrent = s; Predicate pNew = new GroundedPredicate("new"); Predicate pDone = new GroundedPredicate("done"); while (!sCurrent.Contains(pNew.Negate()) || !sCurrent.Contains(pDone)) { Action a1 = d.GroundActionByName(new string[] { "pre-axiom", "" }, sCurrent.Predicates, false); Action a2 = d.GroundActionByName(new string[] { "axiom", "" }, sCurrent.Predicates, false); if (a1 != null && a2 != null) { sCurrent = sCurrent.Apply(a1); sCurrent = sCurrent.Apply(a2); lActions.Add(a1); lActions.Add(a2); } } Action a = d.GroundActionByName(new string[] { "observe-new-F", "" }, sCurrent.Predicates, false); sCurrent = sCurrent.Apply(a); lActions.Add(a); a = d.GroundActionByName(new string[] { "fixpoint", "" }, sCurrent.Predicates, false); sCurrent = sCurrent.Apply(a); lActions.Add(a); return sCurrent; }
public GroundedPredicate(GroundedPredicate gpOther) : base(gpOther.Name, gpOther.Negation) { Constants = new List<Constant>(gpOther.Constants); }
public static List<string> LargeWumpusHeuristic(PartiallySpecifiedState pssCurrent, Domain d) { List<string> lActions = new List<string>(); GroundedPredicate gpAtX = null, gpAtY = null; foreach (GroundedPredicate gp in pssCurrent.Observed) { if (!gp.Negation) { if (gp.Name == "at-x") gpAtX = gp; if (gp.Name == "at-y") gpAtY = gp; } } string sX = gpAtX.Constants[0].Name; string sY = gpAtY.Constants[0].Name; int iX = int.Parse(sX.Split('-')[1]); int iY = int.Parse(sY.Split('-')[1]); VisitedLocations.Add(iX * 1000 + iY); GroundedPredicate gpAlive = new GroundedPredicate("alive"); if (pssCurrent.Hidden.Contains(gpAlive)) lActions.Add("check-alive_" + sX + "_" + sY); GroundedPredicate gpStench = new GroundedPredicate("stench"); gpStench.AddConstant(gpAtX.Constants[0]); gpStench.AddConstant(gpAtY.Constants[0]); if (pssCurrent.Hidden.Contains(gpStench)) lActions.Add("smell-wumpus " + sX + " " + sY); GroundedPredicate gpBreeze = new GroundedPredicate("breeze"); gpBreeze.AddConstant(gpAtX.Constants[0]); gpBreeze.AddConstant(gpAtY.Constants[0]); if (pssCurrent.Hidden.Contains(gpBreeze)) lActions.Add("feel-breeze " + sX + " " + sY); GroundedPredicate gpGold = new GroundedPredicate("gold-at"); gpGold.AddConstant(gpAtX.Constants[0]); gpGold.AddConstant(gpAtY.Constants[0]); if (pssCurrent.Hidden.Contains(gpGold)) lActions.Add("observe-gold " + sX + " " + sY); if (lActions.Count == 0) { List<string> lNotVisited = new List<string>(); List<string> lSafe = new List<string>(); if (iX > 1) { if(!VisitedLocations.Contains((iX - 1) * 1000 + iY)) lNotVisited.Add("move-left"); if (pssCurrent.Observed.Contains(GetSafe(iX - 1, iY))) lSafe.Add("move-left"); } if (iX < Size ) { if(!VisitedLocations.Contains((iX + 1) * 1000 + iY)) lNotVisited.Add("move-right"); if (pssCurrent.Observed.Contains(GetSafe(iX + 1, iY))) lSafe.Add("move-right"); } if (iY > 1) { if(!VisitedLocations.Contains(iX * 1000 + (iY - 1))) lNotVisited.Add("move-up"); if (pssCurrent.Observed.Contains(GetSafe(iX, iY - 1))) lSafe.Add("move-up"); } if (iY < Size) { if(!VisitedLocations.Contains(iX * 1000 + (iY + 1))) lNotVisited.Add("move-down"); if (pssCurrent.Observed.Contains(GetSafe(iX, iY + 1))) lSafe.Add("move-down"); } List<string> lSafeAndNotVisited = new List<string>(lSafe.Intersect(lNotVisited)); if (lSafeAndNotVisited.Count > 0) { int idx = RandomGenerator.Next(lSafeAndNotVisited.Count); lActions.Add(lSafeAndNotVisited[idx]); } else if (lSafe.Count > 0) { int idx = RandomGenerator.Next(lSafe.Count); lActions.Add(lSafe[idx]); } else { int idx = RandomGenerator.Next(4); if (idx == 0) lActions.Add("move-down"); if (idx == 1) lActions.Add("move-up"); if (idx == 2) lActions.Add("move-left"); if (idx == 3) lActions.Add("move-right"); } } return lActions; }
public GroundedPredicate Ground(Dictionary<string, Constant> dBindings) { GroundedPredicate gp = new GroundedPredicate("K" + Knowledge.Name); if (Knowledge is ParametrizedPredicate) { foreach (Argument a in ((ParametrizedPredicate)Knowledge).Parameters) { if (a is Parameter) { if (dBindings.ContainsKey(a.Name)) gp.AddConstant(dBindings[a.Name]); else throw new NotImplementedException(); } else gp.AddConstant((Constant)a); } } else { foreach (Constant c in ((GroundedPredicate)Knowledge).Constants) { gp.AddConstant(c); } } if (Parametrized) { if (dBindings.ContainsKey(Domain.VALUE_PARAMETER)) gp.AddConstant(dBindings[Domain.VALUE_PARAMETER]); else throw new NotImplementedException(); } else { if (Value) gp.AddConstant(new Constant(Domain.VALUE, Domain.TRUE_VALUE)); else gp.AddConstant(new Constant(Domain.VALUE, Domain.FALSE_VALUE)); } return gp; }
public override Formula Reduce(IEnumerable<Predicate> lKnown) { Predicate pReduced = Predicate; if (lKnown.Contains(Predicate)) pReduced = new GroundedPredicate(Domain.TRUE_PREDICATE); if (lKnown.Contains(Predicate.Negate())) pReduced = new GroundedPredicate(Domain.FALSE_PREDICATE); return new PredicateFormula(pReduced); }
public CompoundFormula RemoveNonDeterminism(int iActionIndex, ref int iChoiceIndex, CompoundFormula cfAndChoices) { CompoundFormula cfNew = null; if (Operator == "and") { cfNew = new CompoundFormula("and"); foreach (Formula f in Operands) { if (f is PredicateFormula) { cfNew.AddOperand(f); } else { CompoundFormula cfOperand = ((CompoundFormula)f).RemoveNonDeterminism(iActionIndex, ref iChoiceIndex, cfAndChoices); cfNew.AddOperand(cfOperand); } } } else if (Operator == "oneof" || Operator == "or") { cfNew = new CompoundFormula("and"); CompoundFormula cfChoices = new CompoundFormula(Operator); foreach (Formula f in Operands) { GroundedPredicate pChoice = new GroundedPredicate("Choice"); pChoice.AddConstant(new Constant("ActionIndex", "a" + iActionIndex)); pChoice.AddConstant(new Constant("ChoiceIndex", "c" + iChoiceIndex)); iChoiceIndex++; CompoundFormula cfWhen = new CompoundFormula("when"); cfWhen.AddOperand(pChoice); cfWhen.AddOperand(f); cfNew.AddOperand(cfWhen); cfChoices.AddOperand(pChoice); } cfAndChoices.AddOperand(cfChoices); } else if (Operator == "when") { if(Operands[1] is PredicateFormula) return this; CompoundFormula cfSecond = ((CompoundFormula)Operands[1]).RemoveNonDeterminism(iActionIndex, ref iChoiceIndex, cfAndChoices); bool bInserted = false; cfNew = cfSecond.InsertGiven(Operands[0], out bInserted); if (!bInserted) { cfNew = new CompoundFormula("when"); cfNew.AddOperand(Operands[0]); cfNew.AddOperand(cfSecond); } /* cfNew = new CompoundFormula("when"); if (cfSecond.Operator == "and") { cfNew.AddOperand(Operands[0]); cfNew.AddOperand(cfSecond); } else if (cfSecond.Operator == "when") { CompoundFormula cfAnd = new CompoundFormula("and"); cfAnd.AddOperand(Operands[0]); cfAnd.AddOperand(cfSecond.Operands[0]); cfNew.AddOperand(cfAnd); cfNew.AddOperand(cfSecond.Operands[1]); } * */ } else if (Operator == "not") { cfNew = new CompoundFormula("not"); if (cfNew.Operands[0] is PredicateFormula) cfNew.AddOperand(Operands[0]); else cfNew.AddOperand(((CompoundFormula)Operands[0]).RemoveNonDeterminism(iActionIndex, ref iChoiceIndex, cfAndChoices)); } else throw new NotImplementedException(); return cfNew; }
public bool IsPossibleLocation(int iX, int iY) { GroundedPredicate gp = new GroundedPredicate("at"); gp.AddConstant(new Constant("pos", "p" + iX + "-" + iY)); foreach (CompoundFormula cf in Belief.Hidden) if (cf.Contains(gp)) return true; return false; }
private State ApplyCompute(State s, string sName, List<Action> lActions, Domain d) { State sCurrent = s; Predicate pNew = new GroundedPredicate("new-" + sName); Predicate pDone = new GroundedPredicate("done-" + sName); int i = 0; while (!sCurrent.Contains(pNew.Negate()) || !sCurrent.Contains(pDone) || i < 10) { Action a1 = d.GroundActionByName(new string[] { "pre-" + sName, "" }, sCurrent.Predicates, false); Action a2 = d.GroundActionByName(new string[] { "compute-" + sName, "" }, sCurrent.Predicates, false); if (a1 != null && a2 != null) { sCurrent = sCurrent.Apply(a1); sCurrent = sCurrent.Apply(a2); lActions.Add(a1); lActions.Add(a2); } i++; } Action a = d.GroundActionByName(new string[] { "observe-new-" + sName + "-F", "" }, sCurrent.Predicates, false); sCurrent = sCurrent.Apply(a); lActions.Add(a); a = d.GroundActionByName(new string[] { "post-" + sName, "" }, sCurrent.Predicates, false); sCurrent = sCurrent.Apply(a); lActions.Add(a); return sCurrent; }
public bool IsLocationBlocked(int iX, int iY) { GroundedPredicate gp = new GroundedPredicate("at"); gp.AddConstant(new Constant("pos", "p" + iX + "-" + iY)); Action aChecking = Domain.GetAction("checking"); if (aChecking.Effects.Contains(gp)) return false; return true; }
public override Predicate ToTag() { GroundedPredicate gpNew = new GroundedPredicate(this); if(Negation) gpNew.Name = gpNew.Name + "-Remove"; else gpNew.Name = gpNew.Name + "-Add"; gpNew.Negation = false; return gpNew; }
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; }