File size: 3,385 Bytes
d72771b
 
 
 
 
e0daa77
 
d72771b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e0daa77
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
// The kitchen's household tasks (src/tasks/), done by the scripted demonstrator and scored as the BEHAVIOR
// Challenge scores them: for each task and instance, the share of the goal's conditions that hold at the
// end (Q, partial credit), whether all do, and the efficiency measures (simulated time, base and hand
// distance).
//
// usage: node tools/eval_tasks.mjs [instances=5] [task ...]   (VERBOSE=1 prints what the demonstrator did;
//        ONE_HAND=1 has it carry one object at a time)
import { readFileSync } from "node:fs";
import { join } from "node:path";
import { ASSETS, loadScene } from "./node_env.mjs";
import { Episode, Household } from "../src/tasks/household.js";
import { solve } from "../src/tasks/solver.js";
import { runSkill } from "../src/robots/skills.js";

const instances = Number(process.argv[2] ?? 5);
const only = process.argv.slice(3);
const dir = join(ASSETS, "environments", "kitchen", "tasks");
const registry = JSON.parse(readFileSync(join(dir, "index.json"), "utf8"));
const problems = Object.fromEntries(registry.tasks.map((t) => [t.file, readFileSync(join(dir, t.file), "utf8")]));

const { sim, robot } = await loadScene("kitchen");
sim.reset(() => {
  robot.home();
  robot.snapJoints();
});
sim.run(1.0, () => robot.update());
const settled = sim.snapshot();
const household = new Household(sim, robot, registry, problems);

const summary = [];
for (const task of household.tasks) {
  if (only.length && !only.includes(task.id)) continue;
  const runs = [];
  for (let seed = 0; seed < instances; seed++) {
    household.setup(task, seed, settled);
    const episode = new Episode(household, task, seed);
    const log = [];
    const wall = performance.now();
    runSkill(sim, robot, solve(household, episode, { bothHands: !process.env.ONE_HAND, log: (m) => log.push(`  ${episode.time.toFixed(1)} s: ${m}`) }), {
      maxSeconds: task.timeLimit + 30,
      onTick: () => {
        household.update();
        episode.tick(sim.controlDt);
      },
    });
    if (!episode.outcome) episode.evaluate();
    const r = episode.report();
    runs.push(r);
    const missed = r.goals.filter((g) => !g.done).map((g) => g.goal);
    console.log(`${task.id} #${seed}: Q ${r.q.toFixed(2)} (${r.score})${r.success ? " success" : ""}, ${r.time_s} s, base ${r.base_distance_m} m, hands ${r.hand_distance_m} m (${((performance.now() - wall) / 1000).toFixed(0)} s wall)${missed.length ? ` | missed: ${missed.join("; ")}` : ""}`);
    if (process.env.VERBOSE || !r.success) console.log(log.join("\n"));
  }
  const mean = (f) => runs.reduce((a, r) => a + f(r), 0) / runs.length;
  summary.push({ task: task.id, q: mean((r) => r.q), success: mean((r) => (r.success ? 1 : 0)), time: mean((r) => r.time_s), base: mean((r) => r.base_distance_m), hands: mean((r) => r.hand_distance_m), limit: task.timeLimit });
}
console.log("\ntask                      Q     success  time    base    hands   (limit)");
for (const s of summary) console.log(`${s.task.padEnd(25)} ${s.q.toFixed(2)}  ${(s.success * 100).toFixed(0).padStart(4)}%   ${s.time.toFixed(0).padStart(4)} s  ${s.base.toFixed(1).padStart(4)} m  ${s.hands.toFixed(1).padStart(4)} m  (${s.limit} s)`);
const all = (f) => summary.reduce((a, s) => a + f(s), 0) / summary.length;
console.log(`${"all".padEnd(25)} ${all((s) => s.q).toFixed(2)}  ${(all((s) => s.success) * 100).toFixed(0).padStart(4)}%`);