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