Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
callback_tests.h
Go to the documentation of this file.
1// Copyright 2010-2024 Google LLC
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14#ifndef OR_TOOLS_MATH_OPT_SOLVER_TESTS_CALLBACK_TESTS_H_
15#define OR_TOOLS_MATH_OPT_SOLVER_TESTS_CALLBACK_TESTS_H_
16
17#include <functional>
18#include <iosfwd>
19#include <optional>
20#include <ostream>
21#include <string>
22
23#include "absl/container/flat_hash_set.h"
24#include "gtest/gtest.h"
26#include "ortools/math_opt/parameters.pb.h"
28
29namespace operations_research {
30namespace math_opt {
31
32// Parameters for the MessageCallbackTest suite below.
37 std::string ending_substring,
39
40 // The tested solver.
42
43 // True if the solver is expected to support message callbacks. False if not,
44 // in which case the solver is expected to ignore the callback.
46
47 // True if the solver supports SolveInterrupter.
49
50 // True if the tests should be performed with integer variables.
52
53 // A sub-string expected to be found on the last log lines.
54 std::string ending_substring;
55
56 // Additional parameters to control the solve.
58
59 friend std::ostream& operator<<(std::ostream& out,
60 const MessageCallbackTestParams& params);
61};
62
63// A suite of unit tests to validates that a solver handles message callbacks
64// correctly.
65//
66// To use these tests, in file <solver>_test.cc, write:
67// INSTANTIATE_TEST_SUITE_P(
68// <Solver>MessageCallbackTest, MessageCallbackTest,
69// testing::Values(MessageCallbackTestParams(
70// SolverType::k<Solver>,
71// /*use_new_message_callback=*/true,
72// /*support_message_callback=*/true,
73// /*support_interrupter=*/true,
74// "ENDING")));
76 : public ::testing::TestWithParam<MessageCallbackTestParams> {};
77
78// Parameters for CallbackTest.
82 absl::flat_hash_set<CallbackEvent> supported_events,
83 std::optional<SolveParameters> all_solutions,
84 std::optional<SolveParameters> reaches_cut_callback);
85
86 // The solver to test.
88
89 // True if the tests should be performed with integer variables.
91
92 // If the solver supports adding lazy constraints at the MIP_SOLUTION event.
94
95 // If the solver supports adding cuts at the event MIP_NODE.
97
98 // The events that should be supported by the solver.
99 absl::flat_hash_set<CallbackEvent> supported_events;
100
101 // For a small feasibility problem (objective is zero) with <= 10 feasible
102 // solutions, ensure the solver finds all solutions.
103 std::optional<SolveParameters> all_solutions;
104
105 // Disable as much as possible of presolve, (solver) cuts, and heuristics, so
106 // that we can run a custom cut on this problem. Not setting this value will
107 // result in the test on adding cuts at event kMipNode not running.
108 std::optional<SolveParameters> reaches_cut_callback;
109
110 friend std::ostream& operator<<(std::ostream& out,
111 const CallbackTestParams& params);
112};
113
114// A suite of unit tests to show that a solver handles other callbacks
115// correctly.
116//
117// Note that the tests use callbacks that are not thread-safe and request that
118// the underlying solvers run in single threaded mode.
119//
120// To use these tests, in file <solver>_test.cc, write:
121// INSTANTIATE_TEST_SUITE_P(<Solver>CallbackTest, CallbackTest,
122// testing::Values(CallbackTestParams(
123// SolverType::k<Solver>,
124// /*integer_variables=*/true,
125// /*supported_events=*/{...}));
126// (In `Values` above, you can put any list of `SolverType`s).
127class CallbackTest : public ::testing::TestWithParam<CallbackTestParams> {};
128
129} // namespace math_opt
130} // namespace operations_research
131
132#endif // OR_TOOLS_MATH_OPT_SOLVER_TESTS_CALLBACK_TESTS_H_
SolverType
The solvers supported by MathOpt.
Definition parameters.h:42
In SWIG mode, we don't want anything besides these top-level includes.
friend std::ostream & operator<<(std::ostream &out, const CallbackTestParams &params)
std::optional< SolveParameters > all_solutions
CallbackTestParams(SolverType solver_type, bool integer_variables, bool add_lazy_constraints, bool add_cuts, absl::flat_hash_set< CallbackEvent > supported_events, std::optional< SolveParameters > all_solutions, std::optional< SolveParameters > reaches_cut_callback)
absl::flat_hash_set< CallbackEvent > supported_events
The events that should be supported by the solver.
bool add_lazy_constraints
If the solver supports adding lazy constraints at the MIP_SOLUTION event.
bool integer_variables
True if the tests should be performed with integer variables.
bool add_cuts
If the solver supports adding cuts at the event MIP_NODE.
std::optional< SolveParameters > reaches_cut_callback
Parameters for the MessageCallbackTest suite below.
SolveParameters solve_parameters
Additional parameters to control the solve.
bool support_interrupter
True if the solver supports SolveInterrupter.
std::string ending_substring
A sub-string expected to be found on the last log lines.
MessageCallbackTestParams(SolverType solver_type, bool support_message_callback, bool support_interrupter, bool integer_variables, std::string ending_substring, SolveParameters solve_parameters={})
bool integer_variables
True if the tests should be performed with integer variables.
friend std::ostream & operator<<(std::ostream &out, const MessageCallbackTestParams &params)