function init(nodes, rootNode) {
rootNode = rootNode || nodes[0];
const children = getChildren(nodes, rootNode);
const parents = rootNode.parents.length || 1;
const cost = rootNode.done ? 0 : rootNode.cost || 0;
rootNode.costPrime =
children.reduce((cost, node) => cost + init(nodes, node), cost) / parents;
if (children.length === 0) {
computeReward(nodes, rootNode);
}
if (rootNode === nodes[0]) {
let nextNodeValue = -Infinity;
let nextNode = null;
for (let node of nodes) {
if (getValue(node) > getValue(nextNode) && !node.done) {
if (nextNode) {
nextNode.next = false;
}
nextNode = node;
node.next = true;
} else {
node.next = false;
}
}
}
return rootNode.costPrime;
}