/// <summary> /// Set pass experiences to the agent /// </summary> /// <param name="experiences"></param> public void SetExperiences(IEnumerable <Experience> experiences) { foreach (var e in experiences) { MemoryReplay.Enqueue(e); } }
/// <summary> /// Saves the a experience in memory replay. /// </summary> /// <param name="state">The st.</param> /// <param name="action">At.</param> /// <param name="reward">The rt_plus_1.</param> /// <param name="nextState">The st_plus_1.</param> private void AddExperience(State from, ActionType action, double reward, State to) { var experience = new Experience() { AgentId = Parameters.Id, From = from, Action = CurrentAction, Reward = reward, To = to }; MemoryReplay.Enqueue(experience); }
/// <summary> /// Generates the a mini batch of randoms sample for train and update Q-network wegihts. /// </summary> /// <returns></returns> private IList <Experience> GenerateMiniBatch() { var experiences = new List <Experience>(); if (MemoryReplay.Count <= Parameters.MiniBatchSize) { experiences = MemoryReplay.ToList(); } else { var indexes = Enumerable.Range(0, MemoryReplay.Count - 1).OrderBy(x => RandomGenerator.Next()); experiences = MemoryReplay.Where((e, i) => indexes.Contains(i)).ToList(); } using (var ctx = new DeepQStockContext()) { foreach (var experience in experiences) { if (experience.Id > 0 && !ctx.IsAttached(experience)) { ctx.Experiences.Attach(experience); } if (experience.From == null) { ctx.Entry(experience).Reference(e => e.From).Load(); } if (experience.To == null) { ctx.Entry(experience).Reference(e => e.To).Load(); } } } return(experiences.ToList()); }