ai-planning-val
Version:
Javascript/typescript wrapper for VAL (AI Planning plan validation and evaluation tools from KCL Planning department and the planning community around the ICAPS conference).
166 lines • 8.21 kB
JavaScript
;
/* --------------------------------------------------------------------------------------------
* Copyright (c) Jan Dolejsi. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
* ------------------------------------------------------------------------------------------ */
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ValueSeq = exports.GroundedFunctionValues = void 0;
const process = __importStar(require("child_process"));
const os = __importStar(require("os"));
const pddl_workspace_1 = require("pddl-workspace");
const PlanTimeSeriesParser_1 = require("./PlanTimeSeriesParser");
/**
* Holds graph values for functions grounded from the same lifted function.
*/
class GroundedFunctionValues {
constructor(liftedVariable, values, legend) {
this.liftedVariable = liftedVariable;
this.legend = legend;
this.values = values.map(row => row.map(v => GroundedFunctionValues.undefinedToNull(v)));
}
static undefinedToNull(value) {
return value === undefined ? null : value;
}
adjustForStepFunctions() {
var _a;
const adjustedValues = [];
let previousTime = -1;
for (let index = 0; index < this.values.length; index++) {
let time = (_a = this.values[index][0]) !== null && _a !== void 0 ? _a : Number.NaN;
if (time && previousTime > time) {
time = previousTime + GroundedFunctionValues.TIME_DELTA;
}
else if (previousTime === time) {
time += GroundedFunctionValues.TIME_DELTA;
}
adjustedValues.push([time].concat(this.values[index].slice(1).map(v => v === null ? Number.NaN : v)));
previousTime = time;
}
return new GroundedFunctionValues(this.liftedVariable, adjustedValues, this.legend);
}
toCsv() {
const header1 = ['', this.liftedVariable.getFullName()];
const header2 = ['time', ...this.legend];
return [header1, header2, ...this.values]
.map(row => row.join(', '))
.join(os.EOL);
}
}
exports.GroundedFunctionValues = GroundedFunctionValues;
GroundedFunctionValues.TIME_DELTA = 1e-10;
/** Wrapper for the ValueSeq executable. */
class ValueSeq {
constructor(domainFile, problemFile, planFile, options) {
this.domainFile = domainFile;
this.problemFile = problemFile;
this.planFile = planFile;
this.options = options;
}
evaluateForLifted(liftedFunction, groundedFunctions) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
const csv = yield this.callValueSeq(groundedFunctions);
if (csv === undefined) {
return undefined;
}
const parser = new PlanTimeSeriesParser_1.PlanTimeSeriesParser(groundedFunctions, csv, (_a = this.options) === null || _a === void 0 ? void 0 : _a.adjustDuplicatedTimeStamps);
const functionsValuesValues = parser.getFunctionData(liftedFunction);
if (functionsValuesValues.isConstant()) {
return undefined;
} // it is not interesting...
return new GroundedFunctionValues(liftedFunction, functionsValuesValues.values, functionsValuesValues.legend);
});
}
evaluate(groundedFunctions) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
const csv = yield this.callValueSeq(groundedFunctions);
if (csv === undefined) {
return new Map();
}
const parser = new PlanTimeSeriesParser_1.PlanTimeSeriesParser(groundedFunctions, csv, (_a = this.options) === null || _a === void 0 ? void 0 : _a.adjustDuplicatedTimeStamps);
const functionValues = groundedFunctions.map(functionName => parser.getFunctionValues(functionName));
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return new Map(functionValues.filter(fv => !!fv).map(fv => [fv.variable.getFullName(), fv]));
});
}
evaluateMetric() {
var _a;
return __awaiter(this, void 0, void 0, function* () {
const csv = yield this.callValueSeq([new pddl_workspace_1.Variable('$metrics')]);
if (csv === undefined) {
return new Map();
}
const parser = new PlanTimeSeriesParser_1.PlanTimeSeriesParser([], csv, (_a = this.options) === null || _a === void 0 ? void 0 : _a.adjustDuplicatedTimeStamps);
const functionValues = parser.functions.map(functionName => parser.getFunctionValues(functionName));
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return new Map(functionValues.filter(fv => !!fv).map(fv => [fv.variable.getFullName(), fv]));
});
}
callValueSeq(groundedFunctions) {
var _a, _b, _c, _d;
return __awaiter(this, void 0, void 0, function* () {
if (groundedFunctions.length === 0) {
return undefined;
}
const functions = groundedFunctions
.map(f => f.getFullName())
.map(name => name.toLowerCase());
const valueSeqCommand = (_b = (_a = this.options) === null || _a === void 0 ? void 0 : _a.valueSeqPath) !== null && _b !== void 0 ? _b : 'ValueSeq';
const valueSeqArgs = ["-T", this.domainFile, this.problemFile, this.planFile].concat(functions);
if ((_c = this.options) === null || _c === void 0 ? void 0 : _c.verbose) {
console.log(valueSeqCommand + ' ' + valueSeqArgs.map(a => a.includes(' ') ? `"${a}"` : a).join(' '));
}
const csv = yield new Promise((resolve, reject) => {
process.execFile(valueSeqCommand, valueSeqArgs, { encoding: 'utf8' }, (error, stdout, stderr) => {
if (error) {
reject(error);
return;
}
if (stderr) {
console.warn(stderr);
}
resolve(stdout);
});
});
if ((_d = this.options) === null || _d === void 0 ? void 0 : _d.verbose) {
console.log(csv);
}
return csv;
});
}
}
exports.ValueSeq = ValueSeq;
//# sourceMappingURL=ValueSeq.js.map