Implemented preconditions for derived predicates.

This commit is contained in:
Patrick Lühne 2016-12-08 00:52:09 +01:00
parent 4172d320e4
commit 6355921e59
Signed by: patrick
GPG Key ID: 05F3611E97A70ABF
10 changed files with 263 additions and 124 deletions

View File

@ -26,6 +26,10 @@ class DerivedPredicate: public ExpressionCRTP<DerivedPredicate>
// TODO: consider implementing parsing functions for compatibility with older PDDL versions
public:
explicit DerivedPredicate(size_t id);
size_t id() const;
void setPreconditions(std::vector<Expressions> &&preconditions);
const std::vector<Expressions> &preconditions() const;
@ -38,6 +42,8 @@ class DerivedPredicate: public ExpressionCRTP<DerivedPredicate>
private:
void collectParameters();
size_t m_id;
// The arguments are interpreted as a disjunction of conjunctions
std::vector<Expressions> m_preconditions;

View File

@ -197,7 +197,7 @@ inline ExpressionPointer QuantifiedCRTP<Derived>::simplified()
template<class Derived>
inline ExpressionPointer QuantifiedCRTP<Derived>::decomposed(DerivedPredicates &derivedPredicates)
{
derivedPredicates.emplace_back(new DerivedPredicate());
derivedPredicates.emplace_back(new DerivedPredicate(derivedPredicates.size()));
auto &derivedPredicate = derivedPredicates.back();
m_argument = m_argument->decomposed(derivedPredicates);

View File

@ -0,0 +1,75 @@
#ifndef __PLASP__PDDL__TRANSLATION__PRECONDITION_H
#define __PLASP__PDDL__TRANSLATION__PRECONDITION_H
#include <plasp/output/Formatting.h>
#include <plasp/pddl/Description.h>
#include <plasp/pddl/translation/Primitives.h>
namespace plasp
{
namespace pddl
{
namespace translation
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Precondition
//
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class PrintObjectName>
void translatePreconditionDisjunction(output::ColorStream &outputStream, const std::string &objectType, PrintObjectName printObjectName, const std::vector<Expressions> &preconditionDisjunction);
template<class PrintObjectName>
void translatePreconditionConjunction(output::ColorStream &outputStream, const std::string &objectType, PrintObjectName printObjectName, size_t disjunctionID, const Expressions &preconditionConjunction);
template<class PrintObjectName>
void translatePrecondition(output::ColorStream &outputStream, const std::string &objectType, PrintObjectName printObjectName, size_t disjunctionID, const Expression &precondition);
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class PrintObjectName>
inline void translatePreconditionDisjunction(output::ColorStream &outputStream, const std::string &objectType, PrintObjectName printObjectName, const std::vector<Expressions> &preconditionDisjunction)
{
for (size_t i = 0; i < preconditionDisjunction.size(); i++)
translatePreconditionConjunction(outputStream, objectType, printObjectName, i, preconditionDisjunction[i]);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class PrintObjectName>
inline void translatePreconditionConjunction(output::ColorStream &outputStream, const std::string &objectType, PrintObjectName printObjectName, size_t disjunctionID, const Expressions &preconditionConjunction)
{
for (size_t i = 0; i < preconditionConjunction.size(); i++)
translatePrecondition(outputStream, objectType, printObjectName, disjunctionID, *preconditionConjunction[i]);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class PrintObjectName>
inline void translatePrecondition(output::ColorStream &outputStream, const std::string &objectType, PrintObjectName printObjectName, size_t disjunctionID, const Expression &precondition)
{
outputStream << std::endl << output::Function("precondition") << "(";
printObjectName(outputStream);
outputStream
<< ", " << output::Keyword("disjunct")
<< "(" << output::Number<decltype(disjunctionID)>(disjunctionID)
<< "), ";
translateLiteral(outputStream, precondition);
outputStream << ") :- " << output::Function(objectType.c_str()) << "(";
printObjectName(outputStream);
outputStream << ").";
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
}
#endif

View File

@ -0,0 +1,117 @@
#ifndef __PLASP__PDDL__TRANSLATION__PRIMITIVES_H
#define __PLASP__PDDL__TRANSLATION__PRIMITIVES_H
#include <plasp/output/Formatting.h>
#include <plasp/output/TranslatorException.h>
#include <plasp/pddl/Description.h>
#include <plasp/pddl/expressions/Not.h>
#include <plasp/pddl/expressions/Predicate.h>
namespace plasp
{
namespace pddl
{
namespace translation
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Primitives
//
////////////////////////////////////////////////////////////////////////////////////////////////////
void translatePredicate(output::ColorStream &outputStream, const expressions::Predicate &predicate);
void translateLiteral(output::ColorStream &outputStream, const Expression &literal);
////////////////////////////////////////////////////////////////////////////////////////////////////
inline void translatePredicate(output::ColorStream &outputStream, const expressions::Predicate &predicate)
{
const auto &arguments = predicate.arguments();
if (arguments.empty())
{
outputStream << output::String(predicate.name().c_str());
return;
}
outputStream << "(" << output::String(predicate.name().c_str());
for (const auto &argument : arguments)
{
outputStream << ", ";
if (argument->is<expressions::Constant>())
{
const auto &constant = argument->as<expressions::Constant>();
outputStream << output::Keyword("constant") << "(" << output::String(constant.name().c_str()) << ")";
}
else if (argument->is<expressions::Variable>())
{
const auto &variable = argument->as<expressions::Variable>();
outputStream << output::Variable(variable.name().c_str());
}
else
throw output::TranslatorException("only variables and constants supported in predicates currently");
}
outputStream << ")";
}
////////////////////////////////////////////////////////////////////////////////////////////////////
inline void translateLiteral(output::ColorStream &outputStream, const Expression &literal)
{
// Translate single predicate
if (literal.is<expressions::Predicate>())
{
const auto &predicate = literal.as<expressions::Predicate>();
outputStream << output::Keyword("variable") << "(";
translation::translatePredicate(outputStream, predicate);
outputStream << "), " << output::Keyword("value") << "(";
translation::translatePredicate(outputStream, predicate);
outputStream << ", " << output::Boolean("true") << ")";
}
// Assuming that "not" expression may only contain a predicate
else if (literal.is<expressions::Not>())
{
const auto &notExpression = literal.as<expressions::Not>();
if (notExpression.argument()->expressionType() != Expression::Type::Predicate)
throw output::TranslatorException("only negations of primitive predicates supported as literals currently");
const auto &predicate = notExpression.argument()->as<expressions::Predicate>();
outputStream << output::Keyword("variable") << "(";
translation::translatePredicate(outputStream, predicate);
outputStream << "), " << output::Keyword("value") << "(";
translation::translatePredicate(outputStream, predicate);
outputStream << ", " << output::Boolean("false") << ")";
}
else if (literal.is<expressions::DerivedPredicate>())
{
const auto &derivedPredicate = literal.as<expressions::DerivedPredicate>();
/*m_outputStream << output::Keyword("variable") << "(";
translation::translatePredicate(predicate);
m_outputStream << "), " << output::Keyword("value") << "(";
translation::translatePredicate(predicate);
m_outputStream << ", " << output::Boolean("true") << ")";*/
outputStream << "(derived predicate)";
}
else
throw output::TranslatorException("only primitive predicates and their negations supported as literals currently");
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
}
#endif

View File

@ -9,6 +9,9 @@ file(GLOB pddl_headers "../include/plasp/pddl/*.h")
file(GLOB pddl_expressions_sources "plasp/pddl/expressions/*.cpp")
file(GLOB pddl_expressions_headers "../include/plasp/pddl/expressions/*.h")
file(GLOB pddl_translation_sources "plasp/pddl/translation/*.cpp")
file(GLOB pddl_translation_headers "../include/plasp/pddl/translation/*.h")
file(GLOB sas_sources "plasp/sas/*.cpp")
file(GLOB sas_headers "../include/plasp/sas/*.h")
@ -36,6 +39,9 @@ set(sources
${pddl_expressions_sources}
${pddl_expressions_headers}
${pddl_translation_sources}
${pddl_translation_headers}
${sas_sources}
${sas_headers}

View File

@ -7,6 +7,8 @@
#include <plasp/pddl/expressions/And.h>
#include <plasp/pddl/expressions/Not.h>
#include <plasp/pddl/expressions/Predicate.h>
#include <plasp/pddl/translation/Precondition.h>
#include <plasp/pddl/translation/Primitives.h>
namespace plasp
{
@ -23,8 +25,6 @@ template<class T>
void translateVariablesHead(output::ColorStream &outputStream, const T &variables);
template<class T>
void translateVariablesBody(output::ColorStream &outputStream, const T &variables);
void translateLiteral(output::ColorStream &outputStream, const Expression &literal);
void translatePredicate(output::ColorStream &outputStream, const expressions::Predicate &predicate);
////////////////////////////////////////////////////////////////////////////////////////////////////
@ -204,43 +204,49 @@ void TranslatorASP::translateDerivedPredicates() const
const auto &derivedPredicates = m_description.domain().derivedPredicates();
const auto printDerivedPredicateName =
[&](const auto &derivedPredicate)
{
if (derivedPredicate->parameters().empty())
for (const auto &derivedPredicate : derivedPredicates)
{
const auto printDerivedPredicateName =
[&derivedPredicate](output::ColorStream &outputStream)
{
// TODO: implement
//m_outputStream << output::String(derivedPredicate->name().c_str());
m_outputStream << "<derived>";
outputStream << output::Keyword("derivedVariable") << "(";
return;
}
const auto id = derivedPredicate->id();
// TODO: implement
//m_outputStream << output::String(derivedPredicate->name().c_str());
m_outputStream << "<derived>";
translateVariablesHead(m_outputStream, derivedPredicate->parameters());
m_outputStream << ")";
};
if (derivedPredicate->parameters().empty())
{
outputStream << output::Number<decltype(id)>(id) << ")";
return;
}
std::for_each(derivedPredicates.cbegin(), derivedPredicates.cend(),
[&](const auto &predicate)
{
m_outputStream
<< std::endl
<< output::Function("variable") << "("
<< output::Keyword("derived") << "(";
outputStream << "(" << output::Number<decltype(id)>(id);
translateVariablesHead(outputStream, derivedPredicate->parameters());
outputStream << "))";
};
printDerivedPredicateName(predicate);
m_outputStream << std::endl << output::Function("derivedVariable") << "(";
m_outputStream << "))";
printDerivedPredicateName(m_outputStream);
translateVariablesBody(m_outputStream, predicate->parameters());
m_outputStream << ")";
m_outputStream << ".";
});
translateVariablesBody(m_outputStream, derivedPredicate->parameters());
m_outputStream << std::endl;
m_outputStream << ".";
translation::translatePreconditionDisjunction(m_outputStream, "derivedVariable", printDerivedPredicateName, derivedPredicate->preconditions());
m_outputStream << std::endl;
}
m_outputStream
<< std::endl
<< output::Function("contains") << "("
<< output::Keyword("derivedVariable") << "(" << output::Variable("X") << "), "
<< output::Keyword("value") << "(" << output::Variable("X") << ", " << output::Variable("B") << ")) :- "
<< output::Function("variable") << "(" << output::Keyword("derivedVariable") << "(" << output::Variable("X") << ")), "
<< output::Function("boolean") << "(" << output::Variable("B") << ")."
<< std::endl;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
@ -283,7 +289,7 @@ void TranslatorASP::translateActions() const
m_outputStream << ", ";
translateLiteral(m_outputStream, literal);
translation::translateLiteral(m_outputStream, literal);
m_outputStream << ") :- " << output::Function("action") << "(";
@ -450,91 +456,6 @@ void translateVariablesBody(output::ColorStream &outputStream, const T &variable
////////////////////////////////////////////////////////////////////////////////////////////////////
void translateLiteral(output::ColorStream &outputStream, const Expression &literal)
{
// Translate single predicate
if (literal.is<expressions::Predicate>())
{
const auto &predicate = literal.as<expressions::Predicate>();
outputStream << output::Keyword("variable") << "(";
translatePredicate(outputStream, predicate);
outputStream << "), " << output::Keyword("value") << "(";
translatePredicate(outputStream, predicate);
outputStream << ", " << output::Boolean("true") << ")";
}
// Assuming that "not" expression may only contain a predicate
else if (literal.is<expressions::Not>())
{
const auto &notExpression = literal.as<expressions::Not>();
if (notExpression.argument()->expressionType() != Expression::Type::Predicate)
throw output::TranslatorException("only negations of primitive predicates supported as literals currently");
const auto &predicate = notExpression.argument()->as<expressions::Predicate>();
outputStream << output::Keyword("variable") << "(";
translatePredicate(outputStream, predicate);
outputStream << "), " << output::Keyword("value") << "(";
translatePredicate(outputStream, predicate);
outputStream << ", " << output::Boolean("false") << ")";
}
else if (literal.is<expressions::DerivedPredicate>())
{
const auto &derivedPredicate = literal.as<expressions::DerivedPredicate>();
/*m_outputStream << output::Keyword("variable") << "(";
this->translatePredicate(predicate);
m_outputStream << "), " << output::Keyword("value") << "(";
this->translatePredicate(predicate);
m_outputStream << ", " << output::Boolean("true") << ")";*/
outputStream << "(derived predicate)";
}
else
throw output::TranslatorException("only primitive predicates and their negations supported as literals currently");
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void translatePredicate(output::ColorStream &outputStream, const expressions::Predicate &predicate)
{
const auto &arguments = predicate.arguments();
if (arguments.empty())
{
outputStream << output::String(predicate.name().c_str());
return;
}
outputStream << "(" << output::String(predicate.name().c_str());
for (const auto &argument : arguments)
{
outputStream << ", ";
if (argument->is<expressions::Constant>())
{
const auto &constant = argument->as<expressions::Constant>();
outputStream << output::Keyword("constant") << "(" << output::String(constant.name().c_str()) << ")";
}
else if (argument->is<expressions::Variable>())
{
const auto &variable = argument->as<expressions::Variable>();
outputStream << output::Variable(variable.name().c_str());
}
else
throw output::TranslatorException("only variables and constants supported in predicates currently");
}
outputStream << ")";
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void TranslatorASP::translateProblem() const
{
BOOST_ASSERT(m_description.containsProblem());
@ -579,9 +500,9 @@ void TranslatorASP::translateInitialState() const
const auto &predicate = fact->as<expressions::Predicate>();
m_outputStream << output::Keyword("variable") << "(";
translatePredicate(m_outputStream, predicate);
translation::translatePredicate(m_outputStream, predicate);
m_outputStream << "), " << output::Keyword("value") << "(";
translatePredicate(m_outputStream, predicate);
translation::translatePredicate(m_outputStream, predicate);
m_outputStream << ", " << output::Boolean("true") << ")";
}
// Assuming that "not" expression may only contain a predicate
@ -625,7 +546,7 @@ void TranslatorASP::translateGoal() const
{
m_outputStream << std::endl << output::Function("goal") << "(";
translateLiteral(m_outputStream, goal);
translation::translateLiteral(m_outputStream, goal);
m_outputStream << ").";
}
@ -638,7 +559,7 @@ void TranslatorASP::translateGoal() const
{
m_outputStream << std::endl << output::Function("goal") << "(";
translateLiteral(m_outputStream, *argument);
translation::translateLiteral(m_outputStream, *argument);
m_outputStream << ").";
});

View File

@ -25,7 +25,7 @@ const std::string And::Identifier = "and";
ExpressionPointer And::decomposed(DerivedPredicates &derivedPredicates)
{
derivedPredicates.emplace_back(new DerivedPredicate());
derivedPredicates.emplace_back(new DerivedPredicate(derivedPredicates.size()));
auto &derivedPredicate = derivedPredicates.back();
for (auto &argument : m_arguments)

View File

@ -16,6 +16,20 @@ namespace expressions
//
////////////////////////////////////////////////////////////////////////////////////////////////////
DerivedPredicate::DerivedPredicate(size_t id)
: m_id{id}
{
}
////////////////////////////////////////////////////////////////////////////////////////////////////
size_t DerivedPredicate::id() const
{
return m_id;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void DerivedPredicate::setPreconditions(std::vector<Expressions> &&preconditions)
{
m_preconditions = std::move(preconditions);

View File

@ -107,7 +107,7 @@ ExpressionPointer Not::decomposed(DerivedPredicates &derivedPredicates)
if (m_argument->is<Predicate>() || m_argument->is<DerivedPredicate>())
return this;
derivedPredicates.emplace_back(new DerivedPredicate());
derivedPredicates.emplace_back(new DerivedPredicate(derivedPredicates.size()));
auto &derivedPredicate = derivedPredicates.back();
// Move this expressions arguments to the derived predicate

View File

@ -23,7 +23,7 @@ const std::string Or::Identifier = "or";
ExpressionPointer Or::decomposed(DerivedPredicates &derivedPredicates)
{
derivedPredicates.emplace_back(new DerivedPredicate());
derivedPredicates.emplace_back(new DerivedPredicate(derivedPredicates.size()));
auto &derivedPredicate = derivedPredicates.back();
std::vector<Expressions> preconditions;