<commit_before>﻿using Draughts.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Draughts.Ai.Trainer
{
    class Program
    {
        private const int generationCount = 20;
        
        private const int iterationCount = 10;

        static void Main(string[] args)
        {
            Random random = new Random();
            var contestants = Enumerable.Range(0, generationCount)
                .Select(i => new Contestant<WeightedAiGamePlayer>(
                    new WeightedAiGamePlayer(random))
                ).ToList();

            var gameMatch = new GameMatch();
            foreach (var i in Enumerable.Range(0, iterationCount))
            {
                int gamesPlayed = 0;
                int gamesDrawn = 0;

                Console.WriteLine($"Iteration {i} underway.");

                Parallel.ForEach(contestants, contestant =>
                {
                    foreach (var opponent in contestants.Where(c => c != contestant).ToList())
                    {

                        var matchResult = gameMatch.CompleteMatch(
                            GameStateFactory.StandardStartGameState(),
                            contestant.GamePlayer,
                            opponent.GamePlayer);

                        var _count = Interlocked.Increment(ref gamesPlayed);
                        if (_count % 100 == 0)
                        {
                            Console.WriteLine($"{_count} games played. {gamesDrawn} games drawn.");
                        }

                        contestant.IncrementMatch();
                        opponent.IncrementMatch();
                        if (matchResult == GameMatchOutcome.WhiteWin)
                        {
                            contestant.IncrementWin();
                        }
                        else if (matchResult == GameMatchOutcome.BlackWin)
                        {
                            opponent.IncrementWin();
                        }
                        else if (matchResult == GameMatchOutcome.Draw)
                        {
                            contestant.IncrementDraw();
                            opponent.IncrementDraw();
                            Interlocked.Increment(ref gamesDrawn);
                        }
                    }
                });

                Console.WriteLine("Matches complete.");

                var winningContestant = contestants.OrderByDescending(c => c.Wins).First();

                Console.WriteLine($"Top winner won {winningContestant.Wins} matches.");

                winningContestant.ResetStats();
                var nextContestants = new List<Contestant<WeightedAiGamePlayer>>();
                nextContestants.Add(winningContestant);
                foreach (var contestantI in Enumerable.Range(0, generationCount - 1))
                {
                    var spawnContestant = new Contestant<WeightedAiGamePlayer>(winningContestant.GamePlayer.SpawnNewWeightedAiGamePlayer());
                    nextContestants.Add(spawnContestant);
                }
                contestants = nextContestants;
<commit_msg>More candidates in generation. Now top half of generation are reused.<commit_after>﻿using Draughts.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Draughts.Ai.Trainer
{
    class Program
    {
        private const int generationCount = 40;

        private const int iterationCount = 10;

        static void Main(string[] args)
        {
            Random random = new Random();
            var contestants = Enumerable.Range(0, generationCount)
                .Select(i => new Contestant<WeightedAiGamePlayer>(
                    new WeightedAiGamePlayer(random))
                ).ToList();

            var gameMatch = new GameMatch();
            foreach (var i in Enumerable.Range(0, iterationCount))
            {
                int gamesPlayed = 0;
                int gamesDrawn = 0;

                Console.WriteLine($"Iteration {i} underway.");

                Parallel.ForEach(contestants, contestant =>
                {
                    foreach (var opponent in contestants.Where(c => c != contestant).ToList())
                    {

                        var matchResult = gameMatch.CompleteMatch(
                            GameStateFactory.StandardStartGameState(),
                            contestant.GamePlayer,
                            opponent.GamePlayer);

                        var _count = Interlocked.Increment(ref gamesPlayed);
                        if (_count % 100 == 0)
                        {
                            Console.WriteLine($"{_count} games played. {gamesDrawn} games drawn.");
                        }

                        contestant.IncrementMatch();
                        opponent.IncrementMatch();
                        if (matchResult == GameMatchOutcome.WhiteWin)
                        {
                            contestant.IncrementWin();
                        }
                        else if (matchResult == GameMatchOutcome.BlackWin)
                        {
                            opponent.IncrementWin();
                        }
                        else if (matchResult == GameMatchOutcome.Draw)
                        {
                            contestant.IncrementDraw();
                            opponent.IncrementDraw();
                            Interlocked.Increment(ref gamesDrawn);
                        }
                    }
                });

                Console.WriteLine("Matches complete.");

                var winningContestant = contestants.OrderByDescending(c => c.Wins).First();

                Console.WriteLine($"Top winner won {winningContestant.Wins} matches.");

                var nextContestants = new List<Contestant<WeightedAiGamePlayer>>();
                foreach (var contestantI in contestants.OrderByDescending(c => c.Wins).Take(generationCount / 2))
                {
                    var spawnContestant = new Contestant<WeightedAiGamePlayer>(winningContestant.GamePlayer.SpawnNewWeightedAiGamePlayer());
                    nextContestants.Add(spawnContestant);
                    nextContestants.Add(contestantI);
                    contestantI.ResetStats();
                }
                contestants = nextContestants;

{'fraction_non_alphanumeric': 0.08045801526717557, 'fraction_numerical': 0.004122137404580153, 'mean_word_length': 1.3214032600992205, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1337673', 'n_tokens_mistral': 1575, 'n_tokens_neox': 1460, 'n_words': 298}