Skip to content

Commit

Permalink
Small changes.
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
tommai78101 committed Jul 28, 2022
1 parent 68cc7df commit 959fbd3
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 21 deletions.
5 changes: 5 additions & 0 deletions run_build.cmd
Original file line number Diff line number Diff line change
@@ -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
50 changes: 45 additions & 5 deletions src/GeneticAlgorithmBot.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 15 additions & 12 deletions src/GeneticAlgorithmBot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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!;
Expand Down Expand Up @@ -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<string> ControllerButtons => Emulator.ControllerDefinition.BoolButtons;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/GeneticAlgorithmBot.resx
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@
<metadata name="BotToolTip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>497, 17</value>
</metadata>
<data name="ControlsBox.ToolTip" xml:space="preserve">
<value>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.</value>
</data>
<metadata name="StatsContextMenu.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>594, 17</value>
</metadata>
Expand Down
4 changes: 0 additions & 4 deletions src/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
{
"profiles": {
"GeneticAlgorithmBot": {
"commandName": "Project",
"nativeDebugging": true
},
"EmuHawk": {
"commandName": "Executable",
"executablePath": "E:\\LargeGithubProjects\\Bizhawk-GeneticAlgorithmBot\\BizHawk\\EmuHawk.exe",
Expand Down

0 comments on commit 959fbd3

Please sign in to comment.