UNPKG

@ignitionai/backend-tfjs

Version:

TensorFlow.js backend for IgnitionAI - browser-based reinforcement learning framework

27 lines (26 loc) 694 B
export class ReplayBuffer { buffer = []; capacity; constructor(capacity = 10000) { this.capacity = capacity; } add(exp) { if (this.buffer.length >= this.capacity) { // delete older element this.buffer.shift(); } this.buffer.push(exp); } sample(batchSize) { const sampled = []; const bufferLength = this.buffer.length; for (let i = 0; i < Math.min(batchSize, bufferLength); i++) { const idx = Math.floor(Math.random() * bufferLength); sampled.push(this.buffer[idx]); } return sampled; } size() { return this.buffer.length; } }