-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathant.js
160 lines (129 loc) · 4.03 KB
/
ant.js
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
// Intention states of the ants.
exports.Ant = function () {
return {
searching: true,
returnPath: [],
stepcount: 0
};
}
// Generates an intention based on the numeric left and right.
function intention (n, m) {
if(n > m){
var tmp = n;
n = m;
m = tmp;
}
var random = Math.floor(Math.random() * m) + n;
var frontier = n + ( m/n );
return (random > frontier) ? n : m;
}
function copy(array) {
var newArray = new Array (array.length);
for (var i in array)
newArray[i] = array[i];
return newArray;
}
/*
Does all the dirty probability work and returns the node where the
ant should go next.
@param ant The ant for we have to decide where to go.
@param node The node the ant currently resides on.
*/
function nextNode (ant, node) {
var a = [];
var sum = 0;
var random;
var nEdges = node.edges.length;
if(nEdges == 0)
return undefined;
var edges = copy(node.edges);
edges.sort ();
// Fill array.
for (var i in edges) {
if(edges[i] === undefined)
continue;
var edge = edges[i];
var pheromone = edge.pheromone;
if(pheromone == 0)//use 1 instead for no calc issues
pheromone = 1;
a.push (pheromone + sum);
sum += (pheromone + sum);
}
random = Math.floor(Math.random() * (a[a.length-1]+1));//generates random int between 0 and highest value
for (var i in edges) {
if(edges[i] === undefined)
continue;
if (a[i] >= random)
return edges[i];
}
return undefined;//should never happen
}
exports.step = function (root) {
var stepcount = root.stepcount++;
var _process = function (node) {
for (var i in node.ants) {
//just forward gooing ants
var ant = node.ants[i];
if(ant.stepcount > stepcount)
continue;
var next = nextNode(ant, node);
if(next !== undefined){
//set ant on next node
next.ants.push(ant);
node.ants[i] = undefined;
ant.returnPath.push(node);
next.pheromone++;
ant.stepcount++;
}else{
//ant at leaf node => go home! (implemented in next foreach-loop)
node.antsOnHomerun.push(ant);
node.ants[i] = undefined;
}
}
for (var i in node.antsOnHomerun){
//just homerunning ants
var ant = node.antsOnHomerun[i];
if(ant.stepcount > stepcount)
continue;
ant.stepcount++;
next = ant.returnPath.pop();
if(next !== undefined){
next.antsOnHomerun.push(ant);
}
node.antsOnHomerun[i] = undefined;
node.pheromone++;
}
if(node.ants.length != 0){
//reorder ants in array
var ants = new Array();
for (var i in node.ants){
var ant = node.ants[i];
if(ant !== undefined){
ants.push(ant);
}
}
node.ants = ants;
}
if(node.antsOnHomerun.length != 0){
//reorder ants in array
var ants = new Array();
for (var i in node.antsOnHomerun){
var ant = node.antsOnHomerun[i];
if(ant !== undefined){
ants.push(ant);
}
}
node.antsOnHomerun = ants;
}
for (var i in node.edges)
//recursive process subnodes
if(node.edges[i] !== undefined)
_process(node.edges[i]);
};
_process (root);
}
//n + (m/n) zum festlegen der Grenze,
//dann p zwischen n und m ausrechen,
//anhand von näherem wert entscheiden
//bei mehr als 2 Kindern, zufällig immer zu zwei gruppieren.
//problemchen: 2 pheromonstärken gleich!