|
| 1 | +#include "antares-xpansion/multisolver_interface/SolverAbstract.h" |
| 2 | + |
| 3 | +namespace |
| 4 | +{ |
| 5 | +bool areProblemDimensionsEquals(const SolverAbstract* one, const SolverAbstract* other) |
| 6 | +{ |
| 7 | + return (one->get_ncols() == other->get_ncols()) && (one->get_nrows() == other->get_nrows()) |
| 8 | + && (one->get_nelems() == other->get_nelems()); |
| 9 | +} |
| 10 | + |
| 11 | +bool areObjectiveFunctionEquals(const SolverAbstract* one, const SolverAbstract* other) |
| 12 | +{ |
| 13 | + std::vector<double> obj_one(one->get_ncols()); |
| 14 | + std::vector<double> obj_other(other->get_ncols()); |
| 15 | + one->get_obj(obj_one.data(), 0, one->get_ncols() - 1); |
| 16 | + other->get_obj(obj_other.data(), 0, other->get_ncols() - 1); |
| 17 | + |
| 18 | + return std::ranges::equal(obj_one, obj_other); |
| 19 | +} |
| 20 | + |
| 21 | +template<typename T> |
| 22 | + |
| 23 | +bool IsEqual(const T& lhs, const T& rhs, const T& epsilon = std::numeric_limits<T>::epsilon()) |
| 24 | +{ |
| 25 | + if constexpr (std::is_floating_point_v<T>) |
| 26 | + { |
| 27 | + return std::abs(lhs - rhs) <= epsilon * std::max(std::abs(lhs), std::abs(rhs)); |
| 28 | + } |
| 29 | + else |
| 30 | + { |
| 31 | + return (lhs == rhs); |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +bool areConstraintsEquals(const SolverAbstract* one, const SolverAbstract* other) |
| 36 | +{ |
| 37 | + std::vector<int> mstart(one->get_nrows() + 1); |
| 38 | + std::vector<int> cindex(one->get_nelems()); |
| 39 | + std::vector<double> matval_one(one->get_nelems()); |
| 40 | + int n_one = 0; |
| 41 | + one->get_rows(mstart.data(), |
| 42 | + cindex.data(), |
| 43 | + matval_one.data(), |
| 44 | + one->get_nelems(), |
| 45 | + &n_one, |
| 46 | + 0, |
| 47 | + one->get_nrows() - 1); |
| 48 | + |
| 49 | + std::vector<int> mstart_other(other->get_nrows() + 1); |
| 50 | + std::vector<int> cindex_other(other->get_nelems()); |
| 51 | + std::vector<double> matval_other(other->get_nelems()); |
| 52 | + int n_other = 0; |
| 53 | + other->get_rows(mstart_other.data(), |
| 54 | + cindex_other.data(), |
| 55 | + matval_other.data(), |
| 56 | + other->get_nelems(), |
| 57 | + &n_other, |
| 58 | + 0, |
| 59 | + other->get_nrows() - 1); |
| 60 | + |
| 61 | + if (n_one != n_other) |
| 62 | + { |
| 63 | + return false; |
| 64 | + } |
| 65 | + return std::ranges::equal(mstart, mstart_other) && std::ranges::equal(cindex, cindex_other) |
| 66 | + && std::ranges::equal(matval_one, |
| 67 | + matval_other, |
| 68 | + [](auto l, auto r) { return IsEqual(l, r); }); |
| 69 | +} |
| 70 | + |
| 71 | +bool areRHSEquals(const SolverAbstract* one, const SolverAbstract* other) |
| 72 | +{ |
| 73 | + std::vector<double> rhs_one(one->get_nrows()); |
| 74 | + std::vector<double> rhs_other(other->get_nrows()); |
| 75 | + one->get_rhs(rhs_one.data(), 0, one->get_nrows() - 1); |
| 76 | + other->get_rhs(rhs_other.data(), 0, other->get_nrows() - 1); |
| 77 | + |
| 78 | + return std::ranges::equal(rhs_one, rhs_other, [](auto l, auto r) { return IsEqual(l, r); }); |
| 79 | +} |
| 80 | + |
| 81 | +bool areRowTypesEquals(const SolverAbstract* one, const SolverAbstract* other) |
| 82 | +{ |
| 83 | + std::vector<char> order_one(one->get_nrows()); |
| 84 | + std::vector<char> order_other(other->get_nrows()); |
| 85 | + one->get_row_type(order_one.data(), 0, one->get_nrows() - 1); |
| 86 | + other->get_row_type(order_other.data(), 0, other->get_nrows() - 1); |
| 87 | + |
| 88 | + return std::ranges::equal(order_one, order_other); |
| 89 | +} |
| 90 | + |
| 91 | +bool areBoundsEquals(const SolverAbstract* one, const SolverAbstract* other) |
| 92 | +{ |
| 93 | + std::vector<double> lb_one(one->get_ncols()); |
| 94 | + std::vector<double> lb_other(other->get_ncols()); |
| 95 | + one->get_lb(lb_one.data(), 0, one->get_ncols() - 1); |
| 96 | + other->get_lb(lb_other.data(), 0, other->get_ncols() - 1); |
| 97 | + |
| 98 | + if (!std::ranges::equal(lb_one, lb_other, [](auto l, auto r) { return IsEqual(l, r); })) |
| 99 | + { |
| 100 | + return false; |
| 101 | + } |
| 102 | + |
| 103 | + std::vector<double> ub_one(one->get_ncols()); |
| 104 | + std::vector<double> ub_other(other->get_ncols()); |
| 105 | + one->get_ub(ub_one.data(), 0, one->get_ncols() - 1); |
| 106 | + other->get_ub(ub_other.data(), 0, other->get_ncols() - 1); |
| 107 | + |
| 108 | + if (!std::ranges::equal(ub_one, ub_other, [](auto l, auto r) { return IsEqual(l, r); })) |
| 109 | + { |
| 110 | + return false; |
| 111 | + } |
| 112 | + return true; |
| 113 | +} |
| 114 | +} // namespace |
| 115 | + |
| 116 | +bool SolverAbstract::operator==(const SolverAbstract& other) const |
| 117 | +{ |
| 118 | + if (this == &other) |
| 119 | + { |
| 120 | + return true; |
| 121 | + } |
| 122 | + bool is_equal = true; |
| 123 | + is_equal = is_equal && areProblemDimensionsEquals(this, &other); |
| 124 | + is_equal = is_equal && areObjectiveFunctionEquals(this, &other); |
| 125 | + is_equal = is_equal && areConstraintsEquals(this, &other); |
| 126 | + is_equal = is_equal && areRHSEquals(this, &other); |
| 127 | + is_equal = is_equal && areRowTypesEquals(this, &other); |
| 128 | + is_equal = is_equal && areBoundsEquals(this, &other); |
| 129 | + return is_equal; |
| 130 | +} |
0 commit comments