Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
parser_main.cc
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// This binary reads an input file in the flatzinc format (see
15// http://www.minizinc.org/), parses it, and spits out the model it
16// has built.
17
18#include <string>
19
20#include "absl/flags/flag.h"
21#include "absl/log/check.h"
22#include "absl/strings/match.h"
25#include "ortools/base/timer.h"
30
31ABSL_FLAG(std::string, input, "", "Input file in the flatzinc format.");
32ABSL_FLAG(bool, print, false, "Print model.");
33ABSL_FLAG(bool, presolve, false, "Presolve loaded file.");
34ABSL_FLAG(bool, statistics, false, "Print model statistics");
35
36namespace operations_research {
37namespace fz {
38void ParseFile(const std::string& filename, bool presolve) {
39 WallTimer timer;
40 timer.Start();
41
42 SolverLogger logger;
43 logger.EnableLogging(true);
44 logger.SetLogToStdOut(true);
45
46 SOLVER_LOG(&logger, "Loading ", filename);
47
48 std::string problem_name = filename;
49 // Remove the .fzn extension.
50 CHECK(absl::EndsWith(problem_name, ".fzn"));
51 problem_name.resize(problem_name.size() - 4);
52 // Remove the leading path if present.
53 const size_t found = problem_name.find_last_of("/\\");
54 if (found != std::string::npos) {
55 problem_name = problem_name.substr(found + 1);
56 }
57 SOLVER_LOG(&logger, " - parsed in ", timer.GetInMs(), " ms");
58
59 Model model(problem_name);
60 CHECK(ParseFlatzincFile(filename, &model));
61 if (presolve) {
62 SOLVER_LOG(&logger, "Presolve model");
63 timer.Reset();
64 timer.Start();
65 Presolver presolve(&logger);
66 presolve.Run(&model);
67 SOLVER_LOG(&logger, " - done in ", timer.GetInMs(), " ms");
68 }
69 if (absl::GetFlag(FLAGS_statistics)) {
70 ModelStatistics stats(model, &logger);
71 stats.BuildStatistics();
72 stats.PrintStatistics();
73 }
74 if (absl::GetFlag(FLAGS_print)) {
75 SOLVER_LOG(&logger, model.DebugString());
76 }
77}
78} // namespace fz
79} // namespace operations_research
80
81int main(int argc, char** argv) {
82 const char kUsage[] =
83 "Parses a flatzinc .fzn file, optionally presolve it, and prints it in "
84 "human-readable format";
85 absl::SetProgramUsageMessage(kUsage);
86 absl::ParseCommandLine(argc, argv);
88 operations_research::fz::ParseFile(absl::GetFlag(FLAGS_input),
89 absl::GetFlag(FLAGS_presolve));
90 return 0;
91}
int64_t GetInMs() const
Definition timer.h:47
void Reset()
Definition timer.h:27
void Start()
When Start() is called multiple times, only the most recent is used.
Definition timer.h:32
void EnableLogging(bool enable)
Definition logging.h:46
void SetLogToStdOut(bool enable)
Should all messages be displayed on stdout ?
Definition logging.h:52
void PrintStatistics() const
--— Model statistics --—
Definition model.cc:1134
GRBmodel * model
void InitGoogleLogging(const std::string &usage)
Definition init_google.h:43
void ParseFile(const std::string &filename, bool presolve)
bool ParseFlatzincFile(const std::string &filename, Model *model)
--— public parsing API --—
Definition parser.cc:42
In SWIG mode, we don't want anything besides these top-level includes.
static int input(yyscan_t yyscanner)
int main(int argc, char **argv)
ABSL_FLAG(std::string, input, "", "Input file in the flatzinc format.")
static const char kUsage[]
#define SOLVER_LOG(logger,...)
Definition logging.h:109