コード例 #1
0
        public int run(GeneticRunParameters parameters)
        {
            if (!(parameters is FuzzyGeneticsRunParameters))
                throw new WrongParameterTypeException("Fuzzy Genetics only accepts Fuzzy Genetics Run Parameters");

            FuzzyGeneticsRunParameters fgParameters = (FuzzyGeneticsRunParameters)parameters;

            subGenetics = fgParameters.algorithm.newInstance(initial, fgParameters.algorithmInstanceParameters);

            int bestFitness = 0;
            int periodSinceLastImprovement = 0;

            LinkedList<int> lastFitnesses = new LinkedList<int>();
            for (int n = 0; n < fgParameters.numberToAverageScoreOver; n++) { lastFitnesses.AddFirst(0); }

            while (periodSinceLastImprovement < fgParameters.waitSinceLastImprovement)
            {
                int fitness = subGenetics.run(fgParameters.algorithmRunParameters);

                lastFitnesses.RemoveFirst();
                lastFitnesses.AddLast(fitness);

                int summedFitness = lastFitnesses.Sum();

                periodSinceLastImprovement++;
                if (bestFitness < summedFitness)
                {
                    bestFitness = summedFitness;
                    periodSinceLastImprovement = 0;
                    result = subGenetics.getAll();
                    resultPairs = subGenetics.getAllPairs();
                }

                if (fgParameters.verbose) Console.WriteLine("best of round: " + fitness + ". average = " + summedFitness / fgParameters.numberToAverageScoreOver +
                    " (" + periodSinceLastImprovement + ")");
            }

            return lastFitnesses.Last.Value;
        }