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