Esempio n. 1
0
        /// <summary>
        /// Selects a new solution for reproduction.
        /// </summary>
        /// <returns></returns>
        public int Select(TProblem problem, TObjective objective, Individual <TSolution, TFitness>[] population, System.Collections.Generic.ISet <int> exclude)
        {
            var tournamentSizeInt = (int)System.Math.Ceiling(((_tournamentSize / 100f) * (double)population.Length));
            var tempPop           = new List <Tuple <int, Individual <TSolution, TFitness> > >(tournamentSizeInt);

            while (tempPop.Count < tournamentSizeInt)
            { // keep looping until enough individuals are selected or until no more are available.
                var idx = _random.Generate(population.Length);
                if (exclude == null || !exclude.Contains(idx))
                { // do not tournament excluded solutions.
                    tempPop.Add(new Tuple <int, Individual <TSolution, TFitness> >(idx, population[idx]));
                }
            }

            // sort the population..
            tempPop.Sort((x, y) =>
            {
                return(objective.CompareTo(problem, y.Item2.Fitness, x.Item2.Fitness));
            });

            // choose a candidate.
            for (var idx = 0; idx < tempPop.Count; idx++)
            {     // choose a candidate.
                if (_random.Generate(1.0f) < _tournamentProbability)
                { // candidate choosen!
                    return(tempPop[idx].Item1);
                }
            }
            return(-1);
        }
Esempio n. 2
0
 public bool Has(string arg)
 {
     return(argsFound.Contains(arg));
 }