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 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 gpred = GroundedPredicateFactory.Get(Name, m_lParameters, dBindings, Negation); if (gpred != null) { return(gpred); } gpred = new GroundedPredicate(Name, 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); } } GroundedPredicateFactory.Add(Name, m_lParameters, dBindings, gpred, Negation); return(gpred); }
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 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 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); }
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 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 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); }
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 bool IsRelevantFor(GroundedPredicate gp, GroundedPredicate gpRelevant) { if (!m_dRelevantPredicates.ContainsKey((GroundedPredicate)gp.Canonical())) { return(false); } return(m_dRelevantPredicates[(GroundedPredicate)gp.Canonical()].Contains((GroundedPredicate)gpRelevant.Canonical())); }
public HashSet <GroundedPredicate> GetRelevantPredicates(GroundedPredicate gp) { if (m_dRelevantPredicates.ContainsKey(gp)) { return(m_dRelevantPredicates[gp]); } return(new HashSet <GroundedPredicate>()); }
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 GroundedPredicate(GroundedPredicate gpOther) : base(gpOther.Name, gpOther.Negation) { if (gpOther == Domain.FALSE_PREDICATE || gpOther == Domain.TRUE_PREDICATE) { Console.Write("*"); } Constants = new List <Constant>(gpOther.Constants); }
public int GetPredicateIndex(GroundedPredicate gp) { if (!m_dMapPredicateToIndex.ContainsKey(gp)) { m_dMapPredicateToIndex[gp] = m_lIndexToPredicate.Count; m_lIndexToPredicate.Add(gp); } return(m_dMapPredicateToIndex[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); }
public static GroundedPredicate Get(string sName, List <Argument> lParameters, Dictionary <string, Constant> dBindings, bool bNegation) { string sFullName = GetString(sName, lParameters, dBindings, bNegation); GroundedPredicate gp = null; if (AllGrounded.TryGetValue(sFullName, out gp)) { return(gp); } return(null); }
public static void Add(string sName, List <Argument> lParameters, Dictionary <string, Constant> dBindings, GroundedPredicate gp, bool bNegation) { string sFullName = GetString(sName, lParameters, dBindings, bNegation); string sNotFullName = GetString(sName, lParameters, dBindings, !bNegation); gp.Cached = true; AllGrounded[sFullName] = gp; GroundedPredicate gpNot = (GroundedPredicate)gp.Negate(); gpNot.Cached = true; AllGrounded[sNotFullName] = gpNot; }
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 override bool IsTrue(IEnumerable <Predicate> lKnown, bool bContainsNegations) { if (Predicate == Domain.TRUE_PREDICATE) { return(true); } if (Predicate == 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 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(); } } }
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 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 Predicate PartiallyGround(Dictionary <string, Constant> dBindings) { GroundedPredicate gpred = new GroundedPredicate(Name, Negation); ParametrizedPredicate ppred = new ParametrizedPredicate(Name, 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(Domain.TRUE_PREDICATE); } else { return(Domain.FALSE_PREDICATE); } } return(gpred); } else { return(ppred); } }
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 void AddEffect(Predicate pEffect) { if (pEffect == 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 } }