2025-01-10 11:35:44 +01:00
|
|
|
// Copyright 2010-2025 Google LLC
|
2012-03-28 18:50:03 +00:00
|
|
|
// 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.
|
2014-07-09 15:18:27 +00:00
|
|
|
|
2011-11-03 10:27:53 +00:00
|
|
|
// Function for outputting an assignment problem in DIMACS format:
|
|
|
|
|
// http://lpsolve.sourceforge.net/5.5/DIMACS_asn.htm
|
|
|
|
|
//
|
2011-09-21 15:16:48 +00:00
|
|
|
#ifndef OR_TOOLS_EXAMPLES_PRINT_DIMACS_ASSIGNMENT_H_
|
|
|
|
|
#define OR_TOOLS_EXAMPLES_PRINT_DIMACS_ASSIGNMENT_H_
|
2011-06-22 08:40:00 +00:00
|
|
|
|
2012-06-13 16:14:46 +00:00
|
|
|
#include <cstdio>
|
2011-06-22 08:40:00 +00:00
|
|
|
#include <string>
|
|
|
|
|
|
2018-10-31 16:18:18 +01:00
|
|
|
#include "absl/strings/str_format.h"
|
2023-06-21 17:31:06 +02:00
|
|
|
#include "absl/strings/string_view.h"
|
2018-06-08 16:40:43 +02:00
|
|
|
#include "ortools/base/file.h"
|
2022-09-09 16:49:24 +02:00
|
|
|
#include "ortools/base/helpers.h"
|
2017-04-26 17:30:25 +02:00
|
|
|
#include "ortools/base/logging.h"
|
2024-12-29 19:21:42 +01:00
|
|
|
#include "ortools/base/options.h"
|
2017-04-26 17:30:25 +02:00
|
|
|
#include "ortools/graph/linear_assignment.h"
|
2011-06-22 08:40:00 +00:00
|
|
|
|
|
|
|
|
namespace operations_research {
|
|
|
|
|
|
2023-06-21 17:31:06 +02:00
|
|
|
// Given a LinearSumAssignment object representing an assignment problem
|
2011-11-03 10:27:53 +00:00
|
|
|
// description, outputs the problem in DIMACS format in the output file.
|
|
|
|
|
// For a description of the format, see
|
|
|
|
|
// http://lpsolve.sourceforge.net/5.5/DIMACS_asn.htm
|
2021-01-14 10:48:19 +01:00
|
|
|
template <typename GraphType>
|
|
|
|
|
void PrintDimacsAssignmentProblem(
|
|
|
|
|
const LinearSumAssignment<GraphType>& assignment,
|
2023-06-21 17:31:06 +02:00
|
|
|
absl::string_view output_filename) {
|
2020-10-29 14:25:39 +01:00
|
|
|
File* output;
|
2017-12-08 14:52:49 +01:00
|
|
|
CHECK_OK(file::Open(output_filename, "w", &output, file::Defaults()));
|
2020-10-29 14:25:39 +01:00
|
|
|
const GraphType& graph(assignment.Graph());
|
2014-01-08 12:01:58 +00:00
|
|
|
std::string output_line =
|
2018-10-31 16:18:18 +01:00
|
|
|
absl::StrFormat("p asn %d %d\n", graph.num_nodes(), graph.num_arcs());
|
2017-04-26 17:30:25 +02:00
|
|
|
CHECK_OK(file::WriteString(output, output_line, file::Defaults()));
|
2012-06-13 16:14:46 +00:00
|
|
|
|
2025-01-15 13:51:40 +01:00
|
|
|
for (const typename GraphType::NodeIndex left_node :
|
|
|
|
|
assignment.BipartiteLeftNodes()) {
|
|
|
|
|
output_line = absl::StrFormat("n %d\n", left_node + 1);
|
2017-04-26 17:30:25 +02:00
|
|
|
CHECK_OK(file::WriteString(output, output_line, file::Defaults()));
|
2012-06-13 16:14:46 +00:00
|
|
|
}
|
|
|
|
|
|
2024-12-29 19:21:42 +01:00
|
|
|
for (const auto& arc : graph.AllForwardArcs()) {
|
2018-10-31 16:18:18 +01:00
|
|
|
output_line = absl::StrFormat("a %d %d %d\n", graph.Tail(arc) + 1,
|
|
|
|
|
graph.Head(arc) + 1, assignment.ArcCost(arc));
|
2017-04-26 17:30:25 +02:00
|
|
|
CHECK_OK(file::WriteString(output, output_line, file::Defaults()));
|
2012-06-13 16:14:46 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-22 23:36:58 +02:00
|
|
|
} // namespace operations_research
|
2011-06-22 08:40:00 +00:00
|
|
|
|
2020-10-22 23:36:58 +02:00
|
|
|
#endif // OR_TOOLS_EXAMPLES_PRINT_DIMACS_ASSIGNMENT_H_
|