private Formula ReadFormula(CompoundExpression exp, Dictionary <string, string> dParameterNameToType, bool bParamterized, Domain d)
        {
            bool bPredicate = true;

            //Debug.WriteLine(exp);
            if (d != null && d.IsFunctionExpression(exp.Type))
            {
                Predicate p = ReadFunctionExpression(exp, dParameterNameToType, d);
                return(new PredicateFormula(p));
            }
            else if (IsUniversalQuantifier(exp))
            {
                CompoundExpression eParameter = (CompoundExpression)exp.SubExpressions[0];
                CompoundExpression eBody      = (CompoundExpression)exp.SubExpressions[1];
                string             sParameter = eParameter.Type;
                string             sType      = eParameter.SubExpressions[1].ToString();
                dParameterNameToType[sParameter] = sType;
                ParametrizedFormula cfQuantified = new ParametrizedFormula(exp.Type);
                cfQuantified.Parameters[sParameter] = sType;
                Formula fBody = ReadFormula(eBody, dParameterNameToType, true, d);
                cfQuantified.AddOperand(fBody);
                return(cfQuantified);
            }
            else
            {
                foreach (Expression eSub in exp.SubExpressions)
                {
                    if (eSub is CompoundExpression)
                    {
                        bPredicate = false;
                        break;
                    }
                }
                if (bPredicate)
                {
                    return(ReadPredicate(exp, dParameterNameToType, bParamterized, d));
                }
                else
                {
                    CompoundFormula cf          = new CompoundFormula(exp.Type);
                    int             iExpression = 0;
                    for (iExpression = 0; iExpression < exp.SubExpressions.Count; iExpression++)
                    {
                        if (exp.SubExpressions[iExpression] is StringExpression)
                        {
                            throw new InvalidDataException();
                        }
                        Formula f = ReadFormula((CompoundExpression)exp.SubExpressions[iExpression], dParameterNameToType, bParamterized, d);
                        cf.SimpleAddOperand(f);
                    }
                    if (cf.Operator == "not" && cf.Operands[0] is PredicateFormula)
                    {
                        PredicateFormula fNegate = new PredicateFormula(((PredicateFormula)cf.Operands[0]).Predicate.Negate());
                        return(fNegate);
                    }
                    return(cf);
                }
            }
        }
Example #2
0
        private void ReadInitState(Problem p, Domain d, CompoundExpression eInitState)
        {
            foreach (Expression e in eInitState.SubExpressions)
            {
                CompoundExpression eSub = (CompoundExpression)e;
                if (d.IsFunctionExpression(eSub.Type))
                {
                    p.AddKnown(ReadFunctionExpression(eSub, null, d));
                }
                else if (d.ContainsPredicate(eSub.Type))
                {
                    p.AddKnown(ReadGroundedPredicate(eSub, d));
                }
                else
                {
                    if (eSub.Type != "unknown")
                    {
                        Formula f = ReadGroundedFormula(eSub, d);
                        if (f is CompoundFormula)
                        {
                            p.AddHidden((CompoundFormula)f);
                        }
                        if (f is PredicateFormula)//this happens in (not (p)) statments
                        {
                            p.AddKnown(((PredicateFormula)f).Predicate);
                        }
                    }
                    else
                    {
                        //causing a problem - add operand does some basic reasoning - adding p and ~p results in true for or statements.
                        //skipping unknown for now...

                        Predicate       pUnknown = ReadGroundedPredicate((CompoundExpression)eSub.SubExpressions[0], d);
                        CompoundFormula cfOr     = new CompoundFormula("or");
                        cfOr.SimpleAddOperand(pUnknown);
                        cfOr.SimpleAddOperand(pUnknown.Negate());
                        p.AddHidden(cfOr);
                    }
                }
            }
            p.ComputeRelevanceClosure();
        }
Example #3
0
        private void ConvertToSingleAgentProblem()
        {
            /*foreach (var otherAgent in agents)
             * {
             *  if (otherAgent.Name != agent.Name)
             *  {
             *      RemoveAgentFromDomain(domain, otherAgent);
             *      //problem.RemoveConstant(otherAgent);
             *  }
             * }*/

            // Currently A1 plays
            GroundedPredicate activeAgentPredicate = new GroundedPredicate("active-agent");

            activeAgentPredicate.AddConstant(new Constant(m_AgentDomain.AgentCallsign, m_ActiveAgent.Name));
            m_AgentProblem.AddKnown(activeAgentPredicate);


            ParameterizedPredicate activeAgentParamPredicate = new ParameterizedPredicate("active-agent");
            Parameter pIsAgent = new Parameter(m_AgentDomain.AgentCallsign, "?a");

            activeAgentParamPredicate.AddParameter(pIsAgent);
            m_AgentDomain.AddPredicate(activeAgentParamPredicate);

            ParameterizedPredicate activeAgentParamPredicateJoint = new ParameterizedPredicate("active-agent");
            Parameter pIsAgentJoint = new Parameter(m_AgentDomain.AgentCallsign, "?a1");

            activeAgentParamPredicateJoint.AddParameter(pIsAgentJoint);


            foreach (var action in m_AgentDomain.Actions)
            {
                Action originalAction = action.Clone();

                if (action.Preconditions.ContainsParameter(pIsAgent) || action.Preconditions.ContainsParameter(pIsAgentJoint))
                {
                    if (action.Preconditions.CountAgents(m_AgentDomain.AgentCallsign) > 1)
                    {
                        // Joint Action
                        ParameterizedPredicate paramPredicateAgentAt = new ParameterizedPredicate("agent-at");
                        paramPredicateAgentAt.AddParameter(new Parameter(m_AgentDomain.AgentCallsign, "?a2"));
                        paramPredicateAgentAt.AddParameter(new Parameter("pos", "?start"));
                        action.Preconditions.RemovePredicate(paramPredicateAgentAt);


                        // Joint Action Disaster site
                        ParameterizedPredicate paramPredicateIn = new ParameterizedPredicate("in");
                        paramPredicateIn.AddParameter(new Parameter(m_AgentDomain.AgentCallsign, "?a2"));
                        paramPredicateIn.AddParameter(new Parameter("location", "?r1"));
                        action.Preconditions.RemovePredicate(paramPredicateIn);

                        action.Preconditions.AddPredicate(activeAgentParamPredicateJoint);
                    }
                    else
                    {
                        CompoundFormula newcf = new CompoundFormula("and");
                        newcf.SimpleAddOperand(action.Preconditions);
                        newcf.SimpleAddOperand(activeAgentParamPredicate);
                        action.Preconditions = newcf;
                    }
                }
                action.OriginalActionBeforeRemovingAgent = originalAction;
            }
        }
Example #4
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);
                }
            }
        }