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