Skip to content

Commit fdfc6af

Browse files
committed
rework ortools samples (#4590)
1 parent 1749877 commit fdfc6af

62 files changed

Lines changed: 343 additions & 93 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

ortools/algorithms/samples/knapsack.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@
2121
#include <sstream>
2222
#include <vector>
2323

24+
#include "absl/base/log_severity.h"
25+
#include "absl/log/globals.h"
2426
#include "ortools/algorithms/knapsack_solver.h"
27+
#include "ortools/base/init_google.h"
2528
// [END import]
2629

2730
namespace operations_research {
@@ -86,6 +89,8 @@ void RunKnapsackExample() {
8689
} // namespace operations_research
8790

8891
int main(int argc, char** argv) {
92+
InitGoogle(argv[0], &argc, &argv, true);
93+
absl::SetStderrThreshold(absl::LogSeverityAtLeast::kInfo);
8994
operations_research::RunKnapsackExample();
9095
return EXIT_SUCCESS;
9196
}

ortools/algorithms/samples/simple_knapsack_program.cc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@
2121
#include <sstream>
2222
#include <vector>
2323

24+
#include "absl/base/log_severity.h"
25+
#include "absl/log/globals.h"
2426
#include "absl/log/log.h"
2527
#include "ortools/algorithms/knapsack_solver.h"
28+
#include "ortools/base/init_google.h"
2629
// [END import]
2730

2831
namespace operations_research {
@@ -80,7 +83,9 @@ void SimpleKnapsackProgram() {
8083
} // namespace algorithms
8184
} // namespace operations_research
8285

83-
int main() {
86+
int main(int argc, char* argv[]) {
87+
InitGoogle(argv[0], &argc, &argv, true);
88+
absl::SetStderrThreshold(absl::LogSeverityAtLeast::kInfo);
8489
operations_research::algorithms::SimpleKnapsackProgram();
8590
return EXIT_SUCCESS;
8691
}

ortools/base/init_google.h

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,26 @@
1414
#ifndef OR_TOOLS_BASE_INIT_GOOGLE_H_
1515
#define OR_TOOLS_BASE_INIT_GOOGLE_H_
1616

17-
#include "absl/flags/declare.h"
18-
#include "absl/flags/flag.h"
17+
#include "absl/flags/declare.h" // IWYU pragma: keep
18+
#include "absl/flags/flag.h" // IWYU pragma: keep
1919
#include "absl/flags/parse.h"
2020
#include "absl/flags/usage.h"
2121
#include "absl/log/initialize.h"
22+
#include "absl/strings/string_view.h"
23+
24+
namespace google {
25+
26+
inline void InitGoogleLogging(absl::string_view usage) {
27+
absl::InitializeLog();
28+
if (!usage.empty()) {
29+
absl::SetProgramUsageMessage(usage);
30+
}
31+
}
32+
33+
inline void ShutdownGoogleLogging() {} // No op.
34+
35+
} // namespace google
36+
2237
// Initializes misc google-related things in the binary.
2338
//
2439
// Typically called early on in main() and must be called before other
@@ -31,22 +46,15 @@
3146
// requirement for an element (*argv)[*argc] to exist or to have
3247
// any particular value, unlike the similar array that is passed
3348
// to the `main` function.
34-
inline void InitGoogle(const char* usage, int* argc, char*** argv,
49+
inline void InitGoogle(absl::string_view usage, int* argc, char*** argv,
3550
bool deprecated) {
36-
absl::InitializeLog();
37-
absl::SetProgramUsageMessage(usage);
51+
google::InitGoogleLogging(usage);
3852
absl::ParseCommandLine(*argc, *argv);
3953
}
4054

41-
namespace google {
42-
43-
inline void InitGoogleLogging(const std::string& usage) {
44-
absl::InitializeLog();
45-
absl::SetProgramUsageMessage(usage);
55+
inline void InitGoogle(const char* usage, int* argc, char*** argv,
56+
bool deprecated) {
57+
InitGoogle(absl::NullSafeStringView(usage), argc, argv, deprecated);
4658
}
4759

48-
inline void ShutdownGoogleLogging() {} // No op.
49-
50-
} // namespace google
51-
5260
#endif // OR_TOOLS_BASE_INIT_GOOGLE_H_

ortools/constraint_solver/samples/cp_is_fun_cp.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@ void CPIsFunCp() {
148148
} // namespace operations_research
149149

150150
int main(int argc, char** argv) {
151-
absl::SetStderrThreshold(absl::LogSeverityAtLeast::kInfo);
152151
InitGoogle(argv[0], &argc, &argv, true);
152+
absl::SetStderrThreshold(absl::LogSeverityAtLeast::kInfo);
153153
operations_research::CPIsFunCp();
154154
return EXIT_SUCCESS;
155155
}

ortools/constraint_solver/samples/minimal_jobshop_cp.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,8 @@ void SolveJobShopExample() {
198198
} // namespace operations_research
199199

200200
int main(int argc, char** argv) {
201-
absl::SetStderrThreshold(absl::LogSeverityAtLeast::kInfo);
202201
InitGoogle(argv[0], &argc, &argv, true);
202+
absl::SetStderrThreshold(absl::LogSeverityAtLeast::kInfo);
203203
operations_research::SolveJobShopExample();
204204
return EXIT_SUCCESS;
205205
}

ortools/constraint_solver/samples/nqueens_cp.cc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
#include <sstream>
2020
#include <vector>
2121

22+
#include "absl/base/log_severity.h"
23+
#include "absl/log/globals.h"
24+
#include "ortools/base/init_google.h"
2225
#include "ortools/base/logging.h"
2326
#include "ortools/constraint_solver/constraint_solver.h"
2427
// [END import]
@@ -102,7 +105,9 @@ void NQueensCp(const int board_size) {
102105

103106
} // namespace operations_research
104107

105-
int main(int argc, char** argv) {
108+
int main(int argc, char* argv[]) {
109+
InitGoogle(argv[0], &argc, &argv, true);
110+
absl::SetStderrThreshold(absl::LogSeverityAtLeast::kInfo);
106111
int board_size = 8;
107112
if (argc > 1) {
108113
board_size = std::atoi(argv[1]);

ortools/constraint_solver/samples/nurses_cp.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,8 @@ void SolveNursesExample() {
205205
} // namespace operations_research
206206

207207
int main(int argc, char** argv) {
208-
absl::SetStderrThreshold(absl::LogSeverityAtLeast::kInfo);
209208
InitGoogle(argv[0], &argc, &argv, true);
209+
absl::SetStderrThreshold(absl::LogSeverityAtLeast::kInfo);
210210
operations_research::SolveNursesExample();
211211
return EXIT_SUCCESS;
212212
}

ortools/constraint_solver/samples/rabbits_and_pheasants_cp.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ void RunConstraintProgrammingExample() {
6161
} // namespace operations_research
6262

6363
int main(int argc, char** argv) {
64-
absl::SetStderrThreshold(absl::LogSeverityAtLeast::kInfo);
6564
InitGoogle(argv[0], &argc, &argv, true);
65+
absl::SetStderrThreshold(absl::LogSeverityAtLeast::kInfo);
6666
operations_research::RunConstraintProgrammingExample();
6767
return EXIT_SUCCESS;
6868
}

ortools/constraint_solver/samples/simple_cp_program.cc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
#include <ostream>
1717
#include <string>
1818

19+
#include "absl/base/log_severity.h"
20+
#include "absl/log/globals.h"
21+
#include "ortools/base/init_google.h"
1922
#include "ortools/constraint_solver/constraint_solver.h"
2023
// [END import]
2124

@@ -73,7 +76,9 @@ void SimpleCpProgram() {
7376

7477
} // namespace operations_research
7578

76-
int main(int /*argc*/, char* /*argv*/[]) {
79+
int main(int argc, char* argv[]) {
80+
InitGoogle(argv[0], &argc, &argv, true);
81+
absl::SetStderrThreshold(absl::LogSeverityAtLeast::kInfo);
7782
operations_research::SimpleCpProgram();
7883
return EXIT_SUCCESS;
7984
}

ortools/constraint_solver/samples/simple_ls_program.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,8 @@ void SolveProblem(SolveType solve_type) {
202202
} // namespace operations_research
203203

204204
int main(int argc, char** argv) {
205-
absl::SetStderrThreshold(absl::LogSeverityAtLeast::kInfo);
206205
InitGoogle(argv[0], &argc, &argv, true);
206+
absl::SetStderrThreshold(absl::LogSeverityAtLeast::kInfo);
207207
operations_research::SolveProblem(operations_research::LNS);
208208
operations_research::SolveProblem(operations_research::LS);
209209
operations_research::SolveProblem(operations_research::LS_WITH_FILTER);

0 commit comments

Comments
 (0)