private Action GetRevealedDependencyParameterizedAction() { ParametrizedAction revealDependencyParameterized = new ParametrizedAction("reveal-dependency"); Parameter agentParameter = GetAgentParameter(); Parameter dependencyParameter = GetDependencyParameter(); revealDependencyParameterized.AddParameter(agentParameter); revealDependencyParameterized.AddParameter(dependencyParameter); CompoundFormula preconditions = new CompoundFormula("and"); CompoundFormula effects = new CompoundFormula("and"); ParametrizedPredicate revealedSomething = GetParameterizedRevealedSomething(); ParametrizedPredicate revealedDependency = GetParameterizedRevealed(); ParametrizedPredicate belongsTo = GetParameterizedBelongsTo(); Predicate notRevealedSomething = revealedSomething.Negate(); Predicate notRevealedDependency = revealedDependency.Negate(); preconditions.AddOperand(notRevealedSomething); preconditions.AddOperand(notRevealedDependency); preconditions.AddOperand(belongsTo); effects.AddOperand(revealedSomething); effects.AddOperand(revealedDependency); revealDependencyParameterized.Preconditions = preconditions; revealDependencyParameterized.SetEffects(effects); return(revealDependencyParameterized); }
private Action GetResetDependenciesRevealedAction(GroundedFunctionPredicate totalCostFunctionPredicate) { Action resetDependenciesRevealed = new Action(resetDependenciesActionName); CompoundFormula preconditions = new CompoundFormula("and"); CompoundFormula effects = new CompoundFormula("and"); foreach (Constant agent in agentsConstants) { GroundedPredicate revealedSomething = new GroundedPredicate("revealed-something"); revealedSomething.AddConstant(agent); Predicate notRevealedSomething = revealedSomething.Negate(); preconditions.AddOperand(revealedSomething); effects.AddOperand(notRevealedSomething); } effects.AddOperand(GetIncreaseTotalCostFunction(totalCostFunctionPredicate, revealedDependencyCost)); resetDependenciesRevealed.Preconditions = preconditions; resetDependenciesRevealed.SetEffects(effects); return(resetDependenciesRevealed); }
// StreamWriter sw = new StreamWriter("sss.txt", false); public PdbPlaner(List <Agent> m_agents, List <Landmark> goals, PatternDatabase pd) { // d = m_d; // p = m_p; agents = m_agents; this.pd = pd; publicActions = new List <Action>(); foreach (Agent agent in agents) { agentEffect.Add(agent.name, new HashSet <CompoundFormula>()); foreach (Action act in agent.publicActions) { CompoundFormula eff = new CompoundFormula("and"); foreach (GroundedPredicate gp in act.HashEffects) { if (agent.PublicPredicates.Contains(gp)) { eff.AddOperand(gp); } } if (!agentEffect[agent.name].Contains(eff)) { agentEffect[agent.name].Add(eff); } } agent.initPdbPlaner(); countOfLandmarks += agent.GetNumberOfRestLandmarks(); } PdbVertex.initVertxClass(agents, d, p, goals, pd); }
private void InitializeEffectsWeCanReveal(List <Action> possibleActions, List <Tuple <Action, Predicate> > effectsWeCanReveal) { foreach (Action action in possibleActions) { List <Predicate> currEffects = action.HashEffects; List <Predicate> artificialEffects = new List <Predicate>(); List <Predicate> nonArtificialEffects = new List <Predicate>(); foreach (Predicate p in currEffects) { if (p.Name.Contains(Domain.ARTIFICIAL_PREDICATE)) { //it is an artificial predicate artificialEffects.Add(p); effectsWeCanReveal.Add(new Tuple <Action, Predicate>(action, p)); } else { nonArtificialEffects.Add(p); } } CompoundFormula newEffects = new CompoundFormula("and"); foreach (Predicate p in nonArtificialEffects) { newEffects.AddOperand(p); } action.Effects = newEffects; action.HashEffects = nonArtificialEffects; } }
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 List <Predicate> GetGoals() { List <Predicate> Goals = new List <Predicate>(); Formula fGoal = Goal; if (fGoal is CompoundFormula) { CompoundFormula cfGoal = (CompoundFormula)fGoal; foreach (var item in cfGoal.Operands) { if (item is PredicateFormula) { Goals.Add(((PredicateFormula)item).Predicate); } } } else if (fGoal is PredicateFormula) { Goals.Add(((PredicateFormula)fGoal).Predicate); } else { throw new Exception(); } return(Goals); }
private Action CreateReasoningAction(Predicate pEffect, HashSet <Predicate> lPredicates) { KnowPredicate kpEffect = new KnowPredicate(pEffect); if (Predicates.Contains(kpEffect)) { return(null); } Action a = new KnowledgeAction("Reasoning_" + pEffect.ToString()); a.Preconditions = new CompoundFormula("and"); foreach (Predicate pOther in lPredicates) { if (pOther != pEffect) { KnowPredicate kp = new KnowPredicate(pOther); if (!Predicates.Contains(kp)) { return(null); } ((CompoundFormula)a.Preconditions).AddOperand(new PredicateFormula(kp)); } } CompoundFormula cfEffects = new CompoundFormula("and"); cfEffects.AddOperand(new PredicateFormula(kpEffect)); a.SetEffects(cfEffects); return(a); }
private int WriteReasoningActionsII(StreamWriter sw, CompoundFormula cfHidden, int cStart) { HashSet <Predicate> lPredicates = new HashSet <Predicate>(); cfHidden.GetAllPredicates(lPredicates); int cCurrent = cStart; foreach (GroundedPredicate pEffect in lPredicates) { sw.WriteLine("(:action R" + cCurrent); sw.Write(":precondition (and"); foreach (GroundedPredicate pPrecondition in lPredicates) { if (pPrecondition != pEffect) { sw.Write(" (K" + pPrecondition.Name); foreach (Constant c in pPrecondition.Constants) { sw.Write(" " + c.Name); } sw.Write(")"); } } sw.WriteLine(")"); sw.Write(":effect (K" + pEffect.Name); foreach (Constant c in pEffect.Constants) { sw.Write(" " + c.Name); } sw.WriteLine(")"); sw.WriteLine(")"); cCurrent++; } return(cCurrent); }
/* * * private bool RegressLandmark(Predicate pCurrentLandmark, List<Action> lLandmarkAchievers, HashSet<Predicate> lInitialState, * Dictionary<string, List<Predicate>> dObligatory, Dictionary<string, List<Predicate>> dOptional) * { * * if (lLandmarkAchievers.Count == 0) * return false; * //looking for preconditions that are needed for all actions that achieve p * HashSet<Predicate> lJointPreconditions = GetJointPreconditions(lLandmarkAchievers); * bool bNewLandmarkFound = false; * if (lJointPreconditions.Count > 0) * { * foreach (GroundedPredicate pPrecondition in lJointPreconditions) * { * if (!lInitialState.Contains(pPrecondition)) * { * string sPreconditionName = GetNameAndTag(pPrecondition); * if (!dObligatory.ContainsKey(sPreconditionName)) * dObligatory[sPreconditionName] = new List<Predicate>(); * * dObligatory[sPreconditionName].Add(pPrecondition); * bNewLandmarkFound = true; * } * } * * } * * Dictionary<string, Dictionary<string, List<Predicate>>> dDisjunctions = GetDisjunctivePreconditions(lLandmarkAchievers, lInitialState); * foreach (string sPredicateType in dDisjunctions.Keys)//go over the different pre types * { * Dictionary<string, List<Predicate>> dActionToPrecondition = dDisjunctions[sPredicateType]; * if (dActionToPrecondition.Count == lLandmarkAchievers.Count)//all achivers require a pre of this type * { * if (!dOptional.ContainsKey(sPredicateType)) * dOptional[sPredicateType] = new List<Predicate>(); * foreach (KeyValuePair<string, List<Predicate>> pair in dActionToPrecondition) * { * foreach (Predicate p in pair.Value) * { * if (!lInitialState.Contains(p)) * { * dOptional[sPredicateType].Add(p); * } * } * } * } * } * * * * return true; * } * * private List<Formula> RegressLandmark(Formula fCurrentLandmark, Dictionary<Predicate, List<Action>> dLandmarkAchievers, HashSet<Predicate> lInitialState) * { * //when landmark is (or p1 p2), and p1 requires (or q1 q2) (or r1 r2) and p2 requires p3 and r3, then the result should be (or q1 q2 q3) (or r1 r2 r3) * List<Formula> lFormulas = new List<Formula>(); * Dictionary<string, Dictionary<int, List<Predicate>>> dAllObligatory = new Dictionary<string, Dictionary<int, List<Predicate>>>(); * Dictionary<string, Dictionary<int, List<Predicate>>> dAllOptional = new Dictionary<string, Dictionary<int, List<Predicate>>>(); * int cPredicatesToAchieve = 0; * foreach (GroundedPredicate pLandmark in dLandmarkAchievers.Keys) * { * List<Action> lActions = dLandmarkAchievers[pLandmark]; * Dictionary<string, List<Predicate>> dObligatory = new Dictionary<string, List<Predicate>>(); * Dictionary<string, List<Predicate>> dOptional = new Dictionary<string, List<Predicate>>(); * RegressLandmark(pLandmark, dLandmarkAchievers[pLandmark], lInitialState, dObligatory, dOptional); * string sLandmarkName = GetNameAndTag(pLandmark); * foreach (string sPreType in dObligatory.Keys) * { * if (!dAllObligatory.ContainsKey(sPreType)) * dAllObligatory[sPreType] = new Dictionary<int, List<Predicate>>(); * * dAllObligatory[sPreType][cPredicatesToAchieve] = new List<Predicate>(); * dAllObligatory[sPreType][cPredicatesToAchieve].AddRange(dObligatory[sPreType]); * } * foreach (string sPreType in dOptional.Keys) * { * if (!dAllOptional.ContainsKey(sPreType)) * dAllOptional[sPreType] = new Dictionary<int, List<Predicate>>(); * * dAllOptional[sPreType][cPredicatesToAchieve] = new List<Predicate>(); * dAllOptional[sPreType][cPredicatesToAchieve].AddRange(dOptional[sPreType]); * } * cPredicatesToAchieve++; * } * foreach (string sPreType in dAllObligatory.Keys) * { * if (dAllObligatory[sPreType].Count == cPredicatesToAchieve) * { * CompoundFormula cfOr = new CompoundFormula("or"); * foreach (List<Predicate> lPreconditions in dAllObligatory[sPreType].Values) * { * CompoundFormula cfAnd = new CompoundFormula("and"); * foreach (Predicate p in lPreconditions) * cfAnd.AddOperand(p); * cfOr.AddOperand(cfAnd); * } * Formula fSimplified = cfOr.Simplify(); * //4 options here: * //fSimplified is a single predicate * //fSimplified is a simple disjunction * //fSimplified is a simple conjunction * //fSimplified is a disjunction of simple conjunctions * if (fSimplified is PredicateFormula) * lFormulas.Add(fSimplified); * else * { * CompoundFormula cf = (CompoundFormula)fSimplified; * if (cf.Operator == "and") * { * foreach (PredicateFormula pf in cf.Operands) * lFormulas.Add(pf); * } * else * lFormulas.Add(RemoveConjunctions(cf)); * * * } * } * } * foreach (string sPreType in dAllOptional.Keys) * { * if (dAllOptional[sPreType].Count == cPredicatesToAchieve) * { * CompoundFormula cfOr = new CompoundFormula("or"); * foreach (List<Predicate> lPreconditions in dAllOptional[sPreType].Values) * { * CompoundFormula cfAnd = new CompoundFormula("or"); * foreach (Predicate p in lPreconditions) * cfAnd.AddOperand(p); * cfOr.AddOperand(cfAnd); * } * Formula fSimplified = cfOr.Simplify(); * //4 options here: * //fSimplified is a single predicate * //fSimplified is a simple disjunction * //fSimplified is a simple conjunction * //fSimplified is a disjunction of simple conjunctions * if (fSimplified is PredicateFormula) * lFormulas.Add(fSimplified); * else * { * CompoundFormula cf = (CompoundFormula)fSimplified; * if (cf.Operator == "and") * { * foreach (PredicateFormula pf in cf.Operands) * lFormulas.Add(pf); * } * else * lFormulas.Add(RemoveConjunctions(cf)); * * * } * } * } * * * return lFormulas; * } * * */ private CompoundFormula RemoveConjunctions(CompoundFormula cf) { CompoundFormula cfNew = new CompoundFormula("or"); if (cf.Operator == "and") { foreach (PredicateFormula pf in cf.Operands) { cfNew.AddOperand(pf); } } else { foreach (Formula fSub in cf.Operands) { if (fSub is PredicateFormula) { cfNew.AddOperand(fSub); } else { cfNew.AddOperand(RemoveConjunctions((CompoundFormula)fSub)); } } } return(cfNew); }
public List <VertexHsp> Expand(VertexHsp v, HashSet <CompoundFormula> levelPotential, List <VertexHsp> needUpDate) { List <VertexHsp> lExpanded = new List <VertexHsp>(); foreach (Agent agent in agents) { foreach (Action act in agent.publicActions) { //if (act.Name.Contains("p1_black")) // Console.WriteLine("*"); Program.messages += agents.Count; VertexHsp newVertexHsp = v.OnlyApply(act); if (newVertexHsp != null) { lExpanded.Add(newVertexHsp); CompoundFormula effect = new CompoundFormula("and"); foreach (GroundedPredicate gp in act.HashEffects) { if (agent.PublicPredicates.Contains(gp)) { effect.AddOperand(gp); } } levelPotential.Add(effect); // needUpDate.Add(newVertexHsp); } } } return(lExpanded); }
private List <Action> RemoveDisjunctions(List <Action> lGrounded) { List <Action> lNoDisjunctions = new List <Action>(); foreach (Action a in lGrounded) { //if (a.Name.Contains("move-KW-tag0")) // Console.WriteLine("*"); if (a.Preconditions == null || a.Preconditions is PredicateFormula) { lNoDisjunctions.Add(a); } else { HashSet <Predicate> hsMandatory = new HashSet <Predicate>(); List <List <Formula> > lOptions = new List <List <Formula> >(); ((CompoundFormula)a.Preconditions).IdentifyDisjunctions(lOptions, hsMandatory); CompoundFormula cfAnd = new CompoundFormula("and"); foreach (Predicate p in hsMandatory) { cfAnd.AddOperand(p); } int iIndex = 0; RemoveDisjunctions(a, lOptions, 0, cfAnd, lNoDisjunctions, ref iIndex); } } return(lNoDisjunctions); }
private void AddKnowledgePredicatesToExistingActions(Domain d, List <Predicate> lKnowPredicates) { Actions = new List <Action>(); ParametrizedAction aRevised = null; foreach (Action a in d.Actions) { aRevised = new ParametrizedAction(a.Name); aRevised.Preconditions = AddKnowledgePredicatesToFormula(a.Preconditions, lKnowPredicates); CompoundFormula cfEffects = null; if (a.Effects != null) { cfEffects = AddKnowledgePredicatesToFormula(a.Effects, lKnowPredicates); } else { cfEffects = new CompoundFormula("and"); } if (a.Observe != null) { List <Predicate> lPredicates = GetAllPredicates(a.Observe); foreach (Predicate p in lPredicates) { cfEffects.AddOperand(new PredicateFormula(new KnowPredicate(p))); } } aRevised.SetEffects(cfEffects); Actions.Add(aRevised); } }
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); }
public void AddHidden(CompoundFormula cf) { Domain.AddHidden(cf); /* * if (cf.Operator == "oneof") * { * CompoundFormula cfOr = new CompoundFormula("or"); * for (int j = 0; j < cf.Operands.Count; j++) * { * CompoundFormula cfAnd = new CompoundFormula("and"); * cfAnd.AddOperand(cf.Operands[j]); * for (int i = 0; i < cf.Operands.Count; i++) * { * if (i != j) * cfAnd.AddOperand(cf.Operands[i].Negate()); * } * cfOr.AddOperand(cfAnd.Simplify()); * } * m_lHidden.Add(cfOr); * } * else * m_lHidden.Add(cf); * */ m_lHidden.Add(cf); }
private Action GetRevealedDummyDependencyAction(Agent agent) { Constant a = mapAgentToConstant[agent]; Action dummyAction = new Action("reveal-dummy-dependency_" + a.Name); CompoundFormula preconditions = new CompoundFormula("and"); GroundedPredicate revealedSomething = new GroundedPredicate("revealed-something"); revealedSomething.AddConstant(a); Predicate notRevealedSomething = revealedSomething.Negate(); preconditions.AddOperand(notRevealedSomething); PredicateFormula effects = new PredicateFormula(revealedSomething); foreach (Dependency dependency in agentsDependencies[agent]) { GroundedPredicate revealedDependency = new GroundedPredicate("revealed"); Constant d = mapDependencyToConstant[dependency]; revealedDependency.AddConstant(d); preconditions.AddOperand(revealedDependency); } dummyAction.Preconditions = preconditions; dummyAction.SetEffects(effects); return(dummyAction); }
public Formula RegressDet(Action a) { Formula f = a.RegressDet(Predicate); if (f != null) { return(f); } CompoundFormula cfAndNot = new CompoundFormula("and"); CompoundFormula cfOr = new CompoundFormula("or"); int iCondition = 0; Predicate pNegate = Predicate.Negate(); foreach (CompoundFormula cfCondition in a.GetConditions()) { HashSet <Predicate> lEffects = cfCondition.Operands[1].GetAllPredicates(); if (lEffects.Contains(Predicate)) { cfOr.AddOperand(cfCondition.Operands[0].CreateRegression(Predicate, -1)); } else if (lEffects.Contains(pNegate)) { cfAndNot.AddOperand(cfCondition.Operands[0].CreateRegression(pNegate, -1).Negate()); } iCondition++; } cfOr.AddOperand(this); cfAndNot.AddOperand(cfOr); return(cfAndNot.Simplify()); }
public void AddReasoningActions() { ReasoningActions = new List <Action>(); foreach (CompoundFormula cf in m_lHidden) { if (cf.Operator == "oneof") { foreach (Formula f in cf.Operands) { if (cf.Operands.Count > 2) { CompoundFormula cfNegativeEffects = new CompoundFormula("and"); CompoundFormula cfPositiveEffects = new CompoundFormula("or"); foreach (Formula fOther in cf.Operands) { if (!fOther.Equals(f)) { cfNegativeEffects.AddOperand(f.Negate()); } AddReasoningAction(f, cfNegativeEffects); } } else { AddReasoningAction(cf.Operands[0], cf.Operands[1].Negate()); AddReasoningAction(cf.Operands[1], cf.Operands[0].Negate()); } } } else { throw new NotImplementedException("Not implementing or for now"); } } }
private CompoundFormula AddKnowledgePredicatesToFormula(Formula f, HashSet <Predicate> lKnowPredicates) { CompoundFormula cf = new CompoundFormula("and"); HashSet <Predicate> lPredicates = new HashSet <Predicate>(); f.GetAllPredicates(lPredicates); foreach (Predicate p in lPredicates) { if (lKnowPredicates.Contains(p)) { KnowPredicate kp = new KnowPredicate(p); cf.AddOperand(new PredicateFormula(kp)); } } if (f is CompoundFormula && ((CompoundFormula)f).Operator == "and") { foreach (Formula fSub in ((CompoundFormula)f).Operands) { cf.AddOperand(fSub); } } else { cf.AddOperand(f); } return(cf); }
private Formula ReadFormula(CompoundExpression exp, Dictionary <string, string> dParameterNameToType, bool bParamterized, Domain d) { bool bPredicate = true; //Debug.WriteLine(exp); if (d != null && d.IsFunctionExpression(exp.Type)) { Predicate p = ReadFunctionExpression(exp, dParameterNameToType, d); return(new PredicateFormula(p)); } else if (IsUniversalQuantifier(exp)) { CompoundExpression eParameter = (CompoundExpression)exp.SubExpressions[0]; CompoundExpression eBody = (CompoundExpression)exp.SubExpressions[1]; string sParameter = eParameter.Type; string sType = eParameter.SubExpressions[1].ToString(); dParameterNameToType[sParameter] = sType; ParametrizedFormula cfQuantified = new ParametrizedFormula(exp.Type); cfQuantified.Parameters[sParameter] = sType; Formula fBody = ReadFormula(eBody, dParameterNameToType, true, d); cfQuantified.AddOperand(fBody); return(cfQuantified); } else { foreach (Expression eSub in exp.SubExpressions) { if (eSub is CompoundExpression) { bPredicate = false; break; } } if (bPredicate) { return(ReadPredicate(exp, dParameterNameToType, bParamterized, d)); } else { CompoundFormula cf = new CompoundFormula(exp.Type); int iExpression = 0; for (iExpression = 0; iExpression < exp.SubExpressions.Count; iExpression++) { if (exp.SubExpressions[iExpression] is StringExpression) { throw new InvalidDataException(); } Formula f = ReadFormula((CompoundExpression)exp.SubExpressions[iExpression], dParameterNameToType, bParamterized, d); cf.SimpleAddOperand(f); } if (cf.Operator == "not" && cf.Operands[0] is PredicateFormula) { PredicateFormula fNegate = new PredicateFormula(((PredicateFormula)cf.Operands[0]).Predicate.Negate()); return(fNegate); } return(cf); } } }
public override List <Action> Plan(State sStartState) { CompoundFormula fGoal = (CompoundFormula)m_pProblem.Goal; //List<Predicate> lGoal = new List<Predicate>(); //foreach (PredicateFormula vf in fGoal.Operands) // lGoal.Add(vf.Predicate); State sCurrent = null; List <State> lOpenList = new List <State>(); Dictionary <State, State> dParents = new Dictionary <State, State>(); Dictionary <State, Action> dGeneratingAction = new Dictionary <State, Action>(); Dictionary <State, double> dHeuristic = new Dictionary <State, double>(); Dictionary <State, double> dCost = new Dictionary <State, double>(); int cRepeated = 0; dCost[sStartState] = 0; sStartState.GroundAllActions(); dHeuristic[sStartState] = m_fHeuristic.h(sStartState); lOpenList.Add(sStartState); dParents[sStartState] = null; dGeneratingAction[sStartState] = null; m_cObservedStates = 1; int cHandled = 0; while (lOpenList.Count > 0) { sCurrent = GetMinimalStateBFS(lOpenList, dHeuristic, dCost); cHandled++; Debug.Write("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b" + cHandled + ", " + dHeuristic[sCurrent] + "," + lOpenList.Count); lOpenList.Remove(sCurrent); Dictionary <State, Action> dNextStates = m_fHeuristic.GetNextStates(sCurrent); foreach (State sNext in dNextStates.Keys) { if (!dParents.ContainsKey(sNext)) { m_cObservedStates++; dParents[sNext] = sCurrent; dGeneratingAction[sNext] = dNextStates[sNext]; if (sNext.Contains(fGoal)) { List <Action> lPlan = CreatePlan(sNext, dParents, dGeneratingAction); ValidatePlan(sStartState, lPlan); return(lPlan); } else { dCost[sNext] = dCost[sCurrent] + 1; dHeuristic[sNext] = m_fHeuristic.h(sNext); lOpenList.Add(sNext); } } else if (sNext != null) { cRepeated++; } } } return(null); }
// public static bool IsValid(Dictionary <Constant, PlanResult> plans) { List <PreconditionsAndEffectsAtTime> peatCollection = new List <PreconditionsAndEffectsAtTime>(); int latestTimeOperationObserved = 0; // Collect all the preconditions and effects from all the agent's plans at each timestamp foreach (var plan in plans) { Constant agent = plan.Key; ConditionalPlanTreeNode cptn = plan.Value.Plan; // get all actions List <Action> actionsUsed = new List <Action>(); cptn.GetActionUsed(ref actionsUsed); foreach (var a in actionsUsed) { int actionTime = a.GetTime(); Formula effects = a.Effects; Formula preconditions = a.Preconditions; PreconditionsAndEffectsAtTime peat = new PreconditionsAndEffectsAtTime(); peat.agent = agent; peat.Time = actionTime; peat.Effects = effects; peat.Preconditions = preconditions; peatCollection.Add(peat); //Update latest operation time latestTimeOperationObserved = Math.Max(latestTimeOperationObserved, actionTime); } } // Create aggregated collection of effects over time Dictionary <int, Formula> effectsCollection = new Dictionary <int, Formula>(); Dictionary <int, Formula> preconditionsCollection = new Dictionary <int, Formula>(); for (int i = 0; i <= latestTimeOperationObserved; i++) { // Get all effects at time i CompoundFormula combinedEffects = new CompoundFormula("and"); foreach (var effFormula in peatCollection.FindAll(x => x.Time == i).Select(x => x.Effects).ToList()) { combinedEffects.AddOperand(effFormula); } effectsCollection.Add(i, combinedEffects); // Get all the preconditions at time i CompoundFormula combinedPreconditions = new CompoundFormula("and"); foreach (var effFormula in peatCollection.FindAll(x => x.Time == i).Select(x => x.Preconditions).ToList()) { combinedPreconditions.AddOperand(effFormula); } preconditionsCollection.Add(i, combinedPreconditions); } return(true); }
private List <IMAP.Action> ScanEffectsForConst(Predicate goal) { List <IMAP.Action> ans = new List <IMAP.Action>(); if (Action != null) { string actionName = Action.Name.Split('_')[0]; if (!actionName.StartsWith("wait-goal")) { if (Action.Effects != null) { Formula fEffects = Action.Effects; if (fEffects is CompoundFormula) { CompoundFormula cf = (CompoundFormula)fEffects; foreach (Formula formula in cf.Operands) { if (formula is PredicateFormula) { PredicateFormula pf = (PredicateFormula)formula; if (pf.Predicate.ToString() == goal.ToString()) { ans.Add(Action); } } } } else if (fEffects is PredicateFormula) { PredicateFormula pf = (PredicateFormula)fEffects; if (pf.Predicate.ToString() == goal.ToString()) { ans.Add(Action); } } else { throw new Exception(); } } } } if (SingleChild != null) { ans.AddRange(SingleChild.ScanEffectsForConst(goal)); } if (FalseObservationChild != null) { ans.AddRange(FalseObservationChild.ScanEffectsForConst(goal)); } if (TrueObservationChild != null) { ans.AddRange(TrueObservationChild.ScanEffectsForConst(goal)); } return(ans); }
internal void SetGoals(List <Predicate> wantedGoals) { CompoundFormula cf = new CompoundFormula("and"); foreach (var item in wantedGoals) { cf.AddOperand(new PredicateFormula(item)); } Goal = cf; }
public void AddHidden(string sOperator, List <Predicate> lPredicates) { CompoundFormula cf = new CompoundFormula(sOperator); foreach (Predicate p in lPredicates) { cf.AddOperand(new PredicateFormula(p)); } m_lHidden.Add(cf); }
public List <Vertex> Expand(Vertex v, HashSet <CompoundFormula> levelPotential, List <Vertex> needUpDate) { List <Vertex> lExpanded = new List <Vertex>(); bool stop = false; int counter = -1; foreach (Agent agent in agents) { foreach (Action act in agent.publicActions) { counter++; if (counter <= v.actionNumber) { continue; } //if (act.Name.Contains("p1_black")) // Console.WriteLine("*"); Program.messages += agents.Count; Vertex newVertex = v.Apply(act); if (newVertex != null) { // if (act.Name.Contains("verysmooth")) // Console.WriteLine("*"); lExpanded.Add(newVertex); CompoundFormula effect = new CompoundFormula("and"); foreach (GroundedPredicate gp in act.HashEffects) { if (agent.PublicPredicates.Contains(gp)) { effect.AddOperand(gp); } } levelPotential.Add(effect); if (newVertex.h < v.h && !first) { v.actionNumber = counter; lExpanded.Add(v); //stop = true; } // needUpDate.Add(newVertex); } if (stop) { break; } } if (stop) { break; } } first = false; return(lExpanded); }
private Formula AddPreconditions(Action a) { CompoundFormula cfOr = new CompoundFormula("or"); CompoundFormula cfAnd = new CompoundFormula("and"); cfAnd.AddOperand(a.Preconditions); cfAnd.AddOperand(Negate()); cfOr.AddOperand(cfAnd); cfOr.AddOperand(this); return(cfOr.Simplify()); }
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 CompoundFormula AddKnowledgePredicatesToFormula(Formula f, List <Predicate> lKnowPredicates) { CompoundFormula cf = new CompoundFormula("and"); List <Predicate> lPredicates = GetAllPredicates(f); foreach (Predicate p in lPredicates) { if (lKnowPredicates.Contains(p)) { cf.AddOperand(new PredicateFormula(new KnowPredicate(p))); } } cf.AddOperand(f); return(cf); }
/* * private List<Action> GetRevealedDummyDependencyActions(List<Agent> m_agents) * { * List<Action> actions = new List<Action>(); * foreach(Agent agent in m_agents) * { * int amountOfDependencies = amountOfAgentDependencies[agent]; * if (amountOfDependencies < maxDependenciesOfAgent) //only do this dummy action for agents which don't have the max dependencies amount, so they will not stuck the other agents who want to reveal a dependency... * actions.Add(GetRevealedDummyDependencyAction(agent)); * } * * return actions; * } */ /* * private Action GetRevealedDummyDependencyAction(Agent agent) * { * Constant a = mapAgentToConstant[agent]; * Action dummyAction = new Action("reveal-dummy-dependency_" + a.Name); * * CompoundFormula preconditions = new CompoundFormula("and"); * * GroundedFunctionPredicate amountOfDepRevGrounded = GetAmountOfDependenciesRevealedGrounded(a); * GroundedFunctionPredicate equalsToMax = GetEqualsToMaxGrounded(amountOfDepRevGrounded); * Predicate notEqualsToMax = equalsToMax.Negate(); * * GroundedFunctionPredicate increaseAmountOfDepRev = GetIncreaseAmountOfDependenciesRevealedGrounded(amountOfDepRevGrounded); * //GroundedPredicate revealedSomething = new GroundedPredicate("revealed-something"); * //revealedSomething.AddConstant(a); * * //Predicate notRevealedSomething = revealedSomething.Negate(); * * preconditions.AddOperand(notEqualsToMax); * PredicateFormula effects = new PredicateFormula(increaseAmountOfDepRev); * * foreach (Dependency dependency in agentsDependencies[agent]) * { * GroundedPredicate revealedDependency = new GroundedPredicate("revealed"); * Constant d = mapDependencyToConstant[dependency]; * revealedDependency.AddConstant(d); * * preconditions.AddOperand(revealedDependency); * } * * dummyAction.Preconditions = preconditions; * dummyAction.SetEffects(effects); * * return dummyAction; * } */ /* * private Action GetResetDependenciesRevealedAction(GroundedFunctionPredicate totalCostFunctionPredicate) * { * Action resetDependenciesRevealed = new Action(resetDependenciesActionName); * * CompoundFormula preconditions = new CompoundFormula("and"); * CompoundFormula effects = new CompoundFormula("and"); * * foreach(Constant agent in agentsConstants) * { * GroundedPredicate revealedSomething = new GroundedPredicate("revealed-something"); * revealedSomething.AddConstant(agent); * * Predicate notRevealedSomething = revealedSomething.Negate(); * * preconditions.AddOperand(revealedSomething); * effects.AddOperand(notRevealedSomething); * } * * effects.AddOperand(GetIncreaseTotalCostFunction(totalCostFunctionPredicate, revealedDependencyCost)); * * resetDependenciesRevealed.Preconditions = preconditions; * resetDependenciesRevealed.SetEffects(effects); * * return resetDependenciesRevealed; * } */ private Action GetRevealedDependencyParameterizedAction() { ParametrizedAction revealDependencyParameterized = new ParametrizedAction("reveal-dependency"); Parameter agentParameter = GetAgentParameter(); Parameter dependencyParameter = GetDependencyParameter(); Parameter num1Parameter = GetNum1Parameter(); Parameter num2Parameter = GetNum2Parameter(); revealDependencyParameterized.AddParameter(agentParameter); revealDependencyParameterized.AddParameter(dependencyParameter); revealDependencyParameterized.AddParameter(num1Parameter); revealDependencyParameterized.AddParameter(num2Parameter); CompoundFormula preconditions = new CompoundFormula("and"); CompoundFormula effects = new CompoundFormula("and"); ParametrizedPredicate nextN1N2 = GetParameterizedNext(num1Parameter, num2Parameter); ParametrizedPredicate revealedNumberOfDependenciesN1 = GetParameterizedRevealedNumberOfDependencies(agentParameter, num1Parameter); ParametrizedPredicate revealedNumberOfDependenciesN2 = GetParameterizedRevealedNumberOfDependencies(agentParameter, num2Parameter); //ParametrizedPredicate revealedSomething = GetParameterizedRevealedSomething(); ParametrizedPredicate revealedDependency = GetParameterizedRevealed(); ParametrizedPredicate belongsTo = GetParameterizedBelongsTo(); Predicate NotRevealedNumberOfDependenciesN1 = revealedNumberOfDependenciesN1.Negate(); //Predicate notRevealedSomething = revealedSomething.Negate(); //Predicate notRevealedDependency = revealedDependency.Negate(); preconditions.AddOperand(nextN1N2); preconditions.AddOperand(revealedNumberOfDependenciesN1); //preconditions.AddOperand(notRevealedSomething); //preconditions.AddOperand(notRevealedDependency); preconditions.AddOperand(belongsTo); //effects.AddOperand(revealedSomething); effects.AddOperand(revealedDependency); if (limitingTheNumberOfDependenciesToBeRevealed) { effects.AddOperand(NotRevealedNumberOfDependenciesN1); effects.AddOperand(revealedNumberOfDependenciesN2); } revealDependencyParameterized.Preconditions = preconditions; revealDependencyParameterized.SetEffects(effects); return(revealDependencyParameterized); }
private void AddReasoningActions(Formula fUnknown, HashSet <Predicate> lUnassigned, HashSet <Predicate> lAssigned, Dictionary <List <Predicate>, List <Predicate> > dActions) { if (fUnknown is PredicateFormula) { HashSet <Predicate> lEffects = new HashSet <Predicate>(); Predicate pEffect = ((PredicateFormula)fUnknown).Predicate; if (pEffect.Name != Domain.TRUE_PREDICATE) { lEffects.Add(pEffect); AddReasoningAction(lAssigned, lEffects, dActions); } return; } CompoundFormula cfUnknown = (CompoundFormula)fUnknown; if (cfUnknown.Operator == "and") { bool bAllKnown = true; foreach (Formula f in cfUnknown.Operands) { if (f is CompoundFormula) { bAllKnown = false; } } if (bAllKnown) { AddReasoningAction(lAssigned, lUnassigned, dActions); return; } } Formula fReduced = null; foreach (Predicate p in lUnassigned) { HashSet <Predicate> lUnassignedReduced = new HashSet <Predicate>(lUnassigned); lUnassignedReduced.Remove(p); lAssigned.Add(p); fReduced = cfUnknown.Reduce(lAssigned); AddReasoningActions(fReduced, lUnassignedReduced, lAssigned, dActions); lAssigned.Remove(p); lAssigned.Add(p.Negate()); fReduced = cfUnknown.Reduce(lAssigned); AddReasoningActions(fReduced, lUnassignedReduced, lAssigned, dActions); lAssigned.Remove(p.Negate()); } }