Files
ortools-clone/ortools/flatzinc/parser_main.cc
Mizux Seiha 4f381f6d07 backport from main:
* bump abseil to 20250814
* bump protobuf to v32.0
* cmake: add ccache auto support
* backport flatzinc, math_opt and sat update
2025-09-16 16:25:04 +02:00

83 lines
2.7 KiB
C++

// Copyright 2010-2025 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// This binary reads an input file in the flatzinc format (see
// http://www.minizinc.org/), parses it, and spits out the model it
// has built.
#include <cstddef>
#include <string>
#include "absl/base/log_severity.h"
#include "absl/flags/flag.h"
#include "absl/log/check.h"
#include "absl/log/globals.h"
#include "absl/strings/match.h"
#include "ortools/base/init_google.h"
#include "ortools/base/timer.h"
#include "ortools/flatzinc/model.h"
#include "ortools/flatzinc/parser.h"
#include "ortools/util/logging.h"
ABSL_FLAG(std::string, input, "", "Input file in the flatzinc format.");
ABSL_FLAG(bool, print, false, "Print model.");
ABSL_FLAG(bool, presolve, false, "Presolve loaded file.");
ABSL_FLAG(bool, statistics, false, "Print model statistics");
namespace operations_research {
namespace fz {
void ParseFile(const std::string& filename) {
WallTimer timer;
timer.Start();
SolverLogger logger;
logger.EnableLogging(true);
logger.SetLogToStdOut(true);
SOLVER_LOG(&logger, "Loading ", filename);
std::string problem_name = filename;
// Remove the .fzn extension.
CHECK(absl::EndsWith(problem_name, ".fzn"));
problem_name.resize(problem_name.size() - 4);
// Remove the leading path if present.
const size_t found = problem_name.find_last_of("/\\");
if (found != std::string::npos) {
problem_name = problem_name.substr(found + 1);
}
SOLVER_LOG(&logger, " - parsed in ", timer.GetInMs(), " ms");
Model model(problem_name);
CHECK(ParseFlatzincFile(filename, &model));
if (absl::GetFlag(FLAGS_statistics)) {
ModelStatistics stats(model, &logger);
stats.BuildStatistics();
stats.PrintStatistics();
}
if (absl::GetFlag(FLAGS_print)) {
SOLVER_LOG(&logger, model.DebugString());
}
}
} // namespace fz
} // namespace operations_research
int main(int argc, char** argv) {
const char kUsage[] =
"Parses a flatzinc .fzn file, optionally presolve it, and prints it in "
"human-readable format";
absl::SetStderrThreshold(absl::LogSeverityAtLeast::kInfo);
InitGoogle(kUsage, &argc, &argv, /*remove_flags=*/true);
operations_research::fz::ParseFile(absl::GetFlag(FLAGS_input));
return 0;
}