/// <summary> /// Return a batch of items. /// </summary> /// <param name="random">Specifies the random number generator.</param> /// <param name="nCount">Specifies the number of items to sample.</param> /// <param name="dfBeta">Specifies the degree to use importance weights (0 = no corrections, 1 = full corrections).</param> /// <returns>The prioritized array of items is returned along with the weights and indexes.</returns> public MemoryCollection GetSamples(CryptoRandom random, int nCount, double dfBeta) { int[] rgIdx = getSamplesProportional(random, nCount); double[] rgfWeights = new double[nCount]; double fSum = m_ItSum.sum(); double fMin = m_ItMin.min(); double fPMin = fMin / fSum; double fMaxWeight = (float)Math.Pow(fPMin * m_mem.Count, -dfBeta); MemoryCollection mem = new MemoryCollection(nCount); for (int i = 0; i < rgIdx.Length; i++) { int nIdx = rgIdx[i]; double fItSum = m_ItSum[nIdx]; double fPSample = fItSum / fSum; double fWeight = Math.Pow(fPSample * m_mem.Count, -dfBeta); rgfWeights[i] = fWeight / fMaxWeight; mem.Add(m_mem[nIdx]); } mem.Indexes = rgIdx; mem.Priorities = rgfWeights; return(mem); }
/// <summary> /// Update the priorities of sampled transitions. /// </summary> /// <remarks> /// Sets priority of transitions at index rgIdx[i] in buffer to priorities[i]. /// </remarks> /// <param name="rgSamples">Specifies the list of samples with updated priorities.</param> public void Update(MemoryCollection rgSamples) { int[] rgIdx = rgSamples.Indexes; double[] rgfPriorities = rgSamples.Priorities; if (rgIdx.Length != rgfPriorities.Length) { throw new Exception("The index and priority arrays must have the same length."); } for (int i = 0; i < rgIdx.Length; i++) { int nIdx = rgIdx[i]; double fPriority = rgfPriorities[i]; if (fPriority <= 0) { throw new Exception("The priority at index '" + i.ToString() + "' is zero!"); } if (nIdx < 0 || nIdx >= m_mem.Count) { throw new Exception("The index at index '" + i.ToString() + "' is out of range!"); } double fNewPriority = Math.Pow(fPriority, m_fAlpha); m_ItSum[nIdx] = fNewPriority; m_ItMin[nIdx] = fNewPriority; m_fMaxPriority = Math.Max(m_fMaxPriority, fPriority); } }
/// <summary> /// The constructor. /// </summary> /// <param name="nMax">Specifies the maximum number of items.</param> /// <param name="bPreLoad">Pre-load the data from file.</param> /// <param name="bSaveOnCleanup">Save the memory items on cleanup.</param> /// <param name="strFile">Specifies the name of the file to load from or save to.</param> public FileMemoryCollection(int nMax, bool bPreLoad, bool bSaveOnCleanup, string strFile) { m_mem = new MemoryCollection(nMax); m_strFile = strFile; m_bPreLoaded = bPreLoad; m_bSaveOnCleanup = bSaveOnCleanup; if (bPreLoad) { m_mem.Load(m_strFile); } }
/// <summary> /// The constructor. /// </summary> /// <param name="nMax">Specifies the maximum number of items in the collection.</param> /// <param name="fAlpha">Specifies how much prioritization is used (0 = no prioritization, 1 = full prioritization).</param> public PrioritizedMemoryCollection(int nMax, float fAlpha) { m_mem = new MemoryCollection(nMax); m_fAlpha = fAlpha; while (m_nItCapacity < nMax) { m_nItCapacity *= 2; } m_ItSum = new SumSegmentTree(m_nItCapacity); m_ItMin = new MinSegmentTree(m_nItCapacity); }
/// <summary> /// Retrieves a random sample of items from the list. /// </summary> /// <param name="random">Specifies the random number generator to use.</param> /// <param name="nCount">Specifies the number of items to retrieve.</param> /// <returns>The sampled items are returned in a new MemoryCollection.</returns> public MemoryCollection GetRandomSamples(CryptoRandom random, int nCount) { if (nCount >= Count) { return(this); } MemoryCollection col = new MemoryCollection(nCount); while (col.Count < nCount) { int nIdx = random.Next(Count); col.Add(m_rgItems[nIdx]); } return(col); }
/// <summary> /// Return a batch of items. /// </summary> /// <param name="random">Specifies the random number generator.</param> /// <param name="nCount">Specifies the number of items to sample.</param> /// <param name="dfBeta">Not used.</param> /// <returns>The array of items is returned.</returns> public MemoryCollection GetSamples(CryptoRandom random, int nCount, double dfBeta) { if (m_rgWts == null || m_rgWts.Length != nCount) { m_rgWts = new double[nCount]; for (int i = 0; i < m_rgWts.Length; i++) { m_rgWts[i] = 1.0; } } if (m_rgIdx == null || m_rgIdx.Length != nCount) { m_rgIdx = new int[nCount]; } MemoryCollection mem = new MemoryCollection(nCount); if (m_bPreLoaded) { for (int i = 0; i < nCount; i++) { mem.Add(m_mem[m_nSampleIdx]); m_nSampleIdx++; if (m_nSampleIdx == m_mem.Count) { m_nSampleIdx = 0; } } } else { mem = m_mem.GetRandomSamples(random, nCount); } mem.Indexes = m_rgIdx; mem.Priorities = m_rgWts; return(mem); }
/// <summary> /// Return a batch of items. /// </summary> /// <param name="random">Specifies the random number generator.</param> /// <param name="nCount">Specifies the number of items to sample.</param> /// <param name="dfBeta">Not used.</param> /// <returns>The random array of items is returned.</returns> public MemoryCollection GetSamples(CryptoRandom random, int nCount, double dfBeta) { if (m_rgWts == null || m_rgWts.Length != nCount) { m_rgWts = new double[nCount]; for (int i = 0; i < m_rgWts.Length; i++) { m_rgWts[i] = 1.0; } } if (m_rgIdx == null || m_rgIdx.Length != nCount) { m_rgIdx = new int[nCount]; } MemoryCollection mem = m_mem.GetRandomSamples(random, nCount); mem.Indexes = m_rgIdx; mem.Priorities = m_rgWts; return(mem); }
/// <summary> /// Update - does nothing. /// </summary> /// <param name="rgSamples">Specifies the list of samples.</param> public void Update(MemoryCollection rgSamples) { }
/// <summary> /// The constructor. /// </summary> /// <param name="nMax">Specifies the maximum number of items.</param> public RandomMemoryCollection(int nMax) { m_mem = new MemoryCollection(nMax); }