122 lines
3.5 KiB
TypeScript
122 lines
3.5 KiB
TypeScript
/**
|
|
* Temporal-Lead-Solver Integration
|
|
*
|
|
* Time-series graph database with temporal indices
|
|
* Integration with temporal-lead-solver package
|
|
*
|
|
* Optimized for:
|
|
* - Temporal causality detection
|
|
* - Time-series pattern matching
|
|
* - Lead-lag relationships
|
|
*/
|
|
|
|
import { createUnifiedDatabase } from '../../src/db-unified.js';
|
|
import { ReflexionMemory } from '../../src/controllers/ReflexionMemory.js';
|
|
import { CausalMemoryGraph } from '../../src/controllers/CausalMemoryGraph.js';
|
|
import { EmbeddingService } from '../../src/controllers/EmbeddingService.js';
|
|
import * as path from 'path';
|
|
|
|
export default {
|
|
description: 'Temporal-lead solver with time-series graph database',
|
|
|
|
async run(config: any) {
|
|
const { verbosity = 2, timeSteps = 20 } = config;
|
|
|
|
if (verbosity >= 2) {
|
|
console.log(` ⏰ Initializing Temporal-Lead Solver (T=${timeSteps})`);
|
|
}
|
|
|
|
// Initialize temporal graph database
|
|
const embedder = new EmbeddingService({
|
|
model: 'Xenova/all-MiniLM-L6-v2',
|
|
dimension: 384,
|
|
provider: 'transformers'
|
|
});
|
|
await embedder.initialize();
|
|
|
|
const db = await createUnifiedDatabase(
|
|
path.join(process.cwd(), 'simulation', 'data', 'advanced', 'temporal.graph'),
|
|
embedder,
|
|
{ forceMode: 'graph' }
|
|
);
|
|
|
|
const reflexion = new ReflexionMemory(
|
|
db.getGraphDatabase() as any,
|
|
embedder,
|
|
undefined,
|
|
undefined,
|
|
db.getGraphDatabase() as any
|
|
);
|
|
|
|
const causal = new CausalMemoryGraph(
|
|
db.getGraphDatabase() as any,
|
|
db.getGraphDatabase() as any
|
|
);
|
|
|
|
const results = {
|
|
timeSeriesPoints: 0,
|
|
leadLagPairs: 0,
|
|
temporalCausalEdges: 0,
|
|
avgLagTime: 0,
|
|
totalTime: 0
|
|
};
|
|
|
|
const startTime = performance.now();
|
|
|
|
// Create time-series events
|
|
const episodeIds: number[] = [];
|
|
for (let t = 0; t < timeSteps; t++) {
|
|
const id = await reflexion.storeEpisode({
|
|
sessionId: 'temporal-series',
|
|
task: `event_at_t${t}`,
|
|
reward: 0.5 + 0.5 * Math.sin(t * 0.3), // Sinusoidal pattern
|
|
success: true,
|
|
input: `timestamp_${t}`,
|
|
output: `value_${(0.5 + 0.5 * Math.sin(t * 0.3)).toFixed(2)}`
|
|
});
|
|
episodeIds.push(id);
|
|
results.timeSeriesPoints++;
|
|
}
|
|
|
|
// Detect lead-lag relationships (event at t leads to event at t+k)
|
|
const lagDuration = 3;
|
|
for (let t = 0; t < timeSteps - lagDuration; t++) {
|
|
const leadId = episodeIds[t];
|
|
const lagId = episodeIds[t + lagDuration];
|
|
|
|
await causal.addCausalEdge({
|
|
fromMemoryId: leadId,
|
|
fromMemoryType: 'episode',
|
|
toMemoryId: lagId,
|
|
toMemoryType: 'episode',
|
|
similarity: 0.85,
|
|
uplift: 0.15,
|
|
confidence: 0.90,
|
|
sampleSize: 100,
|
|
mechanism: `temporal_lead_lag_${lagDuration}`
|
|
});
|
|
|
|
results.leadLagPairs++;
|
|
results.temporalCausalEdges++;
|
|
results.avgLagTime += lagDuration;
|
|
}
|
|
|
|
results.avgLagTime /= results.leadLagPairs || 1;
|
|
|
|
const endTime = performance.now();
|
|
results.totalTime = endTime - startTime;
|
|
|
|
db.close();
|
|
|
|
if (verbosity >= 2) {
|
|
console.log(` 📊 Time-Series Points: ${results.timeSeriesPoints}`);
|
|
console.log(` 📊 Lead-Lag Pairs: ${results.leadLagPairs}`);
|
|
console.log(` 📊 Temporal Causal Edges: ${results.temporalCausalEdges}`);
|
|
console.log(` 📊 Avg Lag Time: ${results.avgLagTime.toFixed(1)} steps`);
|
|
console.log(` ⏱️ Duration: ${results.totalTime.toFixed(2)}ms`);
|
|
}
|
|
|
|
return results;
|
|
}
|
|
};
|