Example #1
0
        /// <summary>
        /// Set a method that resolves the sort key used for keyset based pagination.
        /// </summary>
        /// <param name="fieldConfig">The field config.</param>
        /// <param name="sort">The <c>Sort Key</c> builder.</param>
        /// <returns>The <see cref="FieldConfig"/>.</returns>
        /// <exception cref="ArgumentNullException">If <paramref name="fieldConfig"/> or <paramref name="sort"/> is <c>NULL</c>.</exception>
        public static FieldConfig SqlSortKey(this FieldConfig fieldConfig, SortKeyDelegate sort)
        {
            if (fieldConfig == null)
            {
                throw new ArgumentNullException(nameof(fieldConfig));
            }
            if (sort == null)
            {
                throw new ArgumentNullException(nameof(sort));
            }

            return(fieldConfig.WithMetadata(nameof(SortKeyDelegate), sort));
        }
Example #2
0
        /// <summary>
        /// Set a method that resolves the sort key used for keyset based pagination.
        /// </summary>
        /// <param name="fieldType">The field type.</param>
        /// <param name="sort">The <c>Sort Key</c> builder.</param>
        /// <returns>The <see cref="FieldType"/>.</returns>
        /// <exception cref="ArgumentNullException">If <paramref name="fieldType"/> or <paramref name="sort"/> is <c>NULL</c>.</exception>
        public static FieldType SqlSortKey(this FieldType fieldType, SortKeyDelegate sort)
        {
            if (fieldType == null)
            {
                throw new ArgumentNullException(nameof(fieldType));
            }
            if (sort == null)
            {
                throw new ArgumentNullException(nameof(sort));
            }

            return(fieldType.WithMetadata(nameof(SortKeyDelegate), sort));
        }
Example #3
0
 /// <summary>
 /// Sets the <c>Sort Key</c> for the junction configuration.
 /// </summary>
 /// <param name="sortKey">The Sort Key.</param>
 /// <returns>The <see cref="SqlJunctionConfig"/>.</returns>
 public SqlJunctionConfigBuilder SortKey(SortKeyDelegate sortKey)
 {
     SqlJunctionConfig.SortKey = sortKey;
     return(this);
 }
Example #4
0
        public static List <TGene> Tournament(CreateDelegate fnGenerateParent, CrossoverDelegate fnCrossover,
                                              CompeteDelegate fnCompete, TournamentDisplayDelegate fnDisplay, SortKeyDelegate fnSortKey,
                                              int numParents = 10, int maxGenerations = 100)
        {
            var pool = Enumerable.Range(0, 1 + numParents * numParents)
                       .Select(x => new Tuple <List <TGene>, int[]>(fnGenerateParent(), new[] { 0, 0, 0 })).ToList();
            var best      = pool[0].Item1;
            var bestScore = pool[0].Item2;

            int GetSortKey(Tuple <List <TGene>, int[]> x) => fnSortKey(x.Item1, x.Item2[(int)CompetitionResult.Win],
                                                                       x.Item2[(int)CompetitionResult.Tie], x.Item2[(int)CompetitionResult.Loss]);

            for (var generation = 0; generation < maxGenerations; generation++)
            {
                for (var i = 0; i < pool.Count; i++)
                {
                    for (var j = 0; j < pool.Count; j++)
                    {
                        if (i == j)
                        {
                            continue;
                        }
                        var playerA = pool[i].Item1;
                        var scoreA  = pool[i].Item2;
                        var playerB = pool[j].Item1;
                        var scoreB  = pool[j].Item2;
                        var result  = (int)fnCompete(playerA, playerB);
                        scoreA[result]++;
                        scoreB[2 - result]++;
                    }
                }

                pool = pool.OrderByDescending(GetSortKey).ToList();
                if (GetSortKey(pool[0]) > GetSortKey(new Tuple <List <TGene>, int[]>(best, bestScore)))
                {
                    best      = pool[0].Item1;
                    bestScore = pool[0].Item2;
                    fnDisplay(best, bestScore[(int)CompetitionResult.Win], bestScore[(int)CompetitionResult.Tie],
                              bestScore[(int)CompetitionResult.Loss], generation);
                }

                var parents = Enumerable.Range(0, numParents).Select(i => pool[i].Item1).ToList();
                pool = (from i in Enumerable.Range(0, parents.Count)
                        from j in Enumerable.Range(0, parents.Count)
                        where i != j
                        select new Tuple <List <TGene>, int[]>(fnCrossover(parents[i], parents[j]), new[] { 0, 0, 0 }))
                       .ToList();
                pool.AddRange(parents.Select(parent => new Tuple <List <TGene>, int[]>(parent, new[] { 0, 0, 0 })));
                pool.Add(new Tuple <List <TGene>, int[]>(fnGenerateParent(), new[] { 0, 0, 0 }));
            }

            return(best);
        }