forked from kchapelier/wavefunctioncollapse
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodel.js
More file actions
198 lines (155 loc) · 4.22 KB
/
model.js
File metadata and controls
198 lines (155 loc) · 4.22 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
"use strict";
var randomIndice = require('./random-indice');
var Model = function Model () {};
Model.prototype.initiliazedField = false;
Model.prototype.generationComplete = false;
Model.prototype.wave = null;
Model.prototype.changes = null;
Model.prototype.stationary = null;
Model.prototype.FMX = 0;
Model.prototype.FMY = 0;
Model.prototype.T = 0;
Model.prototype.periodic = false;
/**
*
* @param {Function} rng Random number generator function
* @protected
* @returns {*}
*/
Model.prototype.observe = function (rng) {
var min = 1000,
argminx = -1,
argminy = -1,
distribution = new Array(this.T),
entropy,
noise,
sum,
wavex,
r,
x,
y,
t;
for (x = 0; x < this.FMX; x++) {
wavex = this.wave[x];
for (y = 0; y < this.FMY; y++) {
if (this.onBoundary(x, y)) {
continue;
}
sum = 0;
for (t = 0; t < this.T; t++) {
distribution[t] = wavex[y][t] ? this.stationary[t] : 0;
sum+= distribution[t];
}
if (sum === 0) {
return false;
}
for (t = 0; t < this.T; t++) {
distribution[t] /= sum;
}
entropy = 0;
for (var i = 0; i < distribution.length; i++) {
if (distribution[i] > 0) {
entropy+= -distribution[i] * Math.log(distribution[i]);
}
}
noise = 0.000001 * rng();
if (entropy > 0 && entropy + noise < min)
{
min = entropy + noise;
argminx = x;
argminy = y;
}
}
}
if (argminx === -1 && argminy === -1) {
return true;
}
for (t = 0; t < this.T; t++) {
distribution[t] = this.wave[argminx][argminy][t] ? this.stationary[t] : 0;
}
r = randomIndice(distribution, rng());
for (t = 0; t < this.T; t++) {
this.wave[argminx][argminy][t] = (t === r);
}
this.changes[argminx][argminy] = true;
return null;
};
/**
* Execute a single iteration
* @param {Function} rng Random number generator function
* @protected
* @returns {*}
*/
Model.prototype.singleIteration = function (rng) {
var result = this.observe(rng);
if (result !== null) {
this.generationComplete = result;
return !!result;
}
while (this.propagate()) {}
return null;
};
/**
* Execute a fixed number of iterations. Stop when the generation is successful or reaches a contradiction.
* @param {int} [iterations=0] Maximum number of iterations to execute (0 = infinite)
* @param {Function|null} [rng=Math.random] Random number generator function
* @returns {boolean} Success
*/
Model.prototype.iterate = function (iterations, rng) {
var result,
i;
if (!this.initiliazedField) {
this.clear();
}
iterations = iterations || 0;
rng = rng || Math.random;
for (i = 0; i < iterations || iterations === 0; i++) {
result = this.singleIteration(rng);
if (result !== null) {
return !!result;
}
}
return true;
};
/**
* Execute a complete new generation
* @param {Function|null} [rng=Math.random] Random number generator function
* @returns {boolean} Success
*/
Model.prototype.generate = function (rng) {
var result;
rng = rng || Math.random;
this.clear();
while (true) {
result = this.singleIteration(rng);
if (result !== null) {
return !!result;
}
}
};
/**
* Check whether the previous generation completed successfully
* @returns {boolean}
*/
Model.prototype.isGenerationComplete = function () {
return this.generationComplete;
};
/**
* Clear the internal state to start a new generation
*/
Model.prototype.clear = function () {
var x,
y,
t;
for (x = 0; x < this.FMX; x++) {
for (y = 0; y < this.FMY; y++) {
for (t = 0; t < this.T; t++) {
this.wave[x][y][t] = true;
}
this.changes[x][y] = false;
}
}
this.initiliazedField = true;
this.generationComplete = false;
};
module.exports = Model;