-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitVectorMachineBits.scala
More file actions
918 lines (714 loc) · 22.8 KB
/
BitVectorMachineBits.scala
File metadata and controls
918 lines (714 loc) · 22.8 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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
package tsetlin
import scala.util.Random
import BitVectorMachineBits._
import Tsetlin._
import jdk.incubator.vector.IntVector
import jdk.incubator.vector.VectorOperators
import java.util.concurrent.atomic.AtomicLong
final class BitVectorMachineBits(
private[tsetlin] val positiveClassClauses: Array[Clauses],
private[tsetlin] val negativeClassClauses: Array[Clauses])(
implicit val description: Tsetlin.Description)
extends Tsetlin.MutableMachine {
val workspaces: Array[Workspace] = Array.fill(positiveClassClauses.size)(freshWorkspace)
def freshWorkspace = Workspace.fresh(description)
def predict(x: BitVector): Int = {
val ws = freshWorkspace
(0 until description.nClasses).map { cls =>
classOutput(cls, x, true, ws)
val sum = ws.clauseOutputPositive.popcount - ws.clauseOutputNegative.popcount
cls -> sum
}.maxBy(_._2)._1
}
def clauseOutputOf(
clauses: Clauses,
x: BitVector,
predict: Boolean,
clauseOutput: BitVector): Unit = {
inline def binarizePredict(classOutput: Int): Boolean = {
classOutput == 1
}
inline def binarizeClassify(classOutput: Int): Boolean = {
classOutput != -1
}
val array = clauseOutput.underlying
val teams = clauses.teams
var clauseIndex = 0
val nClauses = description.nClauses
var int = 0
var mask = 1
var outputIndex = 0
val stop = (1 << 31)
while (clauseIndex < nClauses) {
val output = teams(clauseIndex).clauseOutputVec(x)
val boolean = if (predict) binarizePredict(output) else binarizeClassify(output)
if (boolean) {
int |= mask
}
if (mask == stop) {
array(outputIndex) = int
outputIndex += 1
mask = 1
int = 0
} else {
mask = (mask << 1)
}
clauseIndex += 1
}
if (clauseIndex % 32 != 0) {
array(outputIndex) = int
}
}
def classOutput(cls: Int, x: BitVector, predict: Boolean, ws: Workspace): Unit = {
clauseOutputOf(positiveClassClauses(cls), x, predict, ws.clauseOutputPositive)
clauseOutputOf(negativeClassClauses(cls), x, predict, ws.clauseOutputNegative)
}
def generateClauseFeedback(
feedbackProbability: Double,
feedbackingValue: Byte,
random: Random,
feedback: Array[Byte]): Unit = {
var i = 0
val size = feedback.size
while (i < size) {
feedback(i) = if (random.nextDouble <= feedbackProbability) {
feedbackingValue
} else {
0
}
i += 1
}
}
def generateClassFeedback(
isTargetClass: Boolean,
clippedSum: Clipped,
random: Random,
ws: Workspace): Unit = {
val threshold = description.threshold
val feedbackProbability = if (isTargetClass) {
(threshold - clippedSum.value) / (2.0 * threshold)
} else {
(threshold + clippedSum.value) / (2.0 * threshold)
}
def f(v: Byte, fs: Array[Byte]): Unit = generateClauseFeedback(feedbackProbability, v, random, fs)
if (isTargetClass) {
f(1, ws.positiveFeedback)
f(2, ws.negativeFeedback)
} else {
f(2, ws.positiveFeedback)
f(1, ws.negativeFeedback)
}
}
def applyType1FeedbackOnClauseOutputBoostPositiveFeedbackVec(
team: Team,
x: BitVector,
random: Random): Unit = {
var i = 0
val size = team.nInts
val bound = IntVectorSpecies.loopBound(size)
val normal = team.normal
val negated = team.negated
val xs = x.underlying
val len = IntVectorSpecies.length
while (i < bound) {
val whichBits = IntVector.fromArray(IntVectorSpecies, xs, i)
// if (x(k)) actionIncludeReinforceInclude --{
normal.incBitsVec(i, whichBits)
// }---
// if (!x(k)) actionIncludeNegatedReinforceInclude --{
negated.incBitsVec(i, whichBits.not)
// }---
// if x(k) actionIncludeNegatedReinforceExclude --{
negated.decBitsVec(i, whichBits.and(stream1overS.sampleVec(random)))
// }---
// if (!x(k)) actionIncludeReinforceExclude --{
normal.decBitsVec(i, stream1overS.sampleVec(random).and(whichBits.not))
// }---
i += len
}
while (i < size) {
val whichBits = xs(i)
// if (x(k)) actionIncludeReinforceInclude --{
normal.incBitsInt(i, whichBits)
// }---
// if (!x(k)) actionIncludeNegatedReinforceInclude --{
negated.incBitsInt(i, ~whichBits)
// }---
// if x(k) actionIncludeNegatedReinforceExclude --{
negated.decBitsInt(i, stream1overS.sample(random) & whichBits)
// }---
// if (!x(k)) actionIncludeReinforceExclude --{
normal.decBitsInt(i, stream1overS.sample(random) & ~whichBits)
// }---
i += 1
}
}
def applyType1FeedbackOnClauseOutput(
team: Team,
x: BitVector,
random: Random): Unit = {
var i = 0
val normal = team.normal
val negated = team.negated
val xs = x.underlying
val size = team.nInts
while (i < size) {
val whichBits = xs(i)
// if (x(k)) actionIncludeReinforceInclude --{
normal.incBitsInt(i, streamDecSOverS.sample(random) & whichBits)
// }---
// if (!x(k)) actionIncludeNegatedReinforceInclude --{
negated.incBitsInt(i, streamDecSOverS.sample(random) & ~whichBits)
// }---
// if x(k) actionIncludeNegatedReinforceExclude --{
negated.decBitsInt(i, stream1overS.sample(random) & whichBits)
// }---
// if (!x(k)) actionIncludeReinforceExclude --{
normal.decBitsInt(i, stream1overS.sample(random) & ~whichBits)
// }---
i += 1
}
}
class Stream(p: Double) {
private[this] val stream = {
val random = new scala.util.Random(3)
val array = new Array[Int](30 * 1000)
var i = 0
while (i < array.size) {
array(i) = uniformRandomInt(random, p)
i += 1
}
array
}
inline def sampleVec(random: Random): IntVector = {
val offset = random.nextInt(stream.size - IntVectorSpecies.length)
IntVector.fromArray(IntVectorSpecies, stream, offset)
}
inline def sample(random: Random): Int = {
stream(random.nextInt(stream.size))
}
}
val stream1overS = new Stream(1.0 / description.s)
val streamDecSOverS = new Stream((description.s - 1.0) / description.s)
def applyType1FeedbackNoClauseOutputVec(
team: Team,
random: Random): Unit = {
var i = 0
val nInts = team.nInts
val normal = team.normal
val negated = team.negated
val bound = IntVectorSpecies.loopBound(nInts)
val len = IntVectorSpecies.length
while (i < bound) {
normal.decBitsVec(i, stream1overS.sampleVec(random))
negated.decBitsVec(i, stream1overS.sampleVec(random))
i += len
}
while (i < nInts) {
normal.decBitsInt(i, stream1overS.sample(random))
negated.decBitsInt(i, stream1overS.sample(random))
i += 1
}
}
def applyType2FeedbackVec(
team: Team,
x: BitVector): Unit = {
val nInts = team.nInts
var i = 0
val xs = x.underlying
val bound = IntVectorSpecies.loopBound(nInts)
val normal = team.normal
val negated = team.negated
val len = IntVectorSpecies.length
while (i < bound) {
val xvec = IntVector.fromArray(IntVectorSpecies, xs, i)
{
val whichBits = xvec.and(negated.includeVecOf(i).not())
negated.incBitsVec(i, whichBits)
}
{
val whichBits = (xvec.or(normal.includeVecOf(i))).not()
normal.incBitsVec(i, whichBits)
}
i += len
}
while (i < nInts) {
val xInt = xs(i)
{
val whichBits = xInt & (~negated.includeBitsOf(i))
negated.incBitsInt(i, whichBits)
}
{
val whichBits = ~(xInt | normal.includeBitsOf(i))
normal.incBitsInt(i, whichBits)
}
i += 1
}
}
def applyFeedbackToClauses(
clauseVotes: BitVector,
clauses: Clauses,
feedbacks: Array[Byte],
x: BitVector,
random: Random): Unit = {
var i = 0
val size = description.nClauses
val teams = clauses.teams
while (i < size) {
val team = teams(i)
feedbacks(i) match {
case 1 =>
if (clauseVotes(i)) {
//if (description.boostTruePositiveFeedback) {
applyType1FeedbackOnClauseOutputBoostPositiveFeedbackVec(team, x, random)
//} else {
//applyType1FeedbackOnClauseOutput(team, x, random)
//}
} else {
applyType1FeedbackNoClauseOutputVec(team, random)
}
case 2 =>
if (clauseVotes(i)) {
applyType2FeedbackVec(teams(i), x)
}
case _ =>
()
}
i += 1
}
}
def applyFeedback(
cls: Int,
x: BitVector,
random: Random,
ws: Workspace): Unit = {
applyFeedbackToClauses(ws.clauseOutputPositive, positiveClassClauses(cls), ws.positiveFeedback, x, random)
applyFeedbackToClauses(ws.clauseOutputNegative, negativeClassClauses(cls), ws.negativeFeedback, x, random)
}
def traceDump(): String = (
s"pos: sync($tSync1) ws($tWs1) class-output($tClassOutput1) gen-feedback($tGenerateClassFeedback1) apply($tApplyFeedback1)\n" +
s"neg: sync($tSync2) ws($tWs2) class-output($tClassOutput2) gen-feedback($tGenerateClassFeedback2) apply($tApplyFeedback2) rand($tPaired)\n"
)
var tSync1 = 0L
var tWs1 = 0L
var tClassOutput1 = 0L
var tGenerateClassFeedback1 = 0L
var tApplyFeedback1 = 0L
var tPaired = 0L
var tSync2 = 0L
var tWs2 = 0L
var tClassOutput2 = 0L
var tGenerateClassFeedback2 = 0L
var tApplyFeedback2 = 0L
def update(x: BitVector, y: Int, random: Random): Unit = {
val targetClass = y
var t6 = 0L
val t0 = System.currentTimeMillis
positiveClassClauses(targetClass).synchronized {
val t1 = System.currentTimeMillis
val ws = workspaces(targetClass)
val t2 = System.currentTimeMillis
classOutput(targetClass, x, predict = false, ws)
val t3 = System.currentTimeMillis
generateClassFeedback(
isTargetClass = true,
ClassOutput.sumUpClauseVotes(ws.clauseOutputPositive, ws.clauseOutputNegative),
random,
ws)
val t5 = System.currentTimeMillis
applyFeedback(
targetClass,
x,
random,
ws)
t6 = System.currentTimeMillis
tSync1 += t1 - t0
tWs1 += t2 - t1
tClassOutput1 += t3 - t2
tGenerateClassFeedback1 += t5 - t3
tApplyFeedback1 += t6 - t5
}
val pairedClass: Int = {
val nClasses = description.nClasses
/*
* Note: index != y and 0 <= index < nClasses
*
* Proof
* r = random.nextInt(nClasses - 1) ∈ 1, 2 ... nClasses - 2
*
* 1 + r + y (mod nClasses) = y => ∃ k ∈ N |
* 1 + r + y = (k * nClasses) + y =>
* 1 + r = k * nClasses
*
* if k = 1
* 1 + r = nClasses but r < nClasses - 2 => r + 1 != nClasses
* otherwise
* 1 + r << (k * nClasses)
*/
(1 + random.nextInt(nClasses - 1) + y) % nClasses
}
val t7 = System.currentTimeMillis
positiveClassClauses(pairedClass).synchronized {
val t8 = System.currentTimeMillis
val ws = workspaces(pairedClass)
val t9 = System.currentTimeMillis
classOutput(pairedClass, x, predict = false, ws)
val t10 = System.currentTimeMillis
generateClassFeedback(
isTargetClass = false,
ClassOutput.sumUpClauseVotes(ws.clauseOutputPositive, ws.clauseOutputNegative),
random,
ws)
val t11 = System.currentTimeMillis
applyFeedback(
pairedClass,
x,
random,
ws)
val t12 = System.currentTimeMillis
tPaired = t7 - t6
tSync2 += t8 - t7
tWs2 += t9 - t8
tClassOutput2 += t10 - t9
tGenerateClassFeedback2 += t11 - t10
tApplyFeedback2 += t12 - t11
}
}
def stateString = (
"BitVectorMachineBits(\n" +
"\t"+positiveClassClauses.map(_.stateString).mkString("Array(", ", ", ")") + "\n" +
"\t"+negativeClassClauses.map(_.stateString).mkString("Array(", ", ", ")") +
")"
)
override def toString = stateString
}
object BitVectorMachineBits {
def uniformRandomInt(random: Random, p: Double ): Int = {
var i = 32
var value = 0
while (i >= 0) {
if (random.nextDouble() <= p) {
value = value | 1
}
if (i != 0) {
value = value << 1
}
i -= 1
}
value;
}
def flipInt(random: Random, p: Double, value: Int): Int = {
var out = value;
var i = 31
var pattern = (1 << 31)
while (i >= 0) {
if ((out & pattern) != 0 && random.nextDouble() <= p) {
out &= ~pattern
}
pattern = (pattern >>> 1)
i -= 1
}
out
}
final class Clauses(
private[tsetlin] val teams: Array[Team])(implicit description: Description) {
def teamAtIndex(index: Int): Team = teams(index)
def stateString = teams.indices.map { i =>
teamAtIndex(i).stateString
}.mkString("Clauses(", ", ", ")")
override def toString = stateString
}
val IntVectorSpecies = IntVector.SPECIES_PREFERRED
/*
* underlying(0): Ints such that underlying(i / 32) = has the (i % 32) bit set if clause(i) is in the include state
* underlying(1):
* .
* .
* .
* underlying(N):
*
* N = ceil(log_2(nStates + 1))
*/
final class State(
private[tsetlin] val nBits: Int,
private[tsetlin] val nInts: Int,
private val underlying: Array[Int]) {
inline def includeBitsOf(i: Int): Int = underlying(i)
inline def includeVecOf(i: Int): IntVector = IntVector.fromArray(IntVectorSpecies, underlying, i)
def includeBit(k: Int): Boolean = {
(includeBitsOf(k / 32) & (1 << (k % 32))) != 0
}
inline def get(bit: Int, int: Int) = underlying((bit * nInts) + int)
def count(bit: Int): Int = {
var i = nBits - 1
var acc = 0
var pow = 1
val intIndex = bit / 32
@inline def bitInInt = bit % 32
val mask = (1 << bitInInt)
while (i >= 0) {
val int = get(i, intIndex)
if ((int & mask) != 0) {
acc |= pow
}
pow = pow << 1
i -= 1
}
acc
}
inline def offsetForIndex(index: Int): Int = ((nBits - 1) * nInts) + index
def incBitsVec(
index: Int,
firstCarry: IntVector): Unit = {
val under = underlying
var nextCarry = firstCarry
var offset = offsetForIndex(index)
while (offset >= 0) {
if (nextCarry.compare(VectorOperators.EQ, 0).allTrue) {
return
}
val carry = nextCarry
val bits = IntVector.fromArray(IntVectorSpecies, under, offset)
nextCarry = bits.and(carry)
bits.lanewise(VectorOperators.XOR, carry).intoArray(under, offset)
offset -= nInts
}
if (nextCarry.compare(VectorOperators.EQ, 0).allTrue) {
return
}
offset = offsetForIndex(index)
while (offset >= 0) {
IntVector.fromArray(IntVectorSpecies, under, offset).or(nextCarry).intoArray(under, offset)
offset -= nInts
}
}
def decBitsVec(
index: Int,
firstCarry: IntVector): Unit = {
val under = underlying
var nextCarry = firstCarry
var offset = offsetForIndex(index)
while (offset >= 0) {
if (nextCarry.compare(VectorOperators.EQ, 0).allTrue) {
return
}
val carry = nextCarry
val bits = IntVector.fromArray(IntVectorSpecies, under, offset)
nextCarry = (bits.not).and(carry)
bits.lanewise(VectorOperators.XOR, carry).intoArray(under, offset)
offset -= nInts
}
if (nextCarry.compare(VectorOperators.EQ, 0).allTrue) {
return
}
nextCarry = nextCarry.not
offset = offsetForIndex(index)
while (offset >= 0) {
IntVector.fromArray(IntVectorSpecies, under, offset).and(nextCarry).intoArray(under, offset)
offset -= nInts
}
}
def incBitsInt(
index: Int,
firstCarry: Int): Unit = {
var offset = offsetForIndex(index)
var nextCarry = firstCarry
val under = underlying
while (offset >= 0) {
if (nextCarry == 0) {
return
}
val carry = nextCarry
nextCarry = under(offset) & carry
under(offset) ^= carry
offset -= nInts
}
if (nextCarry == 0) return
offset = offsetForIndex(index)
while (offset >= 0) {
under(offset) |= nextCarry
offset -= nInts
}
}
def decBitsInt(
index: Int,
firstCarry: Int): Unit = {
var offset = offsetForIndex(index)
var nextCarry = firstCarry
val under = underlying
while (offset >= 0) {
if (nextCarry == 0) {
return
}
val carry = nextCarry
nextCarry = ~under(offset) & carry
under(offset) ^= carry
offset -= nInts
}
if (nextCarry == 0) return
offset = offsetForIndex(index)
while (offset >= 0) {
under(offset) &= ~nextCarry
offset -= nInts
}
}
}
object State {
def initial(nBits: Int, nDimensions: Int): State = {
var i = 0
val nInts = Bits.closest32(nDimensions) / 32
val underlying = new Array[Int](nBits * nInts)
java.util.Arrays.fill(underlying, nInts, underlying.size, -1)
new State(nBits, nInts, underlying)
}
}
final class Team(
private[tsetlin] val normal: State,
private[tsetlin] val negated: State)(implicit description: Description) {
@inline def nInts = normal.nInts
override def toString = patternString
def patternString = {
val include = (0 until description.nDimensions).map { i =>
if (actionInclude(i)) "+" else " "
}.mkString("[", "", "]")
val includeNegated = (0 until description.nDimensions).map { i =>
if (actionIncludeNegated(i)) "-" else " "
}.mkString("[", "", "]")
s"Team($include, $includeNegated)"
}
def stateString = {
(0 until description.nDimensions).map { i =>
normal.count(i)
}.mkString("[", ", ", "]") ++
(0 until description.nDimensions).map { i =>
negated.count(i)
}.mkString("[", ", ", "]")
}
def actionInclude(k: Int): Boolean = {
normal.includeBit(k)
}
def actionIncludeNegated(k: Int): Boolean = {
negated.includeBit(k)
}
inline def clauseOutputVec(vec: BitVector): Byte = {
var literals = false
var output = true
var i = 0
val xs = vec.underlying
val size = xs.length - 2
val bound = IntVectorSpecies.loopBound(size)
while (output && i < bound) {
val x = IntVector.fromArray(IntVectorSpecies, xs, i)
val include = normal.includeVecOf(i)
val negatedInclude = negated.includeVecOf(i)
literals = literals || (include.compare(VectorOperators.NE, 0).anyTrue) || (negatedInclude.compare(VectorOperators.NE, 0).anyTrue)
if (
(include.and(x.not).compare(VectorOperators.NE, 0).anyTrue) ||
(negatedInclude.and(x).compare(VectorOperators.NE, 0).anyTrue)
) {
output = false
}
i += IntVectorSpecies.length
}
while (output && i < size) {
val x = xs(i)
val include = normal.includeBitsOf(i)
val negatedInclude = negated.includeBitsOf(i)
literals = literals || (include != 0) || (negatedInclude != 0)
if (((include & ~x) != 0) || ((negatedInclude & x) != 0)) {
output = false
}
i += 1
}
if (!output) {
-1
} else {
val mask = Bits.ChangeMasks(description.nDimensions % 32)
val x = xs(i) & mask
val include = normal.includeBitsOf(i) & mask
val negatedInclude = negated.includeBitsOf(i) & mask
literals = literals || (include != 0) || (negatedInclude != 0)
if (((include & ~x) != 0) || ((negatedInclude & x) != 0)) {
-1
} else if (!literals) {
0
} else {
1
}
}
}
}
def fromSimpleMachine(
machine: SimpleMutableMachine): BitVectorMachineBits = {
import machine.description
val bm = initial(description)
def copy(
classClauses: Array[SimpleMutableMachine.Clauses],
classClausesTarget: Array[Clauses]): Unit = {
classClauses.zipWithIndex.foreach { case (clauses, cls) =>
clauses.foreachTeamIndex { i =>
val targetTeam = classClausesTarget(cls).teamAtIndex(i)
(0 until description.nDimensions * 2).foreach { k =>
val value = clauses.teamAtIndex(i).automatonAt(k).value
val state = if (k % 2 == 0) targetTeam.normal else targetTeam.negated
val n = k / 2
val index = n / 32
val bit = n % 32
val mask = 1 << bit
if (value >= description.nStates) {
val count = (value - description.nStates) + 1
(0 until count).foreach { _ =>
state.incBitsInt(index, mask)
}
} else {
val count = (description.nStates - value) - 1
(0 until count).foreach { _ =>
state.decBitsInt(index, mask)
}
}
}
}
}
}
copy(machine.positiveClassClauses, bm.positiveClassClauses)
copy(machine.negativeClassClauses, bm.negativeClassClauses)
bm
}
def initial(description: Description): BitVectorMachineBits = {
implicit val d = description
def log2(x: Double): Double = math.log10(x) / math.log10(2.0)
// we need to represent 0.. 2 * nStates
val N = math.ceil(log2(d.nStates + 1)).toInt
def occupyClauses: Array[Clauses] = {
val clauses = new Array[Clauses](d.nClasses)
clauses.indices.foreach { cls =>
def state = State.initial(N, d.nDimensions)
val teams = new Array[Team](d.nClauses)
teams.indices.foreach { n =>
teams(n) = new Team(
normal = state,
negated = state)
}
clauses(cls) = new Clauses(teams)
}
clauses
}
val positiveClassClauses = occupyClauses
val negativeClassClauses = occupyClauses
new BitVectorMachineBits(positiveClassClauses, negativeClassClauses)
}
case class Workspace(
clauseOutputPositive: BitVector,
clauseOutputNegative: BitVector,
positiveFeedback: Array[Byte],
negativeFeedback: Array[Byte])
object Workspace {
def fresh(implicit d: Description): Workspace = {
Workspace(
BitVector.zeroes(d.nClauses),
BitVector.zeroes(d.nClauses),
new Array[Byte](d.nClauses),
new Array[Byte](d.nClauses))
}
}
}