OR-Tools  9.1
parser_main.cc
Go to the documentation of this file.
1// Copyright 2010-2021 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/flags/parse.h"
22#include "absl/flags/usage.h"
24#include "ortools/base/timer.h"
29
30ABSL_FLAG(std::string, input, "", "Input file in the flatzinc format.");
31ABSL_FLAG(bool, print, false, "Print model.");
32ABSL_FLAG(bool, presolve, false, "Presolve loaded file.");
33ABSL_FLAG(bool, statistics, false, "Print model statistics");
34
35namespace operations_research {
36namespace fz {
37void ParseFile(const std::string& filename, bool presolve) {
38 WallTimer timer;
39 timer.Start();
40
41 SolverLogger logger;
42 logger.EnableLogging(true);
43 logger.SetLogToStdOut(true);
44
45 SOLVER_LOG(&logger, "Loading ", filename);
46
47 std::string problem_name = filename;
48 // Remove the .fzn extension.
49 CHECK(absl::EndsWith(problem_name, ".fzn"));
50 problem_name.resize(problem_name.size() - 4);
51 // Remove the leading path if present.
52 const size_t found = problem_name.find_last_of("/\\");
53 if (found != std::string::npos) {
54 problem_name = problem_name.substr(found + 1);
55 }
56 SOLVER_LOG(&logger, " - parsed in ", timer.GetInMs(), " ms");
57
58 Model model(problem_name);
59 CHECK(ParseFlatzincFile(filename, &model));
60 if (presolve) {
61 SOLVER_LOG(&logger, "Presolve model");
62 timer.Reset();
63 timer.Start();
64 Presolver presolve(&logger);
65 presolve.Run(&model);
66 SOLVER_LOG(&logger, " - done in ", timer.GetInMs(), " ms");
67 }
68 if (absl::GetFlag(FLAGS_statistics)) {
69 ModelStatistics stats(model, &logger);
70 stats.BuildStatistics();
71 stats.PrintStatistics();
72 }
73 if (absl::GetFlag(FLAGS_print)) {
74 SOLVER_LOG(&logger, model.DebugString());
75 }
76}
77} // namespace fz
78} // namespace operations_research
79
80int main(int argc, char** argv) {
81 const char kUsage[] =
82 "Parses a flatzinc .fzn file, optionally presolve it, and prints it in "
83 "human-readable format";
84 absl::SetFlag(&FLAGS_log_prefix, false);
85 absl::SetFlag(&FLAGS_logtostderr, true);
86 absl::SetProgramUsageMessage(kUsage);
87 absl::ParseCommandLine(argc, argv);
89 operations_research::fz::ParseFile(absl::GetFlag(FLAGS_input),
90 absl::GetFlag(FLAGS_presolve));
91 return 0;
92}
#define CHECK(condition)
Definition: base/logging.h:491
void Start()
Definition: timer.h:31
void Reset()
Definition: timer.h:26
int64_t GetInMs() const
Definition: timer.h:46
void SetLogToStdOut(bool enable)
Definition: util/logging.h:45
GRBmodel * model
void InitGoogleLogging(const char *argv0)
void ParseFile(const std::string &filename, bool presolve)
Definition: parser_main.cc:37
bool ParseFlatzincFile(const std::string &filename, Model *model)
Definition: parser.cc:38
Collection of objects used to extend the Constraint Solver library.
static int input(yyscan_t yyscanner)
int main(int argc, char **argv)
Definition: parser_main.cc:80
ABSL_FLAG(std::string, input, "", "Input file in the flatzinc format.")
#define SOLVER_LOG(logger,...)
Definition: util/logging.h:63