Implemented simplification of nested quantified expressions.

This commit is contained in:
Patrick Lühne 2016-09-06 18:50:23 +02:00
parent ad6b3d60eb
commit 31068bf89c
5 changed files with 98 additions and 8 deletions

View File

@ -178,7 +178,7 @@ inline ExpressionPointer NAry<Derived>::simplified()
} }
// TODO: recognize tautologies // TODO: recognize tautologies
// TODO: introduce handle boolean values // TODO: introduce/handle boolean values
return this; return this;
} }

View File

@ -31,8 +31,12 @@ class Quantified: public ExpressionCRTP<Derived>
void setArgument(ExpressionPointer argument); void setArgument(ExpressionPointer argument);
ExpressionPointer argument() const; ExpressionPointer argument() const;
Variables &variables();
const Variables &variables() const;
ExpressionPointer reduced() override; ExpressionPointer reduced() override;
ExpressionPointer negationNormalized() override; ExpressionPointer negationNormalized() override;
ExpressionPointer simplified() override;
void print(std::ostream &ostream) const override; void print(std::ostream &ostream) const override;
@ -98,6 +102,22 @@ ExpressionPointer Quantified<Derived>::argument() const
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived>
Variables &Quantified<Derived>::variables()
{
return m_variables;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived>
const Variables &Quantified<Derived>::variables() const
{
return m_variables;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived> template<class Derived>
inline ExpressionPointer Quantified<Derived>::reduced() inline ExpressionPointer Quantified<Derived>::reduced()
{ {
@ -118,17 +138,43 @@ inline ExpressionPointer Quantified<Derived>::negationNormalized()
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived>
inline ExpressionPointer Quantified<Derived>::simplified()
{
m_argument = m_argument->simplified();
// Associate same-type children, such as (forall (?x) (forall (?y) (...)))
if (m_argument->expressionType() != Derived::ExpressionType)
return this;
auto &quantifiedExpression = dynamic_cast<Derived &>(*m_argument);
BOOST_ASSERT(!quantifiedExpression.arguments().empty());
// Unify variables
m_variables.insert(m_variables.end(), quantifiedExpression.variables().begin(), quantifiedExpression.variables().end());
// Move child expression up
m_argument = quantifiedExpression.argument();
// TODO: introduce/handle boolean values
return this;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived> template<class Derived>
inline void Quantified<Derived>::print(std::ostream &ostream) const inline void Quantified<Derived>::print(std::ostream &ostream) const
{ {
ostream << "(" << Derived::Identifier << "("; ostream << "(" << Derived::Identifier << " (";
for (size_t i = 0; i < m_variables.size(); i++) for (size_t i = 0; i < m_variables.size(); i++)
{ {
if (i > 0) if (i > 0)
ostream << " "; ostream << " ";
ostream << m_variables[i]->name(); m_variables[i]->print(ostream);
} }
ostream << ") "; ostream << ") ";

View File

@ -27,6 +27,9 @@ class Variable: public ExpressionCRTP<Variable>
Variables &variables); Variables &variables);
public: public:
Variable();
Variable(std::string name);
void setName(std::string name); void setName(std::string name);
const std::string &name() const; const std::string &name() const;
@ -42,8 +45,6 @@ class Variable: public ExpressionCRTP<Variable>
static void parseDeclaration(Context &context, Variables &parameters); static void parseDeclaration(Context &context, Variables &parameters);
private: private:
Variable();
bool m_isDirty; bool m_isDirty;
std::string m_name; std::string m_name;

View File

@ -26,8 +26,15 @@ namespace expressions
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
Variable::Variable() Variable::Variable()
: m_isDirty{false}
{
}
////////////////////////////////////////////////////////////////////////////////////////////////////
Variable::Variable(std::string name)
: m_isDirty{false}, : m_isDirty{false},
m_type{nullptr} m_name{name}
{ {
} }
@ -180,8 +187,13 @@ bool Variable::isDirty() const
void Variable::print(std::ostream &ostream) const void Variable::print(std::ostream &ostream) const
{ {
// TODO: implement correctly ostream << "?" << m_name;
ostream << "(<variable " << m_name << ">)";
if (m_type)
{
ostream << " - ";
m_type->print(ostream);
}
} }
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -2,9 +2,12 @@
#include <plasp/pddl/expressions/And.h> #include <plasp/pddl/expressions/And.h>
#include <plasp/pddl/expressions/Dummy.h> #include <plasp/pddl/expressions/Dummy.h>
#include <plasp/pddl/expressions/Exists.h>
#include <plasp/pddl/expressions/ForAll.h>
#include <plasp/pddl/expressions/Imply.h> #include <plasp/pddl/expressions/Imply.h>
#include <plasp/pddl/expressions/Not.h> #include <plasp/pddl/expressions/Not.h>
#include <plasp/pddl/expressions/Or.h> #include <plasp/pddl/expressions/Or.h>
#include <plasp/pddl/expressions/Variable.h>
using namespace plasp::pddl; using namespace plasp::pddl;
@ -155,3 +158,31 @@ TEST(PDDLNormalizationTests, DoubleNegationInner)
ASSERT_EQ(output.str(), "(or (not (a)) (not (b)) (not (c)))"); ASSERT_EQ(output.str(), "(or (not (a)) (not (b)) (not (c)))");
} }
////////////////////////////////////////////////////////////////////////////////////////////////////
TEST(PDDLNormalizationTests, SimplifyNestedForAll)
{
auto v1 = expressions::VariablePointer(new expressions::Variable("x"));
auto v2 = expressions::VariablePointer(new expressions::Variable("y"));
auto v3 = expressions::VariablePointer(new expressions::Variable("z"));
auto v4 = expressions::VariablePointer(new expressions::Variable("u"));
auto v5 = expressions::VariablePointer(new expressions::Variable("v"));
auto v6 = expressions::VariablePointer(new expressions::Variable("w"));
auto f1 = expressions::ForAllPointer(new expressions::ForAll);
auto f2 = expressions::ForAllPointer(new expressions::ForAll);
auto d = expressions::DummyPointer(new expressions::Dummy("a"));
f1->variables() = {v1, v2, v3};
f2->variables() = {v4, v5, v6};
f1->setArgument(f2);
f2->setArgument(d);
std::stringstream output;
f1->normalized()->print(output);
ASSERT_EQ(output.str(), "(forall (?x ?y ?z ?u ?v ?w) (a))");
}