All files / lib/evolution adaptive_engine.ts

0% Statements 0/41
0% Branches 0/1
0% Functions 0/1
0% Lines 0/41

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53                                                                                                         
/**
 * Phase 6.3 — Adaptive Evolution Engine
 * يدمج A/B Simulation مع تحديث السجلات وتوليد تطور حقيقي.
 */
 
import fs from 'node:fs';
import { LongTermMemory } from '../memory/long_term_memory';
import { loadRegistry, saveRegistry } from '../policy/registry';
import { runABSimulation } from './ab_simulator';
import { EvolutionConfig } from '../../config/evolution.config';
 
/**
 * تشغيل دورة التطور التلقائي.
 */
export function runAdaptiveEvolution() {
  const memory = LongTermMemory.getState();
  const metrics = memory.metrics || { evolutionScore: 0 };
  const registry = loadRegistry();
  const evolutionLog = [];
 
  for (const policy of registry) {
    const sims = runABSimulation(policy, metrics);
    const accepted = sims.filter((s) => s.accepted);
 
    if (accepted.length > 0) {
      // نختار الأفضل
      const best = accepted.sort((a, b) => b.avgScore - a.avgScore)[0];
      policy.weight = best.newWeight;
      policy.trend = '↑';
      policy.lastUpdate = new Date().toISOString();
    }
 
    evolutionLog.push({
      policyId: policy.policyId,
      tested: sims.length,
      accepted: accepted.length,
      newWeight: policy.weight,
    });
  }
 
  saveRegistry(registry);
  persistLog(evolutionLog);
  return evolutionLog;
}
 
/**
 * حفظ سجل التطور.
 */
function persistLog(log) {
  fs.mkdirSync('./storage', { recursive: true });
  fs.writeFileSync(EvolutionConfig.evolutionLog, JSON.stringify(log, null, 2));
}