-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfitnessbdd.cxx
More file actions
206 lines (171 loc) · 6.13 KB
/
fitnessbdd.cxx
File metadata and controls
206 lines (171 loc) · 6.13 KB
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
198
199
200
201
202
203
204
205
206
/*
* File: fitnessbdd.cpp
* Author: mrazek
*
* Fitness evaluation of the chromosome using BDD
* BDD is used to calculate the error distribution
* which is transformed to the fitness value by the
* formula sum(j**2 * (H(j)))
*
* The library uses BuDDy BDD library
*/
#include "fitnessbdd.h"
#include "chromosomear.h"
#include <bdd.h>
static void bdd_gbchandler(int pre, bddGbcStat *s);
static long long cn(int n, int r);
template <class CH>
FitnessBdd<CH>::FitnessBdd(CH & init)
{
bdd_init(1000000, 1000000);
bdd_setvarnum(init.inputs);
bdd_gbc_hook(bdd_gbchandler);
this->ref = new Reference(init.inputs);
cout << " + Fitness unit: BDD" << endl;
/* prefil the number of coefitiones */
cn_cache = new long long[init.inputs + 1];
for (int i = 0; i <= init.inputs; i++) {
cn_cache[i] = cn(init.inputs, i);
}
}
template <class CH>
FitnessBdd<CH>::~FitnessBdd()
{
delete this->ref;
bdd_done();
}
template <class CH>
T_FIT FitnessBdd<CH>::GetFitness(CH & chrom)
{
bdd median_test = chrom.GenerateBdd();
#if 0
T_FIT fit = 0;
// the histogram is overestimated
// the histogram is not used in the fitness function
fit = 0 * 0 * bdd_satcount(!(median_test ^ ref->GetValidMedian()));
for (int i = 1; i <= chrom.inputs / 2; i++) {
int id = (chrom.inputs/2 - i + 1);
fit += id * id * bdd_satcount(median_test & ref->GetOnesCount(i));
}
for (int i = chrom.inputs / 2 + 1; i < chrom.inputs; i++) {
int id = (i - chrom.inputs/2);
fit += id * id * bdd_satcount((!median_test) & ref->GetOnesCount(i));
}
return fit;
#else
double fit = 0;
int med = (chrom.inputs-1)/2;
double histn[med+2];
double histp[med+2];
histp[med+1] = histn[med+1] = 0;
for (int k = 0; k <= med; k++) {
histn[k] = bdd_satcount(!median_test & ref->GetOnesCount(med+k));
histp[k] = bdd_satcount(median_test & ref->GetOnesCount(med-k+1));
}
// cn_cache represent precalculated combination coefficients for a given number of inputs
// cn_cache[i] = cn(chrom.inputs, i)
// size is chrom.inputs + 1
for (int k = med; k > 0; k--) {
double h = (histn[k]/cn_cache[med+1-k] - histn[k+1]/cn_cache[med+1-k-1]);
//cout << " -" << k << ":" << histn[k] << "=> " << h << "%" << endl;
fit += k*k*h;
}
{
int k = 0;
double h = (histn[k]/cn_cache[med+1-k] - histn[k+1]/cn_cache[med+1-k-1]);
//cout << " " << k << ":" << histn[k] << "=> " << h << "%" << endl;
fit += k*k*h;
}
for (int k = 1; k <= med; k++) {
double h = (histp[k]/cn_cache[med+k] - histp[k+1]/cn_cache[med+k+1]);
//cout << " +" << k << ":" << histp[k] << "=> " << h << "%" << endl;
fit += k*k*h;
}
return -(T_FIT)(1000*fit);
#endif
}
long long factorial(int num) {
long long fact = 1;
for (int i = 1; i <= num; i++) {
fact *= i;
}
return fact;
}
// Function to calculate combination (nCr)
static long long cn(int n, int r) {
// https://stackoverflow.com/questions/9330915/number-of-combinations-n-choose-r-in-c
// better than factorial(n) / (factorial(r) * factorial(n - r))
//cout << "r" << r << "n" << n << endl;
int k = r;
if (k > n) return 0;
if (k * 2 > n) k = n-k;
if (k == 0) return 1;
int result = n;
for( int i = 2; i <= k; ++i ) {
result *= (n-i+1);
result /= i;
}
return result;
}
template <class CH>
void FitnessBdd<CH>::PrintFitness(CH & chrom, ostream & out)
{
int med = (chrom.inputs-1)/2;
double histn[med+2];
double histp[med+2];
bdd median_test = chrom.GenerateBdd(); // output of the candidate median function
//out << "==============================" << endl;
//out << "Size: " << chrom.GetSize() << endl;
//fit = bdd_satcount(median_test ^ ref->GetValidMedian());
histp[med+1] = histn[med+1] = 0;
for (int k = 0; k <= med; k++) {
histn[k] = bdd_satcount(!median_test & ref->GetOnesCount(med+k));
histp[k] = bdd_satcount(median_test & ref->GetOnesCount(med-k+1));
}
// cn_cache represent precalculated combination coefficients for a given number of inputs
// cn_cache[i] = cn(chrom.inputs, i)
// size is chrom.inputs + 1
for (int k = med; k > 0; k--) {
double h = 100*(histn[k]/cn_cache[med+1-k] - histn[k+1]/cn_cache[med+1-k-1]);
out << " -" << k << ":" << histn[k] << "=> " << h << "%" << endl;
}
{
int k = 0;
double h = 100*(histn[k]/cn_cache[med+1-k] - histn[k+1]/cn_cache[med+1-k-1]);
out << " " << k << ":" << histn[k] << "=> " << h << "%" << endl;
}
for (int k = 1; k <= med; k++) {
double h = 100*(histp[k]/cn_cache[med+k] - histp[k+1]/cn_cache[med+k+1]);
out << " +" << k << ":" << histp[k] << "=> " << h << "%" << endl;
}
}
template <class CH>
void FitnessBdd<CH>::GetMinMaxDiff(CH & chrom, int & minimum, int & maximum)
{
minimum = 0;
maximum = 0;
bdd median_test = chrom.GenerateBdd();
for (int i = 1; i <= chrom.inputs / 2 + 1; i++) {
minimum = (chrom.inputs/2 - i + 1);
if (bdd_satone(median_test & ref->GetOnesCount(i)) != bddfalse)
break;
}
for (int i = chrom.inputs - 1; i >= chrom.inputs / 2 + 0; i--) {
maximum = (i - chrom.inputs/2);
if(bdd_satone((!median_test) & ref->GetOnesCount(i)) != bddfalse)
break;
}
}
static void bdd_gbchandler(int pre, bddGbcStat *s)
{
if (!pre)
{
/*printf("myGarbage collection #%d: %d nodes / %d free",
s->num, s->nodes, s->freenodes);
printf(" / %.1fs / %.1fs total\n",
(float)s->time, //(float)(CLOCKS_PER_SEC),
(float)s->sumtime); //(float)CLOCKS_PER_SEC);
*/
}
}
template class FitnessBdd<ChromosomeAr>;