Camargue
cliq.hpp
Go to the documentation of this file.
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
9 #ifndef CMR_CLIQ_H
10 #define CMR_CLIQ_H
11 
12 #include "cut_structs.hpp"
13 #include "util.hpp"
14 
15 #include <array>
16 #include <memory>
17 #include <unordered_map>
18 #include <utility>
19 #include <vector>
20 
21 extern "C" {
22 #include <concorde/INCLUDE/tsp.h>
23 }
24 
25 namespace CMR {
26 namespace Sep {
27 
34 class Clique {
35 public:
36  Clique() = default;
37 
39  Clique(const CCtsp_lpclique &cc_cliq,
40  const std::vector<int> &saved_tour,
41  const std::vector<int> &saved_perm,
42  const std::vector<int> &current_tour);
43 
45  Clique(int start, int end,
46  const std::vector<int> &saved_tour,
47  const std::vector<int> &saved_perm,
48  const std::vector<int> & current_tour);
49 
51  Clique(const std::vector<int> &nodes, const std::vector<int> &perm,
52  bool peserve_order);
53 
54  using Ptr = std::shared_ptr<Clique>;
55 
57  int seg_count() const { return seglist.size(); }
58 
60  const std::vector<Segment> &seg_list() const { return seglist; }
61 
63  std::vector<int> node_list(const std::vector<int> &saved_tour) const;
64 
65  bool operator==(const Clique &rhs) const
66  { return seglist == rhs.seglist; }
67 
69  bool contains(const int index) const {
70  for (const Segment &seg : seglist)
71  if (seg.contains(index))
72  return true;
73  return false;
74  }
75 
76 private:
78  std::vector<Segment> seglist;
79 };
80 
91 class Tooth {
92 public:
93  Tooth() = default;
94 
96  Tooth(const SimpleTooth &T,
97  const std::vector<int> &saved_tour,
98  const std::vector<int> &saved_perm,
99  const std::vector<int> &current_tour);
100 
102  const std::array<Clique, 2> &set_pair() const { return sets; }
103 
104  using Ptr = std::shared_ptr<Tooth>;
105 
106  bool operator==(const Tooth &rhs) const
107  { return sets == rhs.sets; }
108 
109 private:
111  std::array<Clique, 2> sets;
112 };
113 
114 
115 }
116 }
117 
118 namespace std {
119 
121 template<>
122 struct hash<CMR::Sep::Clique> {
124  size_t operator()(const CMR::Sep::Clique &clq) const
125  {
126  size_t val = 0;
127 
128  for (const CMR::Segment &seg : clq.seg_list())
129  val = (val * 65537) + (seg.start * 4099) + seg.end;
130 
131  return val;
132  }
133 };
134 
136 template<>
137 struct hash<CMR::Sep::Tooth> {
139  size_t operator()(const CMR::Sep::Tooth &T) const
140  {
141  size_t val = 0;
142 
143  for (const CMR::Sep::Clique &clq : T.set_pair())
144  for (const CMR::Segment &seg : clq.seg_list())
145  val = (val * 65537) + (seg.start * 4099) + seg.end;
146 
147  return val;
148  }
149 };
150 
151 }
152 
153 namespace CMR {
154 namespace Sep {
155 
167 class CliqueBank {
168 public:
170  CliqueBank(const std::vector<int> &tour, const std::vector<int> &perm);
171 
173  Clique::Ptr add_clique(const Clique &clq);
174 
176  Clique::Ptr add_clique(const CCtsp_lpclique &cc_cliq,
177  const std::vector<int> &tour);
178 
180  Clique::Ptr add_clique(int start, int end,
181  const std::vector<int> &tour);
182 
184  Clique::Ptr add_clique(const std::vector<int> &nodes);
185 
187  Clique::Ptr add_tour_clique(const std::vector<int> &tour_nodes);
188 
190  void steal_clique(Clique::Ptr &clq_ptr, CliqueBank &from_bank);
191 
193  void del_clique(Clique::Ptr &clq_ptr);
194 
195  int size() const
196  { return bank.size(); }
197 
199  using CliqueHash = std::unordered_map<Clique, Clique::Ptr>;
200 
201  using Itr = CliqueHash::iterator;
202  using ConstItr = CliqueHash::const_iterator;
203 
204  Itr begin() { return bank.begin(); }
205  Itr end() { return bank.end(); }
206 
207  ConstItr begin() const { return bank.begin(); }
208  ConstItr end() const { return bank.end(); }
209 
210  const std::vector<int> &ref_tour() const { return saved_tour; }
211  const std::vector<int> &ref_perm() const { return saved_perm; }
212 
213 
214 private:
215  const std::vector<int> saved_tour;
216  const std::vector<int> saved_perm;
218 };
219 
220 
225 class ToothBank {
226 public:
228  ToothBank(const std::vector<int> &tour, const std::vector<int> &perm);
229 
231  ToothBank(const CliqueBank &cbank);
232 
234  Tooth::Ptr add_tooth(const SimpleTooth &T,
235  const std::vector<int> &tour);
236 
238  void del_tooth(Tooth::Ptr &T_ptr);
239 
240  int size() const
241  { return bank.size(); }
242 
244  using ToothHash = std::unordered_map<Tooth, Tooth::Ptr>;
245 
246  using Itr = ToothHash::iterator;
247  using ConstItr = ToothHash::const_iterator;
248 
250  { return bank.begin(); }
251 
252  ConstItr begin() const
253  { return bank.begin(); }
254 
256  { return bank.end(); }
257 
258  ConstItr end() const
259  { return bank.end(); }
260 
261  const std::vector<int> &ref_tour() const
262  { return saved_tour; }
263 
264  const std::vector<int> &ref_perm() const
265  { return saved_perm; }
266 
267 private:
268  const std::vector<int> saved_tour;
269  const std::vector<int> saved_perm;
271 };
272 
273 }
274 }
275 
276 #endif
Clique()=default
Default construct an empty Clique.
bool operator==(const Tooth &rhs) const
Equality operator.
Definition: cliq.hpp:106
size_t operator()(const CMR::Sep::Tooth &T) const
Call operator for hashing a Tooth.
Definition: cliq.hpp:139
Itr begin()
Begin iterator.
Definition: cliq.hpp:249
const std::vector< int > saved_perm
Permutation vector for saved_tour.
Definition: cliq.hpp:216
CliqueHash bank
Hash table of Clique and Clique::Ptr.
Definition: cliq.hpp:217
ConstItr end() const
Const past the end iterator.
Definition: cliq.hpp:258
Storage of a repository of Teeth, for use in building a HyperGraph.
Definition: cliq.hpp:225
int seg_count() const
How many segments are used to represent the Clique.
Definition: cliq.hpp:57
Vertex set structure used in tooth inequalities for domino parity cuts.
Definition: cliq.hpp:91
const std::vector< int > saved_perm
Permutation vec for saved_tour.
Definition: cliq.hpp:269
Definition: cliq.hpp:118
std::vector< Segment > seglist
A vector of start and endpoints of tour intervals stored as Segment.
Definition: cliq.hpp:78
int size() const
Number of teeth in the bank.
Definition: cliq.hpp:240
ToothHash bank
Hash table of Tooth to Tooth::Ptr.
Definition: cliq.hpp:270
std::unordered_map< Clique, Clique::Ptr > CliqueHash
Alias declaration for Clique hash table.
Definition: cliq.hpp:199
std::shared_ptr< Clique > Ptr
shared_ptr alias declaration.
Definition: cliq.hpp:54
int size() const
The number of unique Cliques in the bank.
Definition: cliq.hpp:195
const std::vector< int > & ref_perm() const
Const ref to saved perm for dereferencing.
Definition: cliq.hpp:264
const std::vector< Segment > & seg_list() const
A constant reference to the list of segments in the Clique.
Definition: cliq.hpp:60
bool operator==(const Clique &rhs) const
Equality operator.
Definition: cliq.hpp:65
Itr end()
Past the end iterator.
Definition: cliq.hpp:255
const std::array< Clique, 2 > & set_pair() const
Constant reference to the defining sets.
Definition: cliq.hpp:102
Utility functions, macros, and structures.
size_t operator()(const CMR::Sep::Clique &clq) const
Call operator for hashing a Clique.
Definition: cliq.hpp:124
ToothHash::iterator Itr
Iterator alias.
Definition: cliq.hpp:246
Storage of a repository of Cliques, for use in building a HyperGraph.
Definition: cliq.hpp:167
bool contains(const int index) const
Returns true iff the Clique contains index.
Definition: cliq.hpp:69
const std::vector< int > saved_tour
Saved tour for dereferencing.
Definition: cliq.hpp:268
Class for storing segment lists representing edges of a hypergraph.
Definition: cliq.hpp:34
std::shared_ptr< Tooth > Ptr
Pointer alias declaration.
Definition: cliq.hpp:104
ToothHash::const_iterator ConstItr
Const iterator alias.
Definition: cliq.hpp:247
Representing simple tooth inequalities.
Definition: cut_structs.hpp:39
The namespace for this project.
Definition: abc_nodesel.hpp:20
std::array< Clique, 2 > sets
Teeth are represented as a pair of Cliques for the handle and body.
Definition: cliq.hpp:111
ConstItr begin() const
Const begin iterator.
Definition: cliq.hpp:252
const std::vector< int > saved_tour
Saved tour for dereferencing.
Definition: cliq.hpp:215
std::unordered_map< Tooth, Tooth::Ptr > ToothHash
Alias declaration for Tooth hash table.
Definition: cliq.hpp:244
std::vector< int > node_list(const std::vector< int > &saved_tour) const
A list of literal nodes represented by the Clique.
Definition: cliq.cpp:183
const std::vector< int > & ref_tour() const
Const ref to saved tour for dereferencing.
Definition: cliq.hpp:261
Simple utility struct for storing an interval of nodes.
Definition: util.hpp:198