-
Notifications
You must be signed in to change notification settings - Fork 0
/
PSO_Path_optimized.html
236 lines (208 loc) · 6.08 KB
/
PSO_Path_optimized.html
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
<html>
<head>
<title>PSO path finding with reuse of findings</title>
<!--Graphics library (not mine)-->
<script src="js/lib/three.js"></script>
<!--GUI library (not mine)-->
<script src="js/lib/dat.gui.js"></script>
<!--My scripts:-->
<script src="js/Swarm.js"></script>
<script src="js/Vector2D_operations.js"></script>
<script src="js/PSO.js"></script>
<script src="js/MonoPlot2D.js"></script>
</head>
<body>
<nav style="position: absolute; top:0; left:0, height:2vh; z-index:999">
<a href="." style="color: #66ffff">Back to index.</a>
</nav>
<script>
"use strict";
/*TODO: Maybe drop three.js in favor of pure WebGL.*/
//Shorthands
var PI = Math.PI, cos = Math.cos, sin = Math.sin, max=Math.max, min=Math.min, abs=Math.abs, random=Math.random, exp=Math.exp;
//Globals for graphics
var renderer, scene, camera;
//Globals for simulation
var robot, target, obstacles, pso;
//Game loop function
var animate = (function() {
let t_start = 0.001*performance.now();
let ot = t_start;
return function(t) {
t *= 0.001;
let dt = (t-ot);// || 0.0000001; //0 not allowed
ot = t;
robot.update(t, dt);
renderer.render(scene, camera);
requestAnimationFrame(animate);
};
})();
//INITIALISATION
(function main() {
let pars = {
ignoreObstacles: false,
//take distance between robot and particle
//into account. This currently breaks obstacle
//avoidance, for unknown reasons:
useLRP: false,
psoParticles: 2000
}
//Renderer setup
document.body.style = "overflow: hidden;";
var container = document.createElement("div");
Object.assign(container.style, {
position: "absolute",
top: 0,
left: 0,
width: "100vw",
height: "100vh"
});
document.body.appendChild(container);
let W = container.clientWidth;
let H = container.clientHeight;
W = Math.min(W,H);
H = W;
console.log("W=",W," H=",H);
Object.assign(container.style, {
width: W,
height: H
});
renderer = new THREE.WebGLRenderer({antialias: true});
renderer.setSize(W, H);
//renderer.setClearColor(0x000000);
container.appendChild(renderer.domElement);
//Scene setup:
scene = new THREE.Scene();
scene.add(new THREE.AmbientLight(0xffffff, 0.5));
//Camera setup
camera = new THREE.OrthographicCamera(0, W, H, 0, 1, 200);
camera.position.set(0, 0, 100);
scene.add(camera);
//Define obstacles:
let oRadius = 30;
obstacles = new Swarm({
number: 9,
pointRadius: oRadius,
initProc: function(pa,va,aa) {
pa[0].set([0.4*W,0.6*H]);
pa[1].set([0.9*W,0.8*H]);
pa[2].set([0.5*W,0.5*H]);
pa[3].set([0.55*W,0.4*H]);
pa[4].set([0.25*W,0.35*H]);
pa[5].set([0.6*W,0.55*H]);
pa[6].set([0.5*W,0.3*H]);
pa[7].set([0.4*W,0.2*H]);
pa[8].set([0.3*W,0.3*H]);
}
});
scene.add(obstacles);
//Create the target:
let tx = 0.9*W;
let ty = 0.9*H;
target = new Swarm({
number: 1,
pointRadius: 10,
activeColor: 0x00ff00,
initProc: function(pa,va,aa) {
pa[0][0] = tx;
pa[0][1] = ty;
}
});
scene.add(target);
//Create the PSO pso: //Filter
pso = new PSO2D(["x","y"], [0,0], [W,H],
undefined, //filterFun will be defined later
undefined, //costFun will be defined later
0.95, 0.02, 0.03, 10000, 1, false,0.001);
scene.add(pso);
//Create robot:
let rRadius = 10;
robot = new Swarm({
number: 1,
pointRadius: rRadius,
activeColor: 0xff0000,
initProc: function(pa,va,aa) {
pa[0][0] = 0.1*W;
pa[0][1] = 0.1*H;
},
updateProc: function(pa,va,aa,t,dt) {
let [rx,ry] = pa[0];
let obs = obstacles.positions;
//Update filter function to filter out all particles
//that are occluded by obstacles:
pso.filterFun = function([px,py]) {
if (pars.ignoreObstacles) return true;
let dist = length(sub([px,py],[rx,ry]));
let collDistance = oRadius + rRadius;
//For each obstacle position:
for (let o of obs) {
//Filter out if particle is inside obstacle
if (length(sub(o,[px,py])) < collDistance) return false;
//Trim if far away:
if (length(sub(o,[rx,ry])) > dist) continue;
//Project obstacle on line segment:
let proj = projectPositionToLineSegment(o,[rx,ry],[px,py]);
if (proj === null) continue;
if (proj.distance < collDistance) return false;
}
return true;
}
//console.log("Robot position: [",rx,",",ry,"]");
let psoRange = 12*(oRadius+rRadius);
let miX=Math.max(0,Math.min(W,rx)-psoRange);
let maX=Math.min(W,Math.max(0,rx)+psoRange)
let miY=Math.max(0,Math.min(H,ry)-psoRange);
let maY=Math.min(H,Math.max(0,ry)+psoRange);
pso.mins = [miX, miY];
pso.maxes = [maX, maY];
//Update cost function to account for new robot position:
pso.costFun = function([px,py]) {
//Distance from robot to particle:
//(this currently does not work as intended)
let LRP = pars.useLRP ? length(sub([px,py],[rx,ry])) : 0;
//Distance from particle to target:
let LPT = length(sub([tx,ty],[px,py]));
return LRP + LPT;
},
//Set computed particles and draw range from GUI:
pso.activeParticles = pars.psoParticles;
//Run pso once:
pso.update(t,dt);
//Move robot in direction of best particle position:
let gb = pso.global_best;
let [dx,dy] = [gb[0]-rx, gb[1]-ry];
let dist = length([dx,dy]);
let [vx,vy] = scale([dx,dy],dist > 1 ? 60/dist : 60*dist);
//va[0].set([vx,vy]); //not important now
let movement = scale([vx,vy], dt);
pa[0].set(add([rx,ry], movement));
//Increase cost of pso findings, to enable a new search
//building upon old findings:
let del = length(movement);
pso.global_best_value += del;
for (let i = 0; i < pso.number; i++) {
for (let k = 0; k < 2; k++) {
pso.local_bests[i][k] += del;
}
}
}
});
scene.add(robot);
let gui = new dat.GUI();
gui.add(pars, "ignoreObstacles").onChange(function(value) {
obstacles.activeParticles = value ? 0 : obstacles.number;
});
gui.add(pars, "useLRP");
gui.add(pars, "psoParticles", 1, 10000);
gui.add(pso, "inertia", 0.8,1);
gui.add(pso, "cognitive", 0,0.05);
gui.add(pso, "social", 0,0.05);
gui.add({restart: function() {
robot.initProc(robot.positions);
pso.reset();
}}, "restart");
requestAnimationFrame(animate);
})();
</script>
</body>
</html>