コード例 #1
0
        private float GetFitness(IChromosome c)
        {
            AiSettingsChromosome chromosome = (AiSettingsChromosome)c;

            int aiSettings = chromosome.ToInt();
            AiEvolutionaryAlgorithmData toDelete = null;
            float fitness = 0;

            foreach (AiEvolutionaryAlgorithmData data in settingsScoreData)
            {
                if (data.aiSettings == aiSettings)
                {
                    fitness  = data.score;
                    toDelete = data;
                }
            }

            float maxScore    = ScoreManager.Instance.GetTheHighestScoreValue();
            float playerScore = ScoreManager.Instance.GetScore(0);

            fitness     = fitness / maxScore;
            playerScore = playerScore / maxScore;

            fitness = 1 - Math.Abs(playerScore - fitness);

            if (toDelete != null)
            {
                settingsScoreData.Remove(toDelete);
            }

            return(fitness);
        }
コード例 #2
0
        public void OptimizeAI()
        {
            if (frequency != turnsSinceLastOptimization)
            {
                turnsSinceLastOptimization++;
                return;
            }

            GenerateScoreData();

            int maxSize = GetMaxSize();
            AiSettingsChromosome intelligenceChromosome = new AiSettingsChromosome(3);
            Population           population             = new Population(maxSize, maxSize, intelligenceChromosome);

            FuncFitness fitness;

            if (HistoryManagr.Instance.savingHistory == true)
            {
                fitness = new FuncFitness((c) => { return(GetHistoryFitness(c)); });
            }
            else
            {
                fitness = new FuncFitness((c) => { return(GetFitness(c)); });
            }

            var selection   = new EliteSelection();
            var crossover   = new UniformCrossover(0.5f);
            var mutation    = new DisplacementMutation();
            var termination = new GenerationNumberTermination(2);

            var ga = new GeneticAlgorithm(population, fitness, selection, crossover, mutation);
            List <IChromosome> savedChromosomes = new List <IChromosome>();

            ga.Termination    = termination;
            ga.GenerationRan += (sender, e) =>
            {
                if (ga.GenerationsNumber == 2)
                {
                    savedChromosomes = (List <IChromosome>)ga.Population.CurrentGeneration.Chromosomes;
                }
            };

            ga.Start();

            turnsSinceLastOptimization = 0;
            settingsScoreData.Clear();
            settingsData.Clear();

            SetGeneratedData(savedChromosomes);
            foreach (PlayerData player in PlayerManager.Instance.players)
            {
                EventSaverManager.Instance.SaveDDAEvolutionAIEvent(player.id);
            }
        }
コード例 #3
0
        private float GetHistoryFitness(IChromosome c)
        {
            AiSettingsChromosome chromosome = (AiSettingsChromosome)c;

            int   aiSettings = chromosome.ToInt();
            float fitness    = 0;
            AiEvolutionaryAlgorithmData toDelete = null;

            foreach (AiEvolutionaryAlgorithmData data in settingsScoreData)
            {
                if (data.aiSettings == aiSettings)
                {
                    fitness  = data.score;
                    toDelete = data;
                }
            }

            if (toDelete != null)
            {
                settingsScoreData.Remove(toDelete);
            }

            return(fitness);
        }