-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathessTools.c
197 lines (163 loc) · 4.9 KB
/
essTools.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
#include "ess.h"
// #include <string.h>
/**
* Copy `src` to `dest` using `memcpy`
*/
void copy_Ind(eSSType *eSSParams, Individual *dest, Individual *src){
memcpy(dest->params, src->params, eSSParams->n_params*sizeof(double));
// memcpy(dest->means, src->means, eSSParams->n_params*sizeof(double));
// memcpy(dest->stds, src->stds, eSSParams->n_params*sizeof(double));
dest->mean_cost = src->mean_cost;
dest->var_cost = src->var_cost;
dest->cost = src->cost;
dest->dist = src->dist;
dest->n_stuck = src->n_stuck;
}
/**
* Return the min and min_index of an array
*/
double min(double* array, int len, int* min_index){
double min = array[0];
for (int i = 1; i < len; ++i) {
if (min > array[i]) {
min = array[i];
*min_index = i;
}
}
return min;
}
/**
* Return the max and max_index of an array
*/
double max(double* array, int len, int* max_index){
double max = array[0];
for (int i = 1; i < len; ++i) {
if (max < array[i]) {
max = array[i];
*max_index = i;
}
}
return max;
}
double euclidean_distance(eSSType *eSSParams, Individual *ind1, Individual *ind2){
double distance = 0;
for (int i = 0; i < eSSParams->n_params; ++i)
{
distance += (ind1->params[i] - ind2->params[i]) * (ind1->params[i] - ind2->params[i]);
}
return sqrt(distance);
}
void delete_and_shift(eSSType *eSSParams, Set *set, int set_size, int index_to_delete){
for (int i = index_to_delete; i < set_size - 1; ++i)
{
copy_Ind(eSSParams, &(set->members[i]), &(set->members[i + 1]));
}
}
/**
* Find the closest member of a set to `ind` and return it's index
* @param eSSParams
* @param set Set to look for a closest member in it
* @param set_size Size of the set, it can be smaller than the actual set size
* @param ind `Individual` that we are interested in its closest member to it
* @param ind_index Index of the `ind` if it is already in the set.
* @return index of the closest member of a set to `ind`
*/
int closest_member(eSSType *eSSParams, Set *set, int set_size, Individual *ind, int ind_index){
double dist;
double min;
int min_index;
if (ind_index == set_size - 1 ){
min = euclidean_distance(eSSParams, ind, &(set->members[set_size - 2]));
min_index = set_size - 2;
}
else if (ind_index == 0){
min = euclidean_distance(eSSParams, ind, &(set->members[1]));
min_index = 1;
}
else{
min = euclidean_distance(eSSParams, ind, &(set->members[ind_index - 1]));
min_index = ind_index - 1;
}
for (int i = 0; i < set_size; ++i)
{
if ( i != ind_index ){
dist = euclidean_distance(eSSParams, ind, &(set->members[i]));
if (dist < min ){
min = dist;
min_index = i;
}
}
}
return min_index;
}
bool is_equal_dist(eSSType *eSSParams, Individual *ind1, Individual *ind2){
// bool isEqual = false;
if ( euclidean_distance(eSSParams, ind1, ind2) < eSSParams->euclidean_dist_tol )
// isEqual |= 1;
return true;
return false;
}
/**
* It returns `true` if the difference between paired parameters of and Individual is less than certain amount
* @param eSSParams
* @param ind1
* @param ind2
* @return `true` or `false`
*/
bool is_equal_pairwise(eSSType *eSSParams, Individual *ind1, Individual *ind2){
for (int i = 0; i < eSSParams->n_params; ++i)
{
if (fabs(ind1->params[i] - ind2->params[i]) < eSSParams->param_diff_tol){
return true;
}
}
return false;
}
/*
Check if the Individual exist in the set
*/
int is_exist(eSSType *eSSParams, Set *set, Individual *ind){
int index = -1;
for (int i = 0; i < set->size; ++i)
{
/**
* Check the value of eSSParams->equality_type, if its 0 then consider the euclidean_distance as a measurement
* otherwise, check the pairwise distances of parameters.
*/
if ( true ==
(eSSParams->equality_type == 0 ?
is_equal_dist(eSSParams, &(set->members[i]), ind) : is_equal_pairwise(eSSParams, &(set->members[i]), ind)))
{
index = i;
break;
}
}
return index;
}
/**
* Check to see if the Individual is located in the flatzone or not! Is there any Individual available in the set with similar cost.
* @param eSSParams
* @param set
* @param ind
* @return `true` if the Individual located in the flatzone
*/
bool is_in_flatzone(eSSType *eSSParams, Set *set, Individual *ind){
// bool isInFlatzone = false;
/**
* The loop doesn't check the last item since it is the best sol and
* every good solution in comparison to that is in flatzone coverd by that!
*/
for (int i = 0; i < set->size; ++i)
{
if (ind->cost < set->members[i].cost + ( set->members[i].cost * eSSParams->flatzone_coef)
&& ind->cost > set->members[i].cost - (set->members[i].cost * eSSParams->flatzone_coef))
{
eSSParams->stats->n_flatzone_detected++;
// isInFlatzone = true;
// break;
return true;
}
}
// return isInFlatzone;
return false;
}