-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbinaryCSA.c
194 lines (150 loc) · 4.36 KB
/
binaryCSA.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
/*
Coded by: Emrullah Sonuç
http://3mrullah.com
E-mail: [email protected]
Paper: Sonuç, E.
Binary crow search algorithm for the uncapacitated facility location problem.
Neural Computing & Applications (2021).
DOI: https://doi.org/10.1007/s00521-021-06107-2
*/
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
#include<string.h>
#include<stdbool.h>
#include<float.h>
#define N 400 // Number of crows (Population Size)
#define AP 0.1 // Awareness probability
#define MAX_ITER 80000/N
double UFLP(int loc, int cus, double customer[][loc], double location[loc], bool *per)
{
double cost = 0;
double min_cost = 0;
int i,j;
for(i=0; i<loc; i++)
if (per[i] == 1)
cost = cost + location[i];
for(i=0; i<cus; i++)
{
min_cost = DBL_MAX;
for(j=0; j<loc; j++)
{
if (per[j] == 1 && customer[i][j]<min_cost)
min_cost = customer[i][j];
}
cost = cost + min_cost;
}
return cost;
}
static inline double closed_interval_rand(double x0, double x1)
{
return x0 + (x1 - x0) * rand() / ((double) RAND_MAX);
}
int main(int argc, char**argv)
{
clock_t begin, end;
double time_spent;
begin = clock();
srand(time(NULL));
int cus, loc;
int i,j,iter,control;
char infile[80],indirectory[]="ORLIB-uncap\\\\",outfile[50]="results\\\\";
if (argc != 2){
printf("Usage:\n\t%s input file\n", argv[0]);
exit(1);
}
strcpy(infile, argv[1]);
strcat(indirectory,infile);
FILE *fp = fopen(indirectory, "r");
fscanf(fp, "%d", &loc);
fscanf(fp, "%d", &cus);
double location[loc];
double customer[cus][loc];
double temp;
int tt;
for(i=0;i<loc;i++)
{
fscanf(fp, "%s", &temp);
fscanf(fp, "%lf", &location[i]);
}
for(i=0;i<cus;i++)
{
fscanf(fp, "%d",&tt);
for(j=0;j<loc;j++)
{
fscanf(fp, "%lf", &customer[i][j]);
}
}
double x[loc];
int follow[N];
double obj_crows[N] = {0};
double obj_memory[N] = {0};
bool x_crows[N][loc];
bool x_memory[N][loc];
memset( x_crows, 0, N*loc*sizeof(bool) );
memset( x_memory, 0, N*loc*sizeof(bool) );
double global_best = DBL_MAX;
closed_interval_rand(0,1);
// Memory initialization
for(i=0; i<N; i++)
{
for(j=0; j<loc; j++)
{
if ( closed_interval_rand(0,1) < 0.5 )
{
x_crows[i][j] = 0;
x_memory[i][j] = 0;
}
else
{
x_crows[i][j] = 1;
x_memory[i][j] = 1;
}
}
}
for(i=0; i<N; i++)
{
obj_memory[i] = UFLP(loc, cus, customer, location, x_crows[i]);
}
// Iterations start
for(iter=0; iter<MAX_ITER; iter++)
{
for(i=0; i<N; i++)
{
obj_crows[i] = UFLP(loc, cus, customer, location, x_crows[i]);
if(obj_crows[i] < obj_memory[i])
{
obj_memory[i] = obj_crows[i];
for(j=0; j<loc; j++)
x_memory[i][j] = x_crows[i][j];
}
if (obj_memory[i] < global_best)
global_best = obj_memory[i];
}
for(i=0; i<N; i++)
follow[i]=ceil(N*closed_interval_rand(0,1))-1;
for(i=0; i<N; i++)
{
if( closed_interval_rand(0,1) > AP)
for(j=0; j<loc; j++)
{
x_crows[i][j] = ( x_memory[i][j] ^ ( ( rand() & 1) & ( (x_memory[follow[i]][j] ^ x_memory[i][j]) ) ) );
}
else
for(j=0; j<loc; j++)
{
if ( closed_interval_rand(0,1) < 0.5 )
x_crows[i][j] = 0;
else
x_crows[i][j] = 1;
}
}
//printf("%.3f\n",global_best);
}
end = clock();
time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("\n\n Best: \t %.3f \n Time: \t %.3f sec.\n", global_best, time_spent);
return 0;
}