Skip to content

Instantly share code, notes, and snippets.

@primaryobjects
Created February 24, 2015 03:15
Show Gist options
  • Save primaryobjects/16ba760a5359e9ebee89 to your computer and use it in GitHub Desktop.
Save primaryobjects/16ba760a5359e9ebee89 to your computer and use it in GitHub Desktop.
Planning graph using the strips node.js library.
var strips = require('strips');
var util = require('util');
// Load the domain and problem.
strips.load('./domain-cake.pddl', './problem-cake.pddl', function(domain, problem) {
var S = [];
// S0 - 1 node per literal in initial state.
for (var i in problem.states[0].actions) {
var literal = problem.states[0].actions[i];
S.push(literal);
// Mark this literal as discovered, so we don't include it again.
literalHash[literal.operation + '-' + literal.action] = 1;
}
planningGraph = graph(S, domain);
// Display planning graph.
//console.log(util.inspect(planningGraph, true, 100, true));
printLevel(planningGraph, 2);
});
var literalHash = {};
function graph(S, domain) {
// A0 - link actions where each literal is part of precondition.
for (var i in S) {
var literal = S[i];
S[i].children = [];
// Find actions where this literal is part of the precondition.
for (var j in domain.actions) {
var action = domain.actions[j];
var children = [];
for (var k in action.precondition) {
// Does this precondition action name match our literal?
if (action.precondition[k].action == literal.action && action.precondition[k].operation == literal.operation && action.precondition[k].parameters.length == literal.parameters.length) {
// Match!
// S1 - add literals from each action's effect.
action.children = [];
for (var k in action.effect) {
var effectLiteral = action.effect[k];
// Is this a unique literal?
if (!literalHash[effectLiteral.operation + '-' + effectLiteral.action]) {
// Found a new literal!
effectLiteral = graph([effectLiteral], domain);
action.children.push(effectLiteral[0]);
}
}
S[i].children.push(action);
}
}
}
}
return S;
}
function printLevel(arr, level, index) {
index = index || 0;
//console.log(index);
for (var i in arr) {
if (index < level && arr[i].children) {
printLevel(arr[i].children, level, index + 1);
}
console.log(toString(arr[i]));
}
}
function toString(literalOrAction) {
var result = '';
if (literalOrAction.length > 0) {
literalOrAction = literalOrAction[0];
}
if (literalOrAction.effect) {
// Action.
result = literalOrAction.action;
}
else {
// Literal.
result = literalOrAction.operation + ' ' + literalOrAction.action;
}
return result;
}
;; domain-cake.pddl
;; The "Have Cake and Eat it Too" Problem
(define (domain have-cake-and-eat-it-too)
(:requirements :strips)
(:action eat
:parameters (?cake)
:precondition (and (have ?cake))
:effect (and (eaten ?cake) not (have ?cake)))
(:action bake
:parameters (?cake)
:precondition (not (have ?cake))
:effect (and (have ?cake)))
;; problem-cake.pddl
;; The "Have Cake and Eat it Too" Problem
(define (problem have-cake-eat-cake)
(:domain have-cake-and-eat-it-too)
(:init (and (have cake) not (eaten cake)))
(:goal (and (have cake) (eaten cake)))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment