|
| 1 | +import { Neuroplasticity } from './Neuroplasticity'; |
| 2 | +import { NeuralCircuit } from '../network/NeuralCircuit'; |
| 3 | +import { CorticalNeuron } from '../neurons/CorticalNeuron'; |
| 4 | + |
| 5 | +describe('Neuroplasticity - Self-Healing and Optimization', () => { |
| 6 | + let plasticity: Neuroplasticity; |
| 7 | + let circuit: NeuralCircuit; |
| 8 | + let neuronA: CorticalNeuron; |
| 9 | + let neuronB: CorticalNeuron; |
| 10 | + let neuronC: CorticalNeuron; |
| 11 | + |
| 12 | + beforeEach(() => { |
| 13 | + circuit = new NeuralCircuit({ id: 'circuit-1' }); |
| 14 | + plasticity = new Neuroplasticity({ circuit, pruningThreshold: 0.1 }); |
| 15 | + |
| 16 | + neuronA = new CorticalNeuron({ |
| 17 | + id: 'neuron-a', |
| 18 | + threshold: 0.5, |
| 19 | + maxMemorySize: 10, |
| 20 | + }); |
| 21 | + |
| 22 | + neuronB = new CorticalNeuron({ |
| 23 | + id: 'neuron-b', |
| 24 | + threshold: 0.5, |
| 25 | + maxMemorySize: 10, |
| 26 | + }); |
| 27 | + |
| 28 | + neuronC = new CorticalNeuron({ |
| 29 | + id: 'neuron-c', |
| 30 | + threshold: 0.5, |
| 31 | + maxMemorySize: 10, |
| 32 | + }); |
| 33 | + |
| 34 | + circuit.addNeuron(neuronA); |
| 35 | + circuit.addNeuron(neuronB); |
| 36 | + circuit.addNeuron(neuronC); |
| 37 | + }); |
| 38 | + |
| 39 | + afterEach(async () => { |
| 40 | + await circuit.shutdown(); |
| 41 | + }); |
| 42 | + |
| 43 | + describe('synaptic pruning', () => { |
| 44 | + it('should identify weak connections for pruning', () => { |
| 45 | + circuit.connect(neuronA.id, neuronB.id, { |
| 46 | + weight: 0.05, |
| 47 | + type: 'excitatory', |
| 48 | + speed: 'fast', |
| 49 | + }); |
| 50 | + |
| 51 | + circuit.connect(neuronB.id, neuronC.id, { |
| 52 | + weight: 0.8, |
| 53 | + type: 'excitatory', |
| 54 | + speed: 'fast', |
| 55 | + }); |
| 56 | + |
| 57 | + const weakConnections = plasticity.identifyWeakConnections(); |
| 58 | + |
| 59 | + expect(weakConnections).toHaveLength(1); |
| 60 | + expect(weakConnections[0]?.weight).toBe(0.05); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should prune weak connections', () => { |
| 64 | + circuit.connect(neuronA.id, neuronB.id, { |
| 65 | + weight: 0.05, |
| 66 | + type: 'excitatory', |
| 67 | + speed: 'fast', |
| 68 | + }); |
| 69 | + |
| 70 | + const beforePrune = circuit.getOutgoingConnections(neuronA.id); |
| 71 | + expect(beforePrune).toHaveLength(1); |
| 72 | + |
| 73 | + plasticity.pruneWeakConnections(); |
| 74 | + |
| 75 | + const afterPrune = circuit.getOutgoingConnections(neuronA.id); |
| 76 | + expect(afterPrune).toHaveLength(0); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should strengthen frequently used connections', () => { |
| 80 | + const conn = circuit.connect(neuronA.id, neuronB.id, { |
| 81 | + weight: 0.5, |
| 82 | + type: 'excitatory', |
| 83 | + speed: 'fast', |
| 84 | + }); |
| 85 | + |
| 86 | + // Record usage |
| 87 | + plasticity.recordConnectionUsage(conn.id); |
| 88 | + plasticity.recordConnectionUsage(conn.id); |
| 89 | + plasticity.recordConnectionUsage(conn.id); |
| 90 | + plasticity.recordConnectionUsage(conn.id); |
| 91 | + |
| 92 | + plasticity.strengthenHotPaths(); |
| 93 | + |
| 94 | + const strengthened = circuit.getConnection(conn.id); |
| 95 | + expect(strengthened?.weight).toBeGreaterThan(0.5); |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + describe('neural rewiring', () => { |
| 100 | + it('should detect failed neurons', async () => { |
| 101 | + await circuit.activate(); |
| 102 | + await neuronB.deactivate(); |
| 103 | + |
| 104 | + const failed = plasticity.detectFailedNeurons(); |
| 105 | + |
| 106 | + expect(failed).toHaveLength(1); |
| 107 | + expect(failed[0]?.id).toBe(neuronB.id); |
| 108 | + }); |
| 109 | + |
| 110 | + it('should create bypass connections around failed neurons', async () => { |
| 111 | + circuit.connect(neuronA.id, neuronB.id, { |
| 112 | + weight: 0.8, |
| 113 | + type: 'excitatory', |
| 114 | + speed: 'fast', |
| 115 | + }); |
| 116 | + |
| 117 | + circuit.connect(neuronB.id, neuronC.id, { |
| 118 | + weight: 0.8, |
| 119 | + type: 'excitatory', |
| 120 | + speed: 'fast', |
| 121 | + }); |
| 122 | + |
| 123 | + await circuit.activate(); |
| 124 | + await neuronB.deactivate(); |
| 125 | + |
| 126 | + plasticity.rewireAroundFailure(neuronB.id); |
| 127 | + |
| 128 | + // Should have created direct connection A->C |
| 129 | + const bypassPath = plasticity.findPathBetween(neuronA.id, neuronC.id); |
| 130 | + expect(bypassPath).toBeDefined(); |
| 131 | + expect(bypassPath).not.toContain(neuronB.id); |
| 132 | + }); |
| 133 | + |
| 134 | + it('should strengthen connections through training', () => { |
| 135 | + const conn = circuit.connect(neuronA.id, neuronB.id, { |
| 136 | + weight: 0.5, |
| 137 | + type: 'excitatory', |
| 138 | + speed: 'fast', |
| 139 | + }); |
| 140 | + |
| 141 | + plasticity.trainConnection(conn.id, 10); |
| 142 | + |
| 143 | + const trained = circuit.getConnection(conn.id); |
| 144 | + expect(trained?.weight).toBeGreaterThan(0.5); |
| 145 | + }); |
| 146 | + }); |
| 147 | + |
| 148 | + describe('network health', () => { |
| 149 | + it('should assess network health', () => { |
| 150 | + const health = plasticity.assessNetworkHealth(); |
| 151 | + |
| 152 | + expect(health.score).toBeGreaterThanOrEqual(0); |
| 153 | + expect(health.score).toBeLessThanOrEqual(1); |
| 154 | + }); |
| 155 | + |
| 156 | + it('should provide plasticity statistics', () => { |
| 157 | + // Create fresh circuit for this test |
| 158 | + const freshCircuit = new NeuralCircuit({ id: 'fresh' }); |
| 159 | + const freshPlasticity = new Neuroplasticity({ circuit: freshCircuit }); |
| 160 | + |
| 161 | + const nA = new CorticalNeuron({ id: 'na', threshold: 0.5, maxMemorySize: 10 }); |
| 162 | + const nB = new CorticalNeuron({ id: 'nb', threshold: 0.5, maxMemorySize: 10 }); |
| 163 | + freshCircuit.addNeuron(nA); |
| 164 | + freshCircuit.addNeuron(nB); |
| 165 | + |
| 166 | + freshCircuit.connect(nA.id, nB.id, { |
| 167 | + weight: 0.5, |
| 168 | + type: 'excitatory', |
| 169 | + speed: 'fast', |
| 170 | + }); |
| 171 | + |
| 172 | + const stats = freshPlasticity.getStatistics(); |
| 173 | + |
| 174 | + expect(stats.totalConnections).toBe(1); |
| 175 | + expect(stats.prunedConnections).toBe(0); |
| 176 | + expect(stats.rewiresPerformed).toBe(0); |
| 177 | + }); |
| 178 | + }); |
| 179 | +}); |
0 commit comments