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);
     }
 }
Esempio n. 2
0
        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 override Action Clone()
 {
     ParametrizedAction aNew = new ParametrizedAction(Name);
     aNew.Parameters = Parameters;
     if (Preconditions != null)
         aNew.Preconditions = Preconditions.Clone();
     if (Effects != null)
         aNew.SetEffects( Effects.Clone());
     if( Observe != null )
         aNew.Observe = Observe.Clone();
     aNew.HasConditionalEffects = HasConditionalEffects;
     aNew.ContainsNonDeterministicEffect = ContainsNonDeterministicEffect;
     return aNew;
 }
        private Action ReadAction(CompoundExpression exp, Domain d)
        {
            string sName = exp.SubExpressions[0].ToString();

            sName = sName.Replace("_", "-");
            Action pa          = null;
            int    iExpression = 0;

            for (iExpression = 1; iExpression < exp.SubExpressions.Count; iExpression++)
            {
                if (exp.SubExpressions[iExpression].ToString() == ":parameters")
                {
                    CompoundExpression ceParams = (CompoundExpression)exp.SubExpressions[iExpression + 1];
                    if (ceParams.Type != "N/A")
                    {
                        pa = new ParametrizedAction(sName);
                        ReadParameters((CompoundExpression)exp.SubExpressions[iExpression + 1], (ParametrizedAction)pa);
                    }
                    iExpression++;
                }
                else if (exp.SubExpressions[iExpression].ToString() == ":effect")
                {
                    if (pa == null)
                    {
                        pa = new Action(sName);
                    }
                    ReadEffect((CompoundExpression)exp.SubExpressions[iExpression + 1], pa, d, pa is ParametrizedAction);
                    iExpression++;
                }
                else if (exp.SubExpressions[iExpression].ToString() == ":precondition")
                {
                    if (pa == null)
                    {
                        pa = new Action(sName);
                    }
                    ReadPrecondition((CompoundExpression)exp.SubExpressions[iExpression + 1], pa, d, pa is ParametrizedAction);
                    iExpression++;
                }
                else if (exp.SubExpressions[iExpression].ToString() == ":observe")
                {
                    if (pa == null)
                    {
                        pa = new Action(sName);
                    }
                    ReadObserve((CompoundExpression)exp.SubExpressions[iExpression + 1], pa, d, pa is ParametrizedAction);
                    iExpression++;
                }
            }
            return(pa);
        }
Esempio n. 5
0
        public override Action Clone()
        {
            ParametrizedAction aNew = new ParametrizedAction(Name);

            aNew.Parameters = Parameters;
            if (Preconditions != null)
            {
                aNew.Preconditions = Preconditions.Clone();
            }
            if (Effects != null)
            {
                aNew.SetEffects(Effects.Clone());
            }
            if (Observe != null)
            {
                aNew.Observe = Observe.Clone();
            }
            aNew.HasConditionalEffects          = HasConditionalEffects;
            aNew.ContainsNonDeterministicEffect = ContainsNonDeterministicEffect;
            return(aNew);
        }
        private void ReadParameters(CompoundExpression exp, ParametrizedAction pa)
        {
            //unfortunately, expressions have a weird non standard structure with no type - (?i - pos ?j - pos )
            //so we must have a special case
            List <string> lTokens  = exp.ToTokenList();
            List <string> lNames   = new List <string>();
            string        sType    = "";
            int           iCurrent = 0;

            while (iCurrent < lTokens.Count)
            {
                if (lTokens[iCurrent] == "-")
                {
                    sType = lTokens[iCurrent + 1];
                    foreach (string sName in lNames)
                    {
                        pa.AddParameter(new Parameter(sType, sName));
                    }
                    lNames    = new List <string>();
                    sType     = "";
                    iCurrent += 2;
                }
                else
                {
                    lNames.Add(lTokens[iCurrent]);
                    iCurrent++;
                }
            }
            if (lNames.Count != 0) //allowing no types specified
            {
                foreach (string sName in lNames)
                {
                    pa.AddParameter(new Parameter("OBJ", sName));
                }
            }
        }
 private void ReadParameters(CompoundExpression exp, ParametrizedAction pa)
 {
     //unfortunately, expressions have a weird non standard structure with no type - (?i - pos ?j - pos )
     //so we must have a special case
     List<string> lTokens = exp.ToTokenList();
     List<string> lNames = new List<string>();
     string sType = "";
     int iCurrent = 0;
     while (iCurrent < lTokens.Count)
     {
         if (lTokens[iCurrent] == "-")
         {
             sType = lTokens[iCurrent + 1];
             foreach (string sName in lNames)
                 pa.AddParameter(new Parameter(sType, sName));
             lNames = new List<string>();
             sType = "";
             iCurrent += 2;
         }
         else
         {
             lNames.Add(lTokens[iCurrent]);
             iCurrent++;
         }
     }
     if (lNames.Count != 0) //allowing no types specified
     {
         foreach (string sName in lNames)
             pa.AddParameter(new Parameter("OBJ", sName));
     }
 }
 private Action ReadAction(CompoundExpression exp, Domain d)
 {
     string sName = exp.SubExpressions[0].ToString();
     sName = sName.Replace("_", "-");
     Action pa = null;
     int iExpression = 0;
     for (iExpression = 1; iExpression < exp.SubExpressions.Count; iExpression++)
     {
         if (exp.SubExpressions[iExpression].ToString() == ":parameters")
         {
             CompoundExpression ceParams = (CompoundExpression)exp.SubExpressions[iExpression + 1];
             if (ceParams.Type != "N/A")
             {
                 pa = new ParametrizedAction(sName);
                 ReadParameters((CompoundExpression)exp.SubExpressions[iExpression + 1], (ParametrizedAction)pa);
             }
             iExpression++;
         }
         else if (exp.SubExpressions[iExpression].ToString() == ":effect")
         {
             if (pa == null)
                 pa = new Action(sName);
             ReadEffect((CompoundExpression)exp.SubExpressions[iExpression + 1], pa, d, pa is ParametrizedAction);
             iExpression++;
         }
         else if (exp.SubExpressions[iExpression].ToString() == ":precondition")
         {
             if (pa == null)
                 pa = new Action(sName);
             ReadPrecondition((CompoundExpression)exp.SubExpressions[iExpression + 1], pa, d, pa is ParametrizedAction);
             iExpression++;
         }
         else if (exp.SubExpressions[iExpression].ToString() == ":observe")
         {
             if (pa == null)
                 pa = new Action(sName);
             ReadObserve((CompoundExpression)exp.SubExpressions[iExpression + 1], pa, d, pa is ParametrizedAction);
             iExpression++;
         }
     }
     return pa;
 }
        public Action KnowWhetherTagCompilation(Dictionary<string, List<Predicate>> dTags, Domain d, List<string> lIncludedTags, List<string> lExcludedTags)
        {
            string sName = Name + "-KW";
            foreach (string sTag in lIncludedTags)
                sName += "-" + sTag;
            ParametrizedAction aNew = new ParametrizedAction(sName);
            if (this is ParametrizedAction)
            {
                foreach (Parameter p in ((ParametrizedAction)this).Parameters)
                    aNew.AddParameter(p);
            }

            List<CompoundFormula> lConditions = new List<CompoundFormula>();
            List<Formula> lObligatory = new List<Formula>();
            SplitEffects(lConditions, lObligatory);

            aNew.Preconditions = GetKnowWhetherPreconditions(dTags, d, lIncludedTags, lExcludedTags);

            if (Effects != null)
            {
                HashSet<Predicate> lKnowEffects = new HashSet<Predicate>();
                CompoundFormula cfEffects = new CompoundFormula("and");
                //CompoundFormula cfMandatoryEffects = new CompoundFormula("and");
                foreach (Formula f in lObligatory)
                {
                    f.GetAllPredicates(lKnowEffects);
                }
                if (lKnowEffects.Count > 0)
                {
                    List<Predicate> lFunctionExpressions = new List<Predicate>();
                    List<Predicate> lPredicates = new List<Predicate>();
                    foreach (Predicate p in lKnowEffects)
                    {
                        if (d.IsFunctionExpression(p.Name))
                            lFunctionExpressions.Add(p);
                        else
                            lPredicates.Add(p);
                    }

                    foreach (string sTag in lIncludedTags)
                    {
                        //~KNot t|?t -> effects|t
                        CompoundFormula cfKEffects = new CompoundFormula("and");

                        foreach (Predicate p in lPredicates)
                        {
                            Predicate pAdd = p.GenerateGiven(sTag);
                            cfKEffects.AddOperand(pAdd);
                            if (!d.AlwaysKnown(p))
                            {
                                pAdd = p.GenerateKnowGiven(sTag, true);
                                cfKEffects.AddOperand(pAdd);
                            }
                        }
                        cfEffects.SimpleAddOperand(cfKEffects);
                    }
                    foreach (Predicate p in lFunctionExpressions)
                        cfEffects.AddOperand(p);
                }
                List<Predicate> lAllKnowledgeToRemove = new List<Predicate>();
                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)
                        {
                            cfEffects.SimpleAddOperand(cfK);
                        }
                    }
                    if (SDRPlanner.RemoveAllKnowledge)
                    {
                        foreach (Predicate p in cfCondition.Operands[1].GetAllPredicates())
                        {
                            Predicate pTag = p;
                            if (p.Negation)
                                pTag = p.Negate();
                            if (!lAllKnowledgeToRemove.Contains(pTag))
                                lAllKnowledgeToRemove.Add(pTag);
                        }
                    }
                    else
                    {
                        cfK = CreateTaggedKnowledgeWhetherGainConditions(cfCondition, d, lIncludedTags);
                        if (cfK != null)
                        {
                            cfEffects.SimpleAddOperand(cfK);
                        }

                        cfK = CreateTaggedKnowledgeWhetherLossCondition(cfCondition, d, lIncludedTags);
                        if (cfK != null && cfK.Operands.Count > 0)
                        {
                            cfEffects.SimpleAddOperand(cfK);
                        }
                    }
                     /* causes the plan to add many merge actions
                    foreach (string sTag in lIncludedTags)
                    {
                        foreach (Predicate pForget in lConditionEffects)
                        {
                            if(pForget.Name != Domain.OPTION_PREDICATE)
                                cfEffects.AddOperand(pForget.GenerateKnowGiven(sTag, true).Negate());
                        }
                    }
                      * */
                }
                if (SDRPlanner.RemoveAllKnowledge)
                {
                    foreach (Predicate p in lAllKnowledgeToRemove)
                    {
                        foreach (string sTag in lIncludedTags)
                        {
                            Predicate pNegate = p.GenerateKnowGiven(sTag, true).Negate();
                            cfEffects.AddOperand(pNegate);
                        }
                    }
                }
                aNew.Effects = cfEffects.Simplify();
            }
            if (Observe != null)
            {
                throw new NotImplementedException();
            }
            return aNew;
        }
Esempio n. 10
0
        public List<Action> TagCompilationSplitConditionsNoState(Dictionary<string, List<Predicate>> dTags, Domain d, List<string> lIncludedTags,
            List<string> lExcludedTags, List<Predicate> lAdditionalPredicates)
        {
            string sName = Name;
            foreach (string sTag in lIncludedTags)
                sName += "-" + sTag;
            ParametrizedAction aNew = new ParametrizedAction(sName);

            if (this is ParametrizedAction)
            {
                foreach (Parameter p in ((ParametrizedAction)this).Parameters)
                {
                    aNew.AddParameter(p);
                }
            }

            List<CompoundFormula> lConditions = new List<CompoundFormula>();
            List<Formula> lObligatory = new List<Formula>();
            SplitEffects(lConditions, lObligatory);

            CompoundFormula cfPreconditions = new CompoundFormula("and");
            Formula cfNoStatePreconditions = GetPreconditionsNoState(dTags, d, lIncludedTags, lExcludedTags);
            cfPreconditions.AddOperand(cfNoStatePreconditions); //knowledge loss is the first action, so it will have all the preconditions
            aNew.Preconditions = cfPreconditions;

            if (Effects == null)
                throw new NotImplementedException();

            HashSet<Predicate> lKnowEffects = new HashSet<Predicate>();
            CompoundFormula cfStateEffects = 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);
                    }
                    cfStateEffects.SimpleAddOperand(cfKEffects);
                }
            }

            List<Action> lActions = new List<Action>();

            if (lConditions.Count > 0)
            {
                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);
                        }
                    }
                }
            }

            aNew.Effects = cfStateEffects.Simplify();

            if (lConditions.Count > 0 && SDRPlanner.SplitConditionalEffects)
                lActions.AddRange(aNew.SplitConditions(lAdditionalPredicates));
            else
            {
                ((CompoundFormula)aNew.Preconditions).AddOperand(new GroundedPredicate("NotInAction"));
                lActions.Add(aNew);
            }

            if (Observe != null)
            {
                throw new NotImplementedException();
            }
            return lActions;
        }
Esempio n. 11
0
        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;
        }
Esempio n. 12
0
        public Action KnowWhetherTagObservationTranslation(Dictionary<string, List<Predicate>> dTags, Domain d, List<string> lIncludedTags, List<string> lExcludedTags)
        {
            string sName = Name + "-KW";
            foreach (string sTag in lIncludedTags)
                sName += "-" + sTag;
            ParametrizedAction aNew = new ParametrizedAction(sName);
            if (this is ParametrizedAction)
            {
                foreach (Parameter p in ((ParametrizedAction)this).Parameters)
                    aNew.AddParameter(p);
            }

            if (Observe == null)
                throw new NotImplementedException();
            if (Effects != null)
                throw new NotImplementedException();
            Predicate pObserve = ((PredicateFormula)Observe).Predicate;

            aNew.Preconditions = GetKnowWhetherPreconditions(dTags, d, lIncludedTags, lExcludedTags);

            CompoundFormula cfEffects = new CompoundFormula("and");

            foreach (string sTag in lIncludedTags)
            {
                cfEffects.AddOperand(pObserve.GenerateKnowGiven(sTag, true));
            }

            aNew.Effects = cfEffects;

            return aNew;
        }
Esempio n. 13
0
        public Action KnowWhetherTagObservationTranslation(Dictionary<string, List<Predicate>> dTags, Domain d, string sActionTag)
        {
            string sName = Name + "-KW-" + sActionTag;
            ParametrizedAction aNew = new ParametrizedAction(sName);
            if (this is ParametrizedAction)
            {
                foreach (Parameter p in ((ParametrizedAction)this).Parameters)
                    aNew.AddParameter(p);
            }

             if (Observe == null)
                throw new NotImplementedException();
            if (Effects != null)
                throw new NotImplementedException();
            Predicate pObserve = ((PredicateFormula)Observe).Predicate;

            aNew.Preconditions = GetKnowWhetherPreconditions(dTags, d, sActionTag);

            CompoundFormula cfEffects = new CompoundFormula("and");

            foreach (string sTag in dTags.Keys)
            {

                if (sTag != sActionTag)
                {

                    CompoundFormula cfCondition = new CompoundFormula("when");
                    CompoundFormula cfAnd = new CompoundFormula("and");
                    cfAnd.AddOperand(pObserve.GenerateGiven(sTag));
                    cfAnd.AddOperand(pObserve.GenerateGiven(sActionTag).Negate());
                    cfCondition.AddOperand(cfAnd);

                    Predicate pNotTag = Predicate.GenerateKNot(new Constant(Domain.TAG, sTag), new Constant(Domain.TAG, sActionTag));

                    cfCondition.AddOperand(pNotTag);

                    cfEffects.AddOperand(cfCondition);

                    cfCondition = new CompoundFormula("when");
                    cfAnd = new CompoundFormula("and");
                    cfAnd.AddOperand(pObserve.GenerateGiven(sTag).Negate());
                    cfAnd.AddOperand(pObserve.GenerateGiven(sActionTag));
                    cfCondition.AddOperand(cfAnd);

                    cfCondition.AddOperand(pNotTag);

                    cfEffects.AddOperand(cfCondition);
                }
            }

            aNew.Effects = cfEffects;

            return aNew;
        }
Esempio n. 14
0
        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;
        }
Esempio n. 15
0
        public Action KnowWhetherTagCompilation(Dictionary<string, List<Predicate>> dTags, Domain d, string sActionTag)
        {
            string sName = Name + "-KW-" + sActionTag;
            ParametrizedAction aNew = new ParametrizedAction(sName);
            if (this is ParametrizedAction)
            {
                foreach (Parameter p in ((ParametrizedAction)this).Parameters)
                    aNew.AddParameter(p);
            }

            List<CompoundFormula> lConditions = new List<CompoundFormula>();
            List<Formula> lObligatory = new List<Formula>();
            SplitEffects(lConditions, lObligatory);

            aNew.Preconditions = GetKnowWhetherPreconditions(dTags, d, sActionTag);

            if (Effects != null)
            {
                HashSet<Predicate> lKnowEffects = new HashSet<Predicate>();
                CompoundFormula cfEffects = new CompoundFormula("and");
                CompoundFormula cfMandatoryEffects = new CompoundFormula("and");
                foreach (Formula f in lObligatory)
                {
                    f.GetAllPredicates(lKnowEffects);
                }
                if (lKnowEffects.Count > 0)
                {
                    foreach (string sTag in dTags.Keys)
                    {
                        //~KNot t|?t -> effects|t
                        CompoundFormula cfKEffects = new CompoundFormula("and");

                        foreach (Predicate p in lKnowEffects)
                        {
                            Predicate pAdd = p.GenerateGiven(sTag);
                            cfKEffects.AddOperand(pAdd);
                                //pAdd = p.GenerateKnowGiven(sTag, true);
                                //pAdd = p.GenerateGiven(sTag);
                                //cfKEffects.AddOperand(pAdd);

                        }

                        if (sTag == sActionTag)
                            cfEffects.SimpleAddOperand(cfKEffects);
                        else
                        {
                            CompoundFormula cfCondition = new CompoundFormula("when");
                            Predicate pNotTag = Predicate.GenerateKNot(new Constant(Domain.TAG, sTag),new Constant(Domain.TAG, sActionTag));

                            cfCondition.AddOperand(pNotTag.Negate());
                            cfCondition.AddOperand(cfKEffects);

                            cfEffects.SimpleAddOperand(cfCondition);
                        }
                    }
                }
                /*
                //forgetting: ~K~p
                foreach (Predicate p in lKnowEffects)
                {
                    Predicate pKNotp = new KnowPredicate(p.Negate());
                    cfEffects.AddOperand(pKNotp.Negate());
                }
                 * */
                foreach (CompoundFormula cfCondition in lConditions)
                {
                    CompoundFormula cfK = null, cfAnd = null;
                    HashSet<Predicate> lConditionEffects = cfCondition.Operands[1].GetAllPredicates();
                    cfAnd = new CompoundFormula("and");
                    /*
                    //since this action is done only for a part of the states, you lose all information in the effects
                    foreach (Predicate p in lConditionEffects)
                    {
                        if (p.Name != Domain.OPTION_PREDICATE)
                        {
                            Predicate pK = new KnowPredicate(p);
                            cfAnd.AddOperand(pK.Negate());
                            pK = new KnowPredicate(p.Negate());
                            cfAnd.AddOperand(pK.Negate());
                            pK = new KnowWhetherPredicate(p);
                            cfAnd.AddOperand(pK.Negate());
                        }
                    }
                    if (cfAnd.Operands.Count > 0)
                        cfEffects.SimpleAddOperand(cfAnd);
                     * */
                    foreach (string sTag in dTags.Keys)
                    {

                        cfK = CreateTaggedCondition(cfCondition, d, sTag);
                        if (cfK != null)
                        {
                            if (sTag == sActionTag)
                            {
                                cfEffects.SimpleAddOperand(cfK);
                            }
                            else
                            {
                                Predicate pNotTag = Predicate.GenerateKNot(new Constant(Domain.TAG, sTag),new Constant(Domain.TAG, sActionTag));

                                cfAnd = new CompoundFormula("and");
                                cfAnd.AddOperand(pNotTag.Negate());
                                if (cfK.Operator == "when")
                                {
                                    cfAnd.AddOperand(cfK.Operands[0]);
                                    cfK.Operands[0] = cfAnd;
                                    cfEffects.SimpleAddOperand(cfK);
                                }
                                else
                                    throw new NotImplementedException();
                            }
                        }
                    }
                    /*
                    cfK = CreateTaggedKnowledgeWhetherGainConditions(cfCondition, d, dTags.Keys, sActionTag);
                    if (cfK != null)
                    {
                        cfEffects.SimpleAddOperand(cfK);
                    }

                    cfK = CreateTaggedKnowledgeWhetherLossCondition(cfCondition, d, dTags.Keys, sActionTag);
                    if (cfK != null && cfK.Operands.Count > 0)
                    {
                        cfEffects.SimpleAddOperand(cfK);
                    }
                    */
                }
                aNew.Effects = cfEffects.Simplify();
            }
            if (Observe != null)
            {
                throw new NotImplementedException();
            }
            return aNew;
        }
Esempio n. 16
0
        public Action TagObservationTranslationNoState(Dictionary<string, List<Predicate>> dTags, Domain d, List<string> lIncludedTags, List<string> lExcludedTags)
        {
            string sName = Name;
            foreach (string sTag in lIncludedTags)
                sName += "-" + sTag;
            ParametrizedAction aNew = new ParametrizedAction(sName);
            if (this is ParametrizedAction)
            {
                foreach (Parameter p in ((ParametrizedAction)this).Parameters)
                    aNew.AddParameter(p);
            }

            if (Observe == null)
                throw new NotImplementedException();
            if (Effects != null)
                throw new NotImplementedException();
            Predicate pObserve = ((PredicateFormula)Observe).Predicate;

            aNew.Preconditions = GetPreconditionsNoState(dTags, d, lIncludedTags, lExcludedTags);
            ((CompoundFormula)aNew.Preconditions).AddOperand(new GroundedPredicate("NotInAction"));

            CompoundFormula cfEffects = new CompoundFormula("and");
            /*
            foreach (string sTag in lIncludedTags)
            {
                cfEffects.AddOperand(pObserve.GenerateKnowGiven(sTag, true));
            }
            */
            for (int i = 0; i < lIncludedTags.Count - 1; i++)
            {
                for (int j = i + 1; j < lIncludedTags.Count; j++)
                {
                    string sTag1 = lIncludedTags[i], sTag2 = lIncludedTags[j];
                    CompoundFormula cfWhen = new CompoundFormula("when");
                    CompoundFormula cfGiven = new CompoundFormula("and");
                    CompoundFormula cfEffect = new CompoundFormula("and");
                    cfGiven.AddOperand(pObserve.GenerateGiven(sTag1));
                    cfGiven.AddOperand(pObserve.GenerateGiven(sTag2).Negate());

                    Constant pTag1 = new Constant(Domain.TAG, sTag1);
                    Constant pTag2 = new Constant(Domain.TAG, sTag2);
                    Predicate ppKnowNot1Given2 = Predicate.GenerateKNot(pTag1, pTag2);
                    cfEffect.AddOperand(ppKnowNot1Given2);//no need to add the other side because all KNot will enforce t1 < t2

                    cfWhen.SimpleAddOperand(cfGiven);
                    cfWhen.SimpleAddOperand(cfEffect);

                    cfEffects.SimpleAddOperand(cfWhen);

                    cfWhen = new CompoundFormula("when");
                    cfGiven = new CompoundFormula("and");
                    cfGiven.AddOperand(pObserve.GenerateGiven(sTag1).Negate());
                    cfGiven.AddOperand(pObserve.GenerateGiven(sTag2));
                    cfWhen.SimpleAddOperand(cfGiven);
                    cfWhen.SimpleAddOperand(cfEffect);
                    cfEffects.SimpleAddOperand(cfWhen);

                }
            }

            aNew.Effects = cfEffects;

            return aNew;
        }
Esempio n. 17
0
        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;
        }