Example #1
0
        public void Test4()
        {
            ObjectVariableTerm x = new ObjectVariable('x');

            IndividualConstantTerm <RationalNumber> half =
                (IndividualConstant <RationalNumber>)(new RationalNumber(1, 2));

            IndividualConstantTerm <RationalNumber>
            one = (IndividualConstant <RationalNumber>)(new RationalNumber(1, 1));

            var pr1 = new PredicateFormula(Predicates.More, x, half);
            var pr2 = new PredicateFormula(Predicates.Less, x, one);

            var f1 = new PropositionalConnectiveFormula(Conjunction.GetInstance(), pr1, pr2);
            var f2 = new QuantifierFormula(ExistentialQuantifier.GetInstance(), x, f1);


            IndividualConstantTerm <int> two = new IndividualConstant <int>(2);
            var xSqr = new FunctionTerm(Functions.Pow, x, two);

            var pr3 = new PredicateFormula(Predicates.MoreZero, xSqr);
            var pr4 = new PredicateFormula(Predicates.EqualZero, x);

            var f3 = new PropositionalConnectiveFormula(Disjunction.GetInstance(), pr3, pr4);

            var f      = new PropositionalConnectiveFormula(Conjunction.GetInstance(), f2, f3);
            var actual = SimpleTarskiAlgorithm.QuantifiersElimination(f);

            Formula expected  = f3;
            Formula expected1 = new PropositionalConnectiveFormula(Disjunction.GetInstance(), pr4, pr3);

            Assert.IsTrue(expected.Equals(actual) || expected1.Equals(actual));
        }
        private Action GetRevealedDummyDependencyAction(Agent agent)
        {
            Constant a           = mapAgentToConstant[agent];
            Action   dummyAction = new Action("reveal-dummy-dependency_" + a.Name);

            CompoundFormula preconditions = new CompoundFormula("and");


            GroundedPredicate revealedSomething = new GroundedPredicate("revealed-something");

            revealedSomething.AddConstant(a);

            Predicate notRevealedSomething = revealedSomething.Negate();

            preconditions.AddOperand(notRevealedSomething);
            PredicateFormula effects = new PredicateFormula(revealedSomething);

            foreach (Dependency dependency in agentsDependencies[agent])
            {
                GroundedPredicate revealedDependency = new GroundedPredicate("revealed");
                Constant          d = mapDependencyToConstant[dependency];
                revealedDependency.AddConstant(d);

                preconditions.AddOperand(revealedDependency);
            }

            dummyAction.Preconditions = preconditions;
            dummyAction.SetEffects(effects);

            return(dummyAction);
        }
Example #3
0
        private static void CalcPredicate(Predicate predicate, Stack <object> stack)
        {
            var args = new Term[predicate.Arity];

            if (stack.Count < predicate.Arity)
            {
                throw new ArgumentException(
                          $"expected {predicate.Arity} terms for the predicate {predicate}");
            }
            try
            {
                for (var i = args.Length - 1; i >= 0; i--)
                {
                    args[i] = (Term)stack.Pop();
                }
                var newFormula = new PredicateFormula(predicate, args);

                stack.Push(newFormula);
            }
            catch (Exception)
            {
                throw new ArgumentException(
                          $"expected {predicate.Arity} terms for the predicate {predicate}");
            }
        }
Example #4
0
        private List <IMAP.Action> ScanEffectsForConst(Predicate goal)
        {
            List <IMAP.Action> ans = new List <IMAP.Action>();

            if (Action != null)
            {
                string actionName = Action.Name.Split('_')[0];
                if (!actionName.StartsWith("wait-goal"))
                {
                    if (Action.Effects != null)
                    {
                        Formula fEffects = Action.Effects;
                        if (fEffects is CompoundFormula)
                        {
                            CompoundFormula cf = (CompoundFormula)fEffects;
                            foreach (Formula formula in cf.Operands)
                            {
                                if (formula is PredicateFormula)
                                {
                                    PredicateFormula pf = (PredicateFormula)formula;
                                    if (pf.Predicate.ToString() == goal.ToString())
                                    {
                                        ans.Add(Action);
                                    }
                                }
                            }
                        }
                        else if (fEffects is PredicateFormula)
                        {
                            PredicateFormula pf = (PredicateFormula)fEffects;
                            if (pf.Predicate.ToString() == goal.ToString())
                            {
                                ans.Add(Action);
                            }
                        }
                        else
                        {
                            throw new Exception();
                        }
                    }
                }
            }
            if (SingleChild != null)
            {
                ans.AddRange(SingleChild.ScanEffectsForConst(goal));
            }
            if (FalseObservationChild != null)
            {
                ans.AddRange(FalseObservationChild.ScanEffectsForConst(goal));
            }
            if (TrueObservationChild != null)
            {
                ans.AddRange(TrueObservationChild.ScanEffectsForConst(goal));
            }
            return(ans);
        }
Example #5
0
        public void ZeroEqualZero()
        {
            ObjectVariableTerm x = new ObjectVariable('x');

            IndividualConstantTerm <int> zero = (IndividualConstant <int>) 0;

            var pr = new PredicateFormula(Predicates.Equal, zero, zero);

            var f = new QuantifierFormula(UniversalQuantifier.GetInstance(), x, pr);

            var actual = SimpleTarskiAlgorithm.QuantifiersElimination(f);

            var expected = new PredicateFormula(True.GetInstance());

            Assert.AreEqual(expected, actual);
        }
Example #6
0
        public void Test1()
        {
            ObjectVariableTerm x = new ObjectVariable('x');

            IndividualConstantTerm <RationalNumber> half =
                (IndividualConstant <RationalNumber>)(new RationalNumber(1, 2));

            var formula1 = new PredicateFormula(Predicates.More, x, half);

            var formula2 = new QuantifierFormula(ExistentialQuantifier.GetInstance(), x, formula1);

            var res = SimpleTarskiAlgorithm.QuantifiersElimination(formula2);

            var expected = new PredicateFormula(True.GetInstance());

            Assert.AreEqual(expected, res);
        }
Example #7
0
        public static (Polynomial, Sign) ToPolynomialAndSign(PredicateFormula predicateFormula,
                                                             VariableName variableName)
        {
            var predicate = predicateFormula.Predicate;
            var terms     = predicateFormula.Terms.ToArray();

            if (!Predicates.Interpretations.ContainsKey(predicate))
            {
                throw new NotSupportedException("unknown predicate");
            }

            var sign = Predicates.Interpretations[predicate];

            var polynomial = predicate.Arity switch
            {
                2 => ToPolynomial(terms[0], variableName) - ToPolynomial(terms[1], variableName),
                1 => ToPolynomial(terms[0], variableName),
                _ => throw new NotSupportedException("do not support predicate with arity more then 2")
            };

            return(polynomial, sign);
        }
Example #8
0
        private Action GetStartGoingSoloParameterizedAction()
        {
            ParametrizedAction startGoingSoloParameterized = new ParametrizedAction("start-going-solo");
            Parameter          agentParameter = GetAgentParameter();

            startGoingSoloParameterized.AddParameter(agentParameter);
            ParametrizedPredicate inJoinedStage = GetParameterizedInJoinedStagePredicate();
            ParametrizedPredicate goingSolo     = GetParameterizedGoingSoloPredicate();

            Predicate notInJoinedStage = inJoinedStage.Negate();

            PredicateFormula preconditions = new PredicateFormula(inJoinedStage);
            CompoundFormula  effects       = new CompoundFormula("and");

            effects.AddOperand(goingSolo);
            effects.AddOperand(notInJoinedStage);

            startGoingSoloParameterized.Preconditions = preconditions;
            startGoingSoloParameterized.SetEffects(effects);

            return(startGoingSoloParameterized);
        }
Example #9
0
 public bool ConsistentWith(Formula f)
 {
     if (f is CompoundFormula)
     {
         CompoundFormula cf          = (CompoundFormula)f;
         bool            bConsistent = false;
         foreach (Formula fOperand in cf.Operands)
         {
             bConsistent = ConsistentWith(fOperand);
             if (cf.Operator == "and" && !bConsistent)
             {
                 return(false);
             }
             if (cf.Operator == "or" && bConsistent)
             {
                 return(true);
             }
             if (cf.Operator == "not")
             {
                 return(!bConsistent);
             }
         }
         if (cf.Operator == "and")
         {
             return(true);
         }
         if (cf.Operator == "or")
         {
             return(false);
         }
     }
     else
     {
         PredicateFormula vf = (PredicateFormula)f;
         return(ConsistentWith(vf.Predicate));
     }
     return(false);
 }
Example #10
0
        public void Test2()
        {
            ObjectVariableTerm x = new ObjectVariable('x');

            IndividualConstantTerm <RationalNumber> half =
                (IndividualConstant <RationalNumber>)(new RationalNumber(1, 2));

            IndividualConstantTerm <RationalNumber> one = (IndividualConstant <RationalNumber>) new RationalNumber(1, 1);

            var pr1 = new PredicateFormula(Predicates.More, x, half);

            var pr2 = new PredicateFormula(Predicates.Less, x, one);

            var f1 = new PropositionalConnectiveFormula(Conjunction.GetInstance(), pr1, pr2);

            var f2 = new QuantifierFormula(ExistentialQuantifier.GetInstance(), x, f1);

            var actual = SimpleTarskiAlgorithm.QuantifiersElimination(f2);

            var expected = new PredicateFormula(True.GetInstance());

            Assert.AreEqual(expected, actual);
        }
Example #11
0
        private static Formula ReadPredicate(CompoundExpression exp, Dictionary <string, string> dParameterNameToType, bool bParametrized, Domain d)
        {
            Predicate p           = null;
            int       iExpression = 0;
            string    sName       = "";

            if (bParametrized)
            {
                p = new ParameterizedPredicate(exp.Type);
            }
            else
            {
                p = new GroundedPredicate(exp.Type);
            }
            bool bAllConstants = true;

            for (iExpression = 0; iExpression < exp.SubExpressions.Count; iExpression++)
            {
                sName = exp.SubExpressions[iExpression].ToString();
                if (bParametrized)
                {
                    Argument a = null;
                    if (sName.StartsWith("?"))
                    {
                        // if (!dParameterNameToType.ContainsKey(sName))
                        //     dParameterNameToType.Add(sName,)
                        a             = new Parameter(dParameterNameToType[sName], sName);
                        bAllConstants = false;
                    }
                    else
                    {
                        if (!d.ConstantNameToType.ContainsKey(sName))
                        {
                            d.ConstantNameToType.Add(sName, dParameterNameToType[sName]);
                            //throw new Exception("Predicate " + sName + " undefined");// SAGI co
                        }
                        a = new Constant(d.ConstantNameToType[sName], sName);
                    }
                    ((ParameterizedPredicate)p).AddParameter(a);
                }
                else
                {
                    try
                    {
                        Constant c = new Constant(d.ConstantNameToType[sName], sName);
                        ((GroundedPredicate)p).AddConstant(c);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine();
                    }
                }
            }
            if (bParametrized)
            {
                if (!MatchParametersToPredicateDeclaration((ParameterizedPredicate)p, d))
                {
                    throw new NotImplementedException();
                }
            }

            if (bParametrized && bAllConstants)
            {
                GroundedPredicate gp = new GroundedPredicate(p.Name);
                foreach (Constant c in ((ParameterizedPredicate)p).Parameters)
                {
                    gp.AddConstant(c);
                }
                p = gp;
            }


            PredicateFormula vf = new PredicateFormula(p);

            return(vf);
        }
Example #12
0
        private static Formula ReadFormula(CompoundExpression exp, Dictionary <string, string> dParameterNameToType, bool bParamterized, Domain d)
        {
            bool bPredicate = true;

            //Console.WriteLine(exp);
            if (d != null && d.IsFunctionExpression(exp.Type))
            {
                Predicate p = ReadFunctionExpression(exp, dParameterNameToType, d);
                return(new PredicateFormula(p));
            }
            else if (IsUniversalQuantifier(exp))
            {
                CompoundExpression eParameter = (CompoundExpression)exp.SubExpressions[0];
                CompoundExpression eBody      = (CompoundExpression)exp.SubExpressions[1];
                string             sParameter = eParameter.Type;
                string             sType      = eParameter.SubExpressions[1].ToString();
                dParameterNameToType[sParameter] = sType;
                ParametrizedFormula cfQuantified = new ParametrizedFormula(exp.Type);
                cfQuantified.Parameters[sParameter] = sType;
                Formula fBody = ReadFormula(eBody, dParameterNameToType, true, d);
                cfQuantified.AddOperand(fBody);
                return(cfQuantified);
            }
            else if (exp.Type == "probabilistic")
            {
                ProbabilisticFormula pf = new ProbabilisticFormula();
                int iExpression         = 0;
                for (iExpression = 0; iExpression < exp.SubExpressions.Count; iExpression += 2)
                {
                    //if (exp.SubExpressions[iExpression] is StringExpression)
                    //    throw new InvalidDataException();
                    string sProb = exp.SubExpressions[iExpression].ToString();
                    double dProb = 0.0;
                    if (sProb.Contains("/"))
                    {
                        string[] a = sProb.Split('/');
                        dProb = double.Parse(a[0]) / double.Parse(a[1]);
                    }
                    else
                    {
                        dProb = double.Parse(sProb);
                    }
                    Formula f = ReadFormula((CompoundExpression)exp.SubExpressions[iExpression + 1], dParameterNameToType, bParamterized, d);
                    pf.AddOption(f, dProb);
                }
                return(pf);
            }
            else
            {
                foreach (Expression eSub in exp.SubExpressions)
                {
                    if (eSub is CompoundExpression)
                    {
                        bPredicate = false;
                        break;
                    }
                }
                if (bPredicate)
                {
                    return(ReadPredicate(exp, dParameterNameToType, bParamterized, d));
                }
                else
                {
                    CompoundFormula cf          = new CompoundFormula(exp.Type);
                    int             iExpression = 0;
                    for (iExpression = 0; iExpression < exp.SubExpressions.Count; iExpression++)
                    {
                        if (exp.SubExpressions[iExpression] is StringExpression)
                        {
                            throw new InvalidDataException();
                        }
                        Formula f = ReadFormula((CompoundExpression)exp.SubExpressions[iExpression], dParameterNameToType, bParamterized, d);
                        cf.SimpleAddOperand(f);
                    }
                    if (cf.Operator == "not" && cf.Operands[0] is PredicateFormula)
                    {
                        PredicateFormula fNegate = new PredicateFormula(((PredicateFormula)cf.Operands[0]).Predicate.Negate());
                        return(fNegate);
                    }
                    return(cf);
                }
            }
        }