-
Notifications
You must be signed in to change notification settings - Fork 0
/
tour.c
155 lines (119 loc) · 2.98 KB
/
tour.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
* File: tour.c
* Author: michaelb
* Contents: Contains functions related to the tour struct
* arrays
* Created on 29 July 2013, 8:01 PM
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "city.h"
#include "tour.h"
#include <time.h>
#include <omp.h>
#include "array_helpers.h"
/*
* Function: swap
* --------------------
* utility function that swaps 2 integers
*
* a: first integer
* b: second integer
* value: the element being searched for
*/
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
/*
* Function: find_index
* --------------------
* testing function that prints an array arr of size n
*
* arr: integer array
* n: number of elements in array
*
*/
void printArray(int arr[], int n) {
int i = 0;
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}
/*
* Function: shuffle
* --------------------
* shuffles integer array
*
* array: integer array
* n: size of array
* value: the element being searched for
*/
void shuffle(int *array, size_t n) {
size_t i = 0, j = 0, t = 0;
if (n > 1) {
for (i = 0; i < n - 1; i++) {
j = i + rand() / (RAND_MAX / (n - i) + 1);
t = array[j];
array[j] = array[i];
array[i] = t;
}
}
}
/*
* Function: getDistance
* --------------------
* calculates the total distance of a tour path
*
* cities: array of City structs
* tour: tour to be calculated
* numOfCities: size of cities array
*
* returns: double distance of Tour path
*/
double getDistance(City* cities, Tour* tour, int numOfCities) {
int count = 0, section = 0;
double distance = 0;
City start, end;
for (count = 0; count < numOfCities - 1; count++) {
start = cities[tour->path[count]];
end = cities[tour->path[count + 1]];
section = (end.x - start.x) + (end.y - start.y);
distance += sqrt(abs(section));
}
return distance;
}
/*
* Function: createPath
* --------------------
* dynamically allocates a Tour and creates a shuffled path through cities
*
* tour: Tour struct to be created
* numOfCities: length of tour to be created
*/
void createPath(Tour* tour, int numOfCities) {
int count = 0, i = 0, m = 0, j = 0;
init_array_tour(tour, numOfCities);
for (count = 0; count < numOfCities; count++) {
tour->path[count] = count;
}
m = numOfCities % 5;
for (i = 0; i < m; i++) {
int j = rand() % (i + 1);
swap(&tour->path[i], &tour->path[j]);
}
for (i = m; i < numOfCities; i = i + 5) {
j = rand() % (i + 1);
swap(&tour->path[i], &tour->path[j]);
j = rand() % (i + 2);
swap(&tour->path[i + 1], &tour->path[j]);
j = rand() % (i + 3);
swap(&tour->path[i + 2], &tour->path[j]);
j = rand() % (i + 4);
swap(&tour->path[i + 3], &tour->path[j]);
j = rand() % (i + 5);
swap(&tour->path[i + 4], &tour->path[j]);
}
}