private List <TyWeightsLearner> MixPopulations(List <TyWeightsLearner> oldPopulation, List <TyWeightsLearner> offspring, int generation)
        {
            List <TyWeightsLearner> tmpPopulation = new List <TyWeightsLearner>();

            tmpPopulation.AddRange(oldPopulation);
            tmpPopulation.AddRange(offspring);
            tmpPopulation.Sort(FittestSort);

            List <TyWeightsLearner> newPopulaton = new List <TyWeightsLearner>();

            for (int i = 0; i < PopulationSize; i++)
            {
                newPopulaton.Add(tmpPopulation[i]);
            }

            const int NUM_RAND_CHILDREN = 2;

            //replace the worst NUM_RAND_CHILDREN individuals by random learners:
            for (int i = 0; i < NUM_RAND_CHILDREN; i++)
            {
                TyWeightsLearner randOffspring = new TyWeightsLearner(new TyStateWeights(_random, MIN_WEIGHT, MAX_WEIGHT), generation, _individualId);
                newPopulaton[newPopulaton.Count - 1 - i] = randOffspring;
            }

            return(newPopulaton);
        }
        private void ComputeFitness(TyWeightsLearner learner, List <TyDeckHeroPair> myDeck, List <TyDeckHeroPair> enemyDeck, AbstractAgent enemyAgent)
        {
            learner.BeforeLearn();

            TyMatchSetup training = new TyMatchSetup(TycheAgent.GetLearningAgent(learner.Weights), enemyAgent);

            training.RunRounds(myDeck, enemyDeck, Rounds, MatchesPerRound);

            learner.AfterLearn(training.TotalPlays, training.Agent0Wins);
        }
        private void CsvLog(TyWeightsLearner curLearner)
        {
            _csvHelper.SetColumn(CsvHelper.Column.Id, curLearner.Id);
            _csvHelper.SetColumn(CsvHelper.Column.Generation, curLearner.GenerationBorn);
            _csvHelper.SetColumn(CsvHelper.Column.NumPlays, curLearner.NumPlays);
            _csvHelper.SetColumn(CsvHelper.Column.Current, curLearner.CurWinPercent);
            _csvHelper.SetColumn(CsvHelper.Column.Average, curLearner.AverageWinPercent);

            for (int i = 0; i < (int)TyStateWeights.WeightType.Count; i++)
            {
                var curWeightType = (TyStateWeights.WeightType)i;
                var obj           = Enum.Parse(typeof(CsvHelper.Column), curWeightType.ToString());
                var targetEnum    = (CsvHelper.Column)obj;
                _csvHelper.SetColumn(targetEnum, curLearner.Weights.GetWeight(curWeightType));
            }

            _csvLog.AddCsvEntry(_csvHelper.GetLine());
        }
        public void Clear()
        {
            _individualId  = 0;
            _globalFileLog = new List <string>();

            _csvLog    = new CsvLog();
            _csvHelper = new CsvHelper();
            _csvLog.AddCsvEntry(CsvHelper.GetCsvHeader());

            _random            = new Random();
            _currentPopulation = new List <TyWeightsLearner>();

            for (int i = 0; i < PopulationSize; i++)
            {
                var learner = new TyWeightsLearner(_random, MIN_WEIGHT, MAX_WEIGHT, 0, _individualId);
                _individualId++;
                _currentPopulation.Add(learner);
            }
        }
        /// <summary>
        /// Chooses weighted (by fitness) random weights from either A or B
        /// </summary>
        public static TyStateWeights GetCrossedWeights(TyWeightsLearner lhs, TyWeightsLearner rhs, System.Random rand)
        {
            TyStateWeights newWeights = new TyStateWeights();

            for (int i = 0; i < (int)TyStateWeights.WeightType.Count; i++)
            {
                var   factorType = (TyStateWeights.WeightType)i;
                float weight     = rhs._weights.GetWeight(factorType);

                float chanceLhs = lhs.Fitness / (lhs.Fitness + rhs.Fitness);

                if (rand.RandFloat() < chanceLhs)
                {
                    weight = lhs._weights.GetWeight(factorType);
                }

                newWeights.SetWeight(factorType, weight);
            }

            return(newWeights);
        }
        private List <TyWeightsLearner> GiveBirth(List <TyWeightsLearner> choosen, System.Random random, int generation)
        {
            List <TyWeightsLearner> children = new List <TyWeightsLearner>();

            for (int i = 0; i < OffspringSize; i++)
            {
                List <TyWeightsLearner> parentsToChoose = new List <TyWeightsLearner>(choosen);

                int firstId = random.Next(parentsToChoose.Count);
                var first   = parentsToChoose[firstId];
                parentsToChoose.RemoveAt(firstId);

                int secondId = random.Next(parentsToChoose.Count);
                var second   = parentsToChoose[secondId];

                var childWeights = TyWeightsLearner.GetCrossedWeights(first, second, random);
                childWeights = TyWeightsLearner.GetMutatedWeights(childWeights, random, MutationDeviation);

                children.Add(new TyWeightsLearner(childWeights, generation, _individualId));
                _individualId++;
            }

            return(children);
        }
 /// <summary> Sort from biggest to smallest fitness </summary>
 private int FittestSort(TyWeightsLearner x, TyWeightsLearner y)
 {
     return(y.Fitness.CompareTo(x.Fitness));
 }