RandomSampler 1.1.0

dotnet add package RandomSampler --version 1.1.0
                    
NuGet\Install-Package RandomSampler -Version 1.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="RandomSampler" Version="1.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="RandomSampler" Version="1.1.0" />
                    
Directory.Packages.props
<PackageReference Include="RandomSampler" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add RandomSampler --version 1.1.0
                    
#r "nuget: RandomSampler, 1.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package RandomSampler@1.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=RandomSampler&version=1.1.0
                    
Install as a Cake Addin
#tool nuget:?package=RandomSampler&version=1.1.0
                    
Install as a Cake Tool

Cache sampler

A collection similar to a queue(FIFO) that allows to

  • overwrite the oldest entries (with the extra ability to act on the discarded entries)
  • sample for some unique(random or not) entries from the collection

Reason to be

For a RL implementation, needed to sample from a cache of recent events, but didn't found something that will not shuffle the entire cache before spitting few items.

Usage

It allows to preset the random generator with a custom seed.

See the tests, but in principle is simple as:

Cache.create 2000 [0.0]                         // similar to python zero(2000)
|> Cache.insertSequence [1.0 .. 5000.0]         // insert 5000 elements that are truncated to the last cache capacity 2000 in our case 
|> Cache.insertSequence [5001.0 .. 10_000.0]    // insert another 5000 elements that will retain the last 2000 entries (again the cache capacity)
|> Cache.sample 64                              // sample 64 unique entries from that current cache

[00] 9352.0
[01] 8949.0
[02] 8030.0
[03] 8470.0
[04] 8151.0
[05] 8049.0
[06] 8882.0
[07] 9272.0
[08] 9430.0
[09] 8033.0
[10] 9354.0
...
[60] 8208.0
[61] 9787.0
[62] 8759.0
[63] 9644.0

use cartPole = new CartPoleEnv(AvaloniaEnvViewer.Factory)
let actionsCache = Cache.create actionsSize [0 .. actionsSize - 1]      // creates a cache for actions

let replayBuffer = 
   seq {
       let mutable observations = cartPole.Reset()
       for i = 1 to minReplaySize do
           let action = actionsCache |> Cache.sample 1 |> Seq.head     // sampling just 1 item
           let newObservations = cartPole.Step(action)
           yield {| fromState = observations
                    action = action
                    reward = newObservations.Reward
                    toState = newObservations.Observation |}
           if newObservations.Done
               then observations <- cartPole.Reset()
               else observations <- newObservations.Observation
   }
   |> Cache.create bufferSize      // creating a cache that initially is filled with the first transition from sequence, then the rest of the sequence is added to the cache

...

let newTransitions =
   replayBuffer
   |> Cache.sample 64              // take a batch of random 64 recorded transitions
   |> Seq.map actThenUpdateOnlineValueThenReturnTransition
replayBuffer
|> Cache.insertSequence newTransitions      // insert new transitions in the cache (overwriting the oldest transitions in the cache if the capacity is exceeded)

If you want to store disposable items (like tensors) in the cache, there is a way to dispose the discarded entries on insert operations. See OnItemRemoved property of the Cache class (you can invoke an action like dispose/log/etc. for each discarded entry).

If you are a C# user then explore the API using intellisense on:

  • Mecu.Sampler.sample static method
  • Mecu.Cache class
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.1.0 242 4/21/2023
1.0.0 231 4/17/2023

Added:
- an action for each entry replaced in cache (I use it for disposing tensors).
- added a parameter reversed order Yoda style for  *insertItem*, **insertSequence*, *getSequence* since usually in F# flow you first process the item/items then you insert them into cache.
Changed behaviour:
- the sampling method when the user ask for more samples than 75% of the cache capacity