-
Notifications
You must be signed in to change notification settings - Fork 0
/
production_rule.go
208 lines (188 loc) · 4.93 KB
/
production_rule.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
package lsystem
import (
"pgregory.net/rand"
"strconv"
"strings"
)
type WeightedRule struct {
Probability float64
Catalyst Token
Tokens []Token
}
type ProductionRule struct {
Predecessor Token
Weights []WeightedRule
}
func (r *ProductionRule) String() string {
var sb strings.Builder
sb.WriteRune('"')
sb.WriteString(string(r.Predecessor))
sb.WriteRune('"')
sb.WriteString(": `")
for i, wt := range r.Weights {
sb.WriteString(strconv.FormatFloat(wt.Probability, 'f', 2, 64))
if wt.Catalyst != "" {
sb.WriteString(" *")
sb.WriteString(string(wt.Catalyst))
sb.WriteString(" -> ")
}
sb.WriteString(" ")
for _, t := range wt.Tokens {
sb.WriteString(string(t))
sb.WriteString(" ")
}
if i != len(r.Weights)-1 {
sb.WriteString("; ")
}
}
sb.WriteString("`")
return sb.String()
}
func NewProductionRule(predecessor Token, weights []WeightedRule) ProductionRule {
return ProductionRule{
Predecessor: predecessor,
Weights: weights,
}
}
func (r *ProductionRule) ChooseSuccessor() []Token {
total := 0.0
for _, wt := range r.Weights {
total += wt.Probability
}
random := rand.Float64() * total
for _, wt := range r.Weights {
random -= wt.Probability
if random < 0 {
return wt.Tokens
}
}
return []Token{}
}
type ByteWeightedRule struct {
LowerLimit float64
UpperLimit float64
Catalyst TokenStateId
Successor []TokenStateId
}
type ByteProductionRule struct {
Weights []ByteWeightedRule
PreSampledWeights []uint8
currentIndex int
Predecessor TokenStateId
}
func (r *ProductionRule) EncodeTokens(tokenBytes map[Token]TokenStateId, presample bool) ByteProductionRule {
rule := ByteProductionRule{
Weights: make([]ByteWeightedRule, len(r.Weights), len(r.Weights)),
Predecessor: tokenBytes[r.Predecessor],
}
total := 0.0
for w := 0; w < len(r.Weights); w++ {
wt := r.Weights[w]
encodedTokens := make([]TokenStateId, len(wt.Tokens), len(wt.Tokens))
for i := len(wt.Tokens) - 1; i >= 0; i-- {
t := wt.Tokens[i]
encodedTokens[i] = tokenBytes[t]
}
rule.Weights[w] = ByteWeightedRule{
Catalyst: tokenBytes[wt.Catalyst],
Successor: encodedTokens,
}
rule.Weights[w].LowerLimit = total
total += wt.Probability
rule.Weights[w].UpperLimit = total
}
if presample {
rule.PreSample()
}
return rule
}
func (bp *ByteProductionRule) RandomizeWeights(delta float64, presample bool) {
currentWeights := make([]float64, len(bp.Weights), len(bp.Weights))
for i := 0; i < len(bp.Weights); i++ {
currentWeights[i] = bp.Weights[i].UpperLimit - bp.Weights[i].LowerLimit
}
total := 0.0
for i := 0; i < len(bp.Weights); i++ {
currentWeights[i] += delta - rand.Float64()*2*delta
currentWeights[i] = max(0, currentWeights[i])
bp.Weights[i].LowerLimit = total
total += currentWeights[i]
bp.Weights[i].UpperLimit = total
}
if presample {
bp.PreSample()
}
}
func (bp *ByteProductionRule) PreSample() {
if bp.Weights == nil || len(bp.Weights) == 0 {
return
}
if bp.PreSampledWeights == nil {
bp.PreSampledWeights = make([]uint8, 256, 256)
}
for i := 0; i < 256; i++ {
random := rand.Float64() * (bp.Weights[len(bp.Weights)-1].UpperLimit)
index, _ := bp.findRuleByProbability(random)
bp.PreSampledWeights[i] = index
}
}
func (bp *ByteProductionRule) ChooseSuccessor(l *LSystem, previousToken TokenStateId) []TokenStateId {
emptyToken := l.EmptyTokenId
if previousToken.HasParam() {
previousToken = l.ParamToByte[previousToken]
}
if bp.PreSampledWeights != nil {
rule := bp.Weights[bp.PreSampledWeights[bp.currentIndex]]
bp.currentIndex++
if bp.currentIndex == len(bp.PreSampledWeights) {
bp.currentIndex = 0
}
if rule.Catalyst == emptyToken || rule.Catalyst == previousToken {
return rule.Successor
}
return []TokenStateId{bp.Predecessor}
}
random := rand.Float64() * (bp.Weights[len(bp.Weights)-1].UpperLimit)
_, rule := bp.findRuleByProbability(random)
if rule.Catalyst == emptyToken || rule.Catalyst == previousToken {
return rule.Successor
}
return []TokenStateId{bp.Predecessor}
}
func (bp *ByteProductionRule) findRuleByProbability(p float64) (uint8, ByteWeightedRule) {
// Use binary search to find the successor
lo, hi := 0, len(bp.Weights)
for lo < hi {
mid := (lo + hi) / 2
if p < bp.Weights[mid].LowerLimit {
hi = mid
} else if p >= bp.Weights[mid].UpperLimit {
lo = mid + 1
} else {
return uint8(mid), bp.Weights[mid]
}
}
return 0, ByteWeightedRule{}
}
func (bp *ByteProductionRule) String(tokens [255]Token) string {
var sb strings.Builder
sb.WriteRune('"')
for i, wt := range bp.Weights {
if wt.Successor == nil || len(wt.Successor) == 0 {
continue
}
sb.WriteString(strconv.FormatFloat(wt.UpperLimit-wt.LowerLimit, 'f', 2, 64))
sb.WriteString(" ")
for i, t := range wt.Successor {
sb.WriteString(string(tokens[t]))
if i != len(wt.Successor)-1 {
sb.WriteString(" ")
}
}
if i != len(bp.Weights)-1 {
sb.WriteString(";")
}
}
sb.WriteRune('"')
return sb.String()
}