Fix dimacs_assignment on Windows (#4940)

This commit is contained in:
Guillaume Chatelet
2025-12-08 11:23:19 +01:00
committed by Corentin Le Molgat
parent b7d1dc65dc
commit e09ed7a33a
5 changed files with 73 additions and 53 deletions

View File

@@ -650,12 +650,10 @@ cc_library(
cc_library(
name = "parse_dimacs_assignment",
srcs = ["parse_dimacs_assignment.cc"],
hdrs = ["parse_dimacs_assignment.h"],
deps = [
"//ortools/graph:linear_assignment",
"//ortools/util:filelineiter",
"@abseil-cpp//absl/flags:flag",
"@abseil-cpp//absl/log:check",
"@abseil-cpp//absl/strings",
],

View File

@@ -3,7 +3,6 @@
# You can update this file by running:
# python3 tools/build/bazel2cmake.py examples/cpp/BUILD.bazel
ortools_cxx_binary(
NAME bzl_cc_example_binpacking_2d_sat
SOURCES binpacking_2d_sat.cc
@@ -12,12 +11,17 @@ ortools_cxx_binary(
ortools_cxx_bintest(
NAME bzl_cc_example_binpacking_2d_sat_class01_instance2_test
SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/binpacking_2d_sat_class01_instance2_test.bintest
ENVIRONMENT BINTEST_binpacking_2d_sat=$<TARGET_FILE:bzl_cc_example_binpacking_2d_sat> BINTEST_Class_01.2bp=${CMAKE_SOURCE_DIR}/ortools/packing/testdata/Class_01.2bp
ENVIRONMENT
BINTEST_binpacking_2d_sat=$<TARGET_FILE:bzl_cc_example_binpacking_2d_sat>
BINTEST_Class_01.2bp=${CMAKE_SOURCE_DIR}/ortools/packing/testdata/Class_01.2bp
)
ortools_cxx_library(
NAME bzl_cc_example_cgc
SOURCES cgc.cc cgc.h cgc_data.h
SOURCES
cgc.cc
cgc.h
cgc_data.h
TYPE SHARED
)
@@ -30,7 +34,14 @@ ortools_cxx_binary(
ortools_cxx_bintest(
NAME bzl_cc_example_cgc_test_solution
SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/cgc_test_solution.bintest
ENVIRONMENT BINTEST_cgc_main=$<TARGET_FILE:bzl_cc_example_cgc_main> BINTEST_1.in=${CMAKE_CURRENT_SOURCE_DIR}/testdata/cgc/1.in BINTEST_2.in=${CMAKE_CURRENT_SOURCE_DIR}/testdata/cgc/2.in BINTEST_3.in=${CMAKE_CURRENT_SOURCE_DIR}/testdata/cgc/3.in BINTEST_cgcut1.in=${CMAKE_CURRENT_SOURCE_DIR}/testdata/cgc/cgcut1.in BINTEST_cgcut2.in=${CMAKE_CURRENT_SOURCE_DIR}/testdata/cgc/cgcut2.in BINTEST_cgcut3.in=${CMAKE_CURRENT_SOURCE_DIR}/testdata/cgc/cgcut3.in
ENVIRONMENT
BINTEST_cgc_main=$<TARGET_FILE:bzl_cc_example_cgc_main>
BINTEST_1.in=${CMAKE_CURRENT_SOURCE_DIR}/testdata/cgc/1.in
BINTEST_2.in=${CMAKE_CURRENT_SOURCE_DIR}/testdata/cgc/2.in
BINTEST_3.in=${CMAKE_CURRENT_SOURCE_DIR}/testdata/cgc/3.in
BINTEST_cgcut1.in=${CMAKE_CURRENT_SOURCE_DIR}/testdata/cgc/cgcut1.in
BINTEST_cgcut2.in=${CMAKE_CURRENT_SOURCE_DIR}/testdata/cgc/cgcut2.in
BINTEST_cgcut3.in=${CMAKE_CURRENT_SOURCE_DIR}/testdata/cgc/cgcut3.in
)
ortools_cxx_binary(
@@ -108,7 +119,9 @@ ortools_cxx_binary(
ortools_cxx_bintest(
NAME bzl_cc_example_knapsack_2d_sat_class01_instance2_test
SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/knapsack_2d_sat_class01_instance2_test.bintest
ENVIRONMENT BINTEST_knapsack_2d_sat=$<TARGET_FILE:bzl_cc_example_knapsack_2d_sat> BINTEST_Class_01.2bp=${CMAKE_SOURCE_DIR}/ortools/packing/testdata/Class_01.2bp
ENVIRONMENT
BINTEST_knapsack_2d_sat=$<TARGET_FILE:bzl_cc_example_knapsack_2d_sat>
BINTEST_Class_01.2bp=${CMAKE_SOURCE_DIR}/ortools/packing/testdata/Class_01.2bp
)
ortools_cxx_binary(
@@ -119,7 +132,9 @@ ortools_cxx_binary(
ortools_cxx_bintest(
NAME bzl_cc_example_jobshop_sat_ft06
SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/jobshop_sat_ft06.bintest
ENVIRONMENT BINTEST_jobshop_sat=$<TARGET_FILE:bzl_cc_example_jobshop_sat> BINTEST_ft06=${CMAKE_SOURCE_DIR}/ortools/scheduling/testdata/ft06
ENVIRONMENT
BINTEST_jobshop_sat=$<TARGET_FILE:bzl_cc_example_jobshop_sat>
BINTEST_ft06=${CMAKE_SOURCE_DIR}/ortools/scheduling/testdata/ft06
)
ortools_cxx_binary(
@@ -152,7 +167,9 @@ ortools_cxx_binary(
ortools_cxx_bintest(
NAME bzl_cc_example_shift_minimization_sat_test
SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/shift_minimization_sat_test.bintest
ENVIRONMENT BINTEST_shift_minimization_sat=$<TARGET_FILE:bzl_cc_example_shift_minimization_sat> BINTEST_shift_minimization.dat=${CMAKE_CURRENT_SOURCE_DIR}/testdata/shift_minimization.dat
ENVIRONMENT
BINTEST_shift_minimization_sat=$<TARGET_FILE:bzl_cc_example_shift_minimization_sat>
BINTEST_shift_minimization.dat=${CMAKE_CURRENT_SOURCE_DIR}/testdata/shift_minimization.dat
)
ortools_cxx_binary(
@@ -163,7 +180,9 @@ ortools_cxx_binary(
ortools_cxx_bintest(
NAME bzl_cc_example_weighted_tardiness_sat_test
SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/weighted_tardiness_sat_test.bintest
ENVIRONMENT BINTEST_weighted_tardiness_sat=$<TARGET_FILE:bzl_cc_example_weighted_tardiness_sat> BINTEST_wt40.txt=${CMAKE_CURRENT_SOURCE_DIR}/testdata/wt40.txt
ENVIRONMENT
BINTEST_weighted_tardiness_sat=$<TARGET_FILE:bzl_cc_example_weighted_tardiness_sat>
BINTEST_wt40.txt=${CMAKE_CURRENT_SOURCE_DIR}/testdata/wt40.txt
)
ortools_cxx_binary(
@@ -218,13 +237,17 @@ ortools_cxx_binary(
ortools_cxx_bintest(
NAME bzl_cc_example_pdptw_test
SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/pdptw_test.bintest
ENVIRONMENT BINTEST_pdptw=$<TARGET_FILE:bzl_cc_example_pdptw> BINTEST_lc102.txt=${CMAKE_CURRENT_SOURCE_DIR}/testdata/lc102.txt
ENVIRONMENT
BINTEST_pdptw=$<TARGET_FILE:bzl_cc_example_pdptw>
BINTEST_lc102.txt=${CMAKE_CURRENT_SOURCE_DIR}/testdata/lc102.txt
)
ortools_cxx_bintest(
NAME bzl_cc_example_pdptw_non_homogenous_fleet_test
SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/pdptw_non_homogenous_fleet_test.bintest
ENVIRONMENT BINTEST_pdptw=$<TARGET_FILE:bzl_cc_example_pdptw> BINTEST_lc102.txt=${CMAKE_CURRENT_SOURCE_DIR}/testdata/lc102.txt
ENVIRONMENT
BINTEST_pdptw=$<TARGET_FILE:bzl_cc_example_pdptw>
BINTEST_lc102.txt=${CMAKE_CURRENT_SOURCE_DIR}/testdata/lc102.txt
)
ortools_cxx_binary(
@@ -284,26 +307,32 @@ ortools_cxx_library(
ortools_cxx_library(
NAME bzl_cc_example_parse_dimacs_assignment
SOURCES parse_dimacs_assignment.cc parse_dimacs_assignment.h
TYPE SHARED
SOURCES parse_dimacs_assignment.h
TYPE INTERFACE
)
ortools_cxx_binary(
NAME bzl_cc_example_dimacs_assignment
SOURCES dimacs_assignment.cc
LINK_LIBRARIES bzl_cc_example_parse_dimacs_assignment bzl_cc_example_print_dimacs_assignment
LINK_LIBRARIES
bzl_cc_example_parse_dimacs_assignment
bzl_cc_example_print_dimacs_assignment
)
ortools_cxx_bintest(
NAME bzl_cc_example_dimacs_assignment_min_cost_test
SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/dimacs_assignment_min_cost_test.bintest
ENVIRONMENT BINTEST_dimacs_assignment=$<TARGET_FILE:bzl_cc_example_dimacs_assignment> BINTEST_dimacs_example.txt=${CMAKE_CURRENT_SOURCE_DIR}/testdata/dimacs_example.txt
ENVIRONMENT
BINTEST_dimacs_assignment=$<TARGET_FILE:bzl_cc_example_dimacs_assignment>
BINTEST_dimacs_example.txt=${CMAKE_CURRENT_SOURCE_DIR}/testdata/dimacs_example.txt
)
ortools_cxx_bintest(
NAME bzl_cc_example_dimacs_assignment_max_cost_test
SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/dimacs_assignment_max_cost_test.bintest
ENVIRONMENT BINTEST_dimacs_assignment=$<TARGET_FILE:bzl_cc_example_dimacs_assignment> BINTEST_dimacs_example.txt=${CMAKE_CURRENT_SOURCE_DIR}/testdata/dimacs_example.txt
ENVIRONMENT
BINTEST_dimacs_assignment=$<TARGET_FILE:bzl_cc_example_dimacs_assignment>
BINTEST_dimacs_example.txt=${CMAKE_CURRENT_SOURCE_DIR}/testdata/dimacs_example.txt
)
ortools_cxx_binary(
@@ -314,7 +343,9 @@ ortools_cxx_binary(
ortools_cxx_bintest(
NAME bzl_cc_example_mps_driver_test
SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/mps_driver_test.bintest
ENVIRONMENT BINTEST_mps_driver=$<TARGET_FILE:bzl_cc_example_mps_driver> BINTEST_maximization.mps=${CMAKE_SOURCE_DIR}/ortools/linear_solver/testdata/maximization.mps
ENVIRONMENT
BINTEST_mps_driver=$<TARGET_FILE:bzl_cc_example_mps_driver>
BINTEST_maximization.mps=${CMAKE_SOURCE_DIR}/ortools/linear_solver/testdata/maximization.mps
)
ortools_cxx_binary(
@@ -363,20 +394,26 @@ ortools_cxx_bintest(
ortools_cxx_library(
NAME bzl_cc_example_fap_parser
SOURCES fap_parser.cc fap_parser.h
SOURCES
fap_parser.cc
fap_parser.h
TYPE SHARED
)
ortools_cxx_library(
NAME bzl_cc_example_fap_model_printer
SOURCES fap_model_printer.cc fap_model_printer.h
SOURCES
fap_model_printer.cc
fap_model_printer.h
LINK_LIBRARIES bzl_cc_example_fap_parser
TYPE SHARED
)
ortools_cxx_library(
NAME bzl_cc_example_fap_utilities
SOURCES fap_utilities.cc fap_utilities.h
SOURCES
fap_utilities.cc
fap_utilities.h
LINK_LIBRARIES bzl_cc_example_fap_parser
TYPE SHARED
)
@@ -384,7 +421,10 @@ ortools_cxx_library(
ortools_cxx_binary(
NAME bzl_cc_example_frequency_assignment_problem
SOURCES frequency_assignment_problem.cc
LINK_LIBRARIES bzl_cc_example_fap_model_printer bzl_cc_example_fap_parser bzl_cc_example_fap_utilities
LINK_LIBRARIES
bzl_cc_example_fap_model_printer
bzl_cc_example_fap_parser
bzl_cc_example_fap_utilities
)
ortools_cxx_binary(
@@ -422,4 +462,4 @@ ortools_cxx_bintest(
ortools_cxx_binary(
NAME bzl_cc_example_pdlp_solve
SOURCES pdlp_solve.cc
)
)

View File

@@ -42,6 +42,8 @@ ABSL_FLAG(bool, assignment_static_graph, true,
"Use the StaticGraph representation, "
"otherwise ListGraph or ReverseArcListGraph according "
"to --assignment_reverse_arcs.");
ABSL_FLAG(bool, assignment_maximize_cost, false,
"Negate costs so a max-cost assignment is found.");
namespace operations_research {
@@ -117,7 +119,8 @@ int SolveDimacsAssignment(int argc, char* argv[]) {
// Handle on the graph we will need to delete because the
// LinearSumAssignment object does not take ownership of it.
GraphType* graph = nullptr;
DimacsAssignmentParser<GraphType> parser(argv[1]);
DimacsAssignmentParser<GraphType> parser(
argv[1], absl::GetFlag(FLAGS_assignment_maximize_cost));
LinearSumAssignment<GraphType>* assignment =
parser.Parse(&error_message, &graph);
if (assignment == nullptr) {

View File

@@ -1,19 +0,0 @@
// 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.
#include "examples/cpp/parse_dimacs_assignment.h"
#include "absl/flags/flag.h"
ABSL_FLAG(bool, assignment_maximize_cost, false,
"Negate costs so a max-cost assignment is found.");

View File

@@ -25,15 +25,11 @@
#include <memory>
#include <string>
#include "absl/flags/declare.h"
#include "absl/flags/flag.h"
#include "absl/log/check.h"
#include "absl/strings/string_view.h"
#include "ortools/graph/linear_assignment.h"
#include "ortools/util/filelineiter.h"
ABSL_DECLARE_FLAG(bool, assignment_maximize_cost);
namespace operations_research {
template <typename GraphType>
@@ -43,8 +39,12 @@ class DimacsAssignmentParser {
using ArcIndex = typename GraphType::ArcIndex;
using CostValue = typename LinearSumAssignment<GraphType>::CostValueT;
explicit DimacsAssignmentParser(absl::string_view filename)
: filename_(filename), graph_(nullptr), assignment_(nullptr) {}
explicit DimacsAssignmentParser(absl::string_view filename,
bool maximize_cost)
: filename_(filename),
graph_(nullptr),
assignment_(nullptr),
maximize_cost_(maximize_cost) {}
// Reads an assignment problem description from the given file in
// DIMACS format and returns a LinearSumAssignment object representing
@@ -90,10 +90,9 @@ class DimacsAssignmentParser {
};
ErrorTrackingState state_;
std::unique_ptr<GraphType> graph_;
LinearSumAssignment<GraphType>* assignment_;
bool maximize_cost_;
};
// Implementation is below here.
@@ -163,8 +162,7 @@ void DimacsAssignmentParser<GraphType>::ParseArcLine(const std::string& line) {
state_.bad_line.reset(new std::string(line));
}
ArcIndex arc = graph_->AddArc(tail - 1, head - 1);
assignment_->SetArcCost(
arc, absl::GetFlag(FLAGS_assignment_maximize_cost) ? -cost : cost);
assignment_->SetArcCost(arc, maximize_cost_ ? -cost : cost);
}
// Parameters out of style-guide order because this function is used