Skip to content

Commit

Permalink
random algorithm selection fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ZacharyPatten committed Jan 31, 2021
1 parent 377aa83 commit 029cd81
Show file tree
Hide file tree
Showing 6 changed files with 713 additions and 279 deletions.
6 changes: 3 additions & 3 deletions Sources/Towel/Statics-Random.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static void Next<Step, Random>(int count, int minValue, int maxValue, Rea
where Step : struct, IAction<int>
where Random : struct, IFunc<int, int, int>
{
if (excluded.Length < Math.Sqrt(maxValue - minValue))
if (count * excluded.Length + .5 * Math.Pow(excluded.Length, 2) < (maxValue - minValue) + count + 2 * excluded.Length)
{
NextRollTracking(count, minValue, maxValue, excluded, random, step);
}
Expand All @@ -50,7 +50,7 @@ public static void NextRollTracking<Step, Random>(int count, int minValue, int m
{
throw new ArgumentOutOfRangeException(nameof(count), $"{nameof(count)} < 0");
}
// Algorithm B: O(.5*(count + excluded.Length)^2 + .5*excluded.Length^2), Ω(count + excluded.Length), ε(.5*(count + excluded.Length)^2 + .5*excluded.Length^2)
// Algorithm B: O(count * excluded.Length + .5*excluded.Length^2)
Node<int>? head = null;
int excludeCount = 0;
foreach (int i in excluded) // Θ(excluded.Length)
Expand Down Expand Up @@ -94,7 +94,7 @@ public static void NextRollTracking<Step, Random>(int count, int minValue, int m
throw new ArgumentException("The Random provided returned a value outside the requested range.");
}
Node<int>? node = head;
while (node is not null && node.Value <= roll) // O(.5*(count + excluded.Length)), Ω(0), ε(.5*(count + excluded.Length))
while (node is not null && node.Value <= roll) // O(excluded.Length), Ω(0)
{
roll++;
node = node.Next;
Expand Down
4 changes: 3 additions & 1 deletion Tools/Towel_Benchmarking/DataStructures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,10 @@ Person[] GenerateData(int count)

return new Person[][]
{
GenerateData(10),
GenerateData(100),
GenerateData(100000),
GenerateData(1000),
GenerateData(10000),
};
}
}
Expand Down
5 changes: 2 additions & 3 deletions Tools/Towel_Benchmarking/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,9 @@ public static void Main(string[] args)
};

/// <summary>Runs the benchmarks.</summary>
/// <param name="updateDocumentation">Whether or not to update the </param>
#pragma warning disable IDE1006 // Naming Styles
/// <param name="updateDocumentation">Whether or not to update the docfx documentation.</param>
/// <param name="documentationPath">The path to the docfx documentation file.</param>
[Command] public static void run(
#pragma warning restore IDE1006 // Naming Styles
bool updateDocumentation = false,
string documentationPath = null)
{
Expand Down
32 changes: 19 additions & 13 deletions Tools/Towel_Benchmarking/RandomWithExclusions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ public class RandomWithExclusions
public int MinValue;
[Params(10, 100, 1000, 10000, 100000, 1000000)]
public int MaxValue;
[Params(.01, .1, .25, .5)]
public double CountRatio;
[Params("1: sqrt(sqrt(range))", "2: .5*sqrt(range)", "3: sqrt(range)", "4: 2*sqrt(range)", "5: .5*range")]
[Params("1: sqrt(sqrt(range))", "2: .5*sqrt(range)", "3: sqrt(range)", "4: 2*sqrt(range)")]
public string Count;
[Params("1: sqrt(sqrt(range))", "2: .5*sqrt(range)", "3: sqrt(range)", "4: 2*sqrt(range)")]
public string Exclued;

Random Random;
int[] Excludes;
int Count;
int CountInt;
int[] temp;

[IterationSetup]
Expand All @@ -30,20 +30,26 @@ public void IterationSetup()
int excludeCount = Exclued switch
{
"1: sqrt(sqrt(range))" => (int)(Math.Sqrt(Math.Sqrt(MaxValue - MinValue))),
"2: .5*sqrt(range)" => (int)(.5*Math.Sqrt(MaxValue - MinValue)),
"2: .5*sqrt(range)" => (int)(.5 * Math.Sqrt(MaxValue - MinValue)),
"3: sqrt(range)" => (int)(Math.Sqrt(MaxValue - MinValue)),
"4: 2*sqrt(range)" => (int)(2 * Math.Sqrt(MaxValue - MinValue)),
"5: .5*range" => (int)(.5 * (MaxValue - MinValue)),
_ => throw new NotImplementedException(),
};
Excludes = Random.NextUnique(excludeCount, MinValue, MaxValue);
Count = (int)(CountRatio * (MaxValue - MinValue));
CountInt = Count switch
{
"1: sqrt(sqrt(range))" => (int)(Math.Sqrt(Math.Sqrt(MaxValue - MinValue))),
"2: .5*sqrt(range)" => (int)(.5 * Math.Sqrt(MaxValue - MinValue)),
"3: sqrt(range)" => (int)(Math.Sqrt(MaxValue - MinValue)),
"4: 2*sqrt(range)" => (int)(2 * Math.Sqrt(MaxValue - MinValue)),
_ => throw new NotImplementedException(),
};
}

[Benchmark]
public void Towel()
{
int[] result = Random.Next(Count, MinValue, MaxValue, Excludes);
int[] result = Random.Next(CountInt, MinValue, MaxValue, Excludes);
temp = result;
}

Expand All @@ -66,8 +72,8 @@ public void HashSetAndArray()
pool[i++] = value;
}
}
int[] result = new int[Count];
for (int i = 0; i < Count; i++)
int[] result = new int[CountInt];
for (int i = 0; i < CountInt; i++)
{
int index = Random.Next(0, pool.Length);
result[i] = pool[index];
Expand All @@ -94,8 +100,8 @@ public void SetHashLinkedAndArray()
pool[i++] = value;
}
}
int[] result = new int[Count];
for (int i = 0; i < Count; i++)
int[] result = new int[CountInt];
for (int i = 0; i < CountInt; i++)
{
int index = Random.Next(0, pool.Length);
result[i] = pool[index];
Expand Down Expand Up @@ -144,7 +150,7 @@ public void RelativelySimpleCode()
}
System.Collections.Generic.HashSet<int> exclude = new(Excludes);
System.Collections.Generic.IEnumerable<int> range = Enumerable.Range(MinValue, MaxValue).Where(i => !exclude.Contains(i));
int[] result = Enumerable.Range(0, Count).Select(i => range.ElementAt(Random.Next(range.Count()))).ToArray();
int[] result = Enumerable.Range(0, CountInt).Select(i => range.ElementAt(Random.Next(range.Count()))).ToArray();
temp = result;
}
}
Expand Down
6 changes: 6 additions & 0 deletions Tools/Towel_Benchmarking/Towel_Benchmarking.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
<IsPackable>false</IsPackable>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<NoWarn>1701;1702;IDE1006</NoWarn>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<NoWarn>1701;1702;IDE1006</NoWarn>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.12.1" />
<ProjectReference Include="..\..\Sources\Towel\Towel.csproj" />
Expand Down
Loading

0 comments on commit 029cd81

Please sign in to comment.