Beispiel #1
0
        // Starts the algorithm
        public int[] Run()
        {
            _parentGenoms   = new int[CountEntitiesInGeneration][];
            _childrenGenoms = new int[CountEntitiesInGeneration][];

            _timeCheck?.Start();

            CreateFirstGeneration();

            for (int currGen = 0; currGen < CountGenerations; ++currGen)
            {
                DoSelection();
                CrossGeneration();
                if (UseMutation)
                {
                    MutateGeneration();
                }

                var tmp = _parentGenoms;
                _parentGenoms   = _childrenGenoms;
                _childrenGenoms = tmp;
            }

            // choose the best genom
            int bestFitness = 0;

            int[] bestGenom = null;
            foreach (var genom in _parentGenoms)
            {
                int fitRes = _fintness.Fit(genom);
                if (fitRes > bestFitness)
                {
                    bestFitness = fitRes;
                    bestGenom   = genom;
                }
            }

            _timeCheck?.Stop();

            return(bestGenom);
        }
Beispiel #2
0
        // Gets optimal permutation
        public T[] GetOptimal()
        {
            T[] result = null;
            int maxK   = 0;

            _time?.Start();

            _perm.Generate(_exh.GetSet(), perm =>
            {
                int k = _exh.OptimalСoef(perm);
                if (k > maxK)
                {
                    maxK   = k;
                    result = (T[])perm.Clone();
                }
            });

            _time?.Stop();

            return(result);
        }