Skip to content

Instantly share code, notes, and snippets.

@Zyst
Last active January 20, 2022 15:42
Show Gist options
  • Save Zyst/0da505007b0e8c21418247000f3e7d40 to your computer and use it in GitHub Desktop.
Save Zyst/0da505007b0e8c21418247000f3e7d40 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace epsilon_greedy
{
class Program
{
/*
* SAMPLE OUTPUT:
*
* Program started...
* Finished running in 16134 ms
* Orange was clicked 1586780 times and seen 8814219 times. Click rate is 18.00%
* Blue was clicked 30125 times and seen 603095 times. Click rate is 5.00%
* Green was clicked 58214 times and seen 582689 times. Click rate is 9.99%
* Enter any key to close the program...
*/
static void Main(string[] args)
{
// We'll do this 1 million times
int iterations = 10000000;
// We declare our banners
DemoBanners orange = new DemoBanners { Name = "Orange", TimesClicked = 1, TimesSeen = 1, ClickOdds = 18 };
DemoBanners blue = new DemoBanners { Name = "Blue", TimesClicked = 1, TimesSeen = 1, ClickOdds = 5 };
DemoBanners green = new DemoBanners { Name = "Green", TimesClicked = 1, TimesSeen = 1, ClickOdds = 10 };
Stopwatch stopWatch = new Stopwatch();
Console.WriteLine("Program started...");
stopWatch.Start();
List<DemoBanners> banners = new List<DemoBanners> { orange, blue, green };
Random random = new Random();
for (int i = 0; i < iterations; i++)
{
DemoBanners selected = DemoBanners.Choose(banners);
selected.TimesSeen++;
if (random.Next(1, 101) <= selected.ClickOdds)
{
selected.TimesClicked++;
}
}
stopWatch.Stop();
Console.WriteLine("Finished running in {0} ms", stopWatch.ElapsedMilliseconds);
foreach (DemoBanners b in banners)
{
Console.WriteLine("{0} was clicked {1} times and seen {2} times. Click rate is {3:0.00}%", b.Name, b.TimesClicked, b.TimesSeen, 100m * Decimal.Divide(b.TimesClicked, b.TimesSeen));
}
Console.WriteLine("Enter any key to close the program...");
Console.ReadKey();
}
}
class DemoBanners
{
public string Name { get; set; }
public int TimesClicked { get; set; }
public int TimesSeen { get; set; }
// The odds someone will click on our stuff, % based
public int ClickOdds { get; set; }
public static DemoBanners Choose(List<DemoBanners> banners)
{
Random random = new Random();
// 10% of the time we explore!
if (random.Next(1, 101) <= 10)
{
// We return a random item
return banners[random.Next(0, banners.Count)];
}
else
{
// We return the item with the greatest odds,
// if more than one has the same odds we return the first one we find
decimal maximum = banners.Max(x => Decimal.Divide(x.TimesClicked, x.TimesSeen));
return banners.First(r => Decimal.Divide(r.TimesClicked, r.TimesSeen) == maximum);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment