Пример #1
0
        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);
        }
Пример #2
0
        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 Predicate GenerateGiven(string sTag)
        {
            GroundedPredicate pGiven = new GroundedPredicate("Given" + Name);

            foreach (Constant c in Constants)
            {
                pGiven.AddConstant(c);
            }
            pGiven.AddConstant(new Constant(Domain.TAG, sTag));
            if (Negation)
            {
                return(pGiven.Negate());
            }
            return(pGiven);
        }
        private 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;
        }
        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 static List<string> MasterMindHeuristic(PartiallySpecifiedState pssCurrent, Domain domain)
        {
            List<Constant> lColors = new List<Constant>();
            List<Constant> lPegs = new List<Constant>();
            List<Constant> lValues = new List<Constant>();
            foreach (Constant c in domain.Constants)
            {
                if (c.Type.ToLower() == "color")
                    lColors.Add(c);
                if (c.Type.ToLower() == "peg")
                    lPegs.Add(c);
                if (c.Type.ToLower() == "value")
                    lValues.Add(c);
            }

            //DateTime dtStart = DateTime.Now;

            //BeliefState bs = pssCurrent.m_bsInitialBelief;

            //State s = bs.ChooseState(true);
            //List<Predicate> l = bs.RunSatSolver();

            //Console.WriteLine((DateTime.Now - dtStart).TotalSeconds);

            Permute(lColors);
            List<Constant> lGuessColors = new List<Constant>();
            for (int iPeg = 0; iPeg < lPegs.Count; iPeg++)
            {
                /*
                //foreach (GroundedPredicate gp in s.Predicates)
                foreach (GroundedPredicate gp in l)
                {
                    if (gp.ToString().StartsWith("(on p" + iPeg) && gp.Negation == false)
                    {
                        lGuessColors.Add(gp.Constants[1]);
                    }

                }
                */
                foreach (Constant cColor in lColors)
                {
                    GroundedPredicate gp = new GroundedPredicate("on");
                    gp.AddConstant(lPegs[iPeg]);
                    gp.AddConstant(cColor);
                    if (!pssCurrent.Observed.Contains(gp.Negate()))
                    {
                        lGuessColors.Add(cColor);
                        break;
                    }
                }
                lColors.Remove(lGuessColors.Last());

            }
            if (lGuessColors.Count == lPegs.Count)
            {
                List<string> lActions = new List<string>();
                string sGuess = "guess-all";
                foreach (Constant c in lGuessColors)
                    sGuess += " " + c.Name;
                lActions.Add(sGuess);
                lActions.Add("evaluate-guess");
                foreach (Constant cValue in lValues)
                {
                    lActions.Add("observe-LocationHits " + cValue.Name);
                    lActions.Add("observe-ColorHits " + cValue.Name);
                }
                return lActions;
            }
            return null;
        }
        private bool ConsistentWith(HashSet<GroundedPredicate> hsObservations, GroundedPredicate gpReasoned)
        {
            HashSet<Predicate> hsLearned = new HashSet<Predicate>(hsObservations);

            if (!Hidden.Contains(gpReasoned.Canonical()))
                if (Observed.Contains(gpReasoned.Negate()))
                    return false;

            List<CompoundFormula> lHidden = new List<CompoundFormula>(m_bsInitialBelief.Hidden);
            bool bDone = false;
            while (!bDone)
            {
                bDone = true;
                for (int i = 0; i < lHidden.Count; i++)
                {
                    CompoundFormula cf = lHidden[i];
                    if (cf != null)
                    {
                        Formula fReduced = cf.Reduce(hsLearned);
                        if (fReduced.IsFalse(null))
                            return false;
                        if (fReduced.IsTrue(null))
                        {
                            lHidden[i] = null;
                            continue;
                        }
                        if (fReduced is PredicateFormula)
                        {
                            Predicate p = ((PredicateFormula)fReduced).Predicate;
                            if (gpReasoned.Equals(p.Negate()))
                                return false;
                            if (hsLearned.Add(p))
                                bDone = false;
                            lHidden[i] = null;
                        }
                        else
                        {
                            CompoundFormula cfReduced = (CompoundFormula)fReduced;
                            if (cfReduced.IsSimpleConjunction())
                            {
                                HashSet<Predicate> hsPredicates = cfReduced.GetAllPredicates();
                                foreach (Predicate p in hsPredicates)
                                {
                                    if (gpReasoned.Equals(p.Negate()))
                                        return false;
                                    if (hsLearned.Add(p))
                                        bDone = false;
                                }
                                lHidden[i] = null;
                            }
                            else
                                lHidden[i] = cfReduced;
                        }
                    }
                }
            }
            bool bLearned = hsLearned.Contains(gpReasoned);
            return bLearned;
        }
 public override Predicate GenerateGiven(string sTag)
 {
     GroundedPredicate pGiven = new GroundedPredicate("Given" + Name);
     foreach (Constant c in Constants)
         pGiven.AddConstant(c);
     pGiven.AddConstant(new Constant(Domain.TAG, sTag));
     if (Negation)
         return pGiven.Negate();
     return pGiven;
 }
Пример #9
0
        public static List <string> MasterMindHeuristic(PartiallySpecifiedState pssCurrent, Domain domain)
        {
            List <Constant> lColors = new List <Constant>();
            List <Constant> lPegs   = new List <Constant>();
            List <Constant> lValues = new List <Constant>();

            foreach (Constant c in domain.Constants)
            {
                if (c.Type.ToLower() == "color")
                {
                    lColors.Add(c);
                }
                if (c.Type.ToLower() == "peg")
                {
                    lPegs.Add(c);
                }
                if (c.Type.ToLower() == "value")
                {
                    lValues.Add(c);
                }
            }

            //DateTime dtStart = DateTime.Now;

            //BeliefState bs = pssCurrent.m_bsInitialBelief;

            //State s = bs.ChooseState(true);
            //List<Predicate> l = bs.RunSatSolver();


            //Console.WriteLine((DateTime.Now - dtStart).TotalSeconds);


            Permute(lColors);
            List <Constant> lGuessColors = new List <Constant>();

            for (int iPeg = 0; iPeg < lPegs.Count; iPeg++)
            {
                /*
                 * //foreach (GroundedPredicate gp in s.Predicates)
                 * foreach (GroundedPredicate gp in l)
                 * {
                 *  if (gp.ToString().StartsWith("(on p" + iPeg) && gp.Negation == false)
                 *  {
                 *      lGuessColors.Add(gp.Constants[1]);
                 *  }
                 *
                 *
                 * }
                 */
                foreach (Constant cColor in lColors)
                {
                    GroundedPredicate gp = new GroundedPredicate("on");
                    gp.AddConstant(lPegs[iPeg]);
                    gp.AddConstant(cColor);
                    if (!pssCurrent.Observed.Contains(gp.Negate()))
                    {
                        lGuessColors.Add(cColor);
                        break;
                    }
                }
                lColors.Remove(lGuessColors.Last());
            }
            if (lGuessColors.Count == lPegs.Count)
            {
                List <string> lActions = new List <string>();
                string        sGuess   = "guess-all";
                foreach (Constant c in lGuessColors)
                {
                    sGuess += " " + c.Name;
                }
                lActions.Add(sGuess);
                lActions.Add("evaluate-guess");
                foreach (Constant cValue in lValues)
                {
                    lActions.Add("observe-LocationHits " + cValue.Name);
                    lActions.Add("observe-ColorHits " + cValue.Name);
                }
                return(lActions);
            }
            return(null);
        }
        public static List <string> MineSweeperHeuristic(PartiallySpecifiedState pssCurrent, Domain d, Formula fObserve)
        {
            int[,] aBoard;
            aBoard = new int[Size, Size];
            List <string> lActions = new List <string>();
            List <int>    lUnknown = new List <int>();

            for (int iX = 0; iX < Size; iX++)
            {
                for (int iY = 0; iY < Size; iY++)
                {
                    GroundedPredicate gp = GetPredicate("opened", iX, iY);
                    if (pssCurrent.Observed.Contains(gp))
                    {
                        aBoard[iX, iY] = 1;//opened
                    }
                    else
                    {
                        gp = GetPredicate("mine-at", iX, iY);
                        if (pssCurrent.Observed.Contains(gp))
                        {
                            gp = GetPredicate("flagged", iX, iY);
                            if (pssCurrent.Observed.Contains(gp))
                            {
                                aBoard[iX, iY] = 4;//flagged mine
                            }
                            else
                            {
                                aBoard[iX, iY] = 2;//unflagged mine
                            }
                        }
                        else if (pssCurrent.Observed.Contains(gp.Negate()))
                        {
                            aBoard[iX, iY] = 3;//no mine
                        }
                    }
                }
            }



            List <int> lCandidates = new List <int>();

            for (int iX = 0; iX < Size; iX++)
            {
                for (int iY = 0; iY < Size; iY++)
                {
                    if (aBoard[iX, iY] == 0 || aBoard[iX, iY] == 3)
                    {
                        lCandidates.Add((iX) * 1000 + iY);
                    }
                    else if (aBoard[iX, iY] == 2)
                    {
                        lActions.Add("flag p" + iX + "-" + iY);
                    }
                }
            }

            if (lCandidates.Count > 0)
            {
                int iChosen  = lCandidates[RandomGenerator.Next(lCandidates.Count)];
                int iChosenX = iChosen / 1000;
                int iChosenY = iChosen % 1000;
                lActions.Add("open-cell-" + iChosenX + "-" + iChosenY);
                //lActions.Add("open-cell p" + iChosenX + "-" + iChosenY);
                lActions.Add("observe-dead");
                for (int i = 0; i < 9; i++)
                {
                    lActions.Add("observe-mine-count p" + iChosenX + "-" + iChosenY + " v" + i);
                }
                Shootings++;
            }

            /*
             * else if (lUnknown.Count > 0)
             * {
             * int iChosen = lUnknown[RandomGenerator.Next(lUnknown.Count)];
             * int iChosenX = iChosen / 1000;
             * int iChosenY = iChosen % 1000;
             * lActions.Add("shoot p-" + iChosenX + " p-" + iChosenY);
             * Shootings++;
             * }
             */



            return(lActions);
        }
Пример #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;
        }
Пример #12
0
        public Action ReplaceNonDeterministicEffectsWithOptions(List<string> lAlwaysKnown, int cMaxOptions)
        {
            Action aNew = Clone();
            List<CompoundFormula> lConditions = new List<CompoundFormula>();
            List<Formula> lObligatory = new List<Formula>();
            SplitEffects(lConditions, lObligatory);

            //currently only handling non-determinism in conditional effects
            CompoundFormula cfEffects = new CompoundFormula("and");
            foreach (Formula f in lObligatory)
            {
                if (f is PredicateFormula)
                    cfEffects.AddOperand(f);
                else
                {
                    CompoundFormula cf = (CompoundFormula)f;
                    if (cf.Operator == "oneof" || cf.Operator == "or")
                    {
                        lConditions.Add(cf);
                    }
                    else
                        cfEffects.AddOperand(cf);

                }
            }

            foreach (CompoundFormula cfCondition in lConditions)
            {
                Formula fDeterministicCondition = null;
                if (cfCondition.ContainsNonDeterministicEffect())
                {
                    //BUGBUG - may cause problems when we have different number of options for each condition - not sure!
                    if (cMaxOptions < cfCondition.GetMaxNonDeterministicOptions())
                        cMaxOptions = cfCondition.GetMaxNonDeterministicOptions();
                    int[] aPermutation = Permutation(cMaxOptions);

                    foreach (int iOption in aPermutation)
                    {
                        fDeterministicCondition = cfCondition.ChooseOption(iOption);
                        GroundedPredicate gpOption = new GroundedPredicate("option");
                        gpOption.AddConstant(new Constant(Domain.OPTION, "opt" + iOption));
                        if (cfCondition.Operator == "when")
                        {
                            ((CompoundFormula)((CompoundFormula)fDeterministicCondition).Operands[0]).AddOperand(gpOption);
                            cfEffects.AddOperand(fDeterministicCondition);
                        }
                        else
                        {
                            CompoundFormula cfWhen = new CompoundFormula("when");
                            cfWhen.AddOperand(gpOption);
                            cfWhen.AddOperand(fDeterministicCondition);
                            cfEffects.AddOperand(cfWhen);

                        }
                    }
                }
                else
                    cfEffects.AddOperand(cfCondition);
            }
            for (int iOption = 0; iOption < cMaxOptions; iOption++)
            {
                GroundedPredicate gpCurrentOption = new GroundedPredicate("option");
                gpCurrentOption.AddConstant(new Constant(Domain.OPTION, "opt" + iOption));
                GroundedPredicate gpNextOption = new GroundedPredicate("option");
                gpNextOption.AddConstant(new Constant(Domain.OPTION, "opt" + (iOption + 1) % cMaxOptions));
                CompoundFormula cfWhen = new CompoundFormula("when");
                cfWhen.AddOperand(gpCurrentOption);
                CompoundFormula cfAnd = new CompoundFormula("and");
                cfAnd.AddOperand(gpCurrentOption.Negate());
                cfAnd.AddOperand(gpNextOption);
                cfWhen.AddOperand(cfAnd);
                cfEffects.AddOperand(cfWhen);
            }
            aNew.Effects = cfEffects;
            if (Observe != null)//assuming that there can't be any observations
                throw new NotImplementedException();
            return aNew;
        }
Пример #13
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;
        }
Пример #14
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;
        }