thoughtmcp
Version:
AI that thinks more like humans do - MCP server with human-like cognitive architecture for enhanced reasoning, memory, and self-monitoring
301 lines • 11.4 kB
JavaScript
/**
* Stochastic Neural Processing Implementation
*
* Implements biological-like neural variability and probabilistic processing:
* - Gaussian noise addition to simulate neural variability
* - Stochastic resonance for weak signal enhancement
* - Probabilistic decision sampling mechanisms
* - Temperature-controlled randomness
*/
/**
* StochasticNeuralProcessor implements biological-like neural processing
* with noise, variability, and probabilistic decision making
*/
export class StochasticNeuralProcessor {
noise_level = 0.1; // Default biological noise level
temperature = 1.0; // Controls randomness in sampling
resonance_threshold = 0.3; // Threshold for stochastic resonance
max_noise_level = 0.5; // Maximum allowed noise
status = {
name: "StochasticNeuralProcessor",
initialized: false,
active: false,
last_activity: 0,
};
// Random number generator for reproducible results in tests
rng = Math.random;
/**
* Initialize the stochastic neural processor with configuration
*/
async initialize(config) {
try {
this.noise_level = Math.min(this.max_noise_level, config?.noise_level || 0.1);
this.temperature = config?.temperature || 1.0;
this.resonance_threshold = config?.resonance_threshold || 0.3;
// Set up random number generator if provided (for testing)
if (config?.rng && typeof config.rng === "function") {
this.rng = config.rng;
}
this.status.initialized = true;
this.status.active = true;
this.status.last_activity = Date.now();
}
catch (error) {
this.status.error = `Initialization failed: ${error}`;
throw error;
}
}
/**
* Main processing method - applies stochastic neural processing
*/
async process(input) {
if (!this.status.initialized) {
throw new Error("StochasticNeuralProcessor not initialized");
}
this.status.last_activity = Date.now();
try {
const original_strength = input.strength;
// Phase 1: Add biological noise
const noisy_signal = this.addNoise(input.values, this.noise_level);
// Phase 2: Apply stochastic resonance if signal is weak
let enhanced_signal = noisy_signal;
let enhancement_applied = false;
let resonance_detected = false;
if (input.strength < this.resonance_threshold) {
enhanced_signal = this.applyStochasticResonance(input.values, this.noise_level);
enhancement_applied = true;
resonance_detected = this.detectResonance(input.values, enhanced_signal);
}
// Phase 3: Compute final signal strength
const final_strength = this.computeSignalStrength(enhanced_signal);
// Phase 4: Apply probabilistic sampling if needed
const sampled_signal = this.applySampling(enhanced_signal);
return {
processed_signal: sampled_signal,
noise_level: this.noise_level,
enhancement_applied,
sampling_temperature: this.temperature,
processing_metadata: {
original_strength,
final_strength,
noise_contribution: this.computeNoiseContribution(input.values, enhanced_signal),
resonance_detected,
},
};
}
catch (error) {
this.status.error = `Processing failed: ${error}`;
throw new Error(`Processing failed: ${error}`);
}
}
/**
* Add Gaussian noise to signal - simulates biological neural variability
*/
addNoise(signal, noiseLevel) {
if (noiseLevel <= 0)
return [...signal];
return signal.map((value) => {
const noise = this.generateGaussianNoise(0, noiseLevel);
return value + noise;
});
}
/**
* Apply stochastic resonance - noise can enhance weak signal detection
*/
applyStochasticResonance(signal, noiseLevel) {
// Stochastic resonance works by adding optimal noise to weak signals
const optimal_noise = this.computeOptimalNoise(signal, noiseLevel);
const noisy_signal = this.addNoise(signal, optimal_noise);
// Apply threshold detection with noise
return this.thresholdDetection(noisy_signal, signal);
}
/**
* Sample from probability distribution - implements probabilistic decisions
*/
sampleFromDistribution(distribution) {
if (distribution.length === 0) {
throw new Error("Cannot sample from empty distribution");
}
// Normalize distribution
const sum = distribution.reduce((acc, val) => acc + Math.abs(val), 0);
if (sum === 0) {
// Uniform sampling if all values are zero
return Math.floor(this.rng() * distribution.length);
}
const normalized = distribution.map((val) => Math.abs(val) / sum);
// Apply temperature scaling for controlled randomness
const temperature_scaled = this.applyTemperatureScaling(normalized, this.temperature);
// Sample using cumulative distribution
const random_value = this.rng();
let cumulative = 0;
for (let i = 0; i < temperature_scaled.length; i++) {
cumulative += temperature_scaled[i];
if (random_value <= cumulative) {
return i;
}
}
// Fallback to last index
return distribution.length - 1;
}
/**
* Adjust temperature parameter for randomness control
*/
adjustTemperature(temperature) {
this.temperature = Math.max(0.1, Math.min(10.0, temperature));
this.status.last_activity = Date.now();
}
/**
* Reset processor state
*/
reset() {
this.noise_level = 0.1;
this.temperature = 1.0;
this.status.last_activity = Date.now();
}
/**
* Get current component status
*/
getStatus() {
return { ...this.status };
}
// Private helper methods
/**
* Generate Gaussian noise using Box-Muller transform
*/
generateGaussianNoise(mean = 0, stddev = 1) {
// Box-Muller transform for Gaussian distribution
let u1 = this.rng();
const u2 = this.rng();
// Ensure u1 is not zero to avoid log(0)
while (u1 === 0) {
u1 = this.rng();
}
const z0 = Math.sqrt(-2 * Math.log(u1)) * Math.cos(2 * Math.PI * u2);
return mean + stddev * z0;
}
/**
* Compute optimal noise level for stochastic resonance
*/
computeOptimalNoise(signal, baseNoise) {
const signal_strength = this.computeSignalStrength(signal);
// Optimal noise is typically proportional to signal strength
// but with a minimum level for very weak signals
const optimal_ratio = 0.3; // Empirically determined
const min_noise = 0.05;
return Math.max(min_noise, signal_strength * optimal_ratio, baseNoise);
}
/**
* Apply threshold detection with noise enhancement
*/
thresholdDetection(noisy_signal, original_signal) {
const threshold = this.computeAdaptiveThreshold(original_signal);
return noisy_signal.map((value, index) => {
// If noisy signal crosses threshold but original didn't,
// this represents stochastic resonance enhancement
const original_value = original_signal[index];
const crosses_threshold = Math.abs(value) > threshold;
const original_crosses = Math.abs(original_value) > threshold;
if (crosses_threshold && !original_crosses) {
// Stochastic resonance detected - enhance the signal
return original_value + Math.sign(value) * (threshold * 0.5);
}
return value;
});
}
/**
* Compute adaptive threshold based on signal characteristics
*/
computeAdaptiveThreshold(signal) {
if (signal.length === 0)
return 0.1;
const mean = signal.reduce((sum, val) => sum + Math.abs(val), 0) / signal.length;
const variance = signal.reduce((sum, val) => sum + Math.pow(Math.abs(val) - mean, 2), 0) /
signal.length;
const stddev = Math.sqrt(variance);
// Threshold is typically 2-3 standard deviations above mean
return mean + 2.5 * stddev;
}
/**
* Detect if stochastic resonance occurred
*/
detectResonance(original, enhanced) {
const original_strength = this.computeSignalStrength(original);
const enhanced_strength = this.computeSignalStrength(enhanced);
// Resonance detected if enhanced signal is significantly stronger
const enhancement_ratio = enhanced_strength / (original_strength + 1e-10);
return enhancement_ratio > 1.2; // 20% improvement threshold
}
/**
* Compute signal strength (RMS)
*/
computeSignalStrength(signal) {
if (signal.length === 0)
return 0;
const sum_squares = signal.reduce((sum, val) => sum + val * val, 0);
return Math.sqrt(sum_squares / signal.length);
}
/**
* Compute noise contribution to signal
*/
computeNoiseContribution(original, processed) {
if (original.length !== processed.length)
return 0;
const noise_signal = processed.map((val, i) => val - original[i]);
return this.computeSignalStrength(noise_signal);
}
/**
* Apply temperature scaling to probability distribution
*/
applyTemperatureScaling(probabilities, temperature) {
if (temperature === 1.0)
return probabilities;
// Apply temperature scaling: p_i = exp(log(p_i) / T)
const scaled = probabilities.map((p) => {
if (p <= 0)
return 1e-10; // Avoid log(0)
return Math.exp(Math.log(p) / temperature);
});
// Renormalize
const sum = scaled.reduce((acc, val) => acc + val, 0);
return scaled.map((val) => val / sum);
}
/**
* Apply probabilistic sampling to signal values
*/
applySampling(signal) {
if (this.temperature === 1.0)
return signal;
// For each signal value, add temperature-controlled randomness
return signal.map((value) => {
const noise_scale = this.temperature * 0.1;
const sampling_noise = this.generateGaussianNoise(0, noise_scale);
return value + sampling_noise;
});
}
/**
* Set random number generator (for testing)
*/
setRandomNumberGenerator(rng) {
this.rng = rng;
}
/**
* Get current noise level
*/
getNoiseLevel() {
return this.noise_level;
}
/**
* Get current temperature
*/
getTemperature() {
return this.temperature;
}
/**
* Set noise level with bounds checking
*/
setNoiseLevel(level) {
this.noise_level = Math.max(0, Math.min(this.max_noise_level, level));
this.status.last_activity = Date.now();
}
}
//# sourceMappingURL=StochasticNeuralProcessor.js.map