private static void SetValues(ref AdvancedFighter fighter, IEnumerable <PropertyInfo> properties, int powerlevel) { var propertiesCount = properties.Count(); while (powerlevel > 0) { foreach (var property in properties) { int max = (powerlevel + 1) / propertiesCount; if (max == 0) { max = 1; } int value = Random.Next(1, max); powerlevel -= value; if (powerlevel < 0) { break; } var current = (int)property.GetValue(fighter.Stats); property.SetValue(fighter.Stats, current + value); } } }
public void ShouldTestGeneticAlgorithm() { var population = new Population(5000, 10000, new FighterChromosome()); var fitness = new FighterFitness(); var ga = RunGeneticAlgorithm(population, fitness, 50); var fighter = new AdvancedFighter(); (ga.BestChromosome as FighterChromosome).ApplyTo(fighter); }
public double Evaluate(IChromosome chromosome) { var fighterChromosome = chromosome as FighterChromosome; if (fighterChromosome == null) { return(0); } var geneticFighter = new AdvancedFighter() { Id = Guid.NewGuid() }; fighterChromosome.ApplyTo(geneticFighter); if (geneticFighter.Stats.PowerLevel() != DesiredPowerlevel) { return((Math.Min(DesiredPowerlevel, geneticFighter.Stats.PowerLevel()) - Math.Max(DesiredPowerlevel, geneticFighter.Stats.PowerLevel())) * 1000); } var json = File.ReadAllText("../../../Data/Json/strongFighters.json"); var winCount = 0; var won = false; var fitness = 0D; do { var strongFighters = JsonConvert.DeserializeObject <IEnumerable <AdvancedFighter> >(json); var fighters = new List <IFighterStats>() { geneticFighter, }.Union(strongFighters); Engine engine = Utility.GetDefaultEngine(fighters); var result = engine.StartMatch(); won = result.Scores.First().Id == geneticFighter.Id; if (won) { winCount++; } fitness += GetFitness(geneticFighter, result); }while (won && winCount < 25); return(fitness); }
public static IFighterStats GetFighter(int offensivePowerlevel, int defensivePowerlevel, int utilityPowerlevel, Guid?team) { var fighter = new AdvancedFighter() { Id = Guid.NewGuid(), Team = team, }; AddSkills(fighter); SetValues(ref fighter, OffensiveProperties, offensivePowerlevel); SetValues(ref fighter, DefensiveProperties, defensivePowerlevel); SetValues(ref fighter, UtilityProperties, utilityPowerlevel); return(fighter); }
public static IEnumerable <IFighterStats> GetFighters(int count, Action <IStats> customStats) { for (int i = 0; i < count; i++) { var fighter = new AdvancedFighter() { Id = Guid.NewGuid(), }; AddSkills(fighter); customStats.Invoke(fighter.Stats); yield return(fighter); } }