OR-Tools  9.3
fz.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 is the skeleton for the official flatzinc interpreter. Much
15// of the functionalities are fixed (name of parameters, format of the
16// input): see http://www.minizinc.org/downloads/doc-1.6/flatzinc-spec.pdf
17
18#include <limits>
19
20#include "absl/strings/str_split.h"
21#if defined(__GNUC__) // Linux or Mac OS X.
22#include <signal.h>
23#endif // __GNUC__
24
25#include <csignal>
26#include <iostream>
27#include <string>
28#include <vector>
29
30#include "absl/flags/flag.h"
36#include "ortools/base/timer.h"
42
43ABSL_FLAG(double, time_limit, 0, "time limit in seconds.");
44ABSL_FLAG(bool, all_solutions, false, "Search for all solutions.");
45ABSL_FLAG(int, num_solutions, 0,
46 "Maximum number of solution to search for, 0 means unspecified.");
47ABSL_FLAG(bool, free_search, false,
48 "If false, the solver must follow the defined search."
49 "If true, other search are allowed.");
50ABSL_FLAG(int, threads, 0, "Number of threads the solver will use.");
51ABSL_FLAG(bool, presolve, true, "Presolve the model to simplify it.");
52ABSL_FLAG(bool, statistics, false, "Print solver statistics after search.");
53ABSL_FLAG(bool, read_from_stdin, false,
54 "Read the FlatZinc from stdin, not from a file.");
55ABSL_FLAG(int, fz_seed, 0, "Random seed");
56ABSL_FLAG(std::string, fz_model_name, "stdin",
57 "Define problem name when reading from stdin.");
58ABSL_FLAG(std::string, params, "", "SatParameters as a text proto.");
59ABSL_FLAG(bool, fz_logging, false,
60 "Print logging information from the flatzinc interpreter.");
61ABSL_FLAG(bool, use_flatzinc_format, true,
62 "Display solutions in the flatzinc format");
63
64namespace operations_research {
65namespace fz {
66
67std::vector<char*> FixAndParseParameters(int* argc, char*** argv) {
68 char all_param[] = "--all_solutions";
69 char free_param[] = "--free_search";
70 char threads_param[] = "--threads";
71 char solutions_param[] = "--num_solutions";
72 char logging_param[] = "--fz_logging";
73 char statistics_param[] = "--statistics";
74 char seed_param[] = "--fz_seed";
75 char time_param[] = "--time_limit";
76 bool use_time_param = false;
77 for (int i = 1; i < *argc; ++i) {
78 if (strcmp((*argv)[i], "-a") == 0) {
79 (*argv)[i] = all_param;
80 }
81 if (strcmp((*argv)[i], "-f") == 0) {
82 (*argv)[i] = free_param;
83 }
84 if (strcmp((*argv)[i], "-p") == 0) {
85 (*argv)[i] = threads_param;
86 }
87 if (strcmp((*argv)[i], "-n") == 0) {
88 (*argv)[i] = solutions_param;
89 }
90 if (strcmp((*argv)[i], "-l") == 0) {
91 (*argv)[i] = logging_param;
92 }
93 if (strcmp((*argv)[i], "-s") == 0) {
94 (*argv)[i] = statistics_param;
95 }
96 if (strcmp((*argv)[i], "-r") == 0) {
97 (*argv)[i] = seed_param;
98 }
99 if (strcmp((*argv)[i], "-t") == 0) {
100 (*argv)[i] = time_param;
101 use_time_param = true;
102 }
103 }
104 const char kUsage[] =
105 "Usage: see flags.\nThis program parses and solve a flatzinc problem.";
106
107 absl::SetProgramUsageMessage(kUsage);
108 const std::vector<char*> residual_flags =
109 absl::ParseCommandLine(*argc, *argv);
110 google::InitGoogleLogging((*argv)[0]);
111
112 // Fix time limit if -t was used.
113 if (use_time_param) {
114 absl::SetFlag(&FLAGS_time_limit, absl::GetFlag(FLAGS_time_limit) / 1000.0);
115 }
116 return residual_flags;
117}
118
119Model ParseFlatzincModel(const std::string& input, bool input_is_filename,
120 SolverLogger* logger) {
121 WallTimer timer;
122 timer.Start();
123 // Read model.
124 std::string problem_name =
125 input_is_filename ? input : absl::GetFlag(FLAGS_fz_model_name);
126 if (input_is_filename || absl::EndsWith(problem_name, ".fzn")) {
127 CHECK(absl::EndsWith(problem_name, ".fzn"))
128 << "Unrecognized flatzinc file: `" << problem_name << "'";
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) {
138 } else {
140 }
141
142 SOLVER_LOG(logger, "File ", (input_is_filename ? input : "stdin"),
143 " parsed in ", timer.GetInMs(), " ms");
144 SOLVER_LOG(logger, "");
145
146 // Presolve the model.
147 Presolver presolve(logger);
148 SOLVER_LOG(logger, "Presolve model");
149 timer.Reset();
150 timer.Start();
151 presolve.Run(&model);
152 SOLVER_LOG(logger, " - done in ", timer.GetInMs(), " ms");
153 SOLVER_LOG(logger);
154
155 // Print statistics.
156 ModelStatistics stats(model, logger);
157 stats.BuildStatistics();
158 stats.PrintStatistics();
159 return model;
160}
161
162void LogInFlatzincFormat(const std::string& multi_line_input) {
163 if (multi_line_input.empty()) {
164 std::cout << std::endl;
165 return;
166 }
167 const absl::string_view flatzinc_prefix =
168 absl::GetFlag(FLAGS_use_flatzinc_format) ? "%% " : "";
169 const std::vector<absl::string_view> lines =
170 absl::StrSplit(multi_line_input, '\n', absl::SkipEmpty());
171 for (const absl::string_view& line : lines) {
172 std::cout << flatzinc_prefix << line << std::endl;
173 }
174}
175
176} // namespace fz
177} // namespace operations_research
178
179int main(int argc, char** argv) {
180 // Flatzinc specifications require single dash parameters (-a, -f, -p).
181 // We need to fix parameters before parsing them.
182 const std::vector<char*> residual_flags =
184 // We allow piping model through stdin.
185 std::string input;
186 if (absl::GetFlag(FLAGS_read_from_stdin)) {
187 std::string currentLine;
188 while (std::getline(std::cin, currentLine)) {
189 input.append(currentLine);
190 }
191 } else {
192 if (residual_flags.empty()) {
193 LOG(ERROR) << "Usage: " << argv[0] << " <file>";
194 return EXIT_FAILURE;
195 }
196 input = residual_flags.back();
197 }
198
200 logger.EnableLogging(absl::GetFlag(FLAGS_fz_logging));
201 logger.SetLogToStdOut(false);
205 input, !absl::GetFlag(FLAGS_read_from_stdin), &logger);
207 parameters.display_all_solutions = absl::GetFlag(FLAGS_all_solutions);
208 parameters.use_free_search = absl::GetFlag(FLAGS_free_search);
209 parameters.log_search_progress = absl::GetFlag(FLAGS_fz_logging);
210 if (absl::GetFlag(FLAGS_num_solutions) == 0) {
211 absl::SetFlag(&FLAGS_num_solutions,
212 absl::GetFlag(FLAGS_all_solutions)
214 : 1);
215 }
216 parameters.max_number_of_solutions = absl::GetFlag(FLAGS_num_solutions);
217 parameters.random_seed = absl::GetFlag(FLAGS_fz_seed);
218 parameters.display_statistics = absl::GetFlag(FLAGS_statistics);
219 parameters.number_of_threads = absl::GetFlag(FLAGS_threads);
220 parameters.max_time_in_seconds = absl::GetFlag(FLAGS_time_limit);
221
222 operations_research::SolverLogger solution_logger;
223 solution_logger.SetLogToStdOut(true);
224 solution_logger.EnableLogging(absl::GetFlag(FLAGS_use_flatzinc_format));
225
227 absl::GetFlag(FLAGS_params),
228 &logger, &solution_logger);
229 return EXIT_SUCCESS;
230}
int64_t max
Definition: alldiff_cst.cc:140
#define CHECK(condition)
Definition: base/logging.h:495
#define LOG(severity)
Definition: base/logging.h:420
void Start()
Definition: timer.h:31
void Reset()
Definition: timer.h:26
int64_t GetInMs() const
Definition: timer.h:46
void SetLogToStdOut(bool enable)
Definition: util/logging.h:45
void AddInfoLoggingCallback(std::function< void(const std::string &message)> callback)
Definition: util/logging.cc:23
SatParameters parameters
ModelSharedTimeLimit * time_limit
ABSL_FLAG(double, time_limit, 0, "time limit in seconds.")
int main(int argc, char **argv)
Definition: fz.cc:179
GRBmodel * model
const int ERROR
Definition: log_severity.h:32
void InitGoogleLogging(const char *argv0)
void LogInFlatzincFormat(const std::string &multi_line_input)
Definition: fz.cc:162
bool ParseFlatzincString(const std::string &input, Model *model)
Definition: parser.cc:60
bool ParseFlatzincFile(const std::string &filename, Model *model)
Definition: parser.cc:38
Model ParseFlatzincModel(const std::string &input, bool input_is_filename, SolverLogger *logger)
Definition: fz.cc:119
std::vector< char * > FixAndParseParameters(int *argc, char ***argv)
Definition: fz.cc:67
void SolveFzWithCpModelProto(const fz::Model &fz_model, const fz::FlatzincSatParameters &p, const std::string &sat_params, SolverLogger *logger, SolverLogger *solution_logger)
Collection of objects used to extend the Constraint Solver library.
static int input(yyscan_t yyscanner)
#define SOLVER_LOG(logger,...)
Definition: util/logging.h:69