29#include "absl/flags/flag.h"
30#include "absl/log/check.h"
31#include "absl/log/initialize.h"
32#include "absl/strings/match.h"
33#include "absl/strings/str_split.h"
34#include "absl/strings/string_view.h"
46ABSL_FLAG(
bool, search_all_solutions,
false,
"Search for all solutions.");
48 "Display all improving solutions.");
50 "If false, the solver must follow the defined search."
51 "If true, other search are allowed.");
52ABSL_FLAG(
int, threads, 0,
"Number of threads the solver will use.");
53ABSL_FLAG(
bool, presolve,
true,
"Presolve the model to simplify it.");
54ABSL_FLAG(
bool, statistics,
false,
"Print solver statistics after search.");
56 "Read the FlatZinc from stdin, not from a file.");
59 "Define problem name when reading from stdin.");
60ABSL_FLAG(std::string, params,
"",
"SatParameters as a text proto.");
62 "Print logging information from the flatzinc interpreter.");
63ABSL_FLAG(
bool, ortools_mode,
true,
"Display solutions in the flatzinc format");
69 char all_param[] =
"--search_all_solutions";
70 char print_solutions[] =
"--display_all_solutions";
71 char free_param[] =
"--free_search";
72 char threads_param[] =
"--threads";
73 char logging_param[] =
"--fz_logging";
74 char statistics_param[] =
"--statistics";
75 char seed_param[] =
"--fz_seed";
76 char time_param[] =
"--time_limit";
77 bool use_time_param =
false;
78 bool set_free_search =
false;
79 for (
int i = 1; i < *argc; ++i) {
80 if (strcmp((*argv)[i],
"-a") == 0) {
81 (*argv)[i] = all_param;
83 if (strcmp((*argv)[i],
"-i") == 0) {
84 (*argv)[i] = print_solutions;
86 if (strcmp((*argv)[i],
"-f") == 0) {
87 (*argv)[i] = free_param;
88 set_free_search =
true;
90 if (strcmp((*argv)[i],
"-p") == 0) {
91 (*argv)[i] = threads_param;
93 if (strcmp((*argv)[i],
"-l") == 0) {
94 (*argv)[i] = logging_param;
96 if (strcmp((*argv)[i],
"-s") == 0) {
97 (*argv)[i] = statistics_param;
99 if (strcmp((*argv)[i],
"-r") == 0) {
100 (*argv)[i] = seed_param;
102 if (strcmp((*argv)[i],
"-t") == 0) {
103 (*argv)[i] = time_param;
104 use_time_param =
true;
106 if (strcmp((*argv)[i],
"-v") == 0) {
107 (*argv)[i] = logging_param;
111 "Usage: see flags.\nThis program parses and solve a flatzinc problem.";
113 absl::SetProgramUsageMessage(
kUsage);
114 const std::vector<char*> residual_flags =
115 absl::ParseCommandLine(*argc, *argv);
116 absl::InitializeLog();
119 if (use_time_param) {
120 absl::SetFlag(&FLAGS_time_limit, absl::GetFlag(FLAGS_time_limit) / 1000.0);
124 if (set_free_search && absl::GetFlag(FLAGS_threads) == 0) {
125 absl::SetFlag(&FLAGS_threads, 1);
128 return residual_flags;
137 if (input_is_filename && !absl::EndsWith(
input,
".fzn")) {
138 LOG(FATAL) <<
"Unrecognized flatzinc file: `" <<
input <<
"'";
142 const std::string problem_name = input_is_filename
144 : absl::GetFlag(FLAGS_fz_model_name);
146 if (input_is_filename) {
153 " parsed in ", timer.
GetInMs(),
" ms");
173 if (multi_line_input.empty()) {
174 std::cout << std::endl;
177 const absl::string_view flatzinc_prefix =
178 absl::GetFlag(FLAGS_ortools_mode) ?
"%% " :
"";
179 const std::vector<absl::string_view> lines =
180 absl::StrSplit(multi_line_input,
'\n');
181 for (
const absl::string_view&
line : lines) {
182 std::cout << flatzinc_prefix <<
line << std::endl;
189int main(
int argc,
char** argv) {
192 const std::vector<char*> residual_flags =
196 if (absl::GetFlag(FLAGS_read_from_stdin)) {
197 std::string currentLine;
198 while (std::getline(std::cin, currentLine)) {
199 input.append(currentLine);
202 if (residual_flags.empty()) {
203 LOG(ERROR) <<
"Usage: " << argv[0] <<
" <file>";
206 input = residual_flags.back();
210 if (absl::GetFlag(FLAGS_ortools_mode)) {
221 input, !absl::GetFlag(FLAGS_read_from_stdin), &logger);
225 parameters.search_all_solutions = absl::GetFlag(FLAGS_search_all_solutions);
226 parameters.use_free_search = absl::GetFlag(FLAGS_free_search);
228 absl::GetFlag(FLAGS_fz_logging) || !absl::GetFlag(FLAGS_ortools_mode);
229 parameters.random_seed = absl::GetFlag(FLAGS_fz_seed);
230 parameters.display_statistics = absl::GetFlag(FLAGS_statistics);
231 parameters.number_of_threads = absl::GetFlag(FLAGS_threads);
232 parameters.max_time_in_seconds = absl::GetFlag(FLAGS_time_limit);
233 parameters.ortools_mode = absl::GetFlag(FLAGS_ortools_mode);
240 absl::GetFlag(FLAGS_params),
241 &logger, &solution_logger);
void Start()
When Start() is called multiple times, only the most recent is used.
void EnableLogging(bool enable)
void SetLogToStdOut(bool enable)
Should all messages be displayed on stdout ?
void AddInfoLoggingCallback(std::function< void(const std::string &message)> callback)
void PrintStatistics() const
--— Model statistics --—
ABSL_FLAG(double, time_limit, 0, "time limit in seconds.")
int main(int argc, char **argv)
absl::string_view Stem(absl::string_view path)
void LogInFlatzincFormat(const std::string &multi_line_input)
bool ParseFlatzincString(const std::string &input, Model *model)
bool ParseFlatzincFile(const std::string &filename, Model *model)
--— public parsing API --—
Model ParseFlatzincModel(const std::string &input, bool input_is_filename, SolverLogger *logger)
std::vector< char * > FixAndParseParameters(int *argc, char ***argv)
void SolveFzWithCpModelProto(const fz::Model &fz_model, const fz::FlatzincSatParameters &p, const std::string &sat_params, SolverLogger *logger, SolverLogger *solution_logger)
In SWIG mode, we don't want anything besides these top-level includes.
static int input(yyscan_t yyscanner)
static const char kUsage[]
bool display_all_solutions
#define SOLVER_LOG(logger,...)