Skip to content

Commit

Permalink
made goal optional
Browse files Browse the repository at this point in the history
  • Loading branch information
drexlerd committed Apr 12, 2024
1 parent 12f4947 commit 928d069
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 39 deletions.
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ configure_ccache()

# Boost
# Find Boost headers only according to https://cmake.org/cmake/help/latest/module/FindBoost.html
message("Prefix path: ${CMAKE_PREFIX_PATH}")
configure_boost()
find_package(Boost ${BOOST_MIN_VERSION} REQUIRED PATHS ${CMAKE_PREFIX_PATH} NO_DEFAULT_PATH)
if(Boost_FOUND)
Expand Down
4 changes: 2 additions & 2 deletions include/loki/ast/ast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1238,8 +1238,8 @@ struct Problem : x3::position_tagged
boost::optional<Requirements> requirements;
boost::optional<Objects> objects;
boost::optional<DerivedPredicates> derived_predicates;
Initial initial;
Goal goal;
boost::optional<Initial> initial;
boost::optional<Goal> goal;
boost::optional<ProblemConstraints> constraints;
boost::optional<MetricSpecification> metric_specification;
boost::optional<std::vector<Axiom>> axioms;
Expand Down
13 changes: 6 additions & 7 deletions include/loki/pddl/problem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,23 @@ class ProblemImpl : public Base<ProblemImpl>
std::string m_name;
Requirements m_requirements;
ObjectList m_objects;
PredicateList m_derived_predicates;
GroundLiteralList m_initial_literals;
NumericFluentList m_numeric_fluents;
Condition m_goal_condition;
std::optional<Condition> m_goal_condition;
std::optional<OptimizationMetric> m_optimization_metric;

PredicateList m_derived_predicates;
AxiomList m_axioms;

ProblemImpl(int identifier,
Domain domain,
std::string name,
Requirements requirements,
ObjectList objects,
PredicateList derived_predicates,
GroundLiteralList initial_literals,
NumericFluentList numeric_fluents,
Condition goal_condition,
std::optional<Condition> goal_condition,
std::optional<OptimizationMetric> optimization_metric,
PredicateList derived_predicates,
AxiomList axioms);

// Give access to the constructor.
Expand All @@ -76,11 +75,11 @@ class ProblemImpl : public Base<ProblemImpl>
const std::string& get_name() const;
const Requirements& get_requirements() const;
const ObjectList& get_objects() const;
const PredicateList& get_derived_predicates() const;
const GroundLiteralList& get_initial_literals() const;
const NumericFluentList& get_numeric_fluents() const;
const Condition& get_goal_condition() const;
const std::optional<Condition>& get_goal_condition() const;
const std::optional<OptimizationMetric>& get_optimization_metric() const;
const PredicateList& get_derived_predicates() const;
const AxiomList& get_axioms() const;
};

Expand Down
2 changes: 1 addition & 1 deletion src/ast/parser_def.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ const auto goal_def = (lit('(') >> keyword_lit(":goal")) > precondition_goal_des
const auto problem_constraints_def = (lit('(') >> keyword_lit(":constraints")) > preference_constraint_goal_descriptor > lit(')');
const auto metric_specification_def = (lit('(') >> keyword_lit(":metric")) > (metric_specification_total_cost | metric_specification_general) > lit(')');

const auto problem_def = lit('(') > define_keyword > problem_name > problem_domain_name > -requirements > -objects > -derived_predicates > initial > goal
const auto problem_def = lit('(') > define_keyword > problem_name > problem_domain_name > -requirements > -objects > -derived_predicates > -initial > -goal
> -problem_constraints > -metric_specification > -(*axiom) > lit(')');

/**
Expand Down
10 changes: 8 additions & 2 deletions src/ast/printer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -903,8 +903,14 @@ string parse_text(const ast::Problem& node, const FormattingOptions& options)
{
ss << string(nested_options.indent, ' ') << parse_text(node.objects.value(), nested_options) << "\n";
}
ss << string(nested_options.indent, ' ') << parse_text(node.initial, nested_options) << "\n";
ss << string(nested_options.indent, ' ') << parse_text(node.goal, nested_options) << "\n";
if (node.initial.has_value())
{
ss << string(nested_options.indent, ' ') << parse_text(node.initial.value(), nested_options) << "\n";
}
if (node.goal.has_value())
{
ss << string(nested_options.indent, ' ') << parse_text(node.goal.value(), nested_options) << "\n";
}
if (node.constraints.has_value())
{
ss << string(nested_options.indent, ' ') << parse_text(node.constraints.value(), nested_options) << "\n";
Expand Down
17 changes: 12 additions & 5 deletions src/pddl/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,20 @@ pddl::Problem parse(const ast::Problem& problem_node, Context& context, const pd
/* Initial section */
auto initial_literals = pddl::GroundLiteralList();
auto numeric_fluents = pddl::NumericFluentList();
const auto initial_elements = parse(problem_node.initial, context);
for (const auto& initial_element : initial_elements)
if (problem_node.initial.has_value())
{
std::visit(UnpackingVisitor(initial_literals, numeric_fluents), initial_element);
const auto initial_elements = parse(problem_node.initial.value(), context);
for (const auto& initial_element : initial_elements)
{
std::visit(UnpackingVisitor(initial_literals, numeric_fluents), initial_element);
}
}
/* Goal section */
const auto goal_condition = parse(problem_node.goal, context);
auto goal_condition = std::optional<pddl::Condition>();
if (problem_node.goal.has_value())
{
goal_condition = parse(problem_node.goal.value(), context);
}
/* Metric section */
auto optimization_metric = std::optional<pddl::OptimizationMetric>();
if (problem_node.metric_specification.has_value())
Expand All @@ -191,11 +198,11 @@ pddl::Problem parse(const ast::Problem& problem_node, Context& context, const pd
problem_name,
context.requirements,
objects,
derived_predicates,
initial_literals,
numeric_fluents,
goal_condition,
optimization_metric,
derived_predicates,
axioms);
context.positions.push_back(problem, problem_node);
return problem;
Expand Down
49 changes: 28 additions & 21 deletions src/pddl/problem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,22 @@ ProblemImpl::ProblemImpl(int identifier,
std::string name,
Requirements requirements,
ObjectList objects,
PredicateList derived_predicates,
GroundLiteralList initial_literals,
NumericFluentList numeric_fluents,
Condition goal_condition,
std::optional<Condition> goal_condition,
std::optional<OptimizationMetric> optimization_metric,
PredicateList derived_predicates,
AxiomList axioms) :
Base(identifier),
m_domain(std::move(domain)),
m_name(std::move(name)),
m_requirements(std::move(requirements)),
m_objects(std::move(objects)),
m_derived_predicates(std::move(derived_predicates)),
m_initial_literals(std::move(initial_literals)),
m_numeric_fluents(std::move(numeric_fluents)),
m_goal_condition(std::move(goal_condition)),
m_optimization_metric(std::move(optimization_metric)),
m_derived_predicates(std::move(derived_predicates)),
m_axioms(std::move(axioms))
{
}
Expand All @@ -74,13 +74,14 @@ bool ProblemImpl::is_structurally_equivalent_to_impl(const ProblemImpl& other) c

size_t ProblemImpl::hash_impl() const
{
size_t optimization_hash = (m_optimization_metric.has_value()) ? hash_combine(m_optimization_metric) : 0;
size_t goal_hash = (m_goal_condition.has_value()) ? hash_combine(m_goal_condition.value()) : 0;
size_t optimization_hash = (m_optimization_metric.has_value()) ? hash_combine(m_optimization_metric.value()) : 0;
return hash_combine(m_domain,
m_name,
m_requirements,
hash_container(get_sorted_vector(m_objects)),
hash_container(get_sorted_vector(m_initial_literals)),
m_goal_condition,
goal_hash,
optimization_hash,
hash_container(get_sorted_vector(m_derived_predicates)),
hash_container(get_sorted_vector(m_axioms)));
Expand Down Expand Up @@ -142,7 +143,7 @@ void ProblemImpl::str_impl(std::ostream& out, const FormattingOptions& options)

if (!m_derived_predicates.empty())
{
out << string(nested_options.indent, ' ') << "(:predicates ";
out << string(nested_options.indent, ' ') << "(:derived-predicates ";
for (size_t i = 0; i < m_derived_predicates.size(); ++i)
{
if (i != 0)
Expand All @@ -152,24 +153,30 @@ void ProblemImpl::str_impl(std::ostream& out, const FormattingOptions& options)
out << ")\n";
}

out << string(nested_options.indent, ' ') << "(:init ";
for (size_t i = 0; i < m_initial_literals.size(); ++i)
if (!(m_initial_literals.empty() && m_numeric_fluents.empty()))
{
if (i != 0)
out << string(nested_options.indent, ' ') << "(:init ";
for (size_t i = 0; i < m_initial_literals.size(); ++i)
{
if (i != 0)
out << " ";
m_initial_literals[i]->str(out, nested_options);
}
for (size_t i = 0; i < m_numeric_fluents.size(); ++i)
{
out << " ";
m_initial_literals[i]->str(out, nested_options);
m_numeric_fluents[i]->str(out, nested_options);
}
}
for (size_t i = 0; i < m_numeric_fluents.size(); ++i)
out << ")\n";

if (m_goal_condition.has_value())
{
out << " ";
m_numeric_fluents[i]->str(out, nested_options);
out << string(nested_options.indent, ' ') << "(:goal ";
std::visit(StringifyVisitor(out, options), *m_goal_condition.value());
out << ")\n";
}

out << ")\n";
out << string(nested_options.indent, ' ') << "(:goal ";
std::visit(StringifyVisitor(out, options), *m_goal_condition);

out << ")\n";
if (m_optimization_metric.has_value())
{
out << string(nested_options.indent, ' ') << "(:metric ";
Expand Down Expand Up @@ -199,16 +206,16 @@ const Requirements& ProblemImpl::get_requirements() const { return m_requirement

const ObjectList& ProblemImpl::get_objects() const { return m_objects; }

const PredicateList& ProblemImpl::get_derived_predicates() const { return m_derived_predicates; }

const GroundLiteralList& ProblemImpl::get_initial_literals() const { return m_initial_literals; }

const NumericFluentList& ProblemImpl::get_numeric_fluents() const { return m_numeric_fluents; }

const Condition& ProblemImpl::get_goal_condition() const { return m_goal_condition; }
const std::optional<Condition>& ProblemImpl::get_goal_condition() const { return m_goal_condition; }

const std::optional<OptimizationMetric>& ProblemImpl::get_optimization_metric() const { return m_optimization_metric; }

const PredicateList& ProblemImpl::get_derived_predicates() const { return m_derived_predicates; }

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

}
1 change: 1 addition & 0 deletions tests/integration/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ project(integration_example VERSION "0.0.1" LANGUAGES C CXX)
# CMake modules and macro files
##############################################################

message("Prefix path: ${CMAKE_PREFIX_PATH}")
find_package(loki COMPONENTS parsers REQUIRED PATHS ${CMAKE_PREFIX_PATH} NO_DEFAULT_PATH)


Expand Down

0 comments on commit 928d069

Please sign in to comment.