forked from ChalmersGU-AI-course/shrdlite-course-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Planner.ts
496 lines (445 loc) · 16.6 KB
/
Planner.ts
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
///<reference path="World.ts"/>
///<reference path="Interpreter.ts"/>
///<reference path="./lib/node.d.ts"/>
///<reference path="./astar/AStar.ts"/>
///<reference path="./lib/collections.ts"/>
module Planner {
////////////////////////////////
// global variables
var currentWorldDescription = new collections.Dictionary<String, WorldObject>();
var position : number = 0;
//////////////////////////////////////////////////////////////////////
// exported functions, classes and interfaces/types
export function plan(interpretations : Interpreter.Result[], currentState : WorldState) : Result[] {
var plans : Result[] = [];
interpretations.forEach((intprt) => {
var plan : Result = <Result>intprt;
plan.plan = planInterpretation(plan.intp, currentState);
plans.push(plan);
});
if (plans.length) {
return plans;
} else {
throw new Planner.Error("Found no plans");
}
}
export interface Result extends Interpreter.Result {plan:string[];}
export function planToString(res : Result) : string {
return res.plan.join(", ");
}
export class Error implements Error {
public name = "Planner.Error";
constructor(public message? : string) {}
public toString() {return this.name + ": " + this.message}
}
//////////////////////////////////////////////////////////////////////
// private functions
function planInterpretation(intprt : Interpreter.Literal[][], state : WorldState) : string[] {
var plan : string[] = [];
var goal: PDDL = new PDDL(intprt);
var solution: Astar.Solution<WorldDescription>;
var plan:string[] = new Array<string>();
// find a path from the initial state to the final PDDL using A*
solution = Astar.search(convert(state), null, goal);
if(solution && solution.path){
for (var i = 0; i < solution.path.length; i++) {
// print out the path
console.log("path: " + solution.path[i].toString());
}
for (var i = 1; i < solution.path.length; i++) {
// concatenate the steps between each state
plan = plan.concat(getMoves(solution.path[i-1], solution.path[i]));
}
} else {
throw new Error("no path found");
}
return plan;
}
function getMoves(state1: WorldDescription, state2: WorldDescription): string[] {
var ret: string[] = new Array<string>();
for(var i = 0; i < state1.stacks.length; i++) {
if(state1.stacks[i].length != state2.stacks[i].length) {
var tmp = position - i;
if(tmp>0) {
for(var j = tmp; j > 0; j--) {
position--;
ret.push("l");
}
} else if(tmp < 0) {
for(var j = tmp; j < 0; j++) {
position++;
ret.push("r");
}
}
if(state1.stacks[i].length > state2.stacks[i].length) {
ret.push("p");
} else {
ret.push("d");
}
}
}
return ret;
}
////////////////////////////////////////////////////
// private classes
class PDDL {
// the inner array describes literals connected with an AND,
// the outer one connected with an OR
alternatives : Lit[][];
constructor (input : Lit[][]) {
this.alternatives = input;
}
}
//one expression describing a property of a goal
class Lit implements Interpreter.Literal {
//true/false: goal must/must not forfill the property
pol:boolean;
//a relationship between objects as describet in the grammar
//TODO: is this useful? Maybe TypeScript allows to have a smaller
//space of relationsships
rel:string;
//the objects on which the relationsship works
args:string[]
constructor(pol: boolean, rel: string, args: string[]) {
this.pol = pol;
this.rel = rel;
this.args = args;
}
}
class WorldDescription implements Astar.State {
//heuristical value for this state. Useful if you don't want to call
//the heuristical function each time
h: number;
stacks: WorldObject[][];
crane: WorldObject;
//usage: either both parameter null, then create an empty WorldDescription
// OR : stacks not null, then create a WorldDescription with the given
// stacks.
// WARNING: stacks null, but crane not null will throw an error.
constructor(stacks: string[][], crane: string) {
//if both parameter null, create an empty WorldDescription
if( stacks == null && crane == null){
this.crane = null;
this.stacks = new Array<Array<WorldObject>>();
this.h = 0;
} else if (stacks != null){
//if stacks are given, use them.
this.stacks = new Array<Array<WorldObject>>();
for (var i = 0; i < stacks.length; i++) {
this.stacks[i] = new Array<WorldObject>();
}
for (var i = 0; i < stacks.length; i++) {
for (var j = 0; j < stacks[i].length; j++) {
this.stacks[i].push(currentWorldDescription.getValue(stacks[i][j]));
}
}
if(crane != null){
//if stacks are given and crane is given, use also the crane
this.crane = currentWorldDescription.getValue(crane);
} else {
this.crane = null;
}
// predefine a heuristic value
this.h = 0;
} else {
// if crane is given, but stacks is null
throw new Error("You are stupid! You cant create a world without a stack but with a crane!");
}
}
toString() {
var s : string = "";
for (var i = 0; i < this.stacks.length; i++) {
s += "[";
for (var j = 0; j < this.stacks[i].length; j++) {
s += this.stacks[i][j] + ", ";
}
s += "]";
}
s += "\n" + this.crane + "\n";
return s;
}
clone() : WorldDescription {
var braveNewWorld : WorldDescription = new WorldDescription(null, null);
for (var i = 0; i < this.stacks.length; i++) {
braveNewWorld.stacks[i] = new Array<WorldObject>();
for (var j = 0; j < this.stacks[i].length; j++) {
braveNewWorld.stacks[i].push(this.stacks[i][j]);
}
}
braveNewWorld.crane = this.crane;
return braveNewWorld;
}
match(goal: PDDL) {
for (var i = 0; i < goal.alternatives.length; i++) {
if(this.checkAlt(goal.alternatives[i])){
return true;
}
}
return false;
}
//returns true if ALL literals are true
checkAlt(lits: Lit[]): boolean{
var result: boolean = true;
for(var i = 0; i < lits.length; i++) {
result = result && this.eval(lits[i]);
}
return result;
}
eval(lit: Lit): boolean {
var res: boolean = true;
// find the first argument
for (var i = 0; i < this.stacks.length; i++) {
for (var j = 0; j < this.stacks[i].length; j++) {
if (this.stacks[i][j].name == lit.args[0] || lit.rel == "holding") {
// evaluate based on the relation
switch(lit.rel) {
case "inside": // fall through to ontop since they are the same
case "ontop":
if (this.stacks[i][j-1]
&& lit.args[1] == this.stacks[i][j-1].name)
return lit.pol;
break;
case "rightof":
for (var k = i-1; k > 0; k--) {
for (var l = 0; l < this.stacks[k].length; l++) {
if (this.stacks[k][l].name == lit.args[1])
return lit.pol;
}
}
break;
case "leftof":
for (var k = i+1; k < this.stacks.length; k++) {
for (var l = 0; l < this.stacks[k].length; l++) {
if (this.stacks[k][l].name == lit.args[1])
return lit.pol;
}
}
break;
case "above":
for (var k = 0; k < j; k++) {
if (lit.args[1] == this.stacks[i][k].name) {
return lit.pol;
}
}
break;
case "beside":
if (this.stacks[i-1]) {
for (var k = 0; k < this.stacks[i-1].length; k++) {
if (this.stacks[i-1][k].name == lit.args[1]) {
return lit.pol;
}
}
}
if (this.stacks[i+1]) {
for (var k = 0; k < this.stacks[i+1].length; k++) {
if (this.stacks[i+1][k].name == lit.args[1]) {
return lit.pol;
}
}
}
break;
case "under":
for (var k = j+1; k < this.stacks[i].length; k++) {
if (this.stacks[i][k] && this.stacks[i][k].name == lit.args[1])
return lit.pol;
}
break;
case "holding":
if (this.crane)
return this.crane.name == lit.args[0] == lit.pol;
break;
}
}
}
}
return !lit.pol;
}
//guesses a distance from the current state to goals describet in a PDDL
heuristic(goal: PDDL) {
var curr = 0;
var min = Number.MAX_VALUE;
var found = false;
//go through all OR parts
for(var i = 0; i < goal.alternatives.length; i++) {
//go through all AND parts
for(var j = 0; j < goal.alternatives[i].length; j++){
//go through all touched argumends
if(this.eval(goal.alternatives[i][j])){
//if the AND-statement is already forfilled, don't
//increase the heuristic
break;
}
switch(goal.alternatives[i][j].rel){
case "inside":
case "ontop":
for(var k = 0; k < goal.alternatives[i][j].args.length; k++) {
found = false;
//go through all stacks to find it
for(var l = 0; l < this.stacks.length && !found; l++) {
for(var m = 0; m < this.stacks[l].length && !found; m++) {
//add the number of things above the currend element to
//the heuristic value
// console.log(this.stacks[l][m].name + " vs. " + goal.alternatives[i][j].args[k]);
if(this.stacks[l][m]
&& this.stacks[l][m].name == goal.alternatives[i][j].args[k]) {
curr += (this.stacks[l].length - m);
found = true;
}
}
}
}
break;
case "above":
case "leftof":
case "rightof":
case "holding":
found = false;
//go through all stacks to find it
for(var l = 0; l < this.stacks.length && !found; l++) {
for(var m = 0; m < this.stacks[l].length && !found; m++) {
if(this.stacks[l][m]
&& this.stacks[l][m].name == goal.alternatives[i][j].args[0]) {
curr += (this.stacks[l].length - m);
found = true;
}
}
}
break;
case "under":
found = false;
//go through all stacks to find it
for(var l = 0; l < this.stacks.length && !found; l++) {
for(var m = 0; m < this.stacks[l].length && !found; m++) {
//difference to the previous one is the "1" in the if statement
if(this.stacks[l][m]
&& this.stacks[l][m].name == goal.alternatives[i][j].args[1]) {
curr += (this.stacks[l].length - m);
found = true;
}
}
}
break;
case "beside":
for(var k = 0; k < goal.alternatives[i][j].args.length; k++) {
found = false;
var var1 = Number.MAX_VALUE;
var var2 = Number.MAX_VALUE;
//go through all stacks to find it
for(var l = 0; l < this.stacks.length; l++) {
for(var m = 0; m < this.stacks[l].length; m++) {
//add the number of things above the currend element to
//the heuristic value
if(this.stacks[l][m]
&& this.stacks[l][m].name == goal.alternatives[i][j].args[0]) {
var1 = (this.stacks[l].length - m);
} else if (this.stacks[l][m]
&& this.stacks[l][m].name == goal.alternatives[i][j].args[1]) {
var1 = (this.stacks[l].length - m);
}
}
}
}
curr = Math.min(var1, var2);
break;
}
}
if( curr < min) {
min = curr;
}
curr = 0;
}
return min;
}
//returns all possible neigbours of a state
expand() {
var neighbours = new Array();
if (this.crane == null) {
for (var i = 0; i < this.stacks.length; i++) {
if (this.stacks[i][0] == undefined)
continue;
var topObject : WorldObject = this.stacks[i][this.stacks[i].length - 1];
var newWorld : WorldDescription = this.clone();
newWorld.stacks[i].splice(this.stacks[i].length - 1, 1);
newWorld.crane = topObject;
if (checkIfValid(newWorld))
neighbours.push({cost: 1, state: newWorld});
}
} else {
for (var i = 0; i < this.stacks.length; i++) {
var newWorld : WorldDescription = this.clone();
newWorld.stacks[i].push(this.crane);
newWorld.crane = null;
if (checkIfValid(newWorld))
neighbours.push({cost: 1, state: newWorld});
}
}
return neighbours;
}
}
//describes an object in a world
class WorldObject {
form: String;
size: String;
color: String;
name: String;
constructor(form: String, size: String, color: String, name: String) {
this.form = form;
this.size = size;
this.color = color;
this.name = name;
}
toString() {
return this.name;
}
}
function convert(input: WorldState): WorldDescription {
for (var name in input.objects) {
currentWorldDescription.setValue(name, new WorldObject(input.objects[name].form,
input.objects[name].size,
input.objects[name].color,
name));
}
return new WorldDescription(input.stacks, input.holding);
}
function checkIfValid(state) {
for (var i = 0; i < state.stacks.length; i++) {
for (var j = 1; j < state.stacks[i].length; j++) {
if (!state.stacks[i][j] || !state.stacks[i][j-1]) continue;
var currentObjectDescription = state.stacks[i][j];
var belowObjectDescription = state.stacks[i][j-1];
// balls must be in boxes or on the floor, otherwise they roll away
if (currentObjectDescription.form == "ball"
&& belowObjectDescription.form != "box")
return false;
// balls cannot support anything
if (belowObjectDescription.form == "ball")
return false;
// small objects cannot support large objects
if (currentObjectDescription.size == "large"
&& belowObjectDescription.size == "small")
return false;
// boxes cannot contain pyramids, planks or boxes of the same size
if (belowObjectDescription.form == "box"
&& (currentObjectDescription.form == "pyramid"
|| currentObjectDescription.form == "plank"
|| (currentObjectDescription.form == "box"
&& belowObjectDescription.size == currentObjectDescription.size)))
return false;
// small boxes cannot be supported by small bricks or pyramids
if (currentObjectDescription.form == "box"
&& currentObjectDescription.size == "small"
&& belowObjectDescription.size == "small"
&& (belowObjectDescription.form == "brick"
|| belowObjectDescription.form == "pyramid"))
return false;
// large boxes cannot be supported by large pyramids
if (currentObjectDescription.form == "box"
&& currentObjectDescription.size == "large"
&& belowObjectDescription.form == "pyramid"
&& belowObjectDescription.size == "large")
return false;
}
}
return true;
}
}