-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmlca_automaton.js.orig
324 lines (276 loc) · 8.25 KB
/
mlca_automaton.js.orig
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
/* Automaton
Monolithic iterator: access to all modules
+begin(): initiate the ruleset list, most display parameters, the main loop
the list of layers, the neighborhood libraries and predefined rules and layers
-draw(): draws the cells into the canvas, followed by the grid
-iterate(): compute the next state of all layers
*/
mlca.automaton = {};
mlca.automaton.begin = function () {
'use strict';
console.log("automaton.begin()");
/*
Initializations:
i: index for 'for'
mlca.rulesetList: Array with rulesets to be applied
*/
var i;
mlca.rulesetList = [];
/*
Temp implementation for the SimpleCanvasDisplay
this.play: Boolean to control auto-iterator
this.fps: Maximum animation frames per second
this.cellSize: size for the grid cells
*/
if (mlca.fieldSize === undefined) {
mlca.fieldSize = {x: 100, y: 60};
}
this.play = false;
if (this.fps === undefined) {
this.fps = 10;
}
if (this.cellSize === undefined) { this.cellSize = 10; }
if (!this.displayInfo) {
this.displayInfo = {
cellSize: this.cellSize,
dimensions: mlca.fieldSize
};
}
document.getElementById("canvas").addEventListener('mousemove', function (event) {
document.getElementById("mousex").innerHTML = Math.floor(event.clientX / mlca.automaton.cellSize);
document.getElementById("mousey").innerHTML = Math.floor(event.clientY / mlca.automaton.cellSize);
}, false);
// Fix p/ multiplos event listeners
if (!this.display) { this.display = new mlca.SimpleCanvasDisplay(this.displayInfo); }
//End of temp implementation
/*
mlca.mainloop: Main loop. Calculates elapsed time since last frame
*/
(function () {
var curr = (new Date()).getTime(), last = 0, elaps = 0;
mlca.mainloop = function () {
last = curr;
curr = (new Date()).getTime();
elaps += curr - last;
if (elaps > 1000 / mlca.automaton.fps) {
elaps = (curr - last) % (1000 / mlca.automaton.fps);
if (mlca.automaton.play) {
mlca.automaton.iterate();
}
}
requestAnimationFrame(mlca.mainloop);
};
})();
/*
mlca.layerList: array with layers
*/
mlca.layerList = [];
mlca.layerList.getLayerByID = function(layerID){
for ( i = 0; i < this.length; i += 1){
if (this[i].id === layerID){
return this[i];
}
}
};
/*
Libraries (possibly to be moved)
*/
mlca.kernels = {
self : new mlca.Kernel({
relPosList:[{x:0,y:0}]
}),
ring8 : new mlca.Kernel({
relPosList:[
{x:-1,y:-1},
{x:0,y:-1},
{x:1,y:-1},
{x:1,y:0},
{x:1,y:1},
{x:0,y:1},
{x:-1,y:1},
{x:-1,y:0}
]
})
};
/*
Rules: They state the conditions for cell change and the target state.
TODO: implement stateToCount as a range of states (for countable data types),
and relative targetStates.
*/
mlca.layers = {
formigas :
{
dimensions: mlca.fieldSize,
visibility: true,
type: 'integer',
topology: 'xyloop',
layerID: 'formigas',
DataStructure:mlca.WorstMatrix,
defaultState:{state:0,attributes:[]},
lock:false,
interfaceData:{
stateAlternate: function (s){
if(s.state == 0){
return {state:1, attributes:[0,true]};
}
else if(s.attributes[0] < 7){
return {state:1, attributes:[s.attributes[0] + 1,true]};
}
else{
return {state:0};
}
},
stateRepresentation: function (s){
var ret;
switch (s.state){
case 1:
ret = '#000000';
break;
default:
ret = 'white';
}
return ret;
}// end stateRepresentation
}// end interfaceData
},// end specs
feromonios :
{
dimensions: mlca.fieldSize,
visibility: false,
type: 'integer',
topology: 'xyloop',
layerID: 'feromonios',
DataStructure:mlca.WorstMatrix,
defaultState:{state:0,attributes:[]},
lock:false,
interfaceData:{
stateAlternate: function (s){
if(s.state == 0){
return {state:1, attributes:[21]};
}
else if(s.attributes[0] > 0){
return {state:1, attributes:[s.attributes[0] - 1]};
}
else{
return {state:0};
}
},
stateRepresentation: function (s){
var ret;
switch (s.state){
case 1:
ret = 'yellow';
break;
default:
<<<<<<< HEAD
ret = 'purple';
=======
ret = "rgba(0,0,255,0.1)" ;
>>>>>>> 5bfadafd2859f37538056952d1acb94aa685a0c2
}
return ret;
}// end stateRepresentation
}// end interfaceData
}// end specs
};
//Probability of changing direction
mlca.directionChangeProbability = 0.01;
mlca.feromoneFollowingProbability = 0.999;
mlca.automaton.changeProbs = function changeProbs (form) {
mlca.automaton.play = false;
if(form.PMD.value < 0 || form.PMD.value > 1 || form.PMD.value === undefined){
alert("Valor de probabilidade de mudança invalido, inserir número entre 0 e 1");
}else{
mlca.directionChangeProbability = form.PMD.value;
document.getElementById("PMD").innerHTML = mlca.directionChangeProbability;
mlca.directionChangeProbability = mlca.directionChangeProbability/2;
}
if(form.PSF.value < 0 || form.PSF.value > 1 || form.PSF.value === undefined){
alert("Valor de probabilidade de seguir invalido, inserir número entre 0 e 1");
}
else{
mlca.feromoneFollowingProbability = form.PSF.value;
document.getElementById("PSF").innerHTML = mlca.feromoneFollowingProbability;
}
mlca.rulesetList.splice(0,mlca.rulesetList.length);
mlca.setRules();
mlca.rulesetList.push(new mlca.Ruleset({
ruleList:mlca.rulelists.formigas,
layerID:'formigas'
}));
mlca.rulesetList.push(new mlca.Ruleset({
ruleList:mlca.rulelists.feromonios,
layerID:'feromonios'
}));
}
//Interface prototype
/*
Initializations (will be done through the interface later)
*/
//Load the ruleSets and its rules and conditions
mlca.setRules();
mlca.layerList.push(new mlca.Layer(mlca.layers.formigas));
mlca.layerList.push(new mlca.Layer(mlca.layers.feromonios));
mlca.rulesetList.push(new mlca.Ruleset({
ruleList:mlca.rulelists.formigas,
layerID:'formigas'
}));
mlca.rulesetList.push(new mlca.Ruleset({
ruleList:mlca.rulelists.feromonios,
layerID:'feromonios'
}));
/* //R-Pentamino :D
mlca.layerList[0].readFromString('011\n11\n010',
function(n){
switch (n){
case '1':
return true;
default:
return false;
}
},
{x:10,y:10}
);
*///End Interface prototype
document.getElementById("selectedlayer").innerHTML = mlca.automaton.display.returnLayer().id;
if(mlca.automaton.display.returnLayer().lock){
document.getElementById("lockstatus").innerHTML = " (LOCKED)";
}
document.getElementById("PMD").innerHTML = mlca.directionChangeProbability;
document.getElementById("PSF").innerHTML = mlca.feromoneFollowingProbability;
mlca.directionChangeProbability = mlca.directionChangeProbability/2;
this.draw();
mlca.mainloop();
};
/*
Draw: Draws background, then layers (from bottom to top), then grid.
*/
mlca.automaton.draw = function(){
'use strict';
var i;
this.display.drawBackground();
for (i = 0; i<mlca.layerList.length; i += 1){
this.display.drawLayer(mlca.layerList[i]);
}
this.display.drawGrid();
};
//DEBUG
mlca.automaton.iterate = function() {
'use strict';
var i;
var it = {x:0,y:0};
// Execute Rulesets
for (i = 0; i<mlca.rulesetList.length; i+= 1){
for (it.x = 0; it.x<mlca.fieldSize.x; it.x += 1){
for (it.y = 0; it.y<mlca.fieldSize.y; it.y += 1){
mlca.rulesetList[i].run(it);
}
}
}
//Swap Matrixes
for (i = 0; i<mlca.layerList.length; i += 1){
mlca.layerList[i].swap();
}
//Draw Layers
this.draw();
};