OR-Tools  8.0
fz.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 is the skeleton for the official flatzinc interpreter. Much
15 // of the funcionalities are fixed (name of parameters, format of the
16 // input): see http://www.minizinc.org/downloads/doc-1.6/flatzinc-spec.pdf
17 
18 #if defined(__GNUC__) // Linux or Mac OS X.
19 #include <signal.h>
20 #endif // __GNUC__
21 
22 #include <csignal>
23 #include <iostream>
24 #include <string>
25 #include <vector>
26 
29 #include "ortools/base/logging.h"
31 #include "ortools/base/timer.h"
34 #include "ortools/flatzinc/model.h"
37 
38 DEFINE_double(time_limit, 0, "time limit in seconds.");
39 DEFINE_bool(all_solutions, false, "Search for all solutions.");
40 DEFINE_int32(num_solutions, 0,
41  "Maximum number of solution to search for, 0 means unspecified.");
42 DEFINE_bool(free_search, false,
43  "If false, the solver must follow the defined search."
44  "If true, other search are allowed.");
45 DEFINE_int32(threads, 0, "Number of threads the solver will use.");
46 DEFINE_bool(presolve, true, "Presolve the model to simplify it.");
47 DEFINE_bool(statistics, false, "Print solver statistics after search.");
48 DEFINE_bool(read_from_stdin, false,
49  "Read the FlatZinc from stdin, not from a file.");
50 DEFINE_int32(fz_seed, 0, "Random seed");
51 DEFINE_string(fz_model_name, "stdin",
52  "Define problem name when reading from stdin.");
53 DEFINE_string(params, "", "SatParameters as a text proto.");
54 
55 DECLARE_bool(log_prefix);
56 
58 
59 namespace operations_research {
60 namespace fz {
61 
62 void FixAndParseParameters(int* argc, char*** argv) {
63  absl::SetFlag(&FLAGS_log_prefix, false);
64 
65  char all_param[] = "--all_solutions";
66  char free_param[] = "--free_search";
67  char threads_param[] = "--threads";
68  char solutions_param[] = "--num_solutions";
69  char logging_param[] = "--fz_logging";
70  char statistics_param[] = "--statistics";
71  char seed_param[] = "--fz_seed";
72  char verbose_param[] = "--fz_verbose";
73  char debug_param[] = "--fz_debug";
74  char time_param[] = "--time_limit";
75  bool use_time_param = false;
76  for (int i = 1; i < *argc; ++i) {
77  if (strcmp((*argv)[i], "-a") == 0) {
78  (*argv)[i] = all_param;
79  }
80  if (strcmp((*argv)[i], "-f") == 0) {
81  (*argv)[i] = free_param;
82  }
83  if (strcmp((*argv)[i], "-p") == 0) {
84  (*argv)[i] = threads_param;
85  }
86  if (strcmp((*argv)[i], "-n") == 0) {
87  (*argv)[i] = solutions_param;
88  }
89  if (strcmp((*argv)[i], "-l") == 0) {
90  (*argv)[i] = logging_param;
91  }
92  if (strcmp((*argv)[i], "-s") == 0) {
93  (*argv)[i] = statistics_param;
94  }
95  if (strcmp((*argv)[i], "-r") == 0) {
96  (*argv)[i] = seed_param;
97  }
98  if (strcmp((*argv)[i], "-v") == 0) {
99  (*argv)[i] = verbose_param;
100  }
101  if (strcmp((*argv)[i], "-d") == 0) {
102  (*argv)[i] = debug_param;
103  }
104  if (strcmp((*argv)[i], "-t") == 0) {
105  (*argv)[i] = time_param;
106  use_time_param = true;
107  }
108  }
109  const char kUsage[] =
110  "Usage: see flags.\nThis program parses and solve a flatzinc problem.";
111 
112  gflags::SetUsageMessage(kUsage);
113  gflags::ParseCommandLineFlags(argc, argv, true);
114  google::InitGoogleLogging((*argv)[0]);
115 
116  // Fix time limit if -t was used.
117  if (use_time_param) {
118  FLAGS_time_limit /= 1000.0;
119  }
120 }
121 
122 Model ParseFlatzincModel(const std::string& input, bool input_is_filename) {
123  WallTimer timer;
124  timer.Start();
125  // Read model.
126  std::string problem_name = input_is_filename ? input : FLAGS_fz_model_name;
127  if (input_is_filename || absl::EndsWith(problem_name, ".fzn")) {
128  CHECK(absl::EndsWith(problem_name, ".fzn"));
129  problem_name.resize(problem_name.size() - 4);
130  const size_t found = problem_name.find_last_of("/\\");
131  if (found != std::string::npos) {
132  problem_name = problem_name.substr(found + 1);
133  }
134  }
135  Model model(problem_name);
136  if (input_is_filename) {
137  CHECK(ParseFlatzincFile(input, &model));
138  } else {
139  CHECK(ParseFlatzincString(input, &model));
140  }
141 
142  FZLOG << "File " << (input_is_filename ? input : "stdin") << " parsed in "
143  << timer.GetInMs() << " ms" << FZENDL;
144 
145  // Presolve the model.
146  Presolver presolve;
147  FZLOG << "Presolve model" << FZENDL;
148  timer.Reset();
149  timer.Start();
150  presolve.Run(&model);
151  FZLOG << " - done in " << timer.GetInMs() << " ms" << FZENDL;
152 
153  // Print statistics.
154  ModelStatistics stats(model);
155  stats.BuildStatistics();
156  stats.PrintStatistics();
157  return model;
158 }
159 
160 } // namespace fz
161 } // namespace operations_research
162 
163 int main(int argc, char** argv) {
164  // Flatzinc specifications require single dash parameters (-a, -f, -p).
165  // We need to fix parameters before parsing them.
167  // We allow piping model through stdin.
168  std::string input;
169  if (FLAGS_read_from_stdin) {
170  std::string currentLine;
171  while (std::getline(std::cin, currentLine)) {
172  input.append(currentLine);
173  }
174  } else {
175  if (argc <= 1) {
176  LOG(ERROR) << "Usage: " << argv[0] << " <file>";
177  return EXIT_FAILURE;
178  }
179  input = argv[1];
180  }
181 
184  !FLAGS_read_from_stdin);
186  parameters.display_all_solutions = FLAGS_all_solutions;
187  parameters.use_free_search = FLAGS_free_search;
188  parameters.verbose_logging = FLAGS_fz_logging;
189  parameters.max_number_of_solutions =
190  FLAGS_num_solutions == 0 ? // Not fixed.
191  (FLAGS_num_solutions = FLAGS_all_solutions ? kint32max : 1)
192  : FLAGS_num_solutions;
193  parameters.random_seed = FLAGS_fz_seed;
194  parameters.display_statistics = FLAGS_statistics;
195  parameters.number_of_threads = FLAGS_threads;
196  parameters.max_time_in_seconds = FLAGS_time_limit;
197 
199  FLAGS_params);
200  return EXIT_SUCCESS;
201 }
integral_types.h
threadpool.h
FZENDL
#define FZENDL
Definition: flatzinc/logging.h:31
main
int main(int argc, char **argv)
Definition: fz.cc:163
operations_research::ThreadPool
Definition: threadpool.h:26
model.h
logging.h
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::ParseFlatzincString
bool ParseFlatzincString(const std::string &input, Model *model)
Definition: parser.cc:60
logging.h
DEFINE_bool
DEFINE_bool(all_solutions, false, "Search for all solutions.")
FZLOG
#define FZLOG
Definition: flatzinc/logging.h:32
operations_research::fz::FlatzincSatParameters
Definition: cp_model_fz_solver.h:22
kint32max
static const int32 kint32max
Definition: integral_types.h:59
time_limit
SharedTimeLimit * time_limit
Definition: cp_model_solver.cc:2025
timer.h
WallTimer
Definition: timer.h:23
operations_research::fz::ModelStatistics
Definition: flatzinc/model.h:390
WallTimer::GetInMs
int64 GetInMs() const
Definition: timer.h:46
operations_research::fz::ModelStatistics::PrintStatistics
void PrintStatistics() const
Definition: model.cc:920
presolve.h
model
GRBmodel * model
Definition: gurobi_interface.cc:195
WallTimer::Start
void Start()
Definition: timer.h:31
operations_research::fz::Model
Definition: flatzinc/model.h:315
DEFINE_int32
DEFINE_int32(num_solutions, 0, "Maximum number of solution to search for, 0 means unspecified.")
parser.h
operations_research::fz::FixAndParseParameters
void FixAndParseParameters(int *argc, char ***argv)
Definition: fz.cc:62
input
static int input(yyscan_t yyscanner)
operations_research::fz::ParseFlatzincModel
Model ParseFlatzincModel(const std::string &input, bool input_is_filename)
Definition: fz.cc:122
DECLARE_bool
DECLARE_bool(log_prefix)
absl::SetFlag
void SetFlag(T *flag, const T &value)
Definition: commandlineflags.h:22
operations_research::fz::Presolver::Run
void Run(Model *model)
Definition: presolve.cc:450
DEFINE_double
DEFINE_double(time_limit, 0, "time limit in seconds.")
operations_research::fz::ParseFlatzincFile
bool ParseFlatzincFile(const std::string &filename, Model *model)
Definition: parser.cc:38
operations_research::sat::SolveFzWithCpModelProto
void SolveFzWithCpModelProto(const fz::Model &fz_model, const fz::FlatzincSatParameters &p, const std::string &sat_params)
Definition: cp_model_fz_solver.cc:980
DEFINE_string
DEFINE_string(fz_model_name, "stdin", "Define problem name when reading from stdin.")
operations_research::fz::Presolver
Definition: presolve.h:34
WallTimer::Reset
void Reset()
Definition: timer.h:26
operations_research::fz::ModelStatistics::BuildStatistics
void BuildStatistics()
Definition: model.cc:933
commandlineflags.h
parameters
SatParameters parameters
Definition: cp_model_fz_solver.cc:107
cp_model_fz_solver.h