OR-Tools  9.6
qap_reader.h
Go to the documentation of this file.
1 // Copyright 2010-2022 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 
14 #ifndef OR_TOOLS_UTIL_QAP_READER_H_
15 #define OR_TOOLS_UTIL_QAP_READER_H_
16 
17 #include <string>
18 #include <vector>
19 
20 namespace operations_research {
21 
22 // Quadratic assignment problem (QAP) is a classical combinatorial optimization
23 // problem. See https://en.wikipedia.org/wiki/Quadratic_assignment_problem.
24 // In short, there are a set of n facilities and a set of n locations.
25 // For each pair of locations, a `distance` is specified and for each pair of
26 // facilities a `weight` is specified (e.g., the amount of supplies transported
27 // between the two facilities). The problem is to assign all facilities to
28 // different locations with the goal of minimizing the sum of the distances
29 // multiplied by the corresponding flows.
30 struct QapProblem {
31  // `weights[i][j]` is the amount of flow from facility i to facility j.
32  std::vector<std::vector<int64_t>> weights;
33 
34  // `distances[i][j]` is the distance from location i to location j.
35  std::vector<std::vector<int64_t>> distances;
36 
37  // Best known solution (-1 if not defined).
38  int64_t best_known_solution = -1;
39 
40  // For testing.
41  bool operator==(const QapProblem& q) const {
42  return weights == q.weights && distances == q.distances;
43  }
44 };
45 
46 // Reads a QAP problem from file in a format used in QAPLIB. See
47 // anjos.mgi.polymtl.ca/qaplib/ for more context. The format is "n W D", where
48 // n is the number of factories/locations, and W and D are weight and
49 // distance matrices, respectively. Both W and D are square matrices of size
50 // N x N. Each entry of the matrices must parse as double (we CHECK if it
51 // does). Multiple spaces, or '\n' are disregarded. For example:
52 //
53 // 2
54 //
55 // 0 2
56 // 3 0
57 //
58 // 0 2
59 // 1 0
60 //
61 // defines a problem with two factories and two locations. There are 2 units of
62 // supply flowing from factory 0 to factory 1, and 3 units of supply flowing
63 // from factory 1 to 0. The distance from location 0 to location 1 is equal
64 // to 2, and the distance from location 1 to 0 is equal to 1.
65 QapProblem ReadQapProblemOrDie(const std::string& filepath);
66 
67 } // namespace operations_research
68 
69 #endif // OR_TOOLS_UTIL_QAP_READER_H_
Collection of objects used to extend the Constraint Solver library.
QapProblem ReadQapProblemOrDie(const std::string &filepath)
Definition: qap_reader.cc:29
std::vector< std::vector< int64_t > > weights
Definition: qap_reader.h:32
bool operator==(const QapProblem &q) const
Definition: qap_reader.h:41
std::vector< std::vector< int64_t > > distances
Definition: qap_reader.h:35