Beispiel #1
0
        public async Task <bool> DoTrain(MinFunGettingStarted gettingStarted)
        {
            var firstPopulation = await _selectionHandler.ProcessSelection(null, gettingStarted.ChoromosemeLenght, Selection.Random);

            var maximumChild = new MinFuncIndividual(new double[gettingStarted.ChoromosemeLenght]);

            await Task.Run(() =>
            {
                //var threadId = Thread.CurrentThread.ManagedThreadId;
                for (int i = 0; i < gettingStarted.NumberOfGenerationRepetitions; i++)
                {
                    //threadId = Thread.CurrentThread.ManagedThreadId;
                    TryChanged.Invoke(i);
                    var parent = _selectionHandler.ProcessSelection(firstPopulation, gettingStarted.NumberOfParents, gettingStarted.SelectionList).Result;
                    for (int j = 0; j < gettingStarted.NumberOfParents - 1; j++)
                    {
                        ParentChanged.Invoke(j);
                        var random = RandomHelper.CreateRandom(0, 10);
                        if (random == 5)
                        {
                            //create childs by recombination
                            var childs = _recombinationHandler.ProcessRecombinationHandler(parent[j], parent[j + 1], gettingStarted.Recombinations).Result;

                            // Compute childs fitness to function
                            var child1New = _functionHandler.ProcessHandleFitness(childs.first, gettingStarted.ChoromosemeLenght, gettingStarted.FunctionSelected).Result;
                            var child2New = _functionHandler.ProcessHandleFitness(childs.second, gettingStarted.ChoromosemeLenght, gettingStarted.FunctionSelected).Result;


                            //Choose Max Child//
                            var chooseChild = (child1New.Fitness >= child2New.Fitness) ?
                                              child1New : child2New;

                            if (chooseChild.Fitness > maximumChild.Fitness)
                            {
                                maximumChild = chooseChild;
                                //Invoke
                                MaximumChildChanged.Invoke(maximumChild, MutationOrRecombination.Recombination, j, i);
                            }

                            // میو + لاندا
                            firstPopulation.Add(child1New);
                            firstPopulation.Add(child2New);
                        }
                        else
                        {
                            //create childs by mutation
                            var mutationChild = _mutationnHandler.ProcessMutationHandler(parent[j], gettingStarted.mutationList).Result;

                            //update child fitness => by function
                            var childUpdateFitness = _functionHandler.ProcessHandleFitness(mutationChild, gettingStarted.ChoromosemeLenght, gettingStarted.FunctionSelected).Result;

                            //Choose Max Child //

                            if (childUpdateFitness.Fitness > maximumChild.Fitness)
                            {
                                maximumChild = childUpdateFitness;
                                //Invoke
                                //MaximumChildChanged.Invoke(maximumChild, MutationOrRecombination.Mutation, j, i);
                                MaximumChildChanged.Invoke(maximumChild, MutationOrRecombination.Mutation, j, i);
                            }

                            //update case positive or not
                            _mutationnHandler.ProcessUpdateCase(mutationChild, childUpdateFitness, gettingStarted.mutationList);

                            // میو + لاندا
                            firstPopulation.Add(childUpdateFitness);
                        }
                    }
                    // update sigma
                    _mutationnHandler.ProcessUpdateStatus(gettingStarted.mutationList);
                }
                ExcetedFitness = _functionHandler.ProcessHanldeExcutedFitness(maximumChild, gettingStarted.FunctionSelected).Result;
            });

            return(true);
        }