// Apply an operator's effects to the current state. public List <IPredicate> ApplyAction(Operator action, List <IObject> objects) { // Create a new set of predicates. List <IPredicate> newPredicates = new List <IPredicate>(); // Initialize the state to the previous state. foreach (IPredicate pred in Predicates) { newPredicates.Add(pred.Clone() as Predicate); } // Store the conditional axioms that are true in the current state. List <IAxiom> applicableEffects = ApplicableConditionals(action, objects); action.Conditionals = applicableEffects; List <IPredicate> effects = new List <IPredicate>(); foreach (IPredicate effect in action.Effects) { effects.Add(effect.Clone() as Predicate); } foreach (IAxiom conditional in applicableEffects) { foreach (IPredicate effect in conditional.Effects) { effects.Add(effect.Clone() as Predicate); } } // Update the state using the current step effects. foreach (IPredicate effect in effects) { // Bind the effect's terms for good. //effect.BindTerms(action.Bindings); // Add a positive effect that does not already exist. if (effect.Sign) { if (!newPredicates.Contains(effect)) { newPredicates.Add(effect); } } // Hack because C# is silly... or maybe I am. Predicate found = new Predicate(); // Find if a negative effect already exists. if (!effect.Sign) { foreach (IPredicate pred in newPredicates) { if (pred.ToString().Equals(effect.ToStringPositive())) { found = pred as Predicate; } } } // Remove a negative effect. if (!found.Name.Equals("")) { newPredicates.Remove(found); } } // Return the new set of predicates. return(newPredicates); }
// Find applicable conditional effects. public List <IAxiom> ApplicableConditionals(Operator action, List <IObject> objects) { // Store the conditional axioms that are true in the current state. List <IAxiom> applicableEffects = new List <IAxiom>(); // Loop through the action's conditional effect axioms. foreach (IAxiom conditional in action.Conditionals) { List <Hashtable> bindings = new List <Hashtable>(); Hashtable binding = new Hashtable(); if (conditional.Arity == 0) { Hashtable thisBind = action.Bindings.Clone() as Hashtable; bindings.Add(thisBind); } else { for (int i = 0; i < conditional.Arity; i++) { List <Hashtable> lastBindings = new List <Hashtable>(); foreach (Hashtable lastBinding in bindings) { lastBindings.Add(lastBinding.Clone() as Hashtable); } List <Hashtable> newBindings = new List <Hashtable>(); foreach (IObject obj in objects) { List <Hashtable> theseBindings = new List <Hashtable>(); if (lastBindings.Count > 0) { foreach (Hashtable bind in lastBindings) { Hashtable thisBind = bind.Clone() as Hashtable; thisBind.Add(conditional.TermAt(i), obj.Name); theseBindings.Add(thisBind); } } else { Hashtable thisBind = action.Bindings.Clone() as Hashtable; thisBind.Add(conditional.TermAt(i), obj.Name); theseBindings.Add(thisBind); } newBindings.AddRange(theseBindings); } bindings = newBindings; } } foreach (Hashtable bind in bindings) { Axiom boundAxiom = conditional.Clone() as Axiom; boundAxiom.Bindings = bind; applicableEffects.Add(boundAxiom); } } List <IAxiom> removeCond = new List <IAxiom>(); foreach (IAxiom conditional in applicableEffects) { conditional.BindTerms(); if (!Satisfies(conditional.Preconditions)) { removeCond.Add(conditional); } } foreach (Axiom remove in removeCond) { applicableEffects.Remove(remove); } return(applicableEffects); }