Started implementing derived predicates for decomposing complex PDDL conditions.

This commit is contained in:
Patrick Lühne 2016-09-09 16:42:16 +02:00
parent cc0b9726ed
commit a62cc8b3b9
16 changed files with 213 additions and 0 deletions

View File

@ -7,6 +7,7 @@
#include <plasp/pddl/Parser.h> #include <plasp/pddl/Parser.h>
#include <plasp/pddl/Requirement.h> #include <plasp/pddl/Requirement.h>
#include <plasp/pddl/expressions/Constant.h> #include <plasp/pddl/expressions/Constant.h>
#include <plasp/pddl/expressions/DerivedPredicate.h>
#include <plasp/pddl/expressions/PredicateDeclaration.h> #include <plasp/pddl/expressions/PredicateDeclaration.h>
#include <plasp/pddl/expressions/PrimitiveType.h> #include <plasp/pddl/expressions/PrimitiveType.h>
@ -49,6 +50,9 @@ class Domain
std::vector<std::unique_ptr<Action>> &actions(); std::vector<std::unique_ptr<Action>> &actions();
const std::vector<std::unique_ptr<Action>> &actions() const; const std::vector<std::unique_ptr<Action>> &actions() const;
expressions::DerivedPredicates &derivedPredicates();
const expressions::DerivedPredicates &derivedPredicates() const;
void checkConsistency(); void checkConsistency();
void normalize(); void normalize();
@ -85,6 +89,8 @@ class Domain
std::vector<utils::Stream::Position> m_actionPositions; std::vector<utils::Stream::Position> m_actionPositions;
std::vector<std::unique_ptr<Action>> m_actions; std::vector<std::unique_ptr<Action>> m_actions;
expressions::DerivedPredicates m_derivedPredicates;
}; };
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -40,6 +40,10 @@ class Constant;
using ConstantPointer = boost::intrusive_ptr<Constant>; using ConstantPointer = boost::intrusive_ptr<Constant>;
using Constants = std::vector<ConstantPointer>; using Constants = std::vector<ConstantPointer>;
class DerivedPredicate;
using DerivedPredicatePointer = boost::intrusive_ptr<DerivedPredicate>;
using DerivedPredicates = std::vector<DerivedPredicatePointer>;
class Dummy; class Dummy;
using DummyPointer = boost::intrusive_ptr<Dummy>; using DummyPointer = boost::intrusive_ptr<Dummy>;
@ -103,6 +107,7 @@ class Expression
At, At,
Binary, Binary,
Constant, Constant,
DerivedPredicate,
Dummy, Dummy,
Either, Either,
Exists, Exists,
@ -132,6 +137,7 @@ class Expression
virtual ExpressionPointer prenex(Expression::Type lastQuantifierType = Expression::Type::Exists); virtual ExpressionPointer prenex(Expression::Type lastQuantifierType = Expression::Type::Exists);
virtual ExpressionPointer simplified(); virtual ExpressionPointer simplified();
virtual ExpressionPointer disjunctionNormalized(); virtual ExpressionPointer disjunctionNormalized();
virtual ExpressionPointer decomposed(expressions::DerivedPredicates &derivedPredicates);
ExpressionPointer negated(); ExpressionPointer negated();
virtual void print(std::ostream &ostream) const = 0; virtual void print(std::ostream &ostream) const = 0;

View File

@ -25,6 +25,7 @@ class And: public NAry<And>
public: public:
ExpressionPointer disjunctionNormalized() override; ExpressionPointer disjunctionNormalized() override;
ExpressionPointer decomposed(DerivedPredicates &derivedPredicates) override;
}; };
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -0,0 +1,42 @@
#ifndef __PLASP__PDDL__EXPRESSIONS__DERIVED_PREDICATE_H
#define __PLASP__PDDL__EXPRESSIONS__DERIVED_PREDICATE_H
#include <plasp/pddl/Expression.h>
namespace plasp
{
namespace pddl
{
namespace expressions
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// DerivedPredicate
//
////////////////////////////////////////////////////////////////////////////////////////////////////
class DerivedPredicate: public ExpressionCRTP<DerivedPredicate>
{
public:
static const Expression::Type ExpressionType = Expression::Type::DerivedPredicate;
// TODO: consider implementing parsing functions for compatibility with older PDDL versions
public:
void setArgument(ExpressionPointer argument);
ExpressionPointer argument() const;
void print(std::ostream &ostream) const override;
private:
ExpressionPointer m_argument;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
}
#endif

View File

@ -22,6 +22,9 @@ class Exists: public QuantifiedCRTP<Exists>
static const Expression::Type ExpressionType = Expression::Type::Exists; static const Expression::Type ExpressionType = Expression::Type::Exists;
static const std::string Identifier; static const std::string Identifier;
public:
ExpressionPointer decomposed(DerivedPredicates &derivedPredicates) override;
}; };
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -22,6 +22,9 @@ class ForAll: public QuantifiedCRTP<ForAll>
static const Expression::Type ExpressionType = Expression::Type::ForAll; static const Expression::Type ExpressionType = Expression::Type::ForAll;
static const std::string Identifier; static const std::string Identifier;
public:
ExpressionPointer decomposed(DerivedPredicates &derivedPredicates) override;
}; };
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -39,6 +39,7 @@ class Not: public ExpressionCRTP<Not>
ExpressionPointer prenex(Expression::Type lastExpressionType) override; ExpressionPointer prenex(Expression::Type lastExpressionType) override;
ExpressionPointer simplified() override; ExpressionPointer simplified() override;
ExpressionPointer disjunctionNormalized() override; ExpressionPointer disjunctionNormalized() override;
ExpressionPointer decomposed(DerivedPredicates &derivedPredicates) override;
void print(std::ostream &ostream) const override; void print(std::ostream &ostream) const override;

View File

@ -22,6 +22,9 @@ class Or: public NAry<Or>
static const Expression::Type ExpressionType = Expression::Type::Or; static const Expression::Type ExpressionType = Expression::Type::Or;
static const std::string Identifier; static const std::string Identifier;
public:
ExpressionPointer decomposed(DerivedPredicates &derivedPredicates) override;
}; };
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -232,6 +232,20 @@ const std::vector<std::unique_ptr<Action>> &Domain::actions() const
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
expressions::DerivedPredicates &Domain::derivedPredicates()
{
return m_derivedPredicates;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
const expressions::DerivedPredicates &Domain::derivedPredicates() const
{
return m_derivedPredicates;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void Domain::parseRequirementSection() void Domain::parseRequirementSection()
{ {
auto &parser = m_context.parser; auto &parser = m_context.parser;

View File

@ -15,6 +15,7 @@
#include <plasp/pddl/expressions/Unsupported.h> #include <plasp/pddl/expressions/Unsupported.h>
#include <plasp/pddl/expressions/When.h> #include <plasp/pddl/expressions/When.h>
#include <plasp/utils/ParserException.h> #include <plasp/utils/ParserException.h>
#include <plasp/utils/TranslatorException.h>
namespace plasp namespace plasp
{ {
@ -112,6 +113,13 @@ ExpressionPointer Expression::disjunctionNormalized()
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
ExpressionPointer Expression::decomposed(expressions::DerivedPredicates &)
{
throw utils::TranslatorException("Expression is not in first-order negation normal form and cannot be decomposed");
}
////////////////////////////////////////////////////////////////////////////////////////////////////
ExpressionPointer Expression::negated() ExpressionPointer Expression::negated()
{ {
if (expressionType() == Type::Not) if (expressionType() == Type::Not)

View File

@ -4,6 +4,7 @@
#include <iostream> #include <iostream>
#include <plasp/pddl/expressions/Or.h> #include <plasp/pddl/expressions/Or.h>
#include <plasp/utils/TranslatorException.h>
namespace plasp namespace plasp
{ {
@ -67,6 +68,29 @@ ExpressionPointer And::disjunctionNormalized()
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
ExpressionPointer And::decomposed(DerivedPredicates &derivedPredicates)
{
// Check that all children are simple or negated predicates
std::for_each(m_arguments.begin(), m_arguments.end(),
[&](auto &argument)
{
if (argument->expressionType() == Expression::Type::Not)
{
argument = argument->decomposed(derivedPredicates);
return;
}
if (argument->expressionType() != Expression::Type::Predicate)
return;
throw utils::TranslatorException("Expression is not in first-order negation normal form and cannot be decomposed");
});
return this;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
} }
} }
} }

View File

@ -0,0 +1,44 @@
#include <plasp/pddl/expressions/DerivedPredicate.h>
#include <plasp/pddl/Context.h>
#include <plasp/pddl/ExpressionContext.h>
namespace plasp
{
namespace pddl
{
namespace expressions
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// DerivedPredicate
//
////////////////////////////////////////////////////////////////////////////////////////////////////
void DerivedPredicate::setArgument(ExpressionPointer argument)
{
m_argument = argument;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
ExpressionPointer DerivedPredicate::argument() const
{
return m_argument;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void DerivedPredicate::print(std::ostream &ostream) const
{
ostream << "(:derived <no name> ";
m_argument->print(ostream);
ostream << ")";
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
}

View File

@ -20,6 +20,15 @@ const std::string Exists::Identifier = "exists";
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
ExpressionPointer Exists::decomposed(DerivedPredicates &derivedPredicates)
{
m_argument = m_argument->decomposed(derivedPredicates);
return this;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
} }
} }
} }

View File

@ -3,6 +3,8 @@
#include <algorithm> #include <algorithm>
#include <iostream> #include <iostream>
#include <plasp/pddl/expressions/DerivedPredicate.h>
namespace plasp namespace plasp
{ {
namespace pddl namespace pddl
@ -20,6 +22,18 @@ const std::string ForAll::Identifier = "forall";
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
ExpressionPointer ForAll::decomposed(DerivedPredicates &derivedPredicates)
{
auto derivedPredicate = DerivedPredicatePointer(new DerivedPredicate);
derivedPredicates.push_back(derivedPredicate);
derivedPredicate->setArgument(this);
return derivedPredicate;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
} }
} }
} }

View File

@ -4,6 +4,7 @@
#include <plasp/pddl/expressions/Exists.h> #include <plasp/pddl/expressions/Exists.h>
#include <plasp/pddl/expressions/ForAll.h> #include <plasp/pddl/expressions/ForAll.h>
#include <plasp/pddl/expressions/Or.h> #include <plasp/pddl/expressions/Or.h>
#include <plasp/utils/TranslatorException.h>
namespace plasp namespace plasp
{ {
@ -183,6 +184,19 @@ void Not::print(std::ostream &ostream) const
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
ExpressionPointer Not::decomposed(DerivedPredicates &)
{
if (m_argument->expressionType() != Expression::Type::Not
&& m_argument->expressionType() != Expression::Type::Predicate)
{
throw utils::TranslatorException("Expression is not in first-order negation normal form and cannot be decomposed");
}
return this;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
} }
} }
} }

View File

@ -1,5 +1,7 @@
#include <plasp/pddl/expressions/Or.h> #include <plasp/pddl/expressions/Or.h>
#include <plasp/pddl/expressions/DerivedPredicate.h>
namespace plasp namespace plasp
{ {
namespace pddl namespace pddl
@ -17,6 +19,25 @@ const std::string Or::Identifier = "or";
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
ExpressionPointer Or::decomposed(DerivedPredicates &derivedPredicates)
{
// Check that all children are simple or negated predicates
std::for_each(m_arguments.begin(), m_arguments.end(),
[&](auto &argument)
{
argument = argument->decomposed(derivedPredicates);
});
auto derivedPredicate = DerivedPredicatePointer(new DerivedPredicate);
derivedPredicates.push_back(derivedPredicate);
derivedPredicate->setArgument(this);
return derivedPredicate;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
} }
} }
} }