Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
qap_reader.cc
Go to the documentation of this file.
1// Copyright 2010-2024 Google LLC
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
15
16#include <limits>
17#include <string>
18#include <vector>
19
20#include "absl/log/check.h"
21#include "absl/strings/numbers.h"
22#include "absl/strings/str_split.h"
23#include "absl/strings/string_view.h"
25
26namespace operations_research {
27
28// TODO(user): Unit test cases when the function dies, or return
29// (and test) a status instead.
30QapProblem ReadQapProblemOrDie(absl::string_view filepath) {
31 QapProblem qap_problem;
32
33 int n = 0;
34 int k = 0;
35 for (const std::string& line :
37 const std::vector<std::string> tokens =
38 absl::StrSplit(line, ' ', absl::SkipEmpty());
39 if (tokens.empty()) continue;
40 if (k == 0) {
41 CHECK_GE(tokens.size(), 1);
42 CHECK_LE(tokens.size(), 2);
43 CHECK(absl::SimpleAtoi(tokens[0], &n));
44 qap_problem.weights.resize(n);
45 qap_problem.distances.resize(n);
46 for (int j = 0; j < n; ++j) {
47 qap_problem.weights[j].assign(n, std::numeric_limits<int64_t>::max());
48 qap_problem.distances[j].assign(n, std::numeric_limits<int64_t>::max());
49 }
50 if (tokens.size() == 2) {
51 CHECK(absl::SimpleAtoi(tokens[1], &qap_problem.best_known_solution));
52 }
53 ++k;
54 } else if (k <= n * n) {
55 for (const std::string& token : tokens) {
56 const int i = (k - 1) / n;
57 const int j = (k - 1) % n;
58 int64_t v = 0.0;
59 CHECK(absl::SimpleAtoi(token, &v)) << "'" << token << "'";
60 qap_problem.weights[i][j] = v;
61 ++k;
62 }
63 } else if (k <= 2 * n * n) {
64 for (const std::string& token : tokens) {
65 const int i = (k - n * n - 1) / n;
66 const int j = (k - n * n - 1) % n;
67 int64_t v = 0.0;
68 CHECK(absl::SimpleAtoi(token, &v));
69 qap_problem.distances[i][j] = v;
70 ++k;
71 }
72 } else {
73 CHECK(false) << "File contains more than 1 + 2 * N^2 entries.";
74 }
75 }
76 return qap_problem;
77}
78
79} // namespace operations_research
In SWIG mode, we don't want anything besides these top-level includes.
QapProblem ReadQapProblemOrDie(absl::string_view filepath)
Definition qap_reader.cc:30
int line
std::vector< std::vector< int64_t > > distances
distances[i][j] is the distance from location i to location j.
Definition qap_reader.h:37
int64_t best_known_solution
Best known solution (-1 if not defined).
Definition qap_reader.h:40
std::vector< std::vector< int64_t > > weights
weights[i][j] is the amount of flow from facility i to facility j.
Definition qap_reader.h:34