24 std::vector<std::vector<std::int64_t>> &key_list,
25 std::vector<double> &value_list,
26 std::vector<std::pair<std::int64_t, std::int64_t>> &bounds) {
27 if (key_list.size() != value_list.size()) {
28 throw std::runtime_error(
"Key and value lists must have the same size.");
31 std::unordered_set<std::int64_t> index_set;
32 for (
const auto &key : key_list) {
33 index_set.insert(key.begin(), key.end());
39 std::vector<std::int64_t>(index_set.begin(), index_set.end());
40 std::sort(this->
index_list_.begin(), this->index_list_.end());
45 for (std::size_t i = 0; i < key_list.size(); ++i) {
46 if (key_list[i].empty()) {
50 std::unordered_map<std::int64_t, std::int64_t> index_count;
51 for (
const auto &k : key_list[i]) {
56 std::vector<std::pair<std::int64_t, std::int64_t>> int_keys;
57 for (
const auto &[index, degree] : index_count) {
58 int_keys.emplace_back(index, degree);
61 std::sort(int_keys.begin(), int_keys.end());
69 [](
const auto &a,
const auto &b) {
70 return a.first.size() < b.first.size();
83 std::sort(interactions.begin(), interactions.end());
88 for (std::int64_t index = 0; index < this->
num_variables_; ++index) {
89 std::int64_t max_degree = 0;
90 for (
const auto &[_, degree] : this->index_to_interactions_[index]) {
91 if (degree > max_degree) {
95 if (max_degree == 0) {
96 throw std::runtime_error(
"Variable with no interactions found.");
103 const double MIN_THRESHOLD = 1e-10;
105 auto nonzero_abs_min = [&](
double current_min,
double value) ->
double {
106 if (std::abs(value) > MIN_THRESHOLD) {
107 return std::min(std::abs(current_min), std::abs(value));
112 auto nonzero_abs_max = [&](
double current_max,
double value) ->
double {
113 if (std::abs(value) > MIN_THRESHOLD) {
114 return std::max(std::abs(current_max), std::abs(value));
119 double abs_min_dE = std::numeric_limits<double>::infinity();
120 double abs_max_dE = 0.0;
124 double min_term = std::abs(value);
125 double max_term = std::abs(value);
127 const std::int64_t index = iter.first;
128 const std::int64_t degree = iter.second;
129 const std::int64_t lower = this->
bounds_[index].first;
130 const std::int64_t upper = this->
bounds_[index].second;
131 if (lower <= 0 && 0 <= upper) {
132 max_term *= std::pow(std::max(std::abs(lower), std::abs(upper)), degree);
136 min_term *= std::pow(lower, degree);
137 max_term *= std::pow(upper, degree);
138 }
else if (upper < 0) {
139 min_term *= std::abs(std::pow(upper, degree));
140 max_term *= std::abs(std::pow(lower, degree));
142 throw std::runtime_error(
"Bounds are not valid.");
147 abs_min_dE = nonzero_abs_min(abs_min_dE, min_term);
148 abs_max_dE = nonzero_abs_max(abs_max_dE, max_term);
151 if (std::isinf(abs_min_dE) || abs_max_dE == 0.0) {
152 throw std::runtime_error(
"No valid energy difference found.");
155 return std::make_pair(abs_max_dE, abs_min_dE);
162 const std::vector<std::pair<std::int64_t, std::int64_t>> &
GetBounds()
const {
167 std::pair<std::vector<std::pair<std::int64_t, std::int64_t>>,
double>> &
171 const std::vector<std::vector<std::pair<std::size_t, std::int64_t>>> &
185 std::vector<std::pair<std::int64_t, std::int64_t>>
bounds_;
188 std::pair<std::vector<std::pair<std::int64_t, std::int64_t>>,
double>>
190 std::vector<std::vector<std::pair<std::size_t, std::int64_t>>>
std::pair< double, double > GetMaxMinTerms() const
Definition integer_polynomial_model.hpp:102
std::int64_t GetEachVariableDegreeAt(const std::int64_t index) const
Definition integer_polynomial_model.hpp:175
const std::vector< std::pair< std::int64_t, std::int64_t > > & GetBounds() const
Definition integer_polynomial_model.hpp:162
std::vector< std::pair< std::vector< std::pair< std::int64_t, std::int64_t > >, double > > key_value_list_
Definition integer_polynomial_model.hpp:189
std::int64_t num_variables_
Definition integer_polynomial_model.hpp:184
IntegerPolynomialModel(std::vector< std::vector< std::int64_t > > &key_list, std::vector< double > &value_list, std::vector< std::pair< std::int64_t, std::int64_t > > &bounds)
Definition integer_polynomial_model.hpp:23
double GetConstant() const
Definition integer_polynomial_model.hpp:165
const std::vector< std::int64_t > & GetIndexList() const
Definition integer_polynomial_model.hpp:158
std::vector< std::int64_t > max_variable_degree_
Definition integer_polynomial_model.hpp:192
std::int64_t GetNumVariables() const
Definition integer_polynomial_model.hpp:161
const std::vector< std::vector< std::pair< std::size_t, std::int64_t > > > & GetIndexToInteractions() const
Definition integer_polynomial_model.hpp:172
const std::vector< std::int64_t > & GetEachVariableDegree() const
Definition integer_polynomial_model.hpp:178
std::vector< std::pair< std::int64_t, std::int64_t > > bounds_
Definition integer_polynomial_model.hpp:185
const std::vector< std::pair< std::vector< std::pair< std::int64_t, std::int64_t > >, double > > & GetKeyValueList() const
Definition integer_polynomial_model.hpp:168
std::vector< std::int64_t > index_list_
Definition integer_polynomial_model.hpp:183
std::vector< std::vector< std::pair< std::size_t, std::int64_t > > > index_to_interactions_
Definition integer_polynomial_model.hpp:191
double constant_
Definition integer_polynomial_model.hpp:186
Definition binary_polynomial_model.hpp:24
Definition algorithm.hpp:24