Contextualized Path-Weighting Memory Trace Demo
});
if (!nextNode) break;
current = nextNode;
}
for (let i = 0; i < path.length - 1; i++) {
const pair = [path[i], path[i+1]].sort().join('-');
memory.set(pair, (memory.get(pair) || 0) + 1);
}
return path;
}
function drawPath(path) {
const canvas = document.getElementById('pathCanvas');
const ctx = canvas.getContext('2d');
ctx.clearRect(0, 0, canvas.width, canvas.height);
graph.nodes.forEach(node => {
ctx.beginPath();
ctx.arc(node.x, node.y, 10, 0, 2 * Math.PI);
ctx.fillStyle = '#333';
ctx.fill();
ctx.fillStyle = 'white';
ctx.fillText(node.id, node.x - 5, node.y + 5);
});
graph.edges.forEach(edge => {
const from = graph.nodes.find(n => n.id === edge.from);
const to = graph.nodes.find(n => n.id === edge.to);
ctx.beginPath();
ctx.moveTo(from.x, from.y);
ctx.lineTo(to.x, to.y);
ctx.strokeStyle = '#000';
ctx.stroke();
const midX = (from.x + to.x) / 2;
const midY = (from.y + to.y) / 2;
ctx.fillStyle = '#000';
ctx.fillText(edge.baseCost, midX + 5, midY - 5);
});
if (path.length > 1) {
ctx.strokeStyle = 'red';
ctx.lineWidth = 2;
ctx.beginPath();
ctx.moveTo(graph.nodes.find(n => n.id === path[0]).x, graph.nodes.find(n => n.id === path[0]).y);
for (let i = 1; i < path.length; i++) {
ctx.lineTo(graph.nodes.find(n => n.id === path[i]).x, graph.nodes.find(n => n.id === path[i]).y);
}
ctx.stroke();
}
}
const timelineContainer = document.getElementById('timelineContainer');
function recordEvent(event) {
const div = document.createElement('div');
div.className = 'event';
div.textContent = `Path finding for ${event} -> Cost reduced by memory: ${Object.keys(memory).length > 0 ? 'Yes' : 'No'}`;
timelineContainer.prepend(div);
}
setInterval(() => {
const start = ['A', 'B', 'C'][Math.floor(Math.random() * 3)];
const end = ['C', 'B', 'A'][Math.floor(Math.random() * 3)];
const path = findPath(start, end);
drawPath(path);
recordEvent(`${start} -> ${end}`);
}, 2000);