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 8b3658a1d..b193dcd24 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 diff --git a/ioh/src/numpy.hpp b/ioh/src/numpy.hpp index e811061f2..8c8161eec 100644 --- a/ioh/src/numpy.hpp +++ b/ioh/src/numpy.hpp @@ -8,6 +8,18 @@ namespace py = pybind11; +template +py::array_t make_mutable_array(std::vector& v, py::object owner) +{ + return py::array_t( + {v.size()}, // shape + {sizeof(T)}, // stride + v.data(), // pointer + owner // keep the parent (e.g. Solution) alive + ); +} + + template py::array_t make_array(const std::vector& x) { diff --git a/ioh/src/problem.cpp b/ioh/src/problem.cpp index 5519f5537..b6ff41218 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_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 d3f4ed106..a3220be43 100644 --- a/ioh/src/problem_helpers.cpp +++ b/ioh/src/problem_helpers.cpp @@ -32,8 +32,8 @@ void define_solution(py::module &m, const std::string &name) )pbdoc") .def_property( "x", - [](const Class &c) { - return make_array(c.x); + [](Class &c) { + 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")