From ae796aab806a3468e9ef46a86d0d5f68a212fcc9 Mon Sep 17 00:00:00 2001 From: Dinu23 Date: Tue, 23 Sep 2025 10:17:03 +0200 Subject: [PATCH 1/3] Pybind Graph and Update Bounds Submodular --- .gitignore | 12 ++++----- .../ioh/problem/submodular/graph_problem.hpp | 5 +++- ioh/src/integer.cpp | 26 +++++++++++++++++-- 3 files changed, 34 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index 4ca376be8..eb6e20fdd 100644 --- a/.gitignore +++ b/.gitignore @@ -9,7 +9,7 @@ out/ *.dll *.pyd - +aux/ # Ignore Vagrant runtime files. .vagrant/ @@ -185,11 +185,11 @@ compile_commands.json debug release ioh_data -run.py -py10/ - -gsemo.cpp - +run.py +py10/ + +gsemo.cpp + 2022-SO-BO-main .conda_environment concepts diff --git a/include/ioh/problem/submodular/graph_problem.hpp b/include/ioh/problem/submodular/graph_problem.hpp index 7b1bfb0b3..606e03e22 100644 --- a/include/ioh/problem/submodular/graph_problem.hpp +++ b/include/ioh/problem/submodular/graph_problem.hpp @@ -26,6 +26,7 @@ namespace ioh::problem::submodular // added by saba //! File with constraint variances std::string constraint_variances; + //! The root path of the files fs::path root; @@ -71,6 +72,8 @@ namespace ioh::problem::submodular meta.root = common::file::utils::get_static_root(); return meta; } + + }; //! Abstraction of graph data @@ -273,7 +276,7 @@ namespace ioh::problem::submodular const std::shared_ptr &graph) : IntegerSingleObjective( MetaData(problem_id, instance, name, graph->dimension(), common::OptimizationType::MAX), - Bounds(graph->dimension()), ConstraintSet(std::make_shared(graph))), + Bounds(graph->dimension(),0,1), ConstraintSet(std::make_shared(graph))), graph(graph) { } diff --git a/ioh/src/integer.cpp b/ioh/src/integer.cpp index 9bd3416f1..ac0ae58ed 100644 --- a/ioh/src/integer.cpp +++ b/ioh/src/integer.cpp @@ -360,6 +360,28 @@ void define_submodular_problems(py::module &m) py::class_, std::shared_ptr>(m, "PWTConstraint") .def("__repr__", &pwt::PWTConstraint::repr); + py::class_(m, "GraphMeta", "Graph metadata") + .def_readonly("edge_file", &graph::Meta::edge_file, "File with edge data") + .def_readonly("edge_weights", &graph::Meta::edge_weights, "File with edge weights") + .def_readonly("vertex_weights", &graph::Meta::vertex_weights, "File with vertex weights") + .def_readonly("constraint_weights", &graph::Meta::constraint_weights, "File with constraint weights") + .def_readonly("constraint_variances", &graph::Meta::constraint_variances, "File with constraint variances") + .def_readonly("is_edge", &graph::Meta::is_edge, "Edge type flag") + .def_readonly("digraph", &graph::Meta::digraph, "Is digraph flag") + .def_readonly("constraint_limit", &graph::Meta::constraint_limit, "Maximum value of the constraint") + .def_readonly("n_vertices", &graph::Meta::n_vertices, "Number of vertices"); + + py::class_>(m, "Graph", "Graph data structure") + .def("__repr__", &graph::Graph::repr) + .def("dimension", &graph::Graph::dimension, "Get the dimension of the graph") + .def_readonly("meta", &graph::Graph::meta, "Graph metadata") + .def_readonly("loaded", &graph::Graph::loaded, "Whether the graph is loaded in memory") + .def_readonly("adjacency_list", &graph::Graph::adjacency_list, "Adjacency list representation of the graph") + .def_readonly("edges", &graph::Graph::edges, "Edge list representation of the graph") + .def_readonly("constraint_weights", &graph::Graph::constraint_weights, "Constraint weights") + .def_readonly("constraint_variances", &graph::Graph::constraint_variances, "Constraint variances") + .def_readonly("edge_weights", &graph::Graph::edge_weights, "Edge weights") + .def_readonly("vertex_weights", &graph::Graph::vertex_weights, "Vertex weights"); py::class_>(m, "GraphProblem", "Graph type problem", @@ -415,8 +437,8 @@ void define_submodular_problems(py::module &m) )pbdoc") .def_property_readonly_static( "problems", [](py::object) { return ioh::common::Factory::instance().map(); }, - "All registered problems"); - + "All registered problems") + .def_property_readonly("graph", [](const GraphProblem& self) { return self.graph; }, "The underlying graph structure"); py::class_>(m, "MaxCut", py::is_final(), R"pbdoc( Max-Cut problems From 9299d83d1128455dc5bb388fd54ed4bd7c338b7f Mon Sep 17 00:00:00 2001 From: Dinu23 Date: Wed, 24 Sep 2025 13:39:07 +0200 Subject: [PATCH 2/3] memory fix --- ioh/src/numpy.hpp | 15 +++++++-------- ioh/src/problem.cpp | 6 +++--- ioh/src/problem_helpers.cpp | 2 +- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/ioh/src/numpy.hpp b/ioh/src/numpy.hpp index d0529a545..8c8161eec 100644 --- a/ioh/src/numpy.hpp +++ b/ioh/src/numpy.hpp @@ -9,18 +9,17 @@ namespace py = pybind11; template -py::array_t make_mutable_array(const std::vector& x) +py::array_t make_mutable_array(std::vector& v, py::object owner) { - auto base = py::capsule(x.data(), [](void*){/* no free */}); - py::array_t arr( - {static_cast(x.size())}, - {static_cast(sizeof(T))}, - x.data(), - base + return py::array_t( + {v.size()}, // shape + {sizeof(T)}, // stride + v.data(), // pointer + owner // keep the parent (e.g. Solution) alive ); - return arr; } + template py::array_t make_array(const std::vector& x) { diff --git a/ioh/src/problem.cpp b/ioh/src/problem.cpp index a594b3d0d..4b0fc242c 100644 --- a/ioh/src/problem.cpp +++ b/ioh/src/problem.cpp @@ -237,9 +237,9 @@ void define_wrapper_functions(py::module &m, const std::string &class_name, cons if (tx) { static bool r = register_python_fn(tx.value()); - py::gil_scoped_acquire gil; - py::list px = py::cast(tx.value()(make_mutable_array(x), iid)); - + py::gil_scoped_acquire gil; + py::list px = py::cast(tx.value()(make_mutable_array(x, py::cast(&x)), iid)); + if (px.size() == x.size()) return px.cast>(); else diff --git a/ioh/src/problem_helpers.cpp b/ioh/src/problem_helpers.cpp index b51cf79da..5e65f1cf7 100644 --- a/ioh/src/problem_helpers.cpp +++ b/ioh/src/problem_helpers.cpp @@ -33,7 +33,7 @@ void define_solution(py::module &m, const std::string &name) .def_property( "x", [](const Class &c) { - return make_mutable_array(c.x); + return make_mutable_array(c.x, py::cast(&c)); }, [](Class &self, const std::vector &x) { self.x = x; }, "The search point in a search space, e.g., R^d or {0,1}^d") From ae6b519cc6776702342d10aae152647d14d7313a Mon Sep 17 00:00:00 2001 From: Dinu23 Date: Wed, 24 Sep 2025 13:49:55 +0200 Subject: [PATCH 3/3] small fix --- ioh/src/problem_helpers.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ioh/src/problem_helpers.cpp b/ioh/src/problem_helpers.cpp index 5e65f1cf7..a3220be43 100644 --- a/ioh/src/problem_helpers.cpp +++ b/ioh/src/problem_helpers.cpp @@ -32,7 +32,7 @@ void define_solution(py::module &m, const std::string &name) )pbdoc") .def_property( "x", - [](const Class &c) { + [](Class &c) { return make_mutable_array(c.x, py::cast(&c)); }, [](Class &self, const std::vector &x) { self.x = x; },