-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathscoring_iterators.h
More file actions
126 lines (115 loc) · 3.88 KB
/
scoring_iterators.h
File metadata and controls
126 lines (115 loc) · 3.88 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
/*
* moses/moses/example-progs/scoring_iterators.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 _MOSES_SCORING_ITERATORS_H
#define _MOSES_SCORING_ITERATORS_H
#include <opencog/util/dorepeat.h>
#include <opencog/util/numeric.h> // needed for pow2
#include <opencog/asmoses/combo/combo/vertex.h> // needed for contin_t
namespace opencog { namespace moses {
using namespace opencog::combo;
// Base functor for functors taking an iterator range of value type and result T
template<typename T>
struct iterator_function {
typedef T argument_type;
typedef T result_type;
};
// even_parity(x1, ..., xn) = true iff (int)x1 + ... + (int)xn is even
// where n is the arity of even_parity
struct even_parity : public iterator_function<bool>
{
// [from, to( corresponds to the sequence of inputs of the
// function, the result corresponds to its output
template<typename It>
bool operator()(It from,It to) const {
bool parity = true;
while (from != to)
parity ^= *from++;
return parity;
}
};
// disjunction(x1, ..., xn) = true iff there exists i such that xi is true
struct disjunction : public iterator_function<bool>
{
// [from, to( corresponds to the sequence of inputs of the
// function, the result corresponds to its output
template<typename It>
bool operator()(It from,It to) const {
while (from != to)
if (*from++)
return true;
return false;
}
};
// multiplex(a1, ..., an, d1, ..., dm) = 1 iff m = 2^n and di = 1 if i
// is the address of the string bit described by a1, ..., an.
struct multiplex : public iterator_function<bool>
{
multiplex(unsigned int n) : arity(n) { }
unsigned int arity;
// [from, to( corresponds to the sequence of inputs of the
// function, the result corresponds to its output
template<typename It>
bool operator()(It from, It to) const {
// calculate address
unsigned int addr = 0;
for(unsigned int i = 0; i < arity; ++i)
if(*from++)
addr += pow2(i);
// return the input corresonding to that address
return *(from+addr);
}
};
// majority(x1, ..., xn) = 0 iff n/2 or more arguments are false
struct majority : public iterator_function<bool>
{
majority(unsigned int n) : arity(n) { }
unsigned int arity;
// [from, to( corresponds to the sequence of inputs of the
// function, the result corresponds to its output
template<typename It>
bool operator()(It from, It to) const {
return (unsigned int)std::count(from, to, true) > arity / 2;
}
};
// simple function : f(x)_o = sum_{i={1,o}} x^i
// that is for instance:
// f(x)_3 = x+x^2+x^3
// f(x)_2 = x+x^2
// f(x)_1 = x
// f(x)_0 = 0
struct simple_symbolic_regression : public iterator_function<contin_t>
{
simple_symbolic_regression(int o = 4) : order(o) { }
int order;
template<typename It>
contin_t operator()(It from, It to) const {
contin_t res = 0;
dorepeat(order)
res = (res + 1) * get_contin(*from);
return res;
}
};
} // ~namespace moses
} // ~namespace opencog
#endif