Skip to content

Instantly share code, notes, and snippets.

@nitz
Created July 5, 2023 16:14
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 nitz/9f6e5d67b2f53b83d9922a71c4a14e08 to your computer and use it in GitHub Desktop.
Save nitz/9f6e5d67b2f53b83d9922a71c4a14e08 to your computer and use it in GitHub Desktop.
Pet Simulator X Gild / Rainbow Sim Tests v0
<Query Kind="Program">
<NuGetReference>RandN</NuGetReference>
<Namespace>ChartSeries = System.Windows.Forms.DataVisualization.Charting.Series</Namespace>
<Namespace>CountType = System.Int64</Namespace>
<Namespace>CurrencyType = System.Int64</Namespace>
<Namespace>OddsType = System.Double</Namespace>
<Namespace>RandN</Namespace>
<Namespace>RandN.Distributions</Namespace>
<Namespace>RandN.Distributions.UnitInterval</Namespace>
<Namespace>RandN.Extensions</Namespace>
<Namespace>RandN.Rngs</Namespace>
<Namespace>ResultType = System.Double</Namespace>
<Namespace>System.Windows</Namespace>
<Namespace>System.Windows.Forms</Namespace>
<Namespace>System.Windows.Forms.DataVisualization</Namespace>
<Namespace>System.Windows.Forms.DataVisualization.Charting</Namespace>
</Query>
// Copyright © 2023, Chris Marc Dailey (nitz) <https://cmd.wtf>
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// SPDX-License-Identifier: BSD-3-Clause
// v0: 5 July 2023 - Initial publication.
// Settings
const long SimulationRunCount = 10000000;
const bool ShowSuccessRate = true;
const bool ShowCurrencyEffectiveness = false;
const bool ShowTotalRunCurrencyCost = false;
const bool DumpChartsInline = true;
const int InlineChartWidth = 1280;
const int InlineChartHeight = 720;
void Main()
{
Simulate(configNormalGild);
//Simulate(configNormalRainbow);
//Simulate(configNormalDarkMatter);
Simulate(configHardcoreGild);
//Simulate(configHardcoreRainbow);
//Simulate(configHardcoreDarkMatter);
}
// Constants
const string ItemName = "Pet";
const string ItemNamePlural = $"{ItemName}s";
const string CurrencyName = "Diamond";
const string CurrencyNamePlural = $"{CurrencyName}s";
const float SmallFontSizeScaler = 0.85f;
#region Configs
readonly Config configNormalGild = new()
{
Name = "Normal Gild",
Options = new List<Operation>() {
new Operation(new Costs(1, 250), 0.13),
new Operation(new Costs(2, 400), 0.29),
new Operation(new Costs(3, 600), 0.47),
new Operation(new Costs(4, 800), 0.63),
new Operation(new Costs(5, 1000), 0.88),
new Operation(new Costs(6, 1250), 1.00),
},
};
readonly Config configNormalRainbow = new()
{
Name = "Normal Rainbow",
Options = new List<Operation>() {
new Operation(new Costs(1, 400), 0.13),
new Operation(new Costs(2, 700), 0.29),
new Operation(new Costs(3, 1050), 0.47),
new Operation(new Costs(4, 1400), 0.63),
new Operation(new Costs(5, 1750), 0.88),
new Operation(new Costs(6, 2240), 1.00),
},
};
readonly Config configNormalDarkMatter = new()
{
Name = "Normal Dark Matter",
Options = new List<Operation>()
{
new OperationDarkMatter(new CostsDarkMatter(1, 37500, TimeSpan.FromMinutes(7200), 799)),
new OperationDarkMatter(new CostsDarkMatter(2, 50000, TimeSpan.FromMinutes(4320), 599)),
new OperationDarkMatter(new CostsDarkMatter(3, 62500, TimeSpan.FromMinutes(1440), 399)),
new OperationDarkMatter(new CostsDarkMatter(4, 75000, TimeSpan.FromMinutes(720), 249)),
new OperationDarkMatter(new CostsDarkMatter(5, 87500, TimeSpan.FromMinutes(300), 199)),
new OperationDarkMatter(new CostsDarkMatter(6, 100000, TimeSpan.FromMinutes(30), 99)),
},
};
readonly Config configHardcoreGild = new()
{
Name = "Hardcore Gild",
Options = new List<Operation>() {
new Operation(new Costs(1, 5000), 0.08),
new Operation(new Costs(2, 8000), 0.17),
new Operation(new Costs(3, 12000), 0.29),
new Operation(new Costs(4, 16000), 0.37),
new Operation(new Costs(5, 20000), 0.46),
new Operation(new Costs(6, 25000), 0.55),
new Operation(new Costs(7, 30000), 0.67),
new Operation(new Costs(8, 35000), 0.78),
new Operation(new Costs(9, 40000), 0.88),
new Operation(new Costs(10, 50000), 1.00),
},
};
readonly Config configHardcoreRainbow = new()
{
Name = "Hardcore Rainbow",
Options = new List<Operation>() {
new Operation(new Costs(1, 8000), 0.08),
new Operation(new Costs(2, 14000), 0.17),
new Operation(new Costs(3, 22500), 0.29),
new Operation(new Costs(4, 28000), 0.37),
new Operation(new Costs(5, 35000), 0.46),
new Operation(new Costs(6, 45000), 0.55),
new Operation(new Costs(7, 55000), 0.67),
new Operation(new Costs(8, 65000), 0.78),
new Operation(new Costs(9, 80000), 0.88),
new Operation(new Costs(10, 100000), 1.00),
},
};
readonly Config configHardcoreDarkMatter = new Config
{
Name = "Hardcore Dark Matter",
Options = new List<Operation>()
{
new OperationDarkMatter(new CostsDarkMatter(1, 750000, TimeSpan.FromMinutes(7200), 1299)),
new OperationDarkMatter(new CostsDarkMatter(2, 1000000, TimeSpan.FromMinutes(7200), 949)),
new OperationDarkMatter(new CostsDarkMatter(3, 1250000, TimeSpan.FromMinutes(7200), 799)),
new OperationDarkMatter(new CostsDarkMatter(4, 1500000, TimeSpan.FromMinutes(4320), 599)),
new OperationDarkMatter(new CostsDarkMatter(5, 2000000, TimeSpan.FromMinutes(1440), 399)),
new OperationDarkMatter(new CostsDarkMatter(6, 2500000, TimeSpan.FromMinutes(720), 249)),
new OperationDarkMatter(new CostsDarkMatter(7, 300000, TimeSpan.FromMinutes(300), 199)),
new OperationDarkMatter(new CostsDarkMatter(8, 500000, TimeSpan.FromMinutes(30), 99)),
},
};
#endregion Configs
// Types
record OperationResult(bool Success, Costs Spent);
record Costs(CountType Count, CurrencyType Currency);
record CostsDarkMatter(CountType Count, CurrencyType Currency, TimeSpan Duration, int SkipCost) : Costs(Count, Currency);
record Operation(Costs Inputs, OddsType Odds)
{
private static Dictionary<OddsType, IDistribution<bool>> _rngDist = new();
public IDistribution<bool> Distribution
{
get
{
if (!_rngDist.ContainsKey(Odds))
{
// allow a warning or error if Odds doesn't implicitly cast to double.
_rngDist[Odds] = RandN.Distributions.Bernoulli.FromP(Odds);
}
return _rngDist[Odds];
}
}
public virtual OperationResult Sim(IRng rng)
{
bool success = Distribution.Sample(rng);
return new OperationResult(success, Inputs);
}
}
record OperationDarkMatter(CostsDarkMatter ExtendedInputs) : Operation(ExtendedInputs, 1.0)
{
public override OperationResult Sim(IRng rng)
{
return new OperationResult(true, ExtendedInputs);
}
}
record SimulationRunResult(Costs CostsPerRun)
{
public CountType RunCount { get; set; }
public CountType Successes { get; set; }
public OddsType SuccessRate => RunCount > 0 ? ((OddsType)Successes / RunCount) : 0;
public ResultType GetEffectivenessRate(ResultType idealSuccesses)
{
ResultType effectiveness = (ResultType)Successes / CostsPerRun.Count / idealSuccesses;
return effectiveness;
}
public ResultType GetCurrencyEffectivenessRate(Costs idealCosts)
{
ResultType costPerRun = (ResultType)CostsPerRun.Currency;
ResultType idealRuns = (ResultType)(RunCount * CostsPerRun.Count) / idealCosts.Count;
ResultType currencyEffectiveness = (costPerRun * RunCount) / (idealCosts.Currency * idealRuns);
return currencyEffectiveness;
}
public CurrencyType TotalRunCost => RunCount * CostsPerRun.Currency;
}
class Config
{
public string Name = string.Empty;
public List<Operation> Options = new();
public Operation Ideal => _ideal ??= Options.MaxBy(o => o.Odds) ?? throw new InvalidOperationException("Needs at least one option to find best.");
private Operation? _ideal = null;
public void RunOnce(IRng rng, ref Dictionary<CountType, SimulationRunResult> results)
{
foreach (Operation o in Options)
{
var result = o.Sim(rng);
var cumulative = results[o.Inputs.Count];
cumulative.RunCount++;
cumulative.Successes += result.Success ? 1 : 0;
}
}
public void Run(CountType simulationCount, ref Dictionary<CountType, SimulationRunResult> results)
{
PrimeResults(ref results);
var rng = RandN.StandardRng.Create();
for (int scan = 0; scan < simulationCount; ++scan)
{
RunOnce(rng, ref results);
}
}
private void PrimeResults(ref Dictionary<CountType, SimulationRunResult> results)
{
results.Clear();
foreach (Operation o in Options)
{
results[o.Inputs.Count] = new SimulationRunResult(o.Inputs);
}
}
}
// Helpers
private void Simulate(Config cfg)
{
var chart = GenerateSimulationChart(cfg, RunSimulationConfig(cfg));
if (DumpChartsInline)
{
chart.ToBitmap(InlineChartWidth, InlineChartHeight).Dump(chart.Text);
}
}
private Dictionary<CountType, SimulationRunResult> RunSimulationConfig(Config cfg)
{
Dictionary<CountType, SimulationRunResult> results = new();
cfg.Run(SimulationRunCount, ref results);
return results;
}
private Chart GenerateSimulationChart(Config cfg, Dictionary<CountType, SimulationRunResult> results)
{
Operation ideal = cfg.Ideal;
CountType idealCount = ideal.Inputs.Count;
ResultType idealSuccesses = results[idealCount].Successes / idealCount;
Costs idealCurrencySpend = results[idealCount].CostsPerRun;
int seriesCount = 1;
var chart = results.Chart(d => d.Key, d => d.Value.GetEffectivenessRate(idealSuccesses), Util.SeriesType.Bar);
if (ShowCurrencyEffectiveness)
{
chart.AddYSeries(d => results[d.Key].GetCurrencyEffectivenessRate(idealCurrencySpend), LINQPad.Util.SeriesType.Bar);
seriesCount++;
}
if (ShowSuccessRate)
{
chart.AddYSeries(d => results[d.Key].SuccessRate, LINQPad.Util.SeriesType.Bar);
seriesCount++;
}
if (ShowTotalRunCurrencyCost)
{
chart.AddYSeries(d => results[d.Key].TotalRunCost, LINQPad.Util.SeriesType.Bar, useSecondaryYAxis: true);
seriesCount++;
}
Chart winChart = chart.ToWindowsChart();
winChart.Text = $"{cfg.Name}ing Effectiveness\n{SimulationRunCount:n0} runs per data point";
winChart.Titles.Add(new Title(winChart.Text)
{
Visible = true
});
ChartArea area = winChart.ChartAreas.First();
area.AxisX.Title = $"{ItemNamePlural} per Attempt";
area.AxisX.LabelStyle = new LabelStyle()
{
Format = $"0 {ItemNamePlural}",
};
//area.AxisX.Minimum = 1;
area.AxisX.IsStartedFromZero = false;
area.AxisX.Maximum = results.Keys.Max() + 0.5;
area.AxisY.Title = "Rate";
area.AxisY.LabelStyle = new LabelStyle()
{
Format = "0%"
};
if (!ShowTotalRunCurrencyCost)
{
area.AxisY2.Enabled = AxisEnabled.False;
}
else
{
area.AxisY2.LabelStyle.Format = "n0";
}
int seriesIndex = 0;
ChartSeries effectivenessSeries = winChart.Series[seriesIndex++];
effectivenessSeries.LegendText = "Output % vs. 'Ideal'";
effectivenessSeries.IsValueShownAsLabel = true;
effectivenessSeries.LabelFormat = "0.00%";
if (ShowCurrencyEffectiveness)
{
ChartSeries currencyEffectivenessSeries = winChart.Series[seriesIndex++];
currencyEffectivenessSeries.LegendText = $"{CurrencyNamePlural} Usage % vs 'Ideal'";
currencyEffectivenessSeries.IsValueShownAsLabel = true;
currencyEffectivenessSeries.LabelFormat = "0.00%";
currencyEffectivenessSeries.CustomProperties = "PixelPointWidth=15";
currencyEffectivenessSeries.Font = new System.Drawing.Font(currencyEffectivenessSeries.Font.FontFamily, currencyEffectivenessSeries.Font.Size * SmallFontSizeScaler);
}
if (ShowSuccessRate)
{
ChartSeries successRateSeries = winChart.Series[seriesIndex++];
successRateSeries.LegendText = "Success Rate";
successRateSeries.IsValueShownAsLabel = true;
successRateSeries.LabelFormat = "0.00%";
successRateSeries.CustomProperties = "PixelPointWidth=15";
successRateSeries.Font = new System.Drawing.Font(successRateSeries.Font.FontFamily, successRateSeries.Font.Size * SmallFontSizeScaler);
}
if (ShowTotalRunCurrencyCost)
{
ChartSeries currencyCostSeries = winChart.Series[seriesIndex++];
currencyCostSeries.LegendText = $"{CurrencyName} Cost";
currencyCostSeries.IsValueShownAsLabel = true;
currencyCostSeries.LabelFormat = "n0";
currencyCostSeries.CustomProperties = "PixelPointWidth=15";
currencyCostSeries.Font = new System.Drawing.Font(currencyCostSeries.Font.FontFamily, currencyCostSeries.Font.Size * SmallFontSizeScaler);
}
return winChart;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment