Skip to content

Commit

Permalink
added axioms to domain and problem
Browse files Browse the repository at this point in the history
  • Loading branch information
drexlerd committed Apr 9, 2024
1 parent 982cab4 commit 9e4a19c
Show file tree
Hide file tree
Showing 9 changed files with 194 additions and 12 deletions.
57 changes: 57 additions & 0 deletions include/loki/pddl/axiom.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright (C) 2023 Dominik Drexler and Simon Stahlberg and Simon Stahlberg
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*<
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

#ifndef LOKI_INCLUDE_LOKI_PDDL_AXIOM_HPP_
#define LOKI_INCLUDE_LOKI_PDDL_AXIOM_HPP_

#include "loki/pddl/base.hpp"
#include "loki/pddl/declarations.hpp"
#include "loki/pddl/factory.hpp"

#include <string>

namespace loki::pddl
{
class AxiomImpl : public Base<AxiomImpl>
{
private:
ParameterList m_parameters;
Atom m_atom;
Condition m_condition;

AxiomImpl(int identifier, ParameterList parameters, Atom atom, Condition condition);

// Give access to the constructor.
friend class PDDLFactory<AxiomImpl, Hash<AxiomImpl*>, EqualTo<AxiomImpl*>>;

/// @brief Test for structural equivalence
bool is_structurally_equivalent_to_impl(const AxiomImpl& other) const;
size_t hash_impl() const;
void str_impl(std::ostream& out, const FormattingOptions& options) const;

// Give access to the private interface implementations.
friend class Base<AxiomImpl>;

public:
const ParameterList& get_parameters() const;
const Atom& get_atom() const;
const Condition& get_condition() const;
};

}

#endif
4 changes: 4 additions & 0 deletions include/loki/pddl/declarations.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ class DerivedPredicateImpl;
using DerivedPredicate = const DerivedPredicateImpl*;
using DerivedPredicateList = std::vector<DerivedPredicate>;

class AxiomImpl;
using Axiom = const AxiomImpl*;
using AxiomList = std::vector<Axiom>;

class DomainImpl;
using Domain = const DomainImpl*;
using DomainList = std::vector<Domain>;
Expand Down
5 changes: 4 additions & 1 deletion include/loki/pddl/domain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class DomainImpl : public Base<DomainImpl>
FunctionSkeletonList m_functions;
ActionList m_actions;
DerivedPredicateList m_derived_predicates;
AxiomList m_axioms;

DomainImpl(int identifier,
std::string name,
Expand All @@ -54,7 +55,8 @@ class DomainImpl : public Base<DomainImpl>
PredicateList predicates,
FunctionSkeletonList functions,
ActionList actions,
DerivedPredicateList derived_predicates);
DerivedPredicateList derived_predicates,
AxiomList axioms);

// Give access to the constructor.
friend class loki::PDDLFactory<DomainImpl, loki::Hash<DomainImpl*>, loki::EqualTo<DomainImpl*>>;
Expand All @@ -76,6 +78,7 @@ class DomainImpl : public Base<DomainImpl>
const FunctionSkeletonList& get_functions() const;
const ActionList& get_actions() const;
const DerivedPredicateList& get_derived_predicates() const;
const AxiomList& get_axioms() const;
};

}
Expand Down
5 changes: 5 additions & 0 deletions include/loki/pddl/factories.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include "loki/pddl/action.hpp"
#include "loki/pddl/atom.hpp"
#include "loki/pddl/axiom.hpp"
#include "loki/pddl/conditions.hpp"
#include "loki/pddl/derived_predicate.hpp"
#include "loki/pddl/domain.hpp"
Expand Down Expand Up @@ -67,6 +68,7 @@ using ConditionFactory = PDDLFactory<pddl::ConditionImpl>;
using EffectFactory = PDDLFactory<pddl::EffectImpl>;
using ActionFactory = PDDLFactory<pddl::ActionImpl>;
using DerivedPredicateFactory = PDDLFactory<pddl::DerivedPredicateImpl>;
using AxiomFactory = PDDLFactory<pddl::AxiomImpl>;
using OptimizationMetricFactory = PDDLFactory<pddl::OptimizationMetricImpl>;
using NumericFluentFactory = PDDLFactory<pddl::NumericFluentImpl>;
using DomainFactory = PDDLFactory<pddl::DomainImpl>;
Expand All @@ -90,6 +92,7 @@ using PDDLPositionCache = PositionCache<pddl::RequirementsImpl,
pddl::EffectImpl,
pddl::ActionImpl,
pddl::DerivedPredicateImpl,
pddl::AxiomImpl,
pddl::OptimizationMetricImpl,
pddl::NumericFluentImpl,
pddl::DomainImpl,
Expand All @@ -116,6 +119,7 @@ struct PDDLFactories
EffectFactory effects;
ActionFactory actions;
DerivedPredicateFactory derived_predicates;
AxiomFactory axioms;
OptimizationMetricFactory optimization_metrics;
NumericFluentFactory numeric_fluents;
DomainFactory domains;
Expand All @@ -140,6 +144,7 @@ struct PDDLFactories
effects(EffectFactory(1000)),
actions(ActionFactory(100)),
derived_predicates(DerivedPredicateFactory(100)),
axioms(AxiomFactory(100)),
optimization_metrics(OptimizationMetricFactory(10)),
numeric_fluents(NumericFluentFactory(1000)),
domains(DomainFactory(1)),
Expand Down
6 changes: 5 additions & 1 deletion include/loki/pddl/problem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class ProblemImpl : public Base<ProblemImpl>
NumericFluentList m_numeric_fluents;
Condition m_goal_condition;
std::optional<OptimizationMetric> m_optimization_metric;
// Axioms cannot be de defined for problems but may be added during translation
AxiomList m_axioms;

ProblemImpl(int identifier,
Domain domain,
Expand All @@ -53,7 +55,8 @@ class ProblemImpl : public Base<ProblemImpl>
GroundLiteralList initial_literals,
NumericFluentList numeric_fluents,
Condition goal_condition,
std::optional<OptimizationMetric> optimization_metric);
std::optional<OptimizationMetric> optimization_metric,
AxiomList axioms);

// Give access to the constructor.
friend class loki::PDDLFactory<ProblemImpl, loki::Hash<ProblemImpl*>, loki::EqualTo<ProblemImpl*>>;
Expand All @@ -75,6 +78,7 @@ class ProblemImpl : public Base<ProblemImpl>
const NumericFluentList& get_numeric_fluents() const;
const Condition& get_goal_condition() const;
const std::optional<OptimizationMetric>& get_optimization_metric() const;
const AxiomList& get_axioms() const;
};

}
Expand Down
77 changes: 77 additions & 0 deletions src/pddl/axiom.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright (C) 2023 Dominik Drexler and Simon Stahlberg and Simon Stahlberg
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

#include "loki/pddl/axiom.hpp"

#include "loki/pddl/atom.hpp"
#include "loki/pddl/conditions.hpp"
#include "loki/pddl/parameter.hpp"
#include "loki/pddl/predicate.hpp"
#include "loki/pddl/visitors.hpp"
#include "loki/utils/collections.hpp"
#include "loki/utils/hash.hpp"

namespace loki::pddl
{
AxiomImpl::AxiomImpl(int identifier, ParameterList parameters, Atom atom, Condition condition) :
Base(identifier),
m_parameters(std::move(parameters)),
m_atom(std::move(atom)),
m_condition(std::move(condition))
{
// TODO (Dominik): must ensure that all parameters are used. Add assertion maybe?
}

bool AxiomImpl::is_structurally_equivalent_to_impl(const AxiomImpl& other) const
{
return (m_parameters == other.m_parameters) && (m_atom == other.m_atom) && (m_condition == other.m_condition);
}

size_t AxiomImpl::hash_impl() const { return hash_combine(hash_container(m_parameters), m_atom, m_condition); }

void AxiomImpl::str_impl(std::ostream& out, const FormattingOptions& options) const
{
// Not PDDL language specific. Our own definition of how we want to print it.
auto nested_options = FormattingOptions { options.indent + options.add_indent, options.add_indent };
out << std::string(options.indent, ' ') << "(:axiom"
<< "\n";

out << std::string(nested_options.indent, ' ') << ":vars (";
for (size_t i = 0; i < m_parameters.size(); ++i)
{
if (i != 0)
out << " ";
m_parameters[i]->str(out, options);
}
out << ")\n";

out << std::string(nested_options.indent, ' ') << ":context (";
std::visit(StringifyVisitor(out, nested_options), *m_condition);
out << ")\n";

out << std::string(nested_options.indent, ' ') << ":implies (" << *m_atom << ")\n";

out << ")\n";
}

const ParameterList& AxiomImpl::get_parameters() const { return m_parameters; }

const Atom& AxiomImpl::get_atom() const { return m_atom; }

const Condition& AxiomImpl::get_condition() const { return m_condition; }

}
20 changes: 16 additions & 4 deletions src/pddl/domain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "loki/pddl/domain.hpp"

#include "loki/pddl/action.hpp"
#include "loki/pddl/axiom.hpp"
#include "loki/pddl/derived_predicate.hpp"
#include "loki/pddl/function_skeleton.hpp"
#include "loki/pddl/object.hpp"
Expand All @@ -40,7 +41,8 @@ DomainImpl::DomainImpl(int identifier,
PredicateList predicates,
FunctionSkeletonList functions,
ActionList actions,
DerivedPredicateList derived_predicates) :
DerivedPredicateList derived_predicates,
AxiomList axioms) :
Base(identifier),
m_name(std::move(name)),
m_requirements(std::move(requirements)),
Expand All @@ -49,7 +51,8 @@ DomainImpl::DomainImpl(int identifier,
m_predicates(std::move(predicates)),
m_functions(std::move(functions)),
m_actions(std::move(actions)),
m_derived_predicates(std::move(derived_predicates))
m_derived_predicates(std::move(derived_predicates)),
m_axioms(std::move(axioms))
{
}

Expand All @@ -59,7 +62,8 @@ bool DomainImpl::is_structurally_equivalent_to_impl(const DomainImpl& other) con
&& (get_sorted_vector(m_constants) == get_sorted_vector(other.m_constants))
&& (get_sorted_vector(m_predicates) == get_sorted_vector(other.m_predicates))
&& (get_sorted_vector(m_functions) == get_sorted_vector(other.m_functions)) && (get_sorted_vector(m_actions) == get_sorted_vector(other.m_actions))
&& (get_sorted_vector(m_derived_predicates) == get_sorted_vector(other.m_derived_predicates));
&& (get_sorted_vector(m_derived_predicates) == get_sorted_vector(other.m_derived_predicates))
&& (get_sorted_vector(m_axioms) == get_sorted_vector(other.m_axioms));
}

size_t DomainImpl::hash_impl() const
Expand All @@ -71,7 +75,8 @@ size_t DomainImpl::hash_impl() const
hash_container(get_sorted_vector(m_predicates)),
hash_container(get_sorted_vector(m_functions)),
hash_container(get_sorted_vector(m_actions)),
hash_container(get_sorted_vector(m_derived_predicates)));
hash_container(get_sorted_vector(m_derived_predicates)),
hash_container(get_sorted_vector(m_axioms)));
}

void DomainImpl::str_impl(std::ostream& out, const FormattingOptions& options) const
Expand Down Expand Up @@ -199,6 +204,11 @@ void DomainImpl::str_impl(std::ostream& out, const FormattingOptions& options) c
derived_predicate->str(out, nested_options);
}

for (const auto& axiom : m_axioms)
{
axiom->str(out, nested_options);
}

out << std::string(options.indent, ' ') << ")";
}

Expand All @@ -224,4 +234,6 @@ const ActionList& DomainImpl::get_actions() const { return m_actions; }

const DerivedPredicateList& DomainImpl::get_derived_predicates() const { return m_derived_predicates; }

const AxiomList& DomainImpl::get_axioms() const { return m_axioms; }

}
13 changes: 11 additions & 2 deletions src/pddl/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include "loki/pddl/parser.hpp"

#include "loki/pddl/declarations.hpp"
#include "loki/pddl/domain.hpp"
#include "loki/pddl/exceptions.hpp"
#include "loki/pddl/object.hpp"
Expand Down Expand Up @@ -99,14 +100,18 @@ pddl::Domain parse(const ast::Domain& domain_node, Context& context)
test_predicate_references(predicates, context);
test_function_skeleton_references(function_skeletons, context);

// TODO: add parsing of axioms in the grammar
auto axioms = pddl::AxiomList {};

const auto domain = context.factories.domains.get_or_create<pddl::DomainImpl>(domain_name,
context.requirements,
types,
constants,
predicates,
function_skeletons,
action_list,
derived_predicate_list);
derived_predicate_list,
axioms);
context.positions.push_back(domain, domain_node);
return domain;
}
Expand Down Expand Up @@ -159,14 +164,18 @@ pddl::Problem parse(const ast::Problem& problem_node, Context& context, const pd
// Check references
test_object_references(objects, context);

// Axioms cannot be de defined for problems but may be added during translation
auto axioms = pddl::AxiomList {};

const auto problem = context.factories.problems.get_or_create<pddl::ProblemImpl>(domain,
problem_name,
context.requirements,
objects,
initial_literals,
numeric_fluents,
goal_condition,
optimization_metric);
optimization_metric,
axioms);
context.positions.push_back(problem, problem_node);
return problem;
}
Expand Down
Loading

0 comments on commit 9e4a19c

Please sign in to comment.