OR-Tools  8.2
parser_main.cc
Go to the documentation of this file.
1 // Copyright 2010-2018 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"
26 #include "ortools/flatzinc/model.h"
29 
30 ABSL_FLAG(std::string, file, "", "Input file in the flatzinc format.");
31 ABSL_FLAG(bool, print, false, "Print model.");
32 ABSL_FLAG(bool, presolve, false, "Presolve loaded file.");
33 ABSL_FLAG(bool, statistics, false, "Print model statistics");
34 
35 namespace operations_research {
36 namespace fz {
37 void ParseFile(const std::string& filename, bool presolve) {
38  WallTimer timer;
39  timer.Start();
40 
41  FZLOG << "Loading " << filename << FZENDL;
42 
43  std::string problem_name = filename;
44  // Remove the .fzn extension.
45  CHECK(absl::EndsWith(problem_name, ".fzn"));
46  problem_name.resize(problem_name.size() - 4);
47  // Remove the leading path if present.
48  const size_t found = problem_name.find_last_of("/\\");
49  if (found != std::string::npos) {
50  problem_name = problem_name.substr(found + 1);
51  }
52  FZLOG << " - parsed in " << timer.GetInMs() << " ms" << FZENDL;
53 
54  Model model(problem_name);
55  CHECK(ParseFlatzincFile(filename, &model));
56  if (presolve) {
57  FZLOG << "Presolve model" << FZENDL;
58  timer.Reset();
59  timer.Start();
60  Presolver presolve;
61  presolve.Run(&model);
62  FZLOG << " - done in " << timer.GetInMs() << " ms" << FZENDL;
63  }
64  if (absl::GetFlag(FLAGS_statistics)) {
65  ModelStatistics stats(model);
66  stats.BuildStatistics();
67  stats.PrintStatistics();
68  }
69  if (absl::GetFlag(FLAGS_print)) {
70  FZLOG << model.DebugString() << FZENDL;
71  }
72 }
73 } // namespace fz
74 } // namespace operations_research
75 
76 int main(int argc, char** argv) {
77  const char kUsage[] =
78  "Parses a flatzinc .fzn file, optionally presolve it, and prints it in "
79  "human-readable format";
80  absl::SetFlag(&FLAGS_log_prefix, false);
81  absl::SetFlag(&FLAGS_logtostderr, true);
82  absl::SetProgramUsageMessage(kUsage);
83  absl::ParseCommandLine(argc, argv);
85  operations_research::fz::ParseFile(absl::GetFlag(FLAGS_file),
86  absl::GetFlag(FLAGS_presolve));
87  return 0;
88 }
#define CHECK(condition)
Definition: base/logging.h:495
void Start()
Definition: timer.h:31
void Reset()
Definition: timer.h:26
int64 GetInMs() const
Definition: timer.h:46
#define FZLOG
#define FZENDL
GRBmodel * model
Definition: file.cc:141
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
The vehicle routing library lets one model and solve generic vehicle routing problems ranging from th...
int main(int argc, char **argv)
Definition: parser_main.cc:76
ABSL_FLAG(std::string, file, "", "Input file in the flatzinc format.")