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