-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
226 lines (215 loc) · 5.2 KB
/
main.go
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
// Copyright 2021 The rndnet Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"flag"
"fmt"
"math"
"runtime"
)
var (
// LFSR find lfsr
LFSR = flag.Bool("lfsr", false, "find a lfsr")
// Real uses the real network
Real = flag.Bool("real", false, "real network")
// Random is a random neural network
Random = flag.Bool("random", false, "randome network")
// Shared uses the real network with shared weights
Shared = flag.Bool("shared", false, "real network with share weights")
// Complex uses the complex network
Complex = flag.Bool("complex", false, "complex network")
// RNN uses the recurrent neural network
RNN = flag.Bool("rnn", false, "recurrent neural network")
// Search search for the best seed
Search = flag.Bool("search", false, "search for the best seed")
)
const (
// LFSRMask is a LFSR mask with a maximum period
LFSRMask = 0x80000057
// LFSRInit is an initial LFSR state
LFSRInit = 0x55555555
// NumGenomes is the number of genomes
NumGenomes = 256
// SearchIterations is the number of search iterations
SearchIterations = 256
// Size is the size of the recurrent neural network
Size = 8
)
// Rand is a random number generator
type Rand uint32
// Float32 returns a random float32 between 0 and 1
func (r *Rand) Float32() float32 {
lfsr := *r
if lfsr&1 == 1 {
lfsr = (lfsr >> 1) ^ LFSRMask
} else {
lfsr = lfsr >> 1
}
*r = lfsr
return float32(lfsr) / ((1 << 32) - 1)
}
// Uint32 returns a random uint32
func (r *Rand) Uint32() uint32 {
lfsr := *r
if lfsr&1 == 1 {
lfsr = (lfsr >> 1) ^ LFSRMask
} else {
lfsr = lfsr >> 1
}
*r = lfsr
return uint32(lfsr)
}
func main() {
flag.Parse()
type Result struct {
Seed int
Quality float64
}
process := func(model func(seed int) float64) {
results := make(chan Result, runtime.NumCPU())
routine := func(seed int) {
results <- Result{
Seed: seed,
Quality: model(seed * NumGenomes),
}
}
min, seed, count, j, flight := 1.0, 0, 0, 0, 0
for i := 0; i < runtime.NumCPU() && j < SearchIterations; i++ {
go routine(j)
j++
flight++
}
for j < SearchIterations {
result := <-results
flight--
if result.Quality < min {
min, seed = result.Quality, result.Seed
}
if result.Quality < .1 {
count++
}
go routine(j)
j++
flight++
}
for i := 0; i < flight; i++ {
result := <-results
if result.Quality < min {
min, seed = result.Quality, result.Seed
}
if result.Quality < .1 {
count++
}
}
fmt.Println(min, seed, count)
}
if *LFSR {
// https://en.wikipedia.org/wiki/Linear-feedback_shift_register
// https://users.ece.cmu.edu/~koopman/lfsr/index.html
count, polynomial := 0, uint32(0x80000000)
for polynomial != 0 {
lfsr, period := uint32(1), uint32(0)
for {
lfsr = (lfsr >> 1) ^ (-(lfsr & 1) & polynomial)
period++
if lfsr == 1 {
break
}
}
fmt.Printf("%v period=%v\n", count, period)
if period == math.MaxUint32 {
fmt.Printf("%x\n", polynomial)
return
}
count++
polynomial++
}
return
} else if *Real {
if *Search {
process(RealNetworkModel)
} else {
// 0.02 135 14
RealNetworkModel(135 * NumGenomes)
}
return
} else if *Random {
if *Search {
process(RandomNetworkModel)
} else {
// 0.04666666666666667 1391 32
RandomNetworkModel(1391 * NumGenomes)
}
return
} else if *Complex {
if *Search {
process(ComplexNetworkModel)
} else {
// 0.05333333333333334 186 1
ComplexNetworkModel(186 * NumGenomes)
}
return
} else if *Shared {
if *Search {
// 0.06 152 1
process(SharedNetworkModel)
} else {
SharedNetworkModel(152 * NumGenomes)
}
return
} else if *RNN {
g := Rand(LFSRInit)
waves, inputs, outputs, connections, factor :=
make([]float32, 2), make([]float32, Size), make([]float32, Size), make([][]float32, Size), float32(math.Sqrt(2/float64(Size)))
for i := range connections {
connections[i] = make([]float32, Size)
for j := range connections[i] {
connections[i][j] = 1
}
}
for i := 0; i < 1024; i++ {
rnd := Rand(LFSRInit + 3*Size*Size)
for j := 0; j < Size; j++ {
sum := (2*rnd.Float32() - 1) * factor
for _, wave := range waves {
sum += (2*rnd.Float32() - 1) * factor * wave
}
for k := 0; k < Size; k++ {
if weight := rnd.Float32(); k == j {
sum += (2*weight - 1) * factor * inputs[k]
} else if g.Float32() > 1/float32(connections[j][k]) {
sum += (2*weight - 1) * factor * inputs[k]
}
}
e := float32(math.Exp(float64(sum)))
outputs[j] = e / (e + 1)
}
for j, a := range outputs {
for k, b := range outputs {
if j == k {
continue
}
if a > .5 && b > .5 {
connections[j][k]++
} else if connections[j][k] > 1 {
connections[j][k]--
}
}
}
copy(inputs, outputs)
fmt.Println(i, outputs)
fmt.Println(connections)
switch true {
case waves[0] == 0 && waves[1] == 0:
waves[0], waves[1] = 1, 0
case waves[0] == 1 && waves[1] == 0:
waves[0], waves[1] = 0, 1
case waves[0] == 0 && waves[1] == 1:
waves[0], waves[1] = 1, 1
case waves[0] == 1 && waves[1] == 1:
waves[0], waves[1] = 0, 0
}
}
}
}