Google OR-Tools v9.14
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
symmetry.h
Go to the documentation of this file.
1// Copyright 2010-2025 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_MATH_OPT_ELEMENTAL_SYMMETRY_H_
15#define OR_TOOLS_MATH_OPT_ELEMENTAL_SYMMETRY_H_
16
17#include <array>
18#include <cstddef>
19#include <cstdint>
20#include <string>
21
22#include "absl/log/check.h"
23#include "absl/strings/str_format.h"
24#include "absl/strings/string_view.h"
25
27
28// A tag for no symmetry between two elements.
29struct NoSymmetry {
30 static constexpr absl::string_view GetName() { return "NoSymmetry"; };
31
32 template <size_t n>
33 static constexpr bool Validate(std::array<int64_t, n>&) {
34 // All keys are valid.
35 return true;
36 }
37
38 template <size_t n>
39 static constexpr void Enforce(std::array<int64_t, n>&) {
40 // No symmetry to enforce.
41 }
42
43 template <typename ElementTypeT, size_t n>
44 static constexpr void CheckElementTypes(const std::array<ElementTypeT, n>&) {
45 // No type constraints.
46 }
47};
48
49// A tag to represent a symmetry between two elements `i` and `j`, i.e. the fact
50// that the attribute value for `(key[i],key[j])` and `(key[j],key[i])` are the
51// same. We internally represent such attribute keys with `key[i] <= key[j]`.
52template <int i, int j>
54 static_assert(0 <= i && i < j);
55
56 static std::string GetName() {
57 return absl::StrFormat("ElementSymmetry<%i, %i>", i, j);
58 };
59
60 template <size_t n>
61 static constexpr bool Validate(std::array<int64_t, n>& ids) {
62 static_assert(n > 1, "one-dimensional keys cannot have symmetries");
63 static_assert(j < n);
64 return ids[i] <= ids[j];
65 }
66
67 template <size_t n>
68 static constexpr void Enforce(std::array<int64_t, n>& ids) {
69 static_assert(n > 1, "one-dimensional keys cannot have symmetries");
70 if (ids[i] > ids[j]) {
71 std::swap(ids[i], ids[j]);
72 }
73 }
74
75 template <typename ElementTypeT, size_t n>
76 static constexpr void CheckElementTypes(
77 const std::array<ElementTypeT, n>& element_types) {
78 static_assert(n > 1, "one-dimensional keys cannot have symmetries");
79 static_assert(j < n);
80 CHECK_EQ(element_types[i], element_types[j])
81 << "symmetric elements must be of the same type";
82 }
83};
84
85} // namespace operations_research::math_opt
86
87#endif // OR_TOOLS_MATH_OPT_ELEMENTAL_SYMMETRY_H_
An object oriented wrapper for quadratic constraints in ModelStorage.
Definition gurobi_isv.cc:28
static constexpr void Enforce(std::array< int64_t, n > &ids)
Definition symmetry.h:68
static constexpr bool Validate(std::array< int64_t, n > &ids)
Definition symmetry.h:61
static constexpr void CheckElementTypes(const std::array< ElementTypeT, n > &element_types)
Definition symmetry.h:76
A tag for no symmetry between two elements.
Definition symmetry.h:29
static constexpr void Enforce(std::array< int64_t, n > &)
Definition symmetry.h:39
static constexpr bool Validate(std::array< int64_t, n > &)
Definition symmetry.h:33
static constexpr void CheckElementTypes(const std::array< ElementTypeT, n > &)
Definition symmetry.h:44
static constexpr absl::string_view GetName()
Definition symmetry.h:30