Skip to content

Commit

Permalink
Additional tweaks.
Browse files Browse the repository at this point in the history
  • Loading branch information
tommai78101 committed Aug 13, 2023
1 parent 66c517d commit eaa74f7
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/Common/ExtendedColor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void SetColor(Color color) {
}

public Color ToColor() {
return Color.FromArgb(R, G, B);
return Color.FromArgb(32, Color.FromArgb(R, G, B));
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/Neat/Calculation/Calculator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ public IList<double> Calculate(ExtendedColorWrapper[] inputs) {

// Insert the input values to the Calculator's input nodes.
for (int i = 0; i < InputNodes.Count; i++) {
Color inputColor = inputs[i].ExtendedColor.ToColor();
InputNodes[i].Output = inputColor.GetBrightness();
InputNodes[i].Output = inputs[i].ExtendedColor.Double;
}

// Begin calculations.
Expand Down
50 changes: 44 additions & 6 deletions src/Neat/Genetic/Genome.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,20 @@ public void Mutate() {
}

public void MutateActivationRandom() {
NodeGene? node = Nodes.GetRandomElement();
if (node?.X > 0.1) {
if (!Nodes.Any()) {
return;
}
int retry = 0;
NodeGene node;
do {
retry++;
node = Nodes.GetRandomElement();
} while (node == null && retry < 5);
if (node == null) {
// Give up.
return;
}
if (node.X > 0.1) {
ActivationEnumeration a = ActivationEnumeration.GetRandom();
node.Activation = a.Activation;
}
Expand All @@ -64,9 +76,19 @@ public void MutateLink() {
}

public void MutateNode() {
ConnectionGene connection = Connections.GetRandomElement()!;
if (connection == null)
if (!Connections.Any()) {
return;
}
int retry = 0;
ConnectionGene connection;
do {
retry++;
connection = Connections.GetRandomElement()!;
} while (connection == null && retry < 5);
if (connection == null) {
// Give up.
return;
}

NodeGene from = connection.In;
NodeGene to = connection.Out;
Expand Down Expand Up @@ -96,14 +118,30 @@ public void MutateNode() {
}

public void MutateToggleLink() {
ConnectionGene connection = Connections.GetRandomElement()!;
if (!Connections.Any()) {
return;
}
ConnectionGene connection;
int retry = 0;
do {
retry++;
connection = Connections.GetRandomElement()!;
} while (connection == null && retry < 5);
if (connection != null) {
connection.Enabled = !connection.Enabled;
}
}

public void MutateWeightRandom() {
ConnectionGene connection = Connections.GetRandomElement()!;
if (!Connections.Any()) {
return;
}
ConnectionGene connection;
int retry = 0;
do {
retry++;
connection = Connections.GetRandomElement()!;
} while (connection == null && retry < 5);
if (connection != null) {
connection.Weight = ThreadSafeRandom.GetNormalizedRandom(0, 0.2f) * GenomeConstants.WEIGHT_RANDOM_STRENGTH;
}
Expand Down
8 changes: 4 additions & 4 deletions src/Neat/Genetic/GenomeConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ internal sealed class GenomeConstants {
public static readonly double WEIGHT_SHIFT_STRENGTH = 0.3;

// Weight random strength
public static readonly double WEIGHT_RANDOM_STRENGTH = 0.03;
public static readonly double WEIGHT_RANDOM_STRENGTH = 0.13;

// Probability to add a connection
public static readonly double PROBABILITY_MUTATE_LINK = 0.03;
public static readonly double PROBABILITY_MUTATE_LINK = 0.12;

// Probability to add a node
public static readonly double PROBABILITY_MUTATE_NODE = 0.03;
public static readonly double PROBABILITY_MUTATE_NODE = 0.05;

// Probability to shift a weight
public static readonly double PROBABILITY_MUTATE_WEIGHT_SHIFT = 0.02;
public static readonly double PROBABILITY_MUTATE_WEIGHT_SHIFT = 0.12;

// Probability to change a weight
public static readonly double PROBABILITY_MUTATE_WEIGHT_RANDOM = 0.02;
Expand Down
21 changes: 14 additions & 7 deletions src/Rendering/BatchRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,22 @@ public void RenderInputRegion() {
if (!this.owner.UseNeat || this.gui == null) {
return;
}
int x = IsBotting ? this.owner._inputX : (int) this.owner.InputRegionX.Value;
int y = IsBotting ? this.owner._inputY : (int) this.owner.InputRegionY.Value;
int width = IsBotting ? this.owner._inputWidth : (int) this.owner.InputRegionWidth.Value;
int height = IsBotting ? this.owner._inputHeight : (int) this.owner.InputRegionHeight.Value;
Color inputRegionColor = Color.FromArgb(32, Color.DarkGray);
this.gui.DrawBox(x, y, x + width, y + height, null, inputRegionColor, DisplaySurfaceID.EmuCore);
Color inputOutlineColor = Color.FromArgb(72, Color.Yellow);
Color inputRegionColor = Color.FromArgb(32, Color.Yellow);
int x = (int) this.owner.InputRegionX.Value;
int y = (int) this.owner.InputRegionY.Value;
int width = (int) this.owner.InputRegionWidth.Value;
int height = (int) this.owner.InputRegionHeight.Value;
this.gui.DrawBox(x, y, x + width, y + height, inputOutlineColor, inputRegionColor, DisplaySurfaceID.EmuCore);

if (this.IsBotting) {
Color blockOutlineColor = Color.FromArgb(48, Color.AliceBlue);
int radius = this.owner._inputSampleSize / 2;
foreach (ExtendedColorWrapper block in this.owner._neatInputRegionData) {
this.gui.DrawBox(block.X - block.Radius, block.Y - block.Radius, block.X + block.Radius, block.Y + block.Radius, null, block.ExtendedColor.ToColor(), DisplaySurfaceID.EmuCore);
Color blockColor = block.ExtendedColor.ToColor();
int x1 = (block.X * this.owner._inputSampleSize) + radius + this.owner._inputX;
int y1 = (block.Y * this.owner._inputSampleSize) + radius + this.owner._inputY;
this.gui.DrawBox(x1 - radius, y1 - radius, x1 + radius, y1 + radius, blockOutlineColor, blockColor, DisplaySurfaceID.EmuCore);
}
}
}
Expand Down

0 comments on commit eaa74f7

Please sign in to comment.