-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathscoring_functions.h
More file actions
190 lines (175 loc) · 6.39 KB
/
scoring_functions.h
File metadata and controls
190 lines (175 loc) · 6.39 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
/*
* moses/moses/example-progs/scoring_functions.h
*
* Copyright (C) 2002-2008 Novamente LLC
* All Rights Reserved
*
* Written by Moshe Looks
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License v3 as
* published by the Free Software Foundation and including the exceptions
* at http://opencog.org/wiki/Licenses
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program; if not, write to:
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef _EXAMPLE_SCORING_FUNCTIONS_H
#define _EXAMPLE_SCORING_FUNCTIONS_H
#include <bitset>
#include <cmath>
#include <boost/lexical_cast.hpp>
#include <opencog/util/exceptions.h>
#include <opencog/util/numeric.h>
#include <opencog/util/RandGen.h>
#include <opencog/util/oc_assert.h>
#include <opencog/asmoses/moses/representation/field_set.h>
using namespace opencog;
using namespace moses;
// Example scoring functions.
//
// These scoring functions all implement "toy problems" that typical
// optimization algorithms are expected to do well in solving. Thus,
// for example, "one_max" just counts the number of bits set in a
// bit-string.
//
// These functions take an instance as an argument, and score that
// instance for fitness. Recall that an "instance" is a string of bits
// that encode a set of knob settings; an instance may encode discrete,
// continuous, or string variables.
//
// Recall that the C++ std::unary_fuinction<> template is just a trick
// to make a C++ class behave as if it were a function, so that it can
// be used anywhere a function is used.
unsigned int count_bitz(packed_t pack)
{
std::bitset<sizeof(packed_t)> bits(pack);
return bits.count();
}
// Return, as the score, the total number of bits set in the instance.
struct one_max
{
int operator()(const instance& inst) const
{
// This operates directly on the packed_t of the instance.
//
// boost::make_transform_iterator is a kind of pullback or
// pushforward kind of thing, it creates a new iterator, such
// that the new iterator applies the function count_bits() when
// the iterator is dereferences. Not sure, but I think
// make_transform_iterator is kind-of-like a "monad functor".
return accumulate
(make_transform_iterator(inst.begin(),
count_bitz),
make_transform_iterator(inst.end(),
count_bitz), 0);
}
};
// Return, as the score, the sum total settings of all discrete knob
// settings in the instance.
struct n_max
{
n_max(const field_set& fs) : fields(fs) {}
int operator()(const instance& inst) const
{
return accumulate(fields.begin_disc(inst), fields.end_disc(inst), 0);
}
const field_set& fields;
};
// Return, as the score, the sum total of all continuous knob settings
// in the instance.
struct contin_max
{
contin_max(const field_set& fs) : fields(fs) {}
contin_t operator()(const instance& inst) const
{
return accumulate(fields.begin_contin(inst), fields.end_contin(inst),
contin_t(0));
}
const field_set& fields;
};
// Return, as the score, the absolute difference (the "lp_1" distance)
// between the instance, and a vector of fixed, randomly generated values.
//
// That is, a vector of continuous values, bounded between a min and max,
// are randomly generated. The values of the continuous variables in the
// instance are compared to these random values, taking the absolute value,
// and summed over, thus returning the "lp_1" distance between the instance,
// and the random vector.
//
struct contin_uniform
{
contin_uniform(const field_set& fs, contin_t minval, contin_t maxval)
: fields(fs), target(fs.n_contin_fields())
{
generate(target.begin(), target.end(),
bind(std::plus<contin_t>(),
bind(std::multiplies<contin_t>(),
bind(&RandGen::randdouble, boost::ref(randGen())),
maxval - minval), minval));
}
contin_t operator()(const instance& inst) const
{
contin_t res = 0;
field_set::const_contin_iterator it1 = fields.begin_contin(inst);
for (vector<contin_t>::const_iterator it2 = target.begin();
it2 != target.end();++it1, ++it2)
res -= fabs((*it1) - (*it2));
return res;
}
const field_set& fields;
vector<contin_t> target;
};
// Return, as the score, minus the sum of the squares of all
// continuous knob settings in the instance.
//
struct sphere
{
sphere(const field_set& fs) : fields(fs) {}
contin_t operator()(const instance& inst) const {
contin_t res = 0;
for (field_set::const_contin_iterator it = fields.begin_contin(inst);
it != fields.end_contin(inst);++it) {
contin_t v = *it;
res -= (v * v);
}
return res;
}
const field_set& fields;
};
// Return, as the score, the sum of pairs of numbers organized into
// terms.
//
// Recall that an "term variable" is a knob whose values are trees
// of arbitrary strings. In this case, this function insists that all
// these strings are exactly two characters long, and that each
// character is an ASCII digit. This scoring function then goes over
// all term knobs in the instance, pulls out these two digits, and
// adds them together. The sum of all of these is the returned score.
struct termmax
{
termmax(const field_set& fs) : fields(fs) {}
contin_t operator()(const instance& inst) const
{
contin_t res = 0;
for (field_set::const_term_iterator it = fields.begin_term(inst);
it != fields.end_term(inst);++it) {
term_t s = *it;
OC_ASSERT(s.length() == 2,
"term_t length should be equals to two");
int a = boost::lexical_cast<int>(s[0]);
int b = boost::lexical_cast<int>(s[1]);
res += a + b;
}
return res;
}
const field_set& fields;
};
#endif