Spaces:
Running
Running
File size: 5,632 Bytes
d72771b | 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 | // BDDL, the BEHAVIOR Domain Definition Language: a task is a problem with typed objects, an initial state
// (:init) and a goal (:goal), written as s-expressions, as in BEHAVIOR-1K:
//
// (define (problem picking_up_trash-0)
// (:domain xlerobot-kitchen)
// (:objects can__of__soda.n.01_1 can__of__soda.n.01_2 - can__of__soda.n.01 ashcan.n.01_1 - ashcan.n.01)
// (:init (ontop can__of__soda.n.01_1 countertop.n.01_1) ...)
// (:goal (and (forall (?can__of__soda.n.01 - can__of__soda.n.01) (inside ?can__of__soda.n.01 ?ashcan.n.01_1)))))
//
// This module parses that and grounds a goal into the literals that are scored: the goal's conjuncts, with
// each `forall` expanded over its objects, so partial credit is the share of these that hold (the
// BEHAVIOR Challenge's score). It knows nothing about the simulation: predicates are evaluated by a
// function the caller gives.
/** Tokens: parentheses and atoms; `;` starts a comment to the end of the line. */
function tokens(text) {
return text.replace(/;[^\n]*/g, " ").replace(/\(/g, " ( ").replace(/\)/g, " ) ").split(/\s+/).filter(Boolean);
}
/** The s-expression in `text` as nested arrays of strings. */
export function parseSexpr(text) {
const list = tokens(text);
let i = 0;
const read = () => {
const t = list[i++];
if (t === undefined) throw new Error("BDDL: unexpected end");
if (t === ")") throw new Error("BDDL: unexpected )");
if (t !== "(") return t;
const out = [];
while (list[i] !== ")") {
if (i >= list.length) throw new Error("BDDL: missing )");
out.push(read());
}
i++;
return out;
};
const expr = read();
if (i < list.length) throw new Error("BDDL: text after the problem");
return expr;
}
/** Typed names, "a b - type c - type2", as {name: type}. */
function typed(items) {
const out = {};
let pending = [];
for (let k = 0; k < items.length; k++) {
if (items[k] === "-") {
for (const n of pending) out[n] = items[k + 1];
pending = [];
k++;
} else pending.push(items[k]);
}
for (const n of pending) out[n] = "object";
return out;
}
/** A BDDL problem: {name, domain, objects: {name: type}, init: [expr], goal: expr, text}. */
export function parseProblem(text) {
const e = parseSexpr(text);
if (e[0] !== "define" || e[1]?.[0] !== "problem") throw new Error("BDDL: not a (define (problem ...))");
const problem = { name: e[1][1], domain: null, objects: {}, init: [], goal: ["and"], text };
for (const part of e.slice(2)) {
const [head, ...rest] = part;
if (head === ":domain") problem.domain = rest[0];
else if (head === ":objects") problem.objects = typed(rest);
else if (head === ":init") problem.init = rest;
else if (head === ":goal") problem.goal = rest[0] ?? ["and"];
}
return problem;
}
/** The type (category) of an instance name: "can__of__soda.n.01_2" -> "can__of__soda.n.01". */
export const typeOf = (name) => name.replace(/_\d+$/, "");
/** An expression with variables bound (`?x` -> object) and constants' leading `?` dropped. */
function bind(expr, vars, objects) {
if (typeof expr === "string") {
if (expr in vars) return vars[expr];
if (expr.startsWith("?") && expr.slice(1) in objects) return expr.slice(1);
return expr;
}
if (expr[0] === "forall" || expr[0] === "exists") {
// a quantifier's own variable stays a variable inside it
const [, decl, body] = expr;
const own = new Set(Object.keys(typed(decl)));
const inner = Object.fromEntries(Object.entries(vars).filter(([k]) => !own.has(k)));
return [expr[0], decl, bind(body, inner, objects)];
}
return expr.map((x) => bind(x, vars, objects));
}
/** The objects of a quantifier's type. */
function domainOf(decl, objects) {
const [[variable, type]] = Object.entries(typed(decl));
return { variable, members: Object.keys(objects).filter((n) => objects[n] === type || typeOf(n) === type) };
}
/**
* The goal's scored literals: its conjuncts, `and` flattened and `forall` expanded over its objects,
* each a ground expression (a literal, a negated one, or an `or`/`exists` kept whole).
*/
export function groundGoal(problem) {
const out = [];
const visit = (expr) => {
if (expr[0] === "and") for (const x of expr.slice(1)) visit(x);
else if (expr[0] === "forall") {
const { variable, members } = domainOf(expr[1], problem.objects);
for (const m of members) visit(bind(expr[2], { [variable]: m }, problem.objects));
} else out.push(expr);
};
visit(bind(problem.goal, {}, problem.objects));
return out;
}
/**
* Whether a ground expression holds, with `holds(predicate, args)` for the literals; quantifiers range
* over the problem's objects.
*/
export function evaluate(expr, holds, objects) {
const [head, ...rest] = expr;
switch (head) {
case "and":
return rest.every((x) => evaluate(x, holds, objects));
case "or":
return rest.some((x) => evaluate(x, holds, objects));
case "not":
return !evaluate(rest[0], holds, objects);
case "imply":
return !evaluate(rest[0], holds, objects) || evaluate(rest[1], holds, objects);
case "forall":
case "exists": {
const { variable, members } = domainOf(rest[0], objects);
const each = (m) => evaluate(bind(rest[1], { [variable]: m }, objects), holds, objects);
return head === "forall" ? members.every(each) : members.some(each);
}
default:
return holds(head, rest);
}
}
/** An expression written back as BDDL. */
export function show(expr) {
return typeof expr === "string" ? expr : `(${expr.map(show).join(" ")})`;
}
|