-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.c
61 lines (44 loc) · 1.11 KB
/
util.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
//
// Created by alex on 2/6/18.
//
#include "util.h"
#include "mdvrp.h"
#include <gsl/gsl_rng.h>
#include <time.h>
#include <limits.h>
#include <math.h>
gsl_rng* r;
void initRand(){
time_t t;
time(&t);
r = gsl_rng_alloc(gsl_rng_default);
gsl_rng_set(r, (unsigned long int)t);
}
int randComp(const void* i1, const void* i2){
if (getRandInt() < getRandInt()) return 1;
else return -1;
}
void randIntArr(int low, int high, int size, int* buffer){
for (int i=0;i<size;i++){
buffer[i] = getRandInt() % (high-low) + high;
}
}
int getRandInt(){
return gsl_rng_get(r)/2;
}
double normalRand(){
return gsl_rng_uniform(r);
}
int closestDepot(MDVRP* mdvrp, int customer){
double closestDist = INT_MAX;
int closestIndex;
for (int i=0;i<mdvrp->nDepots;i++){
double dist = sqrt(pow(mdvrp->depots[i].x-mdvrp->customers[customer].x, 2) +
pow(mdvrp->depots[i].y-mdvrp->customers[customer].y,2));
if (dist < closestDist){
closestDist = dist;
closestIndex = i;
}
}
return closestIndex;
}