-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTsetlinMachine.h
More file actions
256 lines (220 loc) · 5.88 KB
/
TsetlinMachine.h
File metadata and controls
256 lines (220 loc) · 5.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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#ifndef _TSETLIN_MACHINE_H_
#define _TSETLIN_MACHINE_H_
#include <stdlib.h>
#include "TsetlinOptions.h"
#define LITERALS (FEATURES*2)
#define PROMOTE (+1)
#define DEMOTE (-1)
struct Clause {
// TA states for positive (even) and negative (odd) polarity literals
int ta[LITERALS];
// clause output (cached value)
int output;
};
struct TsetlinMachine {
Clause clauses[CLAUSES];
#if LOG_ENABLED
int flips;
int countType1;
int countType2;
int voteSum;
bool prevInc[CLAUSES][LITERALS];
#endif
};
/**
* Randomly returns true with the given probability
*/
#define WITH_PROBABILITY(prob) (rand() < (int)((prob)*RAND_MAX))
/**
* Returns voting sign for j-th clause: odd clauses vote -1, even clauses vote +1
*/
#define VOTE(j) (((j)&1) ? -1 : 1)
/**
* Returns polarity of k-th literal: odd literals are negated (1), even literals are positive (0)
*/
#define POLARITY(k) ((k)&1)
/**
* Calculate the value of k-th literal on the given input respecting literal polarity
*/
#define LITERAL_VALUE(input, k) ((input)[(k)/2] ^ POLARITY(k))
/**
* Determine include (1) or exclude (0) decision based on a TA state
*/
#define INCLUDE_LITERAL(state) ((state) > 0)
int calculateOutput(Clause* clause, int input[], int eval) {
clause->output = 1;
int inc = 0;
// calculate conjunction over k literals
// (we can stop early if output becomes false)
for(int k=0; clause->output && k<LITERALS; k++) {
if(INCLUDE_LITERAL(clause->ta[k])) {
clause->output &= LITERAL_VALUE(input, k);
inc = 1;
}
}
if(eval && !inc)
return clause->output = 0;
return clause->output;
}
inline void updateTA(int* ta, int action) {
int nextState = (*ta)+action;
// update, if next state is within allowed states
if(nextState>-NUM_STATES && nextState<=NUM_STATES)
(*ta) = nextState;
}
/*
void reward(Clause* clause, int k) {
if(INCLUDE_LITERAL(clause->ta[k]))
updateTA(&clause->ta[k], +1 );
else
updateTA(&clause->ta[k], -1 );
}
void penalty(Clause* clause, int k) {
if(INCLUDE_LITERAL(clause->ta[k]))
updateTA(&clause->ta[k], -1 );
else
updateTA(&clause->ta[k], +1 );
}
*/
void initialize(TsetlinMachine* tm) {
for(int j=0; j<CLAUSES; j++) {
// set initial TA states to borderline exclude
for(int k=0; k<LITERALS; k+=2) {
if(WITH_PROBABILITY(0.5)) {
tm->clauses[j].ta[k] = 1;
tm->clauses[j].ta[k+1] = 0;
}
else {
tm->clauses[j].ta[k] = 0;
tm->clauses[j].ta[k+1] = 1;
}
// tm->clauses[j].ta[k] = 0;
// tm->clauses[j].ta[k+1] = 0;
}
}
#if LOG_STATUS
tm->flips = 0;
tm->countType1 = 0;
tm->countType2 = 0;
tm->voteSum = 0;
#endif
}
TsetlinMachine* createTsetlinMachine() {
struct TsetlinMachine *tm = (TsetlinMachine *)malloc(sizeof(struct TsetlinMachine));
initialize(tm);
return tm;
}
/**
* Update clauses for the given input vector
* @param input
*/
void calculateClauseOutputs(TsetlinMachine* tm, int input[], int eval) {
for(int j=0; j<CLAUSES; j++) {
calculateOutput(&tm->clauses[j], input, eval);
}
}
/**
* Calculate class voting based on the clause outputs.
* Must be called after calculateClauseOuputs.
*/
int calculateVoting(TsetlinMachine* tm) {
int sum = 0;
for(int j=0; j<CLAUSES; j++) {
// if output is true, the clause is active
if(tm->clauses[j].output) {
// add vote
sum += VOTE(j);
}
}
return sum;
}
void typeIFeedbackLiteral(int k, Clause* clause, int literalValue) {
if(clause->output && literalValue) { // clause is 1 and literal is 1
if(WITH_PROBABILITY(1.0-1.0/L_RATE))
updateTA(&clause->ta[k], PROMOTE);
}
else { // clause is 0 or literal is 0
if(WITH_PROBABILITY(1.0/L_RATE))
updateTA(&clause->ta[k], DEMOTE);
}
}
void typeIFeedback(Clause* clause, int input[]) {
for(int k=0; k<LITERALS; k++) {
typeIFeedbackLiteral(k, clause, LITERAL_VALUE(input, k));
}
}
void typeIIFeedbackLiteral(int k, Clause* clause, int literalValue) {
if(!literalValue && !INCLUDE_LITERAL(clause->ta[k])) // if literal is 0 and excluded
updateTA(&clause->ta[k], PROMOTE);
}
void typeIIFeedback(Clause* clause, int input[]) {
// only if clause is 1
if(clause->output) {
for(int k=0; k<LITERALS; k++) {
typeIIFeedbackLiteral(k, clause, LITERAL_VALUE(input, k));
}
}
}
void update(TsetlinMachine* tm, int input[], int output) {
#if LOG_ENABLED
for(int j=0; j<CLAUSES; j++)
for(int k=0; k<LITERALS; k++) {
tm->prevInc[j][k] = INCLUDE_LITERAL(tm->clauses[j].ta[k]);
}
#endif
calculateClauseOutputs(tm, input, 0);
int classSum = calculateVoting(tm);
#if LOG_ENABLED
tm->voteSum += abs(classSum);
#endif
// calculate feedback probability
double feedbackProbability;
feedbackProbability = (L_THRESHOLD - (double)classSum) / (2.0 * L_THRESHOLD);
if(!output)
feedbackProbability = 1.0 - feedbackProbability;
for(int j=0; j<CLAUSES; j++) {
// inverse the decision for negatively-voting clauses
int y;
if(VOTE(j)>0)
y = output;
else
y = !output;
if(y) {
if(WITH_PROBABILITY(feedbackProbability)) {
#if LOG_ENABLED
tm->countType1++;
#endif
typeIFeedback(&tm->clauses[j], input);
}
}
else {
if(WITH_PROBABILITY(feedbackProbability)) {
#if LOG_ENABLED
tm->countType2++;
#endif
typeIIFeedback(&tm->clauses[j], input);
}
}
}
#if LOG_ENABLED
for(int j=0; j<CLAUSES; j++)
for(int k=0; k<LITERALS; k++) {
if(tm->prevInc[j][k] != INCLUDE_LITERAL(tm->clauses[j].ta[k]))
tm->flips++;
}
#endif
}
int score(TsetlinMachine* tm, int input[]) {
calculateClauseOutputs(tm, input, 1);
return calculateVoting(tm);
}
int countIncluded(TsetlinMachine* tm) {
int count = 0;
for(int j=0; j<CLAUSES; j++)
for(int k=0; k<LITERALS; k++) {
if(INCLUDE_LITERAL(tm->clauses[j].ta[k]))
count++;
}
return count;
}
#endif