22#include "absl/strings/str_cat.h"
23#include "absl/strings/str_join.h"
28using ::testing::MakeMatcher;
29using ::testing::Matcher;
30using ::testing::MatcherInterface;
31using ::testing::MatchResultListener;
40 std::vector<std::string> terms;
41 for (
const auto& var_value_pair :
solution) {
42 terms.push_back(absl::StrCat(SCIPvarGetName(var_value_pair.first),
"=",
43 var_value_pair.second));
45 std::sort(terms.begin(), terms.end());
46 return absl::StrCat(
"{", absl::StrJoin(terms,
","),
"}");
50 const double tolerance) {
51 if (left.size() != right.size())
return false;
52 for (
const auto& kv_pair : left) {
53 if (!right.contains(kv_pair.first))
return false;
54 if (std::abs(kv_pair.second - right.at(kv_pair.first)) > tolerance) {
63class GScipSolutionNearMatcher :
public MatcherInterface<GScipSolution> {
65 GScipSolutionNearMatcher(
const GScipSolution& rhs,
double tolerance)
66 : rhs_(rhs), tolerance_(tolerance) {}
69 MatchResultListener* listener)
const override {
78 void DescribeTo(std::ostream* os)
const override {
79 *os <<
"solution is term-wise within " << tolerance_ <<
" of "
83 void DescribeNegationTo(std::ostream* os)
const override {
84 *os <<
"solution differs by at least " << tolerance_ <<
" from "
90 const double tolerance_;
93void AssertOptimalWithASolution(
const GScipResult& actual_result,
94 const double expected_objective_value,
95 const double tolerance) {
97 EXPECT_NEAR(actual_result.gscip_output.stats().best_bound(),
98 expected_objective_value, tolerance);
99 EXPECT_NEAR(actual_result.gscip_output.stats().best_objective(),
100 expected_objective_value, tolerance);
101 ASSERT_GE(actual_result.solutions.size(), 1);
102 ASSERT_GE(actual_result.objective_values.size(), 1);
103 EXPECT_NEAR(actual_result.objective_values[0], expected_objective_value,
105 EXPECT_THAT(actual_result.primal_ray, ::testing::IsEmpty());
111 return MakeMatcher(
new GScipSolutionNearMatcher(rhs, 0.0));
115 return MakeMatcher(
new GScipSolutionNearMatcher(rhs, tolerance));
119 const double expected_objective_value,
121 const double tolerance) {
122 ASSERT_NO_FATAL_FAILURE(AssertOptimalWithASolution(
123 actual_result, expected_objective_value, tolerance));
129 const GScipResult& actual_result,
const double expected_objective_value,
130 const GScipSolution& expected_solution,
const double tolerance) {
131 ASSERT_NO_FATAL_FAILURE(AssertOptimalWithASolution(
132 actual_result, expected_objective_value, tolerance));
135 for (
const auto& expected_var_val : expected_solution) {
136 ASSERT_TRUE(actual_result.
solutions[0].contains(expected_var_val.first));
137 EXPECT_NEAR(actual_result.
solutions[0].at(expected_var_val.first),
138 expected_var_val.second, tolerance);
static constexpr Status OPTIMAL
EXPECT_THAT(ComputeInfeasibleSubsystem(model, GetParam().solver_type), IsOkAndHolds(IsInfeasible(true, ModelSubset{ .variable_bounds={{x, ModelSubset::Bounds{.lower=false,.upper=true}}},.linear_constraints={ {c, ModelSubset::Bounds{.lower=true,.upper=false}}}})))
void GScipSetOutputEnabled(GScipParameters *parameters, bool output_enabled)
void AssertOptimalWithBestSolution(const GScipResult &actual_result, const double expected_objective_value, const GScipSolution &expected_solution, const double tolerance)
Select next search node to expand Select next item_i to add this new search node to the search Generate a new search node where item_i is not in the knapsack Check validity of this new partial solution(using propagators) - If valid
bool SolutionsAlmostEqual(const GScipSolution &left, const GScipSolution &right, const double tolerance)
std::string SolutionToString(const GScipSolution &solution)
void AssertOptimalWithPartialBestSolution(const GScipResult &actual_result, const double expected_objective_value, const GScipSolution &expected_solution, const double tolerance)
GScipParameters TestGScipParameters()
absl::flat_hash_map< SCIP_VAR *, double > GScipSolution
testing::Matcher< GScipSolution > GScipSolutionEquals(const GScipSolution &rhs)
testing::Matcher< GScipSolution > GScipSolutionAlmostEquals(const GScipSolution &rhs, double tolerance)
std::vector< GScipSolution > solutions