Skip to content

Commit

Permalink
made pruning default for tuple graph and remove the parameter passing
Browse files Browse the repository at this point in the history
  • Loading branch information
drexlerd committed Mar 7, 2024
1 parent c86aa99 commit b667e6a
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 109 deletions.
2 changes: 1 addition & 1 deletion api/python/src/dlplan/novelty/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class TupleNode:


class TupleGraph:
def __init__(self, novelty_base: NoveltyBase, state_space: StateSpace, root_state_index: int, enable_pruning: bool) -> None: ...
def __init__(self, novelty_base: NoveltyBase, state_space: StateSpace, root_state_index: int) -> None: ...
def __repr__(self) -> str: ...
def __str__(self) -> str: ...
def to_dot(self, verbosity_level: int) -> str: ...
Expand Down
2 changes: 1 addition & 1 deletion api/python/src/novelty.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ void init_novelty(py::module_ &m_novelty) {
;

py::class_<TupleGraph, std::shared_ptr<TupleGraph>>(m_novelty, "TupleGraph")
.def(py::init<std::shared_ptr<const NoveltyBase>, std::shared_ptr<const StateSpace>, StateIndex, bool>())
.def(py::init<std::shared_ptr<const NoveltyBase>, std::shared_ptr<const StateSpace>, StateIndex>())
.def("__repr__", &TupleGraph::compute_repr)
.def("__str__", &TupleGraph::str)
.def("to_dot", &TupleGraph::to_dot)
Expand Down
3 changes: 1 addition & 2 deletions include/dlplan/novelty.h
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,7 @@ class TupleGraph
TupleGraph(
std::shared_ptr<const NoveltyBase> novelty_base,
std::shared_ptr<const state_space::StateSpace> state_space,
state_space::StateIndex root_state_index,
bool enable_pruning = true);
state_space::StateIndex root_state_index);
TupleGraph(const TupleGraph &other);
TupleGraph &operator=(const TupleGraph &other);
TupleGraph(TupleGraph &&other);
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from setuptools import setup, find_packages, Extension
from setuptools.command.build_ext import build_ext

__version__ = "0.3.21"
__version__ = "0.3.22"
HERE = Path(__file__).resolve().parent


Expand Down
5 changes: 2 additions & 3 deletions src/novelty/tuple_graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ namespace dlplan::novelty {
TupleGraph::TupleGraph(
std::shared_ptr<const NoveltyBase> novelty_base,
std::shared_ptr<const state_space::StateSpace> state_space,
StateIndex root_state_index,
bool enable_pruning)
StateIndex root_state_index)
: m_novelty_base(novelty_base),
m_state_space(state_space),
m_root_state_index(root_state_index) {
Expand All @@ -26,7 +25,7 @@ TupleGraph::TupleGraph(
if (!m_novelty_base) {
throw std::runtime_error("TupleGraph::TupleGraph - state_space is nullptr.");
}
TupleGraphBuilderResult result = TupleGraphBuilder(novelty_base, state_space, root_state_index, enable_pruning).get_result();
TupleGraphBuilderResult result = TupleGraphBuilder(novelty_base, state_space, root_state_index).get_result();
m_nodes = std::move(result.nodes);
m_node_indices_by_distance = std::move(result.node_indices_by_distance);
m_state_indices_by_distance = std::move(result.state_indices_by_distance);
Expand Down
77 changes: 2 additions & 75 deletions src/novelty/tuple_graph_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,55 +145,6 @@ TupleGraphBuilder::extend_states(TupleIndex cur_node_index) const {
return extended;
}

/*
void TupleGraphBuilder::extend_nodes(
const TupleIndicesSet& curr_novel_tuple_indices,
TupleNodeIndex cur_node_index,
std::unordered_map<TupleIndex, TupleNodeIndex> &novel_tuple_index_to_node)
{
std::vector<TupleNode> curr_tuple_node_layer;
auto extended = extend_states(cur_node_index);
TupleIndices extended_tuple_indices;
for (const auto& pair : extended)
{
const auto succ_tuple_index = pair.first;
const auto& cur_node_extended_states = pair.second;
if (curr_novel_tuple_indices.count(succ_tuple_index)
&& cur_node_extended_states.size() == m_nodes[cur_node_index].get_state_indices().size())
{
auto find = novel_tuple_index_to_node.find(succ_tuple_index);
if (find == novel_tuple_index_to_node.end())
{
auto tuple_node = TupleNode(
-1, // unregistered
succ_tuple_index,
m_novel_tuple_index_to_state_indices.at(succ_tuple_index));
next_tuple_node_layer.push_back(tuple_node);
if (!m_enable_pruning || !test_prune(curr_tuple_node_layer, tuple_node)) {
curr_tuple_node_layer.push_back(tuple_node);
m_nodes.push_back(tuple_node);
novel_tuple_index_to_node.emplace(succ_tuple_index, succ_node_index);
m_nodes[cur_node_index].add_successor(succ_node_index);
m_nodes[succ_node_index].add_predecessor(cur_node_index);
}
}
else
{
succ_node_index = find->second;
m_nodes[cur_node_index].add_successor(succ_node_index);
m_nodes[succ_node_index].add_predecessor(cur_node_index);
}
}
}
}
*/



struct SetHash {
size_t operator()(const StateIndicesSet& set) const {
Expand All @@ -220,11 +171,8 @@ TupleNodeIndices TupleGraphBuilder::compute_nodes_layer(
auto extended = extend_states(cur_node_index);

TupleIndices extended_tuple_indices;
for (const auto& pair : extended)
for (const auto& [succ_tuple_index, cur_node_extended_states] : extended)
{
const auto succ_tuple_index = pair.first;
const auto& cur_node_extended_states = pair.second;

// Check whether all optimal plans for cur_node_index can be extended into optimal plans for succ_tuple_index
if (curr_novel_tuple_indices.count(succ_tuple_index)
&& cur_node_extended_states.size() == m_nodes[cur_node_index].get_state_indices().size())
Expand All @@ -235,7 +183,6 @@ TupleNodeIndices TupleGraphBuilder::compute_nodes_layer(
}
}
}
//std::cout << "curr_extended_tuple_indices: " << curr_extended_tuple_indices << std::endl;

// Compute ordering "supset" t > t' iff S*(t) supset S*(t')
std::unordered_map<TupleIndex, TupleIndicesSet> supset_subgoals;
Expand All @@ -249,14 +196,12 @@ TupleNodeIndices TupleGraphBuilder::compute_nodes_layer(
for (const auto tuple_index_2 : curr_extended_tuple_indices)
{
const auto& subgoals_2 = m_novel_tuple_index_to_state_indices.at(tuple_index_2);
//std::cout << "tuple_index_1: " << tuple_index_1 << " subgoals_1: " << subgoals_1 << " tuple_index_2: " << tuple_index_2 << " subgoals_2: " << subgoals_2 << std::endl;
if ((subgoals_1.size() != subgoals_2.size())
&& std::includes(
subgoals_1.begin(), subgoals_1.end(),
subgoals_2.begin(), subgoals_2.end()))
{
supset_subgoals[tuple_index_1].insert(tuple_index_2);
//std::cout << tuple_index_1 << " > " << tuple_index_2 << std::endl;
}
}
}
Expand All @@ -280,7 +225,6 @@ TupleNodeIndices TupleGraphBuilder::compute_nodes_layer(
TupleNodeIndices curr_tuple_layer;
for (const auto& [_, tuple_index] : curr_tuple_indices)
{
//std::cout << "tuple_index: " << tuple_index << std::endl;
auto node_index = m_nodes.size();
const auto& subgoals = m_novel_tuple_index_to_state_indices.at(tuple_index);
m_nodes.push_back(TupleNode(node_index, tuple_index, subgoals));
Expand All @@ -296,19 +240,6 @@ TupleNodeIndices TupleGraphBuilder::compute_nodes_layer(
return curr_tuple_layer;
}

bool TupleGraphBuilder::test_prune(const std::vector<TupleNode>& tuple_node_layer, const TupleNode& tuple_node)
{
for (const auto& curr_tuple_node : tuple_node_layer)
{
if (std::includes(
curr_tuple_node.get_state_indices().begin(), curr_tuple_node.get_state_indices().end(),
tuple_node.get_state_indices().begin(), tuple_node.get_state_indices().end()))
{
return true;
}
}
return false;
}

void TupleGraphBuilder::build_width_equal_0_tuple_graph()
{
Expand Down Expand Up @@ -357,9 +288,7 @@ void TupleGraphBuilder::build_width_greater_0_tuple_graph()
for (int distance = 1; ; ++distance)
{
StateIndices curr_state_layer = compute_state_layer(m_state_indices_by_distance[distance-1], visited_state_indices);
//std::cout << "curr_state_layer: " << curr_state_layer << std::endl;
auto novel_tuple_indices = compute_novel_tuple_indices_layer(curr_state_layer);
//std::cout << "novel_tuple_indices: " << novel_tuple_indices << std::endl;

if (novel_tuple_indices.empty())
{
Expand All @@ -380,12 +309,10 @@ void TupleGraphBuilder::build_width_greater_0_tuple_graph()
TupleGraphBuilder::TupleGraphBuilder(
std::shared_ptr<const NoveltyBase> novelty_base,
std::shared_ptr<const state_space::StateSpace> state_space,
StateIndex root_state,
bool enable_pruning)
StateIndex root_state)
: m_novelty_base(novelty_base),
m_state_space(state_space),
m_root_state_index(root_state),
m_enable_pruning(enable_pruning),
m_novelty_table(novelty_base)
{
if (!m_novelty_base)
Expand Down
16 changes: 1 addition & 15 deletions src/novelty/tuple_graph_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ class TupleGraphBuilder {
std::shared_ptr<const NoveltyBase> m_novelty_base;
std::shared_ptr<const state_space::StateSpace> m_state_space;
state_space::StateIndex m_root_state_index;
bool m_enable_pruning;
// output
TupleNodes m_nodes;
std::vector<TupleNodeIndices> m_node_indices_by_distance;
Expand Down Expand Up @@ -60,18 +59,6 @@ class TupleGraphBuilder {
std::unordered_map<TupleIndex, StateIndicesSet>
extend_states(TupleIndex cur_node_index) const;

/// @brief Creates all nodes in next layer that are successors for the node
/// in current layer with cur_node_index.
void
extend_nodes(
const TupleIndicesSet& curr_novel_tuple_indices,
TupleNodeIndex cur_node_index,
std::unordered_map<TupleIndex, TupleNodeIndex> &novel_tuple_index_to_node);

/// @brief Return true iff the underlying set of states is a super set or equal
/// to the underlying set of states of another subgoal tuple.
bool test_prune(const std::vector<TupleNode>& tuple_node_layer, const TupleNode& tuple_node);

void build_width_equal_0_tuple_graph();

void build_width_greater_0_tuple_graph();
Expand All @@ -80,8 +67,7 @@ class TupleGraphBuilder {
TupleGraphBuilder(
std::shared_ptr<const NoveltyBase> novelty_base,
std::shared_ptr<const state_space::StateSpace> state_space,
StateIndex root_state,
bool enable_pruning = true);
StateIndex root_state);

TupleGraphBuilderResult get_result();
};
Expand Down
19 changes: 8 additions & 11 deletions tests/novelty/gripper/gripper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,27 @@ TEST(DLPTests, NoveltyGripperTest) {
auto novelty_base_0 = std::make_shared<NoveltyBase>(
state_space->get_instance_info()->get_atoms().size(),
0);
EXPECT_EQ(TupleGraph(novelty_base_0, state_space, 0, false).compute_repr(), "TupleGraph(\n root_state_index=0,\n tuple_nodes=[ TupleNode(index=0, tuple_index=0, state_indices={0}, predecessors=[], successors=[1, 2, 3]),\n TupleNode(index=1, tuple_index=1, state_indices={3}, predecessors=[0], successors=[]),\n TupleNode(index=2, tuple_index=2, state_indices={2}, predecessors=[0], successors=[]),\n TupleNode(index=3, tuple_index=3, state_indices={1}, predecessors=[0], successors=[])\n ],\n node_indices_by_distance=[\n [0],\n [1, 2, 3]\n ],\n state_indices_by_distance=[\n [0],\n [1, 2, 3]\n ]\n)");
EXPECT_EQ(TupleGraph(novelty_base_0, state_space, 0, true).compute_repr(), "TupleGraph(\n root_state_index=0,\n tuple_nodes=[ TupleNode(index=0, tuple_index=0, state_indices={0}, predecessors=[], successors=[1, 2, 3]),\n TupleNode(index=1, tuple_index=1, state_indices={3}, predecessors=[0], successors=[]),\n TupleNode(index=2, tuple_index=2, state_indices={2}, predecessors=[0], successors=[]),\n TupleNode(index=3, tuple_index=3, state_indices={1}, predecessors=[0], successors=[])\n ],\n node_indices_by_distance=[\n [0],\n [1, 2, 3]\n ],\n state_indices_by_distance=[\n [0],\n [1, 2, 3]\n ]\n)");
EXPECT_EQ(TupleGraph(novelty_base_0, state_space, 0).compute_repr(), "TupleGraph(\n root_state_index=0,\n tuple_nodes=[ TupleNode(index=0, tuple_index=0, state_indices={0}, predecessors=[], successors=[1, 2, 3]),\n TupleNode(index=1, tuple_index=1, state_indices={3}, predecessors=[0], successors=[]),\n TupleNode(index=2, tuple_index=2, state_indices={2}, predecessors=[0], successors=[]),\n TupleNode(index=3, tuple_index=3, state_indices={1}, predecessors=[0], successors=[])\n ],\n node_indices_by_distance=[\n [0],\n [1, 2, 3]\n ],\n state_indices_by_distance=[\n [0],\n [1, 2, 3]\n ]\n)");

auto novelty_base_1 = std::make_shared<NoveltyBase>(
state_space->get_instance_info()->get_atoms().size(),
1);

EXPECT_EQ(TupleGraph(novelty_base_1, state_space, 0, false).compute_repr(), "TupleGraph(\n root_state_index=0,\n tuple_nodes=[ TupleNode(index=0, tuple_index=0, state_indices={0}, predecessors=[], successors=[2, 6, 7]),\n TupleNode(index=1, tuple_index=1, state_indices={0}, predecessors=[], successors=[2, 6, 7]),\n TupleNode(index=2, tuple_index=2, state_indices={1}, predecessors=[0, 1, 3, 4, 5], successors=[]),\n TupleNode(index=3, tuple_index=3, state_indices={0}, predecessors=[], successors=[2, 6, 7]),\n TupleNode(index=4, tuple_index=4, state_indices={0}, predecessors=[], successors=[2, 6, 7]),\n TupleNode(index=5, tuple_index=5, state_indices={0}, predecessors=[], successors=[2, 6, 7]),\n TupleNode(index=6, tuple_index=7, state_indices={2}, predecessors=[0, 1, 3, 4, 5], successors=[]),\n TupleNode(index=7, tuple_index=8, state_indices={3}, predecessors=[0, 1, 3, 4, 5], successors=[])\n ],\n node_indices_by_distance=[\n [0, 1, 3, 4, 5],\n [2, 6, 7]\n ],\n state_indices_by_distance=[\n [0],\n [1, 2, 3]\n ]\n)");
EXPECT_EQ(TupleGraph(novelty_base_1, state_space, 0, true).compute_repr(), "TupleGraph(\n root_state_index=0,\n tuple_nodes=[ TupleNode(index=0, tuple_index=0, state_indices={0}, predecessors=[], successors=[1, 2, 3]),\n TupleNode(index=1, tuple_index=2, state_indices={1}, predecessors=[0], successors=[]),\n TupleNode(index=2, tuple_index=7, state_indices={2}, predecessors=[0], successors=[]),\n TupleNode(index=3, tuple_index=8, state_indices={3}, predecessors=[0], successors=[])\n ],\n node_indices_by_distance=[\n [0],\n [1, 2, 3]\n ],\n state_indices_by_distance=[\n [0],\n [1, 2, 3]\n ]\n)");
EXPECT_EQ(TupleGraph(novelty_base_1, state_space, 0).compute_repr(), "TupleGraph(\n root_state_index=0,\n tuple_nodes=[ TupleNode(index=0, tuple_index=0, state_indices={0}, predecessors=[], successors=[1, 2, 3]),\n TupleNode(index=1, tuple_index=2, state_indices={1}, predecessors=[0], successors=[]),\n TupleNode(index=2, tuple_index=7, state_indices={2}, predecessors=[0], successors=[]),\n TupleNode(index=3, tuple_index=8, state_indices={3}, predecessors=[0], successors=[])\n ],\n node_indices_by_distance=[\n [0],\n [1, 2, 3]\n ],\n state_indices_by_distance=[\n [0],\n [1, 2, 3]\n ]\n)");

auto result2 = generate_state_space("domain.pddl", "p-2-0.pddl");
auto state_space2 = result2.state_space;
auto novelty_base_2 = std::make_shared<NoveltyBase>(
state_space2->get_instance_info()->get_atoms().size(),
2);

EXPECT_EQ(TupleGraph(novelty_base_2, state_space2, 0, true).compute_repr(), "TupleGraph(\n root_state_index=0,\n tuple_nodes=[ TupleNode(index=0, tuple_index=0, state_indices={0}, predecessors=[], successors=[1, 5, 8, 12, 15]),\n TupleNode(index=1, tuple_index=54, state_indices={1}, predecessors=[0], successors=[]),\n TupleNode(index=2, tuple_index=79, state_indices={16}, predecessors=[3, 7], successors=[]),\n TupleNode(index=3, tuple_index=82, state_indices={12}, predecessors=[6], successors=[2]),\n TupleNode(index=4, tuple_index=93, state_indices={6}, predecessors=[8], successors=[7]),\n TupleNode(index=5, tuple_index=104, state_indices={4}, predecessors=[0], successors=[6, 14]),\n TupleNode(index=6, tuple_index=106, state_indices={10}, predecessors=[5], successors=[3, 7]),\n TupleNode(index=7, tuple_index=123, state_indices={12}, predecessors=[4, 6], successors=[2]),\n TupleNode(index=8, tuple_index=124, state_indices={2}, predecessors=[0], successors=[4, 17]),\n TupleNode(index=9, tuple_index=131, state_indices={19}, predecessors=[10, 11], successors=[]),\n TupleNode(index=10, tuple_index=134, state_indices={14}, predecessors=[13], successors=[9]),\n TupleNode(index=11, tuple_index=135, state_indices={14}, predecessors=[13, 16], successors=[9]),\n TupleNode(index=12, tuple_index=143, state_indices={3}, predecessors=[0], successors=[13, 14]),\n TupleNode(index=13, tuple_index=145, state_indices={8}, predecessors=[12], successors=[10, 11]),\n TupleNode(index=14, tuple_index=151, state_indices={9}, predecessors=[5, 12], successors=[]),\n TupleNode(index=15, tuple_index=156, state_indices={5}, predecessors=[0], successors=[16, 17]),\n TupleNode(index=16, tuple_index=158, state_indices={11}, predecessors=[15], successors=[11]),\n TupleNode(index=17, tuple_index=163, state_indices={7}, predecessors=[8, 15], successors=[])\n ],\n node_indices_by_distance=[\n [0],\n [1, 5, 8, 12, 15],\n [4, 6, 13, 14, 16, 17],\n [3, 7, 10, 11],\n [2, 9]\n ],\n state_indices_by_distance=[\n [0],\n [1, 2, 3, 4, 5],\n [6, 7, 8, 9, 10, 11],\n [12, 13, 14, 15],\n [16, 17, 18, 19, 20, 21]\n ]\n)");

std::ofstream file("out.dot");
const auto tg = TupleGraph(novelty_base_2, state_space2, 2, true);
file << tg.to_dot(1);
file.close();
std::cout << tg.compute_repr() << std::endl;
EXPECT_EQ(TupleGraph(novelty_base_2, state_space2, 0).compute_repr(), "TupleGraph(\n root_state_index=0,\n tuple_nodes=[ TupleNode(index=0, tuple_index=0, state_indices={0}, predecessors=[], successors=[1, 4, 6, 10, 13]),\n TupleNode(index=1, tuple_index=41, state_indices={1}, predecessors=[0], successors=[]),\n TupleNode(index=2, tuple_index=79, state_indices={16}, predecessors=[3], successors=[]),\n TupleNode(index=3, tuple_index=82, state_indices={12}, predecessors=[5, 7], successors=[2]),\n TupleNode(index=4, tuple_index=92, state_indices={2}, predecessors=[0], successors=[5, 15]),\n TupleNode(index=5, tuple_index=93, state_indices={6}, predecessors=[4], successors=[3]),\n TupleNode(index=6, tuple_index=105, state_indices={4}, predecessors=[0], successors=[7, 12]),\n TupleNode(index=7, tuple_index=106, state_indices={10}, predecessors=[6], successors=[3]),\n TupleNode(index=8, tuple_index=131, state_indices={19}, predecessors=[9], successors=[]),\n TupleNode(index=9, tuple_index=135, state_indices={14}, predecessors=[11, 14], successors=[8]),\n TupleNode(index=10, tuple_index=144, state_indices={3}, predecessors=[0], successors=[11, 12]),\n TupleNode(index=11, tuple_index=145, state_indices={8}, predecessors=[10], successors=[9]),\n TupleNode(index=12, tuple_index=151, state_indices={9}, predecessors=[6, 10], successors=[]),\n TupleNode(index=13, tuple_index=157, state_indices={5}, predecessors=[0], successors=[14, 15]),\n TupleNode(index=14, tuple_index=158, state_indices={11}, predecessors=[13], successors=[9]),\n TupleNode(index=15, tuple_index=163, state_indices={7}, predecessors=[4, 13], successors=[])\n ],\n node_indices_by_distance=[\n [0],\n [1, 4, 6, 10, 13],\n [5, 7, 11, 12, 14, 15],\n [3, 9],\n [2, 8]\n ],\n state_indices_by_distance=[\n [0],\n [1, 2, 3, 4, 5],\n [6, 7, 8, 9, 10, 11],\n [12, 13, 14, 15],\n [16, 17, 18, 19, 20, 21]\n ]\n)");

//std::ofstream file("out.dot");
//const auto tg = TupleGraph(novelty_base_2, state_space2, 2);
//file << tg.to_dot(1);
//file.close();
//std::cout << tg.compute_repr() << std::endl;
}

}

0 comments on commit b667e6a

Please sign in to comment.