|
| 1 | +#include <pybind11/pybind11.h> |
| 2 | +#include <pybind11/numpy.h> |
| 3 | +#include <pybind11/operators.h> |
| 4 | +#include <pybind11/stl.h> |
| 5 | +#include "algo/greedy/GreedySampler.h" |
| 6 | +#include "linalg/Vector.h" |
| 7 | + |
| 8 | +namespace py = pybind11; |
| 9 | +using namespace CAROM; |
| 10 | +using namespace std; |
| 11 | + |
| 12 | + |
| 13 | +class PyGreedySampler : public GreedySampler { |
| 14 | +public: |
| 15 | + using GreedySampler::GreedySampler; |
| 16 | + |
| 17 | + void save(std::string base_file_name) override { |
| 18 | + PYBIND11_OVERRIDE(void,GreedySampler,save,base_file_name ); |
| 19 | + } |
| 20 | +protected: |
| 21 | + void constructParameterPoints() override { |
| 22 | + PYBIND11_OVERRIDE_PURE(void, GreedySampler, constructParameterPoints,); |
| 23 | + } |
| 24 | + void getNextParameterPointAfterConvergenceFailure() override { |
| 25 | + PYBIND11_OVERRIDE_PURE(void, GreedySampler, getNextParameterPointAfterConvergenceFailure,); |
| 26 | + } |
| 27 | +}; |
| 28 | + |
| 29 | +void init_GreedySampler(pybind11::module_ &m) { |
| 30 | + py::class_<GreedyErrorIndicatorPoint>(m, "GreedyErrorIndicatorPoint") |
| 31 | + .def_property_readonly("point", [](GreedyErrorIndicatorPoint &self) { |
| 32 | + return self.point.get(); |
| 33 | + }) |
| 34 | + .def_property_readonly("localROM", [](GreedyErrorIndicatorPoint &self) { |
| 35 | + return self.localROM.get(); |
| 36 | + }); |
| 37 | + |
| 38 | + py::class_<GreedySampler,PyGreedySampler>(m, "GreedySampler") |
| 39 | + .def(py::init<std::vector<Vector>, bool, double, double, double, int, int, std::string, std::string, bool, int, bool>(), |
| 40 | + py::arg("parameter_points"), |
| 41 | + py::arg("check_local_rom"), |
| 42 | + py::arg("relative_error_tolerance"), |
| 43 | + py::arg("alpha"), |
| 44 | + py::arg("max_clamp"), |
| 45 | + py::arg("subset_size"), |
| 46 | + py::arg("convergence_subset_size"), |
| 47 | + py::arg("output_log_path") = "", |
| 48 | + py::arg("warm_start_file_name") = "", |
| 49 | + py::arg("use_centroid") = true, |
| 50 | + py::arg("random_seed") = 1, |
| 51 | + py::arg("debug_algorithm") = false) |
| 52 | + .def(py::init<std::vector<double>,bool, double, double, double, int, int, std::string, std::string, bool, int, bool>(), |
| 53 | + py::arg("parameter_points"), |
| 54 | + py::arg("check_local_rom"), |
| 55 | + py::arg("relative_error_tolerance"), |
| 56 | + py::arg("alpha"), |
| 57 | + py::arg("max_clamp"), |
| 58 | + py::arg("subset_size"), |
| 59 | + py::arg("convergence_subset_size"), |
| 60 | + py::arg("output_log_path") = "", |
| 61 | + py::arg("warm_start_file_name") = "", |
| 62 | + py::arg("use_centroid") = true, |
| 63 | + py::arg("random_seed") = 1, |
| 64 | + py::arg("debug_algorithm") = false) |
| 65 | + .def(py::init<Vector, Vector, int, bool, double, double, double, int, int,std::string, std::string, bool, int, bool>(), |
| 66 | + py::arg("param_space_min"), py::arg("param_space_max"), py::arg("num_parameter_points"), |
| 67 | + py::arg("check_local_rom"), py::arg("relative_error_tolerance"), py::arg("alpha"), |
| 68 | + py::arg("max_clamp"), py::arg("subset_size"), py::arg("convergence_subset_size"), |
| 69 | + py::arg("output_log_path") = "", py::arg("warm_start_file_name") = "", |
| 70 | + py::arg("use_centroid") = true, py::arg("random_seed") = 1, |
| 71 | + py::arg("debug_algorithm") = false |
| 72 | + ) |
| 73 | + .def(py::init<double, double, int, bool, double, double, double, int, int,std::string, std::string, bool, int, bool>(), |
| 74 | + py::arg("param_space_min"), py::arg("param_space_max"), py::arg("num_parameter_points"), |
| 75 | + py::arg("check_local_rom"), py::arg("relative_error_tolerance"), py::arg("alpha"), |
| 76 | + py::arg("max_clamp"), py::arg("subset_size"), py::arg("convergence_subset_size"), |
| 77 | + py::arg("output_log_path") = "", py::arg("warm_start_file_name") = "", |
| 78 | + py::arg("use_centroid") = true, py::arg("random_seed") = 1, |
| 79 | + py::arg("debug_algorithm") = false |
| 80 | + ) |
| 81 | + .def(py::init<std::string, std::string>(), py::arg("base_file_name"), py::arg("output_log_path") = "") |
| 82 | + .def("getNextParameterPoint", [](GreedySampler& self) -> std::unique_ptr<Vector> { |
| 83 | + std::shared_ptr<Vector> result = self.getNextParameterPoint(); |
| 84 | + return std::make_unique<Vector>(*(result.get())); |
| 85 | + }) |
| 86 | + .def("getNextPointRequiringRelativeError", [](GreedySampler& self) -> GreedyErrorIndicatorPoint { |
| 87 | + // Create a deepcopy of the struct, otherwise it will get freed twice |
| 88 | + GreedyErrorIndicatorPoint point = self.getNextPointRequiringRelativeError(); |
| 89 | + Vector *t_pt = nullptr; |
| 90 | + Vector *t_lROM = nullptr; |
| 91 | + |
| 92 | + if (point.point) |
| 93 | + { |
| 94 | + t_pt = new Vector(*(point.point)); |
| 95 | + } |
| 96 | + |
| 97 | + if (point.localROM) |
| 98 | + { |
| 99 | + t_lROM = new Vector(*(point.localROM)); |
| 100 | + } |
| 101 | + |
| 102 | + return createGreedyErrorIndicatorPoint(t_pt, t_lROM); |
| 103 | + }, py::return_value_policy::reference) |
| 104 | + .def("getNextPointRequiringErrorIndicator", [](GreedySampler& self) -> GreedyErrorIndicatorPoint { |
| 105 | + // Create a deepcopy of the struct, otherwise it will get freed twice |
| 106 | + GreedyErrorIndicatorPoint point = self.getNextPointRequiringErrorIndicator(); |
| 107 | + |
| 108 | + Vector *t_pt = nullptr; |
| 109 | + Vector *t_lROM = nullptr; |
| 110 | + |
| 111 | + if (point.point) |
| 112 | + { |
| 113 | + t_pt = new Vector(*(point.point)); |
| 114 | + } |
| 115 | + |
| 116 | + if (point.localROM) |
| 117 | + { |
| 118 | + t_lROM = new Vector(*(point.localROM)); |
| 119 | + } |
| 120 | + |
| 121 | + return createGreedyErrorIndicatorPoint(t_pt, t_lROM); |
| 122 | + }, py::return_value_policy::reference) |
| 123 | + .def("setPointRelativeError", (void (GreedySampler::*) (double))&GreedySampler::setPointRelativeError) |
| 124 | + .def("setPointErrorIndicator", (void (GreedySampler::*) (double,int)) &GreedySampler::setPointErrorIndicator) |
| 125 | + .def("getNearestNonSampledPoint", (int (GreedySampler::*) (CAROM::Vector)) &GreedySampler::getNearestNonSampledPoint) |
| 126 | + .def("getNearestROM", [](GreedySampler& self, Vector point) -> std::unique_ptr<Vector> { |
| 127 | + std::shared_ptr<Vector> result = self.getNearestROM(point); |
| 128 | + if (!result) |
| 129 | + { |
| 130 | + return nullptr; |
| 131 | + } |
| 132 | + return std::make_unique<Vector>(*(result.get())); |
| 133 | + }) |
| 134 | + .def("getParameterPointDomain", &GreedySampler::getParameterPointDomain) |
| 135 | + .def("getSampledParameterPoints", &GreedySampler::getSampledParameterPoints) |
| 136 | + .def("save", &GreedySampler::save) |
| 137 | + .def("__del__", [](GreedySampler& self){ self.~GreedySampler(); }) |
| 138 | + .def("isComplete", &GreedySampler::isComplete); |
| 139 | + |
| 140 | + m.def("createGreedyErrorIndicatorPoint", [](Vector* point, Vector* localROM) { |
| 141 | + return createGreedyErrorIndicatorPoint(point, localROM); |
| 142 | + }); |
| 143 | + m.def("createGreedyErrorIndicatorPoint", [](Vector* point, std::shared_ptr<Vector>& localROM) { |
| 144 | + return createGreedyErrorIndicatorPoint(point, localROM); |
| 145 | + }); |
| 146 | + m.def("getNearestPoint", [](std::vector<Vector>& paramPoints,Vector point) { |
| 147 | + return getNearestPoint(paramPoints, point); |
| 148 | + }); |
| 149 | + m.def("getNearestPoint", [](std::vector<double>& paramPoints, double point) { |
| 150 | + return getNearestPoint(paramPoints, point); |
| 151 | + }); |
| 152 | + m.def("getNearestPointIndex", [](std::vector<Vector> paramPoints, Vector point) { |
| 153 | + return getNearestPointIndex(paramPoints, point); |
| 154 | + }); |
| 155 | + m.def("getNearestPointIndex", [](std::vector<double> paramPoints, double point) { |
| 156 | + return getNearestPointIndex(paramPoints, point); |
| 157 | + }); |
| 158 | +} |
0 commit comments