Camargue
datagroups.hpp
Go to the documentation of this file.
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
7 #ifndef CMR_DATAGROUP_H
8 #define CMR_DATAGROUP_H
9 
10 #include "util.hpp"
11 #include "err_util.hpp"
12 #include "graph.hpp"
13 
14 #include <algorithm>
15 #include <functional>
16 #include <iostream>
17 #include <memory>
18 #include <string>
19 #include <stdexcept>
20 #include <vector>
21 
22 #include <cmath>
23 
24 extern "C" {
25 #include <concorde/INCLUDE/util.h>
26 }
27 
28 
29 namespace CMR {
30 
32 namespace Data {
33 
36 class Instance {
37 public:
38  Instance() noexcept;
39 
41  Instance(const std::string &fname, int seed);
42 
44  Instance(int seed, int ncount, int gridsize);
45 
47  Instance(const std::string &probname, int seed, int ncount,
48  std::vector<int> &elist, std::vector<int> &elen, int default_len);
49 
50  Instance(const std::string &probname, int seed, int ncount,
51  std::vector<int> &elist, std::vector<int> &elen)
52  : Instance(probname, seed, ncount, elist, elen, 0) {}
53 
54  Instance(Instance &&I) noexcept;
55  Instance &operator=(Instance &&I) noexcept;
56 
57  ~Instance();
58 
59 
61  CCdatagroup* ptr() const { return const_cast<CCdatagroup*>(&dat); }
62 
64  double edgelen(int end0, int end1) const
65  { return CCutil_dat_edgelen(end0, end1, ptr()); }
66 
68  const std::function<double(int, int)> edgelen_func() const
69  { return [this](int e0, int e1){ return edgelen(e0, e1); }; }
70 
71  int node_count() const { return nodecount; }
72  int seed() const { return random_seed; }
73 
75  double tour_length(const std::vector<int> &tour_nodes) const;
76 
78  const std::string &problem_name() const { return pname; }
79 
80 private:
81  CCdatagroup dat;
82 
83  int nodecount;
84  int random_seed;
85 
86  std::string pname;
87 };
88 
89 }
90 
91 namespace Graph {
92 
94 enum class EdgePlan {
95  Linkern,
96  Delaunay,
97 };
98 
100 class CoreGraph {
101 public:
102  CoreGraph() = default;
103 
105  CoreGraph(const Data::Instance &inst, Graph::EdgePlan edge_plan);
106 
109  : CoreGraph(inst, EdgePlan::Linkern) {}
110 
112  CoreGraph(int ncount, int ecount, const int *elist,
113  const std::function<double(int, int)> edgelen);
114 
116  CoreGraph(const std::vector<int> &tour_nodes,
117  const std::function<double(int, int)> edgelen);
118 
119  int node_count() const { return nodecount; }
120  int edge_count() const { return edges.size(); }
121 
123  int find_edge_ind(int end0, int end1) const;
124 
125  Edge get_edge(int index) const { return edges[index]; }
126 
127  std::vector<Edge> &get_edges() { return edges; }
128  const std::vector<Edge> &get_edges() const { return edges; }
129 
130  const AdjList &get_adj() const { return adj_list; }
131 
132  void add_edge(int end0, int end1, int len);
133  void add_edge(const Edge &e);
134 
135  void remove_edges();
136 
138  template<typename numtype>
139  void tour_edge_vec(const std::vector<int> &tour_nodes,
140  std::vector<numtype> &tour_edges,
141  double &tour_val) const;
142 
143 private:
144  std::vector<Edge> edges;
145  AdjList adj_list;
146  int nodecount;
147 };
148 
149 }
150 
151 namespace Data {
152 
154 struct BestGroup {
155  BestGroup() : min_tour_value(0) {}
156 
158  BestGroup(const Instance &inst, Graph::CoreGraph &core_graph);
159 
161  BestGroup(const Instance &inst, Graph::CoreGraph &core_graph,
162  const std::string &tourfile);
163 
165  std::vector<int> best_tour_edges;
166 
167  std::vector<int> best_tour_nodes;
168  std::vector<int> perm;
169 
170  double min_tour_value;
171 };
172 
174 struct SupportGroup {
175  SupportGroup() = default;
176 
178  SupportGroup(const std::vector<Graph::Edge> &edges,
179  const std::vector<double> &lp_x,
180  std::vector<int> &island,
181  int ncount);
182 
183  SupportGroup(SupportGroup &&SG) noexcept;
184  SupportGroup &operator=(SupportGroup &&SG) noexcept;
185 
186  bool in_subtour_poly();
187 
188  std::vector<double> lp_vec;
189  std::vector<int> support_indices;
190  std::vector<int> support_elist;
191  std::vector<double> support_ecap;
192 
194 
195  bool connected;
196  bool integral;
197 };
198 
200 void make_cut_test(const std::string &tsp_fname,
201  const std::string &tour_nodes_fname,
202  const std::string &lp_sol_fname,
203  Graph::CoreGraph &core_graph,
204  Data::BestGroup &best_data,
205  std::vector<double> &lp_edges,
206  Data::SupportGroup &supp_data);
207 
209 void make_cut_test(const std::string &tsp_fname,
210  const std::string &tour_nodes_fname,
211  const std::string &lp_sol_fname,
212  Graph::CoreGraph &core_graph,
213  Data::BestGroup &best_data,
214  std::vector<double> &lp_edges,
215  Data::SupportGroup &supp_data,
216  Data::Instance &inst);
217 
218 }
219 
221 
222 namespace Graph {
223 
230 template<typename numtype>
231 void CoreGraph::tour_edge_vec(const std::vector<int> &tour_nodes,
232  std::vector<numtype> &tour_edges,
233  double &tour_val) const
234 {
235  using std::cerr;
236  using std::endl;
237  using std::runtime_error;
238  using std::vector;
239 
240  tour_edges.resize(edges.size());
241  std::fill(tour_edges.begin(), tour_edges.end(), 0);
242  tour_val = 0.0;
243 
244  int ncount = nodecount;
245 
246  for (int i = 0; i < ncount; ++i) {
247  EndPts e(tour_nodes[i], tour_nodes[(i + 1) % ncount]);
248  int ind = find_edge_ind(e.end[0], e.end[1]);
249  if (ind == -1) {
250  cerr << e << " not in CoreGraph" << endl;
251  throw runtime_error("Missing tour edge in CoreGraph");
252  }
253  tour_edges[ind] = 1;
254  tour_val += edges[ind].len;
255  }
256 }
257 
258 }
259 }
260 
261 #endif
Support graph data for an LP solution.
Definition: datagroups.hpp:174
std::vector< double > lp_vec
The LP solution vector.
Definition: datagroups.hpp:188
int node_count() const
Number of nodes.
Definition: datagroups.hpp:71
std::vector< double > support_ecap
Weights on nonzero entries.
Definition: datagroups.hpp:191
std::vector< int > best_tour_edges
Binary vector of tour edges.
Definition: datagroups.hpp:165
void make_cut_test(const std::string &tsp_fname, const std::string &tour_nodes_fname, const std::string &lp_sol_fname, Graph::CoreGraph &core_graph, Data::BestGroup &best_data, std::vector< double > &lp_edges, Data::SupportGroup &supp_data)
Load just enough data to test separation routines.
double edgelen(int end0, int end1) const
Edge length between two nodes in an Instance.
Definition: datagroups.hpp:64
Header for classes/structures/functions to work with graphs.
Information about the current best tour.
Definition: datagroups.hpp:154
std::vector< int > support_indices
Nonzero entries of lp_vec.
Definition: datagroups.hpp:189
CCdatagroup dat
The Concorde data structure being managed.
Definition: datagroups.hpp:81
bool connected
Is the graph connected.
Definition: datagroups.hpp:195
Instance() noexcept
Default construct an empty instance.
Definition: datagroups.cpp:48
int seed() const
Random seed used.
Definition: datagroups.hpp:72
Graph structures for the edges currently in a CoreLP::Relaxation.
Definition: datagroups.hpp:100
CoreGraph(const Data::Instance &inst)
Construct a CoreGraph as above with EdgePlan::Linkern edges.
Definition: datagroups.hpp:108
10 LK tours, with quadnearest for tiny instances.
const std::function< double(int, int)> edgelen_func() const
A function object for the edge length.
Definition: datagroups.hpp:68
Storing TSP instance data.
Definition: datagroups.hpp:36
Representing graph edges and costs.
Definition: graph.hpp:29
ERROR HANDLING CLASSES AND ROUTINES.
std::vector< int > support_elist
Node-node elist for nonzero entries.
Definition: datagroups.hpp:190
Utility functions, macros, and structures.
std::vector< int > perm
Defined by perm[best_tour_nodes[i]] = i.
Definition: datagroups.hpp:168
double min_tour_value
The tour length.
Definition: datagroups.hpp:170
CCdatagroup * ptr() const
Access the raw pointer to the data, for use by Concorde routines.
Definition: datagroups.hpp:61
bool integral
Is the solution vector integral.
Definition: datagroups.hpp:196
EdgePlan
Edge generation protocol to use.
Definition: datagroups.hpp:94
The namespace for this project.
Definition: abc_nodesel.hpp:20
Graph::AdjList supp_graph
Adjacency list rep of support graph.
Definition: datagroups.hpp:193
Representation of a graph as an adjacency list.
Definition: graph.hpp:80
Simple base class for storing edge of a graph as a sorted pair of nodes.
Definition: util.hpp:239
double tour_length(const std::vector< int > &tour_nodes) const
Get the length of the tour in tour_nodes.
Definition: datagroups.cpp:177
const std::string & problem_name() const
The TSPLIB instance name or the random problem dimensions.
Definition: datagroups.hpp:78
std::vector< int > best_tour_nodes
Sequence of nodes in the tour.
Definition: datagroups.hpp:167
Delaunay triangulation.