Skip to content

Commit

Permalink
more cleaning on graphs
Browse files Browse the repository at this point in the history
  • Loading branch information
lperron committed Jan 6, 2025
1 parent 8722fe4 commit aac0dae
Show file tree
Hide file tree
Showing 25 changed files with 1,511 additions and 1,293 deletions.
4 changes: 4 additions & 0 deletions examples/cpp/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,7 @@ cc_library(

cc_library(
name = "parse_dimacs_assignment",
srcs = ["parse_dimacs_assignment.cc"],
hdrs = ["parse_dimacs_assignment.h"],
deps = [
"//ortools/base",
Expand Down Expand Up @@ -877,6 +878,7 @@ cc_test(
# Frequency Assignment Problem
cc_library(
name = "fap_parser",
srcs = ["fap_parser.cc"],
hdrs = ["fap_parser.h"],
deps = [
"//ortools/base",
Expand All @@ -890,6 +892,7 @@ cc_library(

cc_library(
name = "fap_model_printer",
srcs = ["fap_model_printer.cc"],
hdrs = ["fap_model_printer.h"],
deps = [
":fap_parser",
Expand All @@ -902,6 +905,7 @@ cc_library(

cc_library(
name = "fap_utilities",
srcs = ["fap_utilities.cc"],
hdrs = ["fap_utilities.h"],
deps = [
":fap_parser",
Expand Down
3 changes: 2 additions & 1 deletion examples/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,17 @@ list(FILTER CXX_SRCS EXCLUDE REGEX ".*/course_scheduling_run.cc") # missing prot
list(FILTER CXX_SRCS EXCLUDE REGEX ".*/course_scheduling.cc") # missing proto
list(FILTER CXX_SRCS EXCLUDE REGEX ".*/dimacs_assignment.cc") # crash
list(FILTER CXX_SRCS EXCLUDE REGEX ".*/dobble_ls.cc") # Too long
list(FILTER CXX_SRCS EXCLUDE REGEX ".*/fap_*.cc") # crash
list(FILTER CXX_SRCS EXCLUDE REGEX ".*/frequency_assignment_problem.cc") # crash
list(FILTER CXX_SRCS EXCLUDE REGEX ".*/jobshop_sat.cc") # crash
list(FILTER CXX_SRCS EXCLUDE REGEX ".*/knapsack_2d_sat.cc")
list(FILTER CXX_SRCS EXCLUDE REGEX ".*/mps_driver.cc") # crash
list(FILTER CXX_SRCS EXCLUDE REGEX ".*/multi_knapsack_sat.cc") # crash
list(FILTER CXX_SRCS EXCLUDE REGEX ".*/network_routing_sat.cc")
list(FILTER CXX_SRCS EXCLUDE REGEX ".*/parse_dimacs_assignment.*")
list(FILTER CXX_SRCS EXCLUDE REGEX ".*/pdlp_solve.cc")
list(FILTER CXX_SRCS EXCLUDE REGEX ".*/pdptw.cc")
list(FILTER CXX_SRCS EXCLUDE REGEX ".*/shift_minimization_sat.cc")
list(FILTER CXX_SRCS EXCLUDE REGEX ".*/pdlp_solve.cc")
list(FILTER CXX_SRCS EXCLUDE REGEX ".*/strawberry_fields_with_column_generation.cc") # Too long
list(FILTER CXX_SRCS EXCLUDE REGEX ".*/vector_bin_packing_solver.cc")
list(FILTER CXX_SRCS EXCLUDE REGEX ".*/weighted_tardiness_sat.cc")
Expand Down
93 changes: 93 additions & 0 deletions examples/cpp/fap_model_printer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Copyright 2010-2024 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/fap_model_printer.h"

#include <string>
#include <vector>

#include "absl/strings/str_format.h"
#include "absl/strings/string_view.h"
#include "ortools/base/logging.h"

namespace operations_research {

FapModelPrinter::FapModelPrinter(
const absl::btree_map<int, FapVariable>& variables,
const std::vector<FapConstraint>& constraints, absl::string_view objective,
const std::vector<int>& values)
: variables_(variables),
constraints_(constraints),
objective_(objective),
values_(values) {}

FapModelPrinter::~FapModelPrinter() = default;

void FapModelPrinter::PrintFapVariables() {
LOG(INFO) << "Variable File:";
for (const auto& it : variables_) {
std::string domain = "{";
for (const int value : it.second.domain) {
absl::StrAppendFormat(&domain, "%d ", value);
}
domain.append("}");

std::string hard = " ";
if (it.second.hard) {
hard = " hard";
}

LOG(INFO) << "Variable " << absl::StrFormat("%3d: ", it.first)
<< absl::StrFormat("(degree: %2d) ", it.second.degree)
<< absl::StrFormat("%3d", it.second.domain_index)
<< absl::StrFormat("%3d", it.second.initial_position)
<< absl::StrFormat("%3d", it.second.mobility_index)
<< absl::StrFormat("%8d", it.second.mobility_cost)
<< absl::StrFormat(" (%2d) ", it.second.domain_size) << domain
<< hard;
}
}

void FapModelPrinter::PrintFapConstraints() {
LOG(INFO) << "Constraint File:";
for (const FapConstraint& ct : constraints_) {
std::string hard = " ";
if (ct.hard) {
hard = " hard";
}

LOG(INFO) << absl::StrFormat("%3d ", ct.variable1)
<< absl::StrFormat("%3d ", ct.variable2) << ct.type << " "
<< ct.operation << " " << absl::StrFormat("%3d", ct.value)
<< absl::StrFormat("%3d", ct.weight_index)
<< absl::StrFormat("%8d", ct.weight_cost) << hard;
}
}

void FapModelPrinter::PrintFapObjective() {
LOG(INFO) << "Objective: " << objective_;
}

void FapModelPrinter::PrintFapValues() {
LOG(INFO) << absl::StrFormat("Values(%d): ",
static_cast<int>(values_.size()));
std::string domain = " ";
for (const int value : values_) {
absl::StrAppendFormat(&domain, "%d ", value);
}
LOG(INFO) << domain;
}

} // namespace operations_research
73 changes: 5 additions & 68 deletions examples/cpp/fap_model_printer.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@
#ifndef OR_TOOLS_EXAMPLES_FAP_MODEL_PRINTER_H_
#define OR_TOOLS_EXAMPLES_FAP_MODEL_PRINTER_H_

#include <map>
#include <string>
#include <vector>

#include "absl/container/btree_map.h"
#include "absl/strings/str_format.h"
#include "examples/cpp/fap_parser.h"

namespace operations_research {
Expand All @@ -35,6 +33,11 @@ class FapModelPrinter {
FapModelPrinter(const absl::btree_map<int, FapVariable>& variables,
const std::vector<FapConstraint>& constraints,
absl::string_view objective, const std::vector<int>& values);

// This type is neither copyable nor movable.
FapModelPrinter(const FapModelPrinter&) = delete;
FapModelPrinter& operator=(const FapModelPrinter&) = delete;

~FapModelPrinter();

void PrintFapObjective();
Expand All @@ -47,73 +50,7 @@ class FapModelPrinter {
const std::vector<FapConstraint> constraints_;
const std::string objective_;
const std::vector<int> values_;
DISALLOW_COPY_AND_ASSIGN(FapModelPrinter);
};

FapModelPrinter::FapModelPrinter(const absl::btree_map<int, FapVariable>& variables,
const std::vector<FapConstraint>& constraints,
absl::string_view objective,
const std::vector<int>& values)
: variables_(variables),
constraints_(constraints),
objective_(objective),
values_(values) {}

FapModelPrinter::~FapModelPrinter() {}

void FapModelPrinter::PrintFapVariables() {
LOG(INFO) << "Variable File:";
for (const auto& it : variables_) {
std::string domain = "{";
for (const int value : it.second.domain) {
absl::StrAppendFormat(&domain, "%d ", value);
}
domain.append("}");

std::string hard = " ";
if (it.second.hard) {
hard = " hard";
}

LOG(INFO) << "Variable " << absl::StrFormat("%3d: ", it.first)
<< absl::StrFormat("(degree: %2d) ", it.second.degree)
<< absl::StrFormat("%3d", it.second.domain_index)
<< absl::StrFormat("%3d", it.second.initial_position)
<< absl::StrFormat("%3d", it.second.mobility_index)
<< absl::StrFormat("%8d", it.second.mobility_cost)
<< absl::StrFormat(" (%2d) ", it.second.domain_size) << domain
<< hard;
}
}

void FapModelPrinter::PrintFapConstraints() {
LOG(INFO) << "Constraint File:";
for (const FapConstraint& ct : constraints_) {
std::string hard = " ";
if (ct.hard) {
hard = " hard";
}

LOG(INFO) << absl::StrFormat("%3d ", ct.variable1)
<< absl::StrFormat("%3d ", ct.variable2) << ct.type << " "
<< ct.operation << " " << absl::StrFormat("%3d", ct.value)
<< absl::StrFormat("%3d", ct.weight_index)
<< absl::StrFormat("%8d", ct.weight_cost) << hard;
}
}

void FapModelPrinter::PrintFapObjective() {
LOG(INFO) << "Objective: " << objective_;
}

void FapModelPrinter::PrintFapValues() {
LOG(INFO) << absl::StrFormat("Values(%d): ", values_.size());
std::string domain = " ";
for (const int value : values_) {
absl::StrAppendFormat(&domain, "%d ", value);
}
LOG(INFO) << domain;
}

} // namespace operations_research
#endif // OR_TOOLS_EXAMPLES_FAP_MODEL_PRINTER_H_
Loading

0 comments on commit aac0dae

Please sign in to comment.