|
| 1 | +using Ecosystem_Simulator.Core.Interfaces; |
| 2 | +using System; |
| 3 | +namespace Ecosystem_Simulator.Core |
| 4 | +{ |
| 5 | + public class CritterGenome : IGenome |
| 6 | + { |
| 7 | + public float Speed { get; private set; } |
| 8 | + public float SightRadius { get; private set; } |
| 9 | + public float MetabolismEfficiency { get; private set; } |
| 10 | + public float ReproductionThreshold { get; private set; } |
| 11 | + public float GetGeneValue(string name) |
| 12 | + { |
| 13 | + return 0; |
| 14 | + } |
| 15 | + public void Mutate() { } |
| 16 | + public void Mutate(float ParentSpeed, float ParentSightRadius, float ParentMetabolismEfficiency, float ParentReproductionThreshold) |
| 17 | + { |
| 18 | + |
| 19 | + |
| 20 | + //Calculating change and adding it to parent genes |
| 21 | + float newSpeed = ParentSpeed + (float)((Settings.Rng.NextDouble() * 2 - 1) * (ParentSpeed * Settings.CritterMutationRate)); |
| 22 | + float newSightRadius = ParentSightRadius + (float)((Settings.Rng.NextDouble() * 2 - 1) * (ParentSightRadius * Settings.CritterMutationRate)); |
| 23 | + float newMetabolismEfficiency = ParentMetabolismEfficiency + (float)((Settings.Rng.NextDouble() * 2 - 1) * (ParentMetabolismEfficiency * Settings.CritterMutationRate)); |
| 24 | + float newReproductionThreshold = ParentReproductionThreshold + (float)((Settings.Rng.NextDouble() * 2 - 1) * (ParentReproductionThreshold * Settings.CritterMutationRate)); |
| 25 | + |
| 26 | + //clamping to max values |
| 27 | + this.Speed = Math.Max(Settings.MinCritterSpeed,Math.Min(Settings.MaxCritterSpeed, newSpeed)); |
| 28 | + this.SightRadius = Math.Max(Settings.MinCritterSightradius,Math.Min(Settings.MaxCritterSightradius, newSightRadius)); |
| 29 | + this.MetabolismEfficiency = Math.Max(Settings.MinCritterMetabolismEfficiency,Math.Min(Settings.MaxCritterMetabolismEfficiency, newMetabolismEfficiency)); |
| 30 | + this.ReproductionThreshold = Math.Max(Settings.MinCritterReproductionThreshold,Math.Min(Settings.MaxCritterReproductionThreshold, newReproductionThreshold)); |
| 31 | + } |
| 32 | + |
| 33 | + public CritterGenome(float Speed, float SightRadius, float MetabolismEfficiency, float ReproductionThreshold, bool newBorn = false) |
| 34 | + { |
| 35 | + if (newBorn) |
| 36 | + { |
| 37 | + Mutate(Speed,SightRadius,MetabolismEfficiency,ReproductionThreshold); |
| 38 | + } |
| 39 | + else |
| 40 | + { |
| 41 | + this.Speed = Speed; |
| 42 | + this.SightRadius = SightRadius; |
| 43 | + this.MetabolismEfficiency = MetabolismEfficiency; |
| 44 | + this.ReproductionThreshold = ReproductionThreshold; |
| 45 | + } |
| 46 | + |
| 47 | + } |
| 48 | + |
| 49 | + public CritterGenome() |
| 50 | + { |
| 51 | + Speed = Settings.StartingCritterSpeed; |
| 52 | + SightRadius = Settings.StartingCritterSightRadius; |
| 53 | + MetabolismEfficiency = Settings.StartingCritterMetabolismEfficiency; |
| 54 | + ReproductionThreshold = Settings.StartingCritterReproductionThreshold; |
| 55 | + } |
| 56 | + |
| 57 | + } |
| 58 | +} |
0 commit comments