Skip to content

Commit

Permalink
Merge pull request #1 from DillonZChen/dev
Browse files Browse the repository at this point in the history
0.5.2
  • Loading branch information
DillonZChen authored Sep 23, 2024
2 parents e09d7be + e74d3bd commit d7aef25
Show file tree
Hide file tree
Showing 13 changed files with 164 additions and 53 deletions.
36 changes: 9 additions & 27 deletions docs/examples/blocksworld.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
"metadata": {},
"outputs": [],
"source": [
"%pip install numpy scikit-learn wlplan\n",
"%pip install -i https://test.pypi.org/simple/ pymimir-dzc-fork==0.1.3"
"%pip install numpy scikit-learn wlplan"
]
},
{
Expand All @@ -19,7 +18,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -43,7 +42,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -69,7 +68,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -166,18 +165,9 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"X.shape=(1348, 10442)\n",
"y.shape=(1348,)\n"
]
}
],
"outputs": [],
"source": [
"feature_generator = WLFeatures(domain=wlplan_domain, iterations=4)\n",
"feature_generator.collect(dataset)\n",
Expand All @@ -196,17 +186,9 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss=1.0842255191254774e-17\n"
]
}
],
"outputs": [],
"source": [
"linear_kernel = DotProduct(sigma_0=0, sigma_0_bounds=\"fixed\")\n",
"model = GaussianProcessRegressor(kernel=linear_kernel, alpha=1e-7, random_state=0)\n",
Expand Down Expand Up @@ -240,7 +222,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.14"
"version": "3.10.12"
}
},
"nbformat": 4,
Expand Down
2 changes: 2 additions & 0 deletions include/data/dataset.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ namespace data {

Dataset(const planning::Domain &domain, const std::vector<ProblemStates> &data);

size_t get_size() const;

private:
std::unordered_map<std::string, int> predicate_to_arity;

Expand Down
24 changes: 23 additions & 1 deletion include/feature_generation/wl_features.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

class int_vector_hasher {
public:

// https://stackoverflow.com/a/27216842
std::size_t operator()(std::vector<int> const &vec) const {
std::size_t seed = vec.size();
Expand Down Expand Up @@ -71,6 +70,12 @@ namespace feature_generation {
// for iteration j = 0, ..., iterations - 1
std::vector<std::vector<long>> seen_colour_statistics;

// training statistics
int n_seen_graphs;
int n_seen_nodes;
int n_seen_edges;
std::set<int> seen_initial_colours;

public:
WLFeatures(const planning::Domain &domain,
std::string graph_representation,
Expand All @@ -85,11 +90,18 @@ namespace feature_generation {
// collect training colours
void collect(const data::Dataset dataset);

void collect(const planning::State state);

void collect(const std::vector<graph::Graph> &graphs);

// set problem for graph generator if it exists
void set_problem(const planning::Problem &problem);

// get string representation of WL colours agnostic to the number of collected colours
std::string get_string_representation(const Embedding &embedding);

std::string get_string_representation(const planning::State &state);

// assumes training is done, and returns a feature matrix X
std::vector<Embedding> embed(const data::Dataset &dataset);

Expand Down Expand Up @@ -121,6 +133,16 @@ namespace feature_generation {

std::vector<long> get_unseen_counts() const { return seen_colour_statistics[0]; };

int get_n_seen_graphs() const { return n_seen_graphs; }

int get_n_seen_nodes() const { return n_seen_nodes; }

int get_n_seen_edges() const { return n_seen_edges; }

int get_n_seen_initial_colours() const { return seen_initial_colours.size(); }

int get_n_seen_refined_colours() const { return (int)colour_hash.size(); }

/* Other useful functions */

std::unordered_map<std::vector<int>, int, int_vector_hasher>
Expand Down
11 changes: 6 additions & 5 deletions include/graph/ilg_generator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ enum class ILGFactDescription { ILG_FACT_DESCRIPTIONS };
namespace graph {
class ILGGenerator : public GraphGenerator {
public:
ILGGenerator(const planning::Domain &domain);
ILGGenerator(const planning::Domain &domain, bool differentiate_constant_objects);

// Change the base graph based on the input problem
void set_problem(const planning::Problem &problem) override;
Expand All @@ -54,16 +54,17 @@ namespace graph {
void dump_graph() const override;

private:
/* The following variables remain constant for all problems */
const planning::Domain &domain;
const std::unordered_map<std::string, int> predicate_to_colour;
bool differentiate_constant_objects;

/* These variables get reset every time a new problem is set */
std::shared_ptr<Graph> base_graph;
std::unordered_set<std::string> positive_goal_names;
std::unordered_set<std::string> negative_goal_names;
std::shared_ptr<planning::Problem> problem;

/* The following variables remain constant for all problems */
const planning::Domain &domain;
const std::unordered_map<std::string, int> predicate_to_colour;

// Do not use a vector here because colours can be negative, i.e. constant objects
std::map<int, std::string> colour_to_description;
int fact_colour(const int predicate_idx, const ILGFactDescription &fact_description) const;
Expand Down
27 changes: 27 additions & 0 deletions install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash

# Show commands
set -x
# Exit on the first error
set -e

# Build with all cores
export MAKEFLAGS="-j$(nproc)"

# Install the package from sources
mkdir -p _wlplan
pip install . -v

# Make sure required tools are installed
pip install pybind11-stubgen

# Generate stubs
rm -rf _wlplan/*.pyi
pybind11-stubgen _wlplan -o .

# Generate documentation
pip install sphinx sphinx_rtd_theme
cd docs
rm -rf _build/
make html
cd ..
8 changes: 8 additions & 0 deletions src/data/dataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,12 @@ namespace data {
}
}
}

size_t Dataset::get_size() const {
size_t ret = 0;
for (const auto &problem_states : data) {
ret += problem_states.states.size();
}
return ret;
}
} // namespace data
30 changes: 28 additions & 2 deletions src/feature_generation/wl_features.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ namespace feature_generation {
neighbour_container = std::make_shared<NeighbourContainer>(multiset_hash);
seen_colour_statistics = std::vector<std::vector<long>>(2, std::vector<long>(iterations, 0));
store_weights = false;

n_seen_graphs = 0;
n_seen_nodes = 0;
n_seen_edges = 0;
seen_initial_colours = std::set<int>();
}

WLFeatures::WLFeatures(const std::string &filename) {
Expand Down Expand Up @@ -202,6 +207,9 @@ namespace feature_generation {
const auto &graph = graphs[graph_i];
std::unordered_map<int, int> histogram;
int n_nodes = graph.nodes.size();
n_seen_graphs++;
n_seen_nodes += n_nodes;
n_seen_edges += graph.get_n_edges();
std::vector<int> colours(n_nodes);
for (int node_i = 0; node_i < n_nodes; node_i++) {
cur_collecting_layer = 0;
Expand All @@ -211,6 +219,7 @@ namespace feature_generation {
}
histogram[col]++;
colours[node_i] = col;
seen_initial_colours.insert(col);
}
graph_histograms.push_back(histogram);
graph_colours.push_back(colours);
Expand Down Expand Up @@ -293,6 +302,23 @@ namespace feature_generation {
}
}

std::string WLFeatures::get_string_representation(const Embedding &embedding) {
std::string str_embed = "";
for (size_t i = 0; i < embedding.size(); i++) {
int count = embedding[i];
if (count == 0) {
continue;
}
str_embed += std::to_string(i) + "." + std::to_string(count) + ".";
}
return str_embed;
}

std::string WLFeatures::get_string_representation(const planning::State &state) {
Embedding x = embed(state);
return get_string_representation(x);
}

std::vector<Embedding> WLFeatures::embed(const data::Dataset &dataset) {
std::vector<graph::Graph> graphs = convert_to_graphs(dataset);
if (graphs.size() == 0) {
Expand Down Expand Up @@ -422,8 +448,8 @@ namespace feature_generation {
return int_colour_hash;
}

std::unordered_map<std::string, int>
WLFeatures::int_to_str_colour_hash(std::unordered_map<std::vector<int>, int, int_vector_hasher> int_colour_hash) const {
std::unordered_map<std::string, int> WLFeatures::int_to_str_colour_hash(
std::unordered_map<std::vector<int>, int, int_vector_hasher> int_colour_hash) const {
std::unordered_map<std::string, int> str_colour_hash;
for (const auto &pair : int_colour_hash) {
std::string colour_str = "";
Expand Down
2 changes: 1 addition & 1 deletion src/graph/graph_generator_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace graph {
std::shared_ptr<GraphGenerator> create_graph_generator(const std::string &name,
const planning::Domain &domain) {
if (name == "ilg") {
return std::make_shared<ILGGenerator>(domain);
return std::make_shared<ILGGenerator>(domain, false);
} else if (name == "custom") {
return NULL;
} else {
Expand Down
42 changes: 28 additions & 14 deletions src/graph/ilg_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@ char const *fact_description_name[] = {ILG_FACT_DESCRIPTIONS};
#define to_atom_node(x) x.to_string()

namespace graph {
ILGGenerator::ILGGenerator(const planning::Domain &domain)
: domain(domain), predicate_to_colour(domain.predicate_to_colour) {
/// initialise initial node colours
// add constant object colours
for (size_t i = 0; i < domain.constant_objects.size(); i++) {
int colour = -(i + 1);
colour_to_description[colour] = "_constant_object_ " + domain.constant_objects[i];
ILGGenerator::ILGGenerator(const planning::Domain &domain, bool differentiate_constant_objects)
: domain(domain),
predicate_to_colour(domain.predicate_to_colour),
differentiate_constant_objects(differentiate_constant_objects) {
// initialise initial node colours
if (differentiate_constant_objects) {
// add constant object colours
for (size_t i = 0; i < domain.constant_objects.size(); i++) {
int colour = -(i + 1);
colour_to_description[colour] = "_constant_object_ " + domain.constant_objects[i];
}
}

colour_to_description[0] = "_object_";
Expand All @@ -37,31 +41,37 @@ namespace graph {
this->problem = std::make_shared<planning::Problem>(problem);

/// add nodes
int colour;

// add constant object nodes
for (size_t i = 0; i < problem.get_constant_objects().size(); i++) {
std::string node = to_obj_node(domain.constant_objects[i]);
int colour = -(i + 1);
if (differentiate_constant_objects) {
colour = -(i + 1);
} else {
colour = 0;
}
graph.add_node(node, colour);
}

// objects
for (const auto &object : problem.get_problem_objects()) {
std::string node = to_obj_node(object);
int colour = 0;
colour = 0;
graph.add_node(node, colour);
}

// atoms
for (const auto &atom : problem.get_positive_goals()) {
std::string node = to_atom_node(atom);
int colour = fact_colour(atom, ILGFactDescription::F_POS_GOAL);
colour = fact_colour(atom, ILGFactDescription::F_POS_GOAL);
graph.add_node(node, colour);
positive_goal_names.insert(node);
}

for (const auto &atom : problem.get_negative_goals()) {
std::string node = to_atom_node(atom);
int colour = fact_colour(atom, ILGFactDescription::F_NEG_GOAL);
colour = fact_colour(atom, ILGFactDescription::F_NEG_GOAL);
graph.add_node(node, colour);
negative_goal_names.insert(node);
}
Expand Down Expand Up @@ -116,7 +126,8 @@ namespace graph {
neg_goal_changed_pred.push_back(pred_idx);
}
} else {
atom_node = graph->add_node(atom_node_str, fact_colour(pred_idx, ILGFactDescription::NON_GOAL));
atom_node =
graph->add_node(atom_node_str, fact_colour(pred_idx, ILGFactDescription::NON_GOAL));
if (store_changes) {
n_nodes_added++;
}
Expand All @@ -138,11 +149,14 @@ namespace graph {

void ILGGenerator::reset_graph() const {
for (size_t i = 0; i < pos_goal_changed.size(); i++) {
base_graph->change_node_colour(pos_goal_changed[i], fact_colour(pos_goal_changed_pred[i], ILGFactDescription::F_POS_GOAL));
base_graph->change_node_colour(
pos_goal_changed[i],
fact_colour(pos_goal_changed_pred[i], ILGFactDescription::F_POS_GOAL));
}

for (const auto &node : neg_goal_changed) {
base_graph->change_node_colour(node, fact_colour(neg_goal_changed_pred[node], ILGFactDescription::F_NEG_GOAL));
base_graph->change_node_colour(
node, fact_colour(neg_goal_changed_pred[node], ILGFactDescription::F_NEG_GOAL));
}

for (int i = 0; i < n_nodes_added; i++) {
Expand Down
Loading

0 comments on commit d7aef25

Please sign in to comment.