MerchantRPG.GeneticParty.Processing.PartyFitness.Translate C# (CSharp) Method

Translate() public method

public Translate ( IChromosome chromosome ) : string
chromosome IChromosome
return string
        public string Translate(IChromosome chromosome)
        {
            var chromValue = (chromosome as ShortArrayChromosome).Value;
            var description = new ChromosomeDescription(null, 0, 0, 0);
            foreach(var desc in descriptions)
                if (chromValue.SequenceEqual(desc.Key))
                {
                    description = desc.Value;
                    break;
                }

            descriptions.Clear();
            descriptions.Add(chromValue, description);

            if (description.InvalidGenes > 0)
                return "Invalid genes: " + description.InvalidGenes;

            var sb = new StringBuilder();

            for (int heroI = 0; heroI < simulator.PartySize; heroI++)
            {
                if (heroI == description.FrontRowCount && simulator.PartySize > 1)
                    sb.AppendLine("Back row:");

                var build = description.Builds[heroI];
                var hero = Library.Heroes[build.HeroType];
                sb.Append(hero.Name + ": ");

                for(int i = 0; i < build.Items.Length; i++)
                {
                    if (i > 0)
                        sb.Append(", ");
                    if (simulator.ItemChoices(hero, i) == 0)
                        continue;
                    sb.Append(simulator.ItemData(hero, i, build.Items[i]).Name);
                }

                sb.Append(", ");
                sb.Append(build.EnhancmentCounts[0]);
                sb.Append(" x ");
                sb.Append(potionName(build.EnhancmentTypes[0]));

                sb.Append(", ");
                sb.Append(build.EnhancmentCounts[1]);
                sb.Append(" x ");
                sb.Append(potionName(build.EnhancmentTypes[1]));
                sb.AppendLine();
            }

            sb.Append(description.Deaths + " deaths");
            return sb.ToString();
        }

Usage Example

コード例 #1
0
        private static void HardestOpponent()
        {
            var challenges = Library.Monsters.ToDictionary(x => x, x => 0.0);

            while (true)
            {
                var minScore = challenges.Values.Min();
                var monster = challenges.First(x => x.Value == minScore).Key;

                var simulator = monster.MaxPartyMembers > 1 ?
                (ASimulator)new PartySimulator(monster, 40, 40, false) :
                (ASimulator)new SingleHeroSimulator(monster, 40, 40);

                var fitnessEvaluator = new PartyFitness(simulator);
                var population = new Population(20,
                    new ShortArrayChromosome(fitnessEvaluator.ChromosomeLength, fitnessEvaluator.ChromosomeMaxValue),
                    fitnessEvaluator,
                    new RankSelection());
                population.RandomSelectionPortion = 0.15;

                double lastFitness = 0;
                int stagnationLimit = minScore < 1e-4 ? 1000 : 10000;

                for (int stagnation = 0; stagnation < stagnationLimit; stagnation++)
                {
                    population.RunEpoch();
                    if (population.BestChromosome.Fitness > lastFitness)
                    {
                        lastFitness = population.BestChromosome.Fitness;
                        stagnation = 0;

                        if (lastFitness > challenges[monster])
                        {
                            challenges[monster] = lastFitness;

                            Console.WriteLine(monster.Name);
                            Console.Write(fitnessEvaluator.Translate(population.BestChromosome).Trim());
                            Console.WriteLine(" " + population.BestChromosome.Fitness);
                            Console.WriteLine();
                        }
                    }

                    population.MutationRate = stagnation > 1000 ?
                        Math.Min(stagnation / 3000.0, 0.25) :
                        0.1;
                }
            }
        }
All Usage Examples Of MerchantRPG.GeneticParty.Processing.PartyFitness::Translate