|
| 1 | +#include <pybind11/pybind11.h> |
| 2 | +#include <pybind11/stl.h> |
| 3 | + |
| 4 | +#include <map> |
| 5 | +#include <mutex> |
| 6 | + |
| 7 | +#include "lp_data/HighsOptions.h" |
| 8 | + |
| 9 | +namespace py = pybind11; |
| 10 | + |
| 11 | +class HighsOptionsManager { |
| 12 | +public: |
| 13 | + HighsOptionsManager() { |
| 14 | + initialize_log_options(); |
| 15 | + for (const auto &record : highs_options_.records) { |
| 16 | + record_type_lookup_.emplace(record->name, record->type); |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + const HighsOptions &get_highs_options() const { return highs_options_; } |
| 21 | + |
| 22 | + const std::map<std::string, HighsOptionType> &get_record_type_lookup() const { |
| 23 | + return record_type_lookup_; |
| 24 | + } |
| 25 | + |
| 26 | + template <typename OptionRecordType, typename T> |
| 27 | + bool check_option(const std::string &name, const T value) { |
| 28 | + std::lock_guard<std::mutex> guard(highs_options_mutex); |
| 29 | + HighsInt idx = 0; |
| 30 | + const OptionStatus idx_status = getOptionIndex( |
| 31 | + highs_log_options, name.c_str(), highs_options_.records, idx); |
| 32 | + |
| 33 | + if (OptionStatus::kOk != idx_status) { |
| 34 | + return false; |
| 35 | + } |
| 36 | + |
| 37 | + OptionRecordType &record = |
| 38 | + static_cast<OptionRecordType &>(*highs_options_.records.at(idx)); |
| 39 | + const OptionStatus check_status = |
| 40 | + checkOptionValue(highs_log_options, record, value); |
| 41 | + if (OptionStatus::kIllegalValue == check_status) { |
| 42 | + return false; |
| 43 | + } |
| 44 | + |
| 45 | + return true; |
| 46 | + } |
| 47 | + |
| 48 | +private: |
| 49 | + HighsOptions highs_options_; |
| 50 | + std::mutex highs_options_mutex; |
| 51 | + std::map<std::string, HighsOptionType> record_type_lookup_; |
| 52 | + HighsLogOptions highs_log_options; |
| 53 | + |
| 54 | + static constexpr bool log_to_console = false; |
| 55 | + static constexpr bool output_flag = true; |
| 56 | + |
| 57 | + void initialize_log_options() { |
| 58 | + highs_log_options.log_stream = nullptr; |
| 59 | + highs_log_options.output_flag = const_cast<bool *>(&output_flag); |
| 60 | + highs_log_options.log_to_console = const_cast<bool *>(&log_to_console); |
| 61 | + highs_log_options.log_dev_level = nullptr; |
| 62 | + highs_log_options.user_log_callback = nullptr; |
| 63 | + highs_log_options.user_log_callback_data = nullptr; |
| 64 | + highs_log_options.user_callback_data = nullptr; |
| 65 | + highs_log_options.user_callback_active = false; |
| 66 | + } |
| 67 | +}; |
| 68 | + |
| 69 | +PYBIND11_MODULE(_highs_options, m) { |
| 70 | + py::class_<HighsOptionsManager>(m, "HighsOptionsManager") |
| 71 | + .def(py::init<>()) |
| 72 | + .def("get_option_type", |
| 73 | + [](const HighsOptionsManager &manager, const std::string &name) { |
| 74 | + const auto &lookup = manager.get_record_type_lookup().find(name); |
| 75 | + if (manager.get_record_type_lookup().end() == lookup) { |
| 76 | + return -1; |
| 77 | + } |
| 78 | + return static_cast<int>(lookup->second); |
| 79 | + }) |
| 80 | + .def("get_all_option_types", &HighsOptionsManager::get_record_type_lookup) |
| 81 | + .def("get_highs_options_records", |
| 82 | + [](const HighsOptionsManager &manager) { |
| 83 | + std::vector<std::string> records_names; |
| 84 | + for (const auto &record : manager.get_highs_options().records) { |
| 85 | + records_names.push_back(record->name); |
| 86 | + } |
| 87 | + return records_names; |
| 88 | + }) |
| 89 | + .def("check_int_option", |
| 90 | + [](HighsOptionsManager &self, const std::string &name, int value) { |
| 91 | + try { |
| 92 | + return self.check_option<OptionRecordInt, int>(name, value); |
| 93 | + } catch (const std::exception &e) { |
| 94 | + py::print("Exception caught in check_int_option:", e.what()); |
| 95 | + return false; |
| 96 | + } |
| 97 | + }) |
| 98 | + .def( |
| 99 | + "check_double_option", |
| 100 | + [](HighsOptionsManager &self, const std::string &name, double value) { |
| 101 | + try { |
| 102 | + return self.check_option<OptionRecordDouble, double>(name, value); |
| 103 | + } catch (const std::exception &e) { |
| 104 | + py::print("Exception caught in check_double_option:", e.what()); |
| 105 | + return false; |
| 106 | + } |
| 107 | + }) |
| 108 | + .def("check_string_option", |
| 109 | + [](HighsOptionsManager &self, const std::string &name, |
| 110 | + const std::string &value) { |
| 111 | + try { |
| 112 | + return self.check_option<OptionRecordString, std::string>(name, |
| 113 | + value); |
| 114 | + } catch (const std::exception &e) { |
| 115 | + py::print("Exception caught in check_string_option:", e.what()); |
| 116 | + return false; |
| 117 | + } |
| 118 | + }); |
| 119 | +} |
0 commit comments