From 959fbd3f123352e721af8b2fff3bcc77410a717c Mon Sep 17 00:00:00 2001 From: tom_mai78101 Date: Thu, 28 Jul 2022 19:19:35 -0400 Subject: [PATCH] Small changes. - The UI can now allow the user to modify the mutation rate. - Improved tooltip description. - Modified the label text, so it is more meaningful. - Updated the CMD to store a backup copy of the config.ini file. --- run_build.cmd | 5 +++ src/GeneticAlgorithmBot.Designer.cs | 50 ++++++++++++++++++++++++++--- src/GeneticAlgorithmBot.cs | 27 +++++++++------- src/GeneticAlgorithmBot.resx | 3 ++ src/Properties/launchSettings.json | 4 --- 5 files changed, 68 insertions(+), 21 deletions(-) diff --git a/run_build.cmd b/run_build.cmd index 432bdf0..abc559f 100644 --- a/run_build.cmd +++ b/run_build.cmd @@ -1,2 +1,7 @@ cd BizHawk +if not exist config_bak.ini ( + cp config.ini config_bak.ini +) else ( + cp config_bak.ini config.ini +) .\EmuHawk.exe --open-ext-tool-dll=GeneticAlgorithmBot \ No newline at end of file diff --git a/src/GeneticAlgorithmBot.Designer.cs b/src/GeneticAlgorithmBot.Designer.cs index 067b0bc..5739ad5 100644 --- a/src/GeneticAlgorithmBot.Designer.cs +++ b/src/GeneticAlgorithmBot.Designer.cs @@ -165,6 +165,8 @@ public void InitializeComponent() { this.ControlProbabilityPanel = new System.Windows.Forms.Panel(); this.btnCopyBestInput = new System.Windows.Forms.Button(); this.GoalGroupBox = new System.Windows.Forms.GroupBox(); + this.MutationRateNumeric = new System.Windows.Forms.NumericUpDown(); + this.MutationRateLabel = new System.Windows.Forms.Label(); this.PopulationSizeNumeric = new System.Windows.Forms.NumericUpDown(); this.PopulationSizeLabel = new System.Windows.Forms.Label(); this.panel6 = new System.Windows.Forms.Panel(); @@ -237,6 +239,7 @@ public void InitializeComponent() { this.panel4.SuspendLayout(); this.ControlsBox.SuspendLayout(); this.GoalGroupBox.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)(this.MutationRateNumeric)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.PopulationSizeNumeric)).BeginInit(); this.panel6.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.TieBreak3Numeric)).BeginInit(); @@ -593,9 +596,8 @@ public void InitializeComponent() { this.ControlsBox.Size = new System.Drawing.Size(442, 369); this.ControlsBox.TabIndex = 2007; this.ControlsBox.TabStop = false; - this.ControlsBox.Text = "Input Probability Controls [?]"; - this.BotToolTip.SetToolTip(this.ControlsBox, "This panel here indicates the likelihood the specified controls are chosen when t" + - "he bot mutates its training model."); + this.ControlsBox.Text = "Input Mutation Probability Controls [?]"; + this.BotToolTip.SetToolTip(this.ControlsBox, resources.GetString("ControlsBox.ToolTip")); // // ControlProbabilityPanel // @@ -625,6 +627,8 @@ public void InitializeComponent() { // this.GoalGroupBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); + this.GoalGroupBox.Controls.Add(this.MutationRateNumeric); + this.GoalGroupBox.Controls.Add(this.MutationRateLabel); this.GoalGroupBox.Controls.Add(this.PopulationSizeNumeric); this.GoalGroupBox.Controls.Add(this.PopulationSizeLabel); this.GoalGroupBox.Controls.Add(this.label4); @@ -642,10 +646,43 @@ public void InitializeComponent() { this.GoalGroupBox.Text = "Memory Value Information [?]"; this.BotToolTip.SetToolTip(this.GoalGroupBox, "This panel indicates what RAM values in the memory you want to use for the bot to" + " determine its fitness level as it undergoes training."); + // + // MutationRateNumeric + // + this.MutationRateNumeric.DecimalPlaces = 3; + this.MutationRateNumeric.Increment = new decimal(new int[] { + 1, + 0, + 0, + 196608}); + this.MutationRateNumeric.Location = new System.Drawing.Point(380, 25); + this.MutationRateNumeric.Maximum = new decimal(new int[] { + 1, + 0, + 0, + 0}); + this.MutationRateNumeric.Name = "MutationRateNumeric"; + this.MutationRateNumeric.Size = new System.Drawing.Size(47, 20); + this.MutationRateNumeric.TabIndex = 1007; + this.MutationRateNumeric.Value = new decimal(new int[] { + 2, + 0, + 0, + 131072}); + this.MutationRateNumeric.ValueChanged += new System.EventHandler(this.MutationRateNumeric_ValueChanged); + // + // MutationRateLabel + // + this.MutationRateLabel.AutoSize = true; + this.MutationRateLabel.Location = new System.Drawing.Point(297, 29); + this.MutationRateLabel.Name = "MutationRateLabel"; + this.MutationRateLabel.Size = new System.Drawing.Size(77, 13); + this.MutationRateLabel.TabIndex = 1006; + this.MutationRateLabel.Text = "Mutation Rate:"; // // PopulationSizeNumeric // - this.PopulationSizeNumeric.Location = new System.Drawing.Point(303, 25); + this.PopulationSizeNumeric.Location = new System.Drawing.Point(245, 25); this.PopulationSizeNumeric.Maximum = new decimal(new int[] { 9999, 0, @@ -669,7 +706,7 @@ public void InitializeComponent() { // PopulationSizeLabel // this.PopulationSizeLabel.AutoSize = true; - this.PopulationSizeLabel.Location = new System.Drawing.Point(214, 29); + this.PopulationSizeLabel.Location = new System.Drawing.Point(156, 28); this.PopulationSizeLabel.Name = "PopulationSizeLabel"; this.PopulationSizeLabel.Size = new System.Drawing.Size(83, 13); this.PopulationSizeLabel.TabIndex = 1002; @@ -1231,6 +1268,7 @@ public void InitializeComponent() { this.ControlsBox.ResumeLayout(false); this.GoalGroupBox.ResumeLayout(false); this.GoalGroupBox.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)(this.MutationRateNumeric)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.PopulationSizeNumeric)).EndInit(); this.panel6.ResumeLayout(false); this.panel6.PerformLayout(); @@ -1256,6 +1294,8 @@ public void InitializeComponent() { private IContainer components; public BizHawk.WinForms.Controls.LocLabelEx BestGenerationNumberLabel; public BizHawk.WinForms.Controls.LocLabelEx locLabelEx3; + private NumericUpDown MutationRateNumeric; + private Label MutationRateLabel; /// /// Returns the cached ResourceManager instance used by this class. diff --git a/src/GeneticAlgorithmBot.cs b/src/GeneticAlgorithmBot.cs index 1e27441..f9b4742 100644 --- a/src/GeneticAlgorithmBot.cs +++ b/src/GeneticAlgorithmBot.cs @@ -32,8 +32,6 @@ public sealed partial class GeneticAlgorithmBot : ToolFormBase, IExternalToolFor private bool _previousInvisibleEmulation = false; - private const bool _debug_mode_skipRestart = false; - private string _lastOpenedRom = ""; private MemoryDomain _currentDomain = default!; @@ -137,6 +135,11 @@ public int PopulationSize { set => PopulationSizeNumeric.Value = value; } + public decimal MutationRate { + get => MutationRateNumeric.Value; + set => MutationRateNumeric.Value = (decimal) value; + } + public ClickyVirtualPadController Controller => InputManager.ClickyVirtualPadController; public IList ControllerButtons => Emulator.ControllerDefinition.BoolButtons; @@ -267,10 +270,6 @@ public override void Restart() { // This has to do with loading and saving save states, which is something the bot needs to function. _ = StatableCore!; - if (_debug_mode_skipRestart) { - return; - } - if (_currentDomain == null || MemoryDomains.Contains(_currentDomain)) { _currentDomain = MemoryDomains.MainMemory; _bigEndian = _currentDomain.EndianType == MemoryDomain.Endian.Big; @@ -627,11 +626,12 @@ private bool LoadBotFile(string path) { // Attempts to load GeneticAlgorithmBot .BOT file save data. botData = (BotData) ConfigService.LoadWithType(json); } - catch (InvalidCastException e) { + catch (InvalidCastException) { // If exception is thrown, attempt to load BasicBot .BOT file save data instead. botData = Utils.BotDataReflectionCopy(ConfigService.LoadWithType(json)); } - } catch (InvalidCastException e) { + } + catch (InvalidCastException e) { using ExceptionBox dialog = new(e); this.ShowDialogAsChild(dialog); return false; @@ -862,7 +862,7 @@ public void OnBotLoad(object sender, EventArgs eventArgs) { DialogResult = DialogResult.Cancel; return; } - } + } // Reject the tool from loading. else { DialogController.ShowMessageBox("Unsupported BizHawk version detected. Please report the issue on TASVideo Forum @ https://tasvideos.org/Forum/Topics/23453"); @@ -889,6 +889,11 @@ public void PopulationSizeNumeric_ValueChanged(object sender, EventArgs e) { this.populationManager.IsInitialized = false; } + public void MutationRateNumeric_ValueChanged(object sender, EventArgs e) { + AssessRunButtonStatus(); + this.populationManager.IsInitialized = false; + } + public void ClearStatsContextMenuItem_Click(object sender, EventArgs e) { Runs = 0; Frames = 0; @@ -1154,8 +1159,6 @@ public void TieBreaker3Box_TextChanged(object sender, EventArgs e) { public static class Utils { public static Random RNG { get; } = new Random((int) DateTime.Now.Ticks); - public static readonly double MUTATION_RATE = 0.02; - public static readonly double CROSSOVER_RATE = 50.0; public static bool IsBetter(GeneticAlgorithmBot bot, BotAttempt best, BotAttempt comparison, BotAttempt current) { @@ -1374,7 +1377,7 @@ public void Reproduce() { // Uniform distribution mutation. for (int rate = 0; rate < child.FrameLength; rate++) { - if (Utils.RNG.NextDouble() <= Utils.MUTATION_RATE) { + if (Utils.RNG.NextDouble() <= decimal.ToDouble(this.bot.MutationRate)) { child.RandomizeFrameInput(); } } diff --git a/src/GeneticAlgorithmBot.resx b/src/GeneticAlgorithmBot.resx index 5d8690a..10c4b05 100644 --- a/src/GeneticAlgorithmBot.resx +++ b/src/GeneticAlgorithmBot.resx @@ -120,6 +120,9 @@ 497, 17 + + This panel here indicates the likelihood the specified controls are chosen when the bot mutates its training model. The weighted values determine the how likely it is for the mutation to enter this button. + 594, 17 diff --git a/src/Properties/launchSettings.json b/src/Properties/launchSettings.json index 1d668e3..d509b15 100644 --- a/src/Properties/launchSettings.json +++ b/src/Properties/launchSettings.json @@ -1,9 +1,5 @@ { "profiles": { - "GeneticAlgorithmBot": { - "commandName": "Project", - "nativeDebugging": true - }, "EmuHawk": { "commandName": "Executable", "executablePath": "E:\\LargeGithubProjects\\Bizhawk-GeneticAlgorithmBot\\BizHawk\\EmuHawk.exe",