Skip to content

Commit

Permalink
Added a numeric up/down control to let the user determine how large t…
Browse files Browse the repository at this point in the history
…he population size should be.
  • Loading branch information
tommai78101 committed Jul 25, 2022
1 parent cb8df15 commit f2c113d
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 8 deletions.
42 changes: 41 additions & 1 deletion src/GeneticAlgorithmBot.Designer.cs

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

22 changes: 15 additions & 7 deletions src/GeneticAlgorithmBot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ public int FrameLength {
set => FrameLengthNumeric.Value = value;
}

public int PopulationSize {
get => (int) PopulationSizeNumeric.Value;
set => PopulationSizeNumeric.Value = value;
}

public ClickyVirtualPadController Controller => InputManager.ClickyVirtualPadController;

public IList<string> ControllerButtons => Emulator.ControllerDefinition.BoolButtons;
Expand Down Expand Up @@ -250,7 +255,7 @@ public GeneticAlgorithmBot() {
this.Margin = new(0, 0, 0, 8);
}
this.Settings = new GeneticAlgorithmBotSettings();
this.populationManager = new GeneticAlgorithm(this, Utils.POPULATION_SIZE);
this.populationManager = new GeneticAlgorithm(this);
this.MainOperator.SelectedItem = ">=";
}

Expand Down Expand Up @@ -760,6 +765,11 @@ public void FrameLengthNumeric_ValueChanged(object sender, EventArgs e) {
this.populationManager.IsInitialized = false;
}

public void PopulationSizeNumeric_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 @@ -930,8 +940,6 @@ public void ClearBestButton_Click(object sender, EventArgs e) {
public static class Utils {
public static Random RNG { get; } = new Random((int) DateTime.Now.Ticks);

public static readonly int POPULATION_SIZE = 4;

public static readonly double MUTATION_RATE = 0.02;

public static readonly double CROSSOVER_RATE = 50.0;
Expand Down Expand Up @@ -993,13 +1001,13 @@ public class GeneticAlgorithm : IComparer {
public bool IsInitialized { get; set; }
public bool IsBestSet => this._bestRecording.IsSet;

public GeneticAlgorithm(GeneticAlgorithmBot owner, int populationSize) {
public GeneticAlgorithm(GeneticAlgorithmBot owner) {
this.bot = owner;
this.IsInitialized = false;
this._bestRecording = new InputRecording(owner, this);
this._beginning = new BotAttempt();
this.population = new InputRecording[populationSize];
for (int i = 0; i < populationSize; i++) {
this.population = new InputRecording[1];
for (int i = 0; i < this.population.Length; i++) {
this.population[i] = new InputRecording(owner, this);
}
}
Expand Down Expand Up @@ -1077,7 +1085,7 @@ public void Initialize() {
this.SetOrigin();
this.currentIndex = 0;
this.StartFrameNumber = this.bot._startFrame;
this.population = new InputRecording[Utils.POPULATION_SIZE];
this.population = new InputRecording[this.bot.PopulationSize];
for (int i = 0; i < this.population.Length; i++) {
this.population[i] = new InputRecording(this.bot, this);
this.population[i].Reset(0);
Expand Down

0 comments on commit f2c113d

Please sign in to comment.