tsp.h
Go to the documentation of this file.
1 //==================================================================
2 // File : tsp.h
3 // Author : rsagalyn
4 // Date : Aug 18, 2013
5 // Description :
6 //==================================================================
7 
8 #pragma once
9 
10 #ifndef MWM_H_
11 #define MWM_H_
12 
13 #include <algorithm>
14 #include <cmath>
15 #include <cstdlib>
16 #include <cstring>
17 #include <ctime>
18 #include <fstream>
19 #include <iomanip>
20 #include <iostream>
21 #include <limits>
22 #include <pthread.h>
23 #include <queue>
24 #include <stack>
25 #include <string>
26 #include <stdio.h>
27 #include <vector>
28 
29 #include "twoOpt.h"
30 
31 // Eigen
32 #include <Eigen/Dense>
34 
35 //#include "../Global.h"
36 //#include "../DistanceMetric.h"
37 
38 using namespace std;
39 
40 // Toggle printing debugging info to console
41 #define DEBUG 0
42 
43 // Number of threads to use to fill N x N cost matrix
44 #define THREADS 1
45 
46 // Calcualte lowest index controlled by thread id
47 #define START_AT(id,p,n) ((id)*(n)/(p))
48 
49 // Calculate highest index controlled by thread id
50 #define END_AT(id,p,n) (START_AT((id)+1,p,n)-1)
51 
52 
53 
54 class TSP
55 {
56 private:
57 
58  // x and y coords of a node
59  struct City
60  {
61  double x;
62  double y;
63  };
64 
65  // Filename supplied on command line to read from
66  string inFname;
67 
68  // Program-generated filename to output to
69  string outFname;
70 
71  // List of odd nodes
72  vector<int>odds;
73 
74  // Smaller cost matrix used to store distances between odd nodes
75  // Used to find minimum matching on odd nodes
76  double **cost; // dsy: maybe useless
77 
78  // Initialization function
79  void getNodeCount();
80 
81  // Find odd vertices in graph
82  void findOdds();
83 
84  // Prim helper function
85  int minKey(double key[], bool mstSet[]);
86 
87 
88 protected:
89 
90 
91 public:
92  // Number of nodes
93  int n;
94 
95  // euler circuit
96  vector<int>circuit;
97 
98  // Store cities and coords read in from file
99  vector<City>cities;
100 
101  // Full n x n cost matrix of distances between each city
102  double **graph;
103 
104  // Current shortest path length
105  double pathLength;
106 
107 
108  // Adjacency list
109  // Array of n dynamic arrays, each holding a list of nodes it's index is attached to
110  vector<int> *adjlist;
111 
112 
113  int start_idx[THREADS];
114 
115  int end_idx[THREADS];
116 
117  // n x 2 array to store length of TSP path starting at each node
118  // col 0 => starting index col 1 => path length from that node
119  double **path_vals;
120 
121  // Constructor
122  TSP(string in, string out);
123  //TSP(vector<Point_2> nodes); // dsy
124  //TSP(vector<Point_2> nodes, vector<vector<double>> weights); // dsy
125  TSP(vector<Eigen::Vector2d> nodes, vector<vector<double>> weights); // dsy
126 
127  // Destructor
128  ~TSP();
129 
130  // Calculate distance
131  double get_distance(struct City c1, struct City c2);
132 
133 
134  // Initialization functions
135  void readCities();
136  void fillMatrix_threads();
137 
138  // Find MST using Prim's algorithm
139  void findMST_old();
140 
141 
142  // Find perfect matching
143  void perfect_matching();
144 
145  // Find best node to start euler at
146  // Doesn't create tour, just checks
147  int find_best_path(int);
148 
149  // Create tour starting at specified node
150  void create_tour(int);
151 
152  // Private functions implemented by create_tour() and find_best_path()
153  void euler (int pos, vector<int> &);
154  //void euler(int);
155  void make_hamilton(vector<int> &, double&);
156 
157  // Calls twoOpt function
158  void make_shorter();
159 
160 
161  // Debugging functions
162  void printCities();
163  void printAdjList();
164  void printResult();
165  void printEuler();
166  void printPath();
167 
168  // Get node count
169  int get_size() {return n;};
170 };
171 
172 #endif /* MWM_H_ */
double ** cost
Definition: tsp.h:76
#define THREADS
Definition: tsp.h:44
int n
Definition: tsp.h:93
vector< int > * adjlist
Definition: tsp.h:110
int get_size()
Definition: tsp.h:169
double pathLength
Definition: tsp.h:105
double x
Definition: tsp.h:61
string outFname
Definition: tsp.h:69
vector< int > odds
Definition: tsp.h:72
double ** path_vals
Definition: tsp.h:119
string inFname
Definition: tsp.h:66
Definition: tsp.h:54
double ** graph
Definition: tsp.h:102
double y
Definition: tsp.h:62
vector< int > circuit
Definition: tsp.h:96
vector< City > cities
Definition: tsp.h:99
Definition: tsp.h:59


co_scan
Author(s):
autogenerated on Mon Feb 28 2022 23:00:56