public Population(int count)
 {
     Generation = 0;
     Players    = new  List <PopulationItem> ();
     for (int i = 0; i < count; i++)
     {
         var item = new PopulationItem();
         item.Player = AIGeneticPlayer.CreateRandom();
         Players.Add(item);
     }
 }
Esempio n. 2
0
    public override Player Clone()
    {
        AIGeneticPlayer clone = new AIGeneticPlayer();

        clone._whereToGoDecider   = _whereToGoDecider.Clone();
        clone._humansCountDecider = _humansCountDecider.Clone();
        clone._useAnyResourceFromTopCardDecider = _useAnyResourceFromTopCardDecider.Clone();
        clone._resourceFromTopCardDecider       = _resourceFromTopCardDecider.Clone();
        clone._resourceFromCharityDecider       = _resourceFromCharityDecider.Clone();
        clone._resourceFromInstrumentsDecider   = _resourceFromInstrumentsDecider.Clone();
        clone._leaveHungryDecider = _leaveHungryDecider.Clone();
        return(clone);
    }
Esempio n. 3
0
    public static AIGeneticPlayer CreateRandom()
    {
        AIGeneticPlayer player = new AIGeneticPlayer();

        player._whereToGoDecider   = GetRandomDecider(WhereToGoInputsCount, AdditionalNeuronsCount, 16);
        player._humansCountDecider = GetRandomDecider(GetUsedHumansInputsCount, AdditionalNeuronsCount, 9);
        player._useAnyResourceFromTopCardDecider = GetRandomDecider(GetAnyResourceFromTopCardInputsCount, AdditionalNeuronsCount, 2);
        player._resourceFromTopCardDecider       = GetRandomDecider(GetResourceFromTopCardInputsCount, AdditionalNeuronsCount, 4);
        player._resourceFromCharityDecider       = GetRandomDecider(GetResourceFromCharityInputsCount, AdditionalNeuronsCount, 6);
        player._resourceFromInstrumentsDecider   = GetRandomDecider(GetResourceFromInstrumentsInputsCount, AdditionalNeuronsCount, 4);
        player._leaveHungryDecider = GetRandomDecider(GetLeaveHungryInputsCount, AdditionalNeuronsCount, 2);
        return(player);
    }
Esempio n. 4
0
    public static AIGeneticPlayer CreateFromCrossover(AIGeneticPlayer parent1, AIGeneticPlayer parent2)
    {
        const float     MutationRate = 0.005f;
        AIGeneticPlayer player       = new AIGeneticPlayer();

        player._whereToGoDecider   = GetDeciderFromCrossover(parent1._whereToGoDecider, parent2._whereToGoDecider, MutationRate, WhereToGoInputsCount, AdditionalNeuronsCount, 16);
        player._humansCountDecider = GetDeciderFromCrossover(parent1._humansCountDecider, parent2._humansCountDecider, MutationRate, GetUsedHumansInputsCount, AdditionalNeuronsCount, 9);
        player._useAnyResourceFromTopCardDecider = GetDeciderFromCrossover(parent1._useAnyResourceFromTopCardDecider, parent2._useAnyResourceFromTopCardDecider, MutationRate, GetAnyResourceFromTopCardInputsCount, AdditionalNeuronsCount, 2);
        player._resourceFromTopCardDecider       = GetDeciderFromCrossover(parent1._resourceFromTopCardDecider, parent2._resourceFromTopCardDecider, MutationRate, GetResourceFromTopCardInputsCount, AdditionalNeuronsCount, 4);
        player._resourceFromCharityDecider       = GetDeciderFromCrossover(parent1._resourceFromCharityDecider, parent2._resourceFromCharityDecider, MutationRate, GetResourceFromCharityInputsCount, AdditionalNeuronsCount, 6);
        player._resourceFromInstrumentsDecider   = GetDeciderFromCrossover(parent1._resourceFromInstrumentsDecider, parent2._resourceFromInstrumentsDecider, MutationRate, GetResourceFromInstrumentsInputsCount, AdditionalNeuronsCount, 4);
        player._leaveHungryDecider = GetDeciderFromCrossover(parent1._leaveHungryDecider, parent2._leaveHungryDecider, MutationRate, GetLeaveHungryInputsCount, AdditionalNeuronsCount, 2);
        return(player);
    }
    private IEnumerator RunningGeneration(int generationsCount)
    {
        for (int generationInd = 0; generationInd < generationsCount; generationInd++)
        {
            Thread thread = new Thread(new ThreadStart(() => {
                PlayGeneration(_population.Players, GamesPerPlayer, (maxScores, averageScores, model) => {
                    for (int i = 0; i < _population.Players.Count; i++)
                    {
                        _population.Players[i].MaxScore     = maxScores[i];
                        _population.Players[i].AverageScore = averageScores[i];
                        _bestGamePlayerModel = model;
                    }
                });
            }));
            thread.Start();
            while (thread.IsAlive)
            {
                yield return(new WaitForEndOfFrame());
            }
            _population.Generation++;

            _population.Players.Sort((player1, player2) => {
                return(player1.AverageScore.CompareTo(player2.AverageScore));
            });

            // Remove worst genes.
            int DeathCount = Mathf.RoundToInt(DeathRate * _population.Players.Count);
            for (int i = 0; i < DeathCount; i++)
            {
                _population.Players.RemoveAt(0);
            }

            // Add best genes.
            int ReproductiveCount = Mathf.RoundToInt(ReproductivePercent * _population.Players.Count);
            for (int i = 0; i < DeathCount; i++)
            {
                int             parent1Ind = _population.Players.Count - UnityEngine.Random.Range(0, ReproductiveCount) - 1;
                int             parent2Ind = _population.Players.Count - UnityEngine.Random.Range(0, ReproductiveCount) - 1;
                AIGeneticPlayer child      = AIGeneticPlayer.CreateFromCrossover(_population.Players[parent1Ind].Player, _population.Players[parent2Ind].Player);
                PopulationItem  childItem  = new PopulationItem();
                childItem.Player = child;
                _population.Players.Insert(0, childItem);
            }
            UpdateView();
        }
    }
Esempio n. 6
0
    void PlayMatch()
    {
        List <Player> players = new List <Player> ()
        {
            new HumanPlayer(_view.TurnView),
            AIGeneticPlayer.CreateRandom(),
            AIGeneticPlayer.CreateRandom(),
            new AIRandomPlayer()
        };

        _game = new Game(players);
        _view.Init(_game);
        Thread thread = new Thread(new ThreadStart(() => { _game.Play(() => {
                Debug.Log("Match ended");
            }); }));

        thread.Start();
    }