-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIOModelSquareErr.c
197 lines (163 loc) · 5.42 KB
/
IOModelSquareErr.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// Set libraries to include
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <omp.h>
// Read a file of doubles of size MAT_SIZE to an array of size MAT_SIZE
void read_double_file(FILE *infile, int MAT_SIZE, double *MAT) {
for(int i = 0; i < MAT_SIZE; i++) {
fscanf(infile, "%lf", &MAT[i]);
};
};
// Read a file of integers of size MAT_SIZE to an array of size MAT_SIZE
void read_int_file(FILE *infile, int MAT_SIZE, int *MAT) {
for(int i = 0; i < MAT_SIZE; i++) {
fscanf(infile, "%d", &MAT[i]);
};
};
// Read a specified file into a double matrix of size MAT_SIZE
void read_custmat_double(int MAT_SIZE, double *doubmat, char *arr) {
FILE *infile;
if(!(infile = fopen(arr, "r"))) {
printf("Error opening input file\n");
exit(EXIT_FAILURE);
}
read_double_file(infile, MAT_SIZE, doubmat);
fclose(infile);
}
// Read a specified file into a integer matrix of size MAT_SIZE
void read_custmat_int(int MAT_SIZE, int *intmat, char *arr) {
FILE *infile;
if(!(infile = fopen(arr, "r"))) {
printf("Error opening input file\n");
exit(EXIT_FAILURE);
}
read_int_file(infile, MAT_SIZE, intmat);
fclose(infile);
}
int main(int argc, char** argv) {
// Check enough arguments have been provided
if(argc != 6) {
printf("Too many or too few arguments, 6 required %d provided \n", argc);
exit(1);
}
// Initialise the variables
// The values determining the iterations over alpha
double starta, enda, stepa;
// The number of locations
int n;
// Read in the inputs
sscanf(argv[1], "%lf", &starta);
sscanf(argv[2], "%lf", &enda);
sscanf(argv[3], "%lf", &stepa);
sscanf(argv[4], "%d", &n);
// Open the file to write the data to
FILE *f;
f = fopen(argv[5], "w");
if (f == NULL) {
printf("Error opening output file \n");
exit(1);
}
// Initialise the arrays of the input data and misc variables
double *distmat, *popmatpow, *travmat, *O, *Sij;
double *popmat;
double error;
double alpha;
// Allocate the memory for each array
distmat = (double *) malloc(n * n * sizeof(double));
popmat = (double *) malloc(n * sizeof(double));
popmatpow = (double *) malloc(n * sizeof(double));
O = (double *) malloc(n * sizeof(double));
travmat = (double *) malloc(n * n * sizeof(double));
Sij = (double *) malloc(n * n * sizeof(double));
// Read in the data for the distances, the flows and the population sizes
read_custmat_double(n * n, distmat, "distmat.dat");
read_custmat_double(n * n, travmat, "travmat.dat");
read_custmat_double(n, popmat, "popsize.dat");
// Evaluate O_i
#pragma omp parallel for
for(int i = 0; i < n; i ++) {
O[i] = 0;
for(int j = 0; j < n; j++) {
if(i!=j) {
O[i] += travmat[i * n + j];
}
}
}
// Set alpha to the starting values
alpha = starta;
// Variable to check if on the final iteration
int finala = 0;
//double itime0 = omp_get_wtime();
// Evaluate Sij
#pragma omp parallel for
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
int iter = i * n + j;
double distij = distmat[iter];
double Sijval = 0;
if( i != j ) {
for(int k = 0; k < n; k++) {
if(distij >= distmat[i * n + k]) {
Sijval += popmat[k];
}
}
}
Sij[iter] = Sijval;
}
}
// Initialise variable to store total population size
double totalpop = 0;
// Calculate the total population size
#pragma omp parallel for reduction(+: totalpop)
for(int i = 0; i < n; i++) {
totalpop += popmat[i];
}
// Loop over the values of alpha
while(alpha <= enda) {
//double time0 = omp_get_wtime();
// Reset the error
error = 0;
// Precalcilate the denominator (and invert)
double denom = 1 / (1 - exp(-1.0 * alpha * totalpop));
#pragma omp parallel for reduction(+: error)
for(int i = 0; i < n; i++) {
// Find the inverse of O_i
double inv = 1 / O[i];
double errorval = 0;
for(int j = 0; j < n; j++) {
if(j != i) {
int iter = i * n + j;
// Calculate the residual at this step
double val = (travmat[iter] * inv) - ((exp(-1 * alpha * (Sij[iter] - popmat[j])) - exp(-1 * alpha * Sij[iter])) * denom);
// Add the square of the residual to the error
errorval += val * val;
}
}
// Add the error from the i terms to the total error
error += errorval;
}
// Save the value of alpha and the current error
fprintf(f, "%g %g \n", alpha, error);
// Check if the next loop is the last iteration
if(alpha + stepa < enda) {
alpha += stepa;
} else if(!finala) {
alpha = enda;
finala = 1;
} else{
break;
}
}
// printf("Total time taken was %g\n", omp_get_wtime() - itime0);
// Free the memory for the arrays
free(distmat);
free(popmat);
free(travmat);
free(popmatpow);
free(O);
free(Sij);
// Close the files
fclose(f);
return 0;
}