-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmodel.go
199 lines (168 loc) · 5.31 KB
/
model.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
package wfc
import (
// "fmt"
"image"
"math"
"math/rand"
"time"
)
type Iterator interface {
Iterate(iterations int) (image.Image, bool, bool)
}
type Generator interface {
Generate() (image.Image, bool)
}
type AppliedAlgorithm interface {
Iterator
Generator
OnBoundary(x, y int) bool
Propagate() bool
Clear()
}
type BaseModel struct {
InitiliazedField bool // Generation Initialized
RngSet bool // Random number generator set by user
GenerationSuccessful bool // Generation has run into a contradiction
Wave [][][]bool // All possible patterns (t) that could fit coordinates (x, y)
Changes [][]bool // Changes made in interation of propagation
Stationary []float64 // Array of weights (by frequency) for each pattern (matches index in patterns field)
T int // Count of patterns
Periodic bool // Output is periodic (ie tessellates)
Fmx, Fmy int // Width and height of output
Rng func() float64 // Random number generator supplied at generation time
}
/**
* Observe
* returns: finished (bool)
*/
func (baseModel *BaseModel) Observe(specificModel AppliedAlgorithm) bool {
min := 1000.0
argminx := -1
argminy := -1
distribution := make([]float64, baseModel.T)
// Find the point with minimum entropy (adding a little noise for randomness)
for x := 0; x < baseModel.Fmx; x++ {
for y := 0; y < baseModel.Fmy; y++ {
if specificModel.OnBoundary(x, y) {
continue
}
sum := 0.0
for t := 0; t < baseModel.T; t++ {
if baseModel.Wave[x][y][t] {
distribution[t] = baseModel.Stationary[t]
} else {
distribution[t] = 0.0
}
sum += distribution[t]
}
if sum == 0.0 {
baseModel.GenerationSuccessful = false
return true // finished, unsuccessful
}
for t := 0; t < baseModel.T; t++ {
distribution[t] /= sum
}
entropy := 0.0
for i := 0; i < len(distribution); i++ {
if distribution[i] > 0.0 {
entropy += -distribution[i] * math.Log(distribution[i])
}
}
noise := 0.000001 * baseModel.Rng()
if entropy > 0 && entropy+noise < min {
min = entropy + noise
argminx = x
argminy = y
}
}
}
if argminx == -1 && argminy == -1 {
baseModel.GenerationSuccessful = true
return true // finished, successful
}
for t := 0; t < baseModel.T; t++ {
if baseModel.Wave[argminx][argminy][t] {
distribution[t] = baseModel.Stationary[t]
} else {
distribution[t] = 0.0
}
}
r := randomIndice(distribution, baseModel.Rng())
for t := 0; t < baseModel.T; t++ {
baseModel.Wave[argminx][argminy][t] = (t == r)
}
baseModel.Changes[argminx][argminy] = true
return false // Not finished yet
}
/**
* Execute a single iteration
* returns: finished (bool)
*/
func (baseModel *BaseModel) SingleIteration(specificModel AppliedAlgorithm) bool {
finished := baseModel.Observe(specificModel)
if finished {
return true
}
for specificModel.Propagate() {
// Empty loop
}
return false // Not finished yet
}
/**
* Execute a fixed number of iterations. Stop when the generation succeedes or fails.
*/
func (baseModel *BaseModel) Iterate(specificModel AppliedAlgorithm, iterations int) bool {
if !baseModel.InitiliazedField {
specificModel.Clear()
}
for i := 0; i < iterations; i++ {
finished := baseModel.SingleIteration(specificModel)
if finished {
return true
}
}
return false // Not finished yet
}
/**
* Execute a complete new generation until success or failure.
*/
func (baseModel *BaseModel) Generate(specificModel AppliedAlgorithm) {
specificModel.Clear()
for {
finished := baseModel.SingleIteration(specificModel)
if finished {
return
}
}
}
/**
* Check whether the generation completed successfully
*/
func (baseModel *BaseModel) IsGenerationSuccessful() bool {
return baseModel.GenerationSuccessful
}
/**
* Set the seed for the random number generator. Useful for a stable testing environment.
*/
func (baseModel *BaseModel) SetSeed(seed int64) {
baseModel.Rng = rand.New(rand.NewSource(seed)).Float64
baseModel.RngSet = true
}
/**
* Clear the internal state to start a new generation
*/
func (baseModel *BaseModel) ClearBase(specificModel AppliedAlgorithm) {
for x := 0; x < baseModel.Fmx; x++ {
for y := 0; y < baseModel.Fmy; y++ {
for t := 0; t < baseModel.T; t++ {
baseModel.Wave[x][y][t] = true
}
baseModel.Changes[x][y] = false
}
}
if !baseModel.RngSet {
baseModel.Rng = rand.New(rand.NewSource(time.Now().UnixNano())).Float64
}
baseModel.InitiliazedField = true
baseModel.GenerationSuccessful = false
}