Skip to content

Instantly share code, notes, and snippets.

@ppolsinelli
Created August 19, 2018 17:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ppolsinelli/2adec82d344d5182ab93ed9b94b2bf3b to your computer and use it in GitHub Desktop.
Save ppolsinelli/2adec82d344d5182ab93ed9b94b2bf3b to your computer and use it in GitHub Desktop.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class Memory
{
float decay;
Dictionary<string, float> episodes = new Dictionary<string, float>();
public Memory(float decay)
{
this.decay = decay;
}
public int Count()
{
return episodes.Count;
}
public List<string> AllEpisodes()
{
return episodes.Keys.ToList();
}
public float EpisodeCurrentMemory(string episode)
{
if (!episodes.Keys.Contains(episode))
return 0;
return episodes[episode];
}
public void AddEpisode(string episodeLabel)
{
Dictionary<string, float> episodesRefreshed = new Dictionary<string, float>();
foreach (var episodeInMem in episodes.Keys)
{
float currentMemory = episodes[episodeInMem] - decay;
if (currentMemory > 0)
{
episodesRefreshed[episodeInMem] = currentMemory;
}
}
episodesRefreshed[episodeLabel] = 1;
episodes = episodesRefreshed;
}
public string GetMostFresh(List<string> possibleEpisodes, bool addChosenAsEpisode)
{
if (possibleEpisodes == null || possibleEpisodes.Count == 0)
return null;
float minRequiredToJoin = 1;
foreach (var possibleEpisode in possibleEpisodes)
{
if (!episodes.ContainsKey(possibleEpisode))
{
minRequiredToJoin = 0;
break;
}
minRequiredToJoin = Mathf.Min(minRequiredToJoin, episodes[possibleEpisode]);
}
List<string> candidates = new List<string>();
foreach (var possibleEpisode in possibleEpisodes)
{
if (!episodes.ContainsKey(possibleEpisode))
{
candidates.Add(possibleEpisode);
}
else
{
if (episodes[possibleEpisode] <= minRequiredToJoin)
{
candidates.Add(possibleEpisode);
}
}
}
candidates.Shuffle();
return candidates[0];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment