public void Crossingover(AbstractTrack firstParent, AbstractTrack secondParent, AbstractTrack firstChild,
                                 AbstractTrack secondChild)
        {
            if (_index == _proxyCrossingoverList.Count)
            {
                _index = 0;
            }
            double currentResult = firstParent.GetTrackLength() < secondParent.GetTrackLength() ? firstParent.GetTrackLength() : secondParent.GetTrackLength();
            double oneProcentGain = (currentResult - _bestResult) / 100;

            DateTime startTime = DateTime.Now;
            _proxyCrossingoverList[_index].GetCrossingover().Crossingover(firstParent, secondParent, firstChild, secondChild);
            DateTime endTime = DateTime.Now;
            TimeSpan time = endTime - startTime;
            _proxyCrossingoverList[_index].AddOperationTime(time);

            double newResult = firstChild.GetTrackLength() < secondChild.GetTrackLength() ? firstChild.GetTrackLength() : secondChild.GetTrackLength();
            double procentGain;
            if (newResult < currentResult)
            {
                procentGain = 100 - (newResult - _bestResult) / oneProcentGain;
                _proxyCrossingoverList[_index].AddGoodStart();
            }
            else
            {
                procentGain = 0;
                _proxyCrossingoverList[_index].AddBadStart();
            }
            _proxyCrossingoverList[_index].IncreaseProgress(procentGain);
            _index++;
        }
 public void Mutation(AbstractTrack mutant)
 {
     int countOfAlleles = mutant.Genotype.Length;
     Random randomPosition = new Random();
     int firstPosition = randomPosition.Next(countOfAlleles);
     int secondPosition = randomPosition.Next(countOfAlleles);
     while (firstPosition == secondPosition)
     {
         secondPosition = randomPosition.Next(countOfAlleles);
     }
     int chromosome = mutant.Genotype[secondPosition];
     mutant.Genotype[secondPosition] = mutant.Genotype[firstPosition];
     mutant.Genotype[firstPosition] = chromosome;
     int thirdPosition = randomPosition.Next(countOfAlleles);
     while (thirdPosition == firstPosition || thirdPosition == secondPosition)
     {
         thirdPosition = randomPosition.Next(countOfAlleles);
     }
     int fourthPosition = randomPosition.Next(countOfAlleles);
     while (fourthPosition == firstPosition || fourthPosition == secondPosition || fourthPosition == thirdPosition)
     {
         fourthPosition = randomPosition.Next(countOfAlleles);
     }
     chromosome = mutant.Genotype[thirdPosition];
     mutant.Genotype[thirdPosition] = mutant.Genotype[fourthPosition];
     mutant.Genotype[fourthPosition] = chromosome;
     mutant.TypeOfSelection = MutationName;
 }
        public void Mutation(AbstractTrack mutant)
        {
            if (_index == _proxyMutationList.Count)
            {
                _index = 0;
            }
            double currentResult = mutant.GetTrackLength();
            double oneProcentGain = (currentResult - _bestResult)/100;

            DateTime startTime = DateTime.Now;
            _proxyMutationList[_index].GetMutation().Mutation(mutant);
            DateTime endTime = DateTime.Now;
            TimeSpan time = endTime - startTime;
            _proxyMutationList[_index].AddOperationTime(time);

            double newResult = mutant.GetTrackLength();
            double procentGain;
            if (newResult < currentResult)
            {
                procentGain = 100 - (newResult - _bestResult) / oneProcentGain;
                _proxyMutationList[_index].AddGoodStart();
            }
            else
            {
                procentGain = 0;
                _proxyMutationList[_index].AddBadStart();
            }
            _proxyMutationList[_index].IncreaseProgress(procentGain);
            _index++;
        }
 private static void ChildrenFill(AbstractTrack firstParent, AbstractTrack secondParent, AbstractTrack child)
 {
     int countOfAlleles = firstParent.Genotype.Length;
     int m = 0, n = countOfAlleles -1, k = 0, l = countOfAlleles - 1;
     while (child.Genotype.Contains(AbstractTrack.AggregateOfGenotype))
     {
         for (int i = m; i < countOfAlleles; i++)
         {
             if (!child.Genotype.Contains(firstParent.Genotype[i]))
             {
                 child.Genotype[k] = firstParent.Genotype[i]; // todo где то тут выход за пределы массива, но пока не обнаружен
                 k++;
                 m = i;
                 break;
             }
         }
         for (int i = n; i > 0; i--)
         {
             if (!child.Genotype.Contains(secondParent.Genotype[i]))
             {
                 child.Genotype[l] = secondParent.Genotype[i];
                 l--;
                 n = i;
                 break;
             }
         }
     }
     child.TypeOfCrossingover = CrossingoverName;
 }
 public void Selection(AbstractTrack[] parentTracks, AbstractTrack[] childTracks)
 {
     for (int i = 0; i < parentTracks.Length; i++)
     {
         if (parentTracks[i].GetTrackLength() > childTracks[i].GetTrackLength())
         {
             parentTracks[i] = childTracks[i];
         }
         parentTracks[i].TypeOfSelection = SelectionName;
     }
 }
        public void Selection(AbstractTrack[] parentTracks, AbstractTrack[] childTracks)
        {
            double sumOfProgress = _proxySelectionList.Sum(proxySelection => proxySelection.GetProgress());
            double[] sectorsOfWheel = new double[_proxySelectionList.Count];
            for (int i = 0; i < _proxySelectionList.Count; i++)
            {
                sectorsOfWheel[i] = _proxySelectionList[i].GetProgress() / sumOfProgress * 100; // формула для поиска размера сектров колеса рулетки
            }

            int fallenSector = _random.Next(100);
            double sum = 0;
            for (int j = 0; j < sectorsOfWheel.Length; j++)
            {
                sum += sectorsOfWheel[j];
                if (sum > fallenSector)
                {
                    int countOfTracks = parentTracks.Length;
                    AbstractTrack[] allTracks = new AbstractTrack[parentTracks.Length * 2];
                    for (int i = 0; i < countOfTracks; i++)
                    {
                        allTracks[i] = parentTracks[i].Clone();
                        allTracks[i + countOfTracks] = childTracks[i].Clone();
                    }
                    Array.Sort(allTracks);
                    double currentResult = allTracks[0].GetTrackLength();

                    DateTime startTime = DateTime.Now;
                    _proxySelectionList[j].GetSelection().Selection(parentTracks, childTracks);
                    DateTime endTime = DateTime.Now;
                    TimeSpan time = endTime - startTime;
                    _proxySelectionList[j].AddOperationTime(time);

                    AbstractTrack[] newParentTracks = new AbstractTrack[parentTracks.Length];
                    for (int i = 0; i < countOfTracks; i++)
                    {
                        newParentTracks[i] = parentTracks[i].Clone();
                    }
                    Array.Sort(newParentTracks);
                    double sumOfParentTracks = newParentTracks.Sum(track => track.GetTrackLength());
                    double newResult = newParentTracks[0].GetTrackLength();
                    double sumOfBestResult = newResult * newParentTracks.Length;
                    if ((0 == newResult.CompareTo(currentResult)) && (0 != sumOfParentTracks.CompareTo(sumOfBestResult)))
                    {
                        _proxySelectionList[j].AddGoodStart();
                    }
                    else
                    {
                        _proxySelectionList[j].AddBadStart();
                    }
                    break;
                }
            }
        }
 public void Mutation(AbstractTrack mutant)
 {
     Random randomPosition = new Random();
     int firstPosition = randomPosition.Next(mutant.Genotype.Length - 1);
     int secondPosition = randomPosition.Next(mutant.Genotype.Length - 1);
     while (firstPosition == secondPosition)
     {
         secondPosition = randomPosition.Next(mutant.Genotype.Length - 1);
     }
     int chromosome = mutant.Genotype[secondPosition];
     mutant.Genotype[secondPosition] = mutant.Genotype[firstPosition];
     mutant.Genotype[firstPosition] = chromosome;
     mutant.TypeOfSelection = MutationName;
 }
 public void Crossingover(AbstractTrack firstParent, AbstractTrack secondParent, AbstractTrack firstChild,
                          AbstractTrack secondChild)
 {
     int countOfAlleles = firstParent.Genotype.Length;
     Random randomPositionOne = new Random();
     Random randomPositionTwo = new Random();
     int positionOne = randomPositionOne.Next(1, countOfAlleles - 1);
     int positionTwo = randomPositionTwo.Next(1, countOfAlleles - 1);
     while (positionOne == positionTwo)
     {
         positionTwo = randomPositionTwo.Next(1, countOfAlleles);
     }
     if (positionOne > positionTwo)
     {
         int buff = positionOne;
         positionOne = positionTwo;
         positionTwo = buff;
     }
     for (int i = positionOne; i <= positionTwo; i++)
     {
         firstChild.Genotype[i] = firstParent.Genotype[i];
         secondChild.Genotype[i] = secondParent.Genotype[i];
     }
     for (int i = 0; i < countOfAlleles; i++)
     {
         if (firstChild.Genotype[i] == AbstractTrack.AggregateOfGenotype)
         {
             for (int j = 0; j < countOfAlleles; j++)
             {
                 if (!firstChild.Genotype.Contains(secondParent.Genotype[j]))
                 {
                     firstChild.Genotype[i] = secondParent.Genotype[j];
                     break;
                 }
             }
             for (int j = 0; j < countOfAlleles; j++)
             {
                 if (!secondChild.Genotype.Contains(firstParent.Genotype[j]))
                 {
                     secondChild.Genotype[i] = firstParent.Genotype[j];
                     break;
                 }
             }
         }
     }
     firstChild.TypeOfCrossingover = CrossingoverName;
     secondChild.TypeOfCrossingover = CrossingoverName;
 }
示例#9
0
 public GEngine(AbstractTrack[] tracks, int pCrossingover, int pMutation, IFitnessFunction fitnessFunction, IMutation mutation, ICrossingover crossingover, ISelection selection)
 {
     _countOfPerson = tracks.Length;
     _tracks = new AbstractTrack[_countOfPerson];
     _tracks = tracks;
     _pCrossingover = pCrossingover;
     _pMutation = pMutation;
     _fitnessFunction = fitnessFunction;
     _mutation = mutation;
     _crossingover = crossingover;
     _selection = selection;
     _geneticsDataSet = new DB_GeneticsDataSet();
     _launchId = Guid.NewGuid();
     _launchTableAdapter = new DB_GeneticsDataSetTableAdapters.LaunchesTableAdapter();
     _personsTableAdapter = new DB_GeneticsDataSetTableAdapters.PersonsTableAdapter();
 }
        public bool Fitness(AbstractTrack[] tracks)
        {
            double[] results = new double[tracks.Length];
            for (int i = 0; i < tracks.Length; i++)
            {
                results[i] = tracks[i].GetTrackLength();
            }
            _bestResult = results.Min();

            _actualCountOfReps++;
            if (_actualCountOfReps == _specifiedCountOfReps)
            {
                return false;
            }
            return true;
        }
        public void MyTestInitialize()
        {
            int[,] ribs = new int[6, 2] { { 0, 1 }, { 0, 2 }, { 0, 3 }, { 1, 2 }, { 1, 3 }, { 2, 3 } };
            double[] weights = new double[] { 3, 5, 10, 7, 8, 9 };
            _graph = new UndirectedConnectedGraph(ribs, weights);

            _parents = new AbstractTrack[CountOfTracks];
            _parents[0] = new UnclosedTrack(new int[] { 0, 1, 3, 2 }, _graph);
            _parents[1] = new UnclosedTrack(new int[] { 1, 0, 2, 3 }, _graph);
            _parents[2] = new UnclosedTrack(new int[] { 2, 0, 3, 1 }, _graph);

            _childs = new AbstractTrack[CountOfTracks];
            _childs[0] = new UnclosedTrack(new int[] { 2, 1, 0, 3 }, _graph);
            _childs[1] = new UnclosedTrack(new int[] { 0, 3, 2, 1 }, _graph);
            _childs[2] = new UnclosedTrack(new int[] { 0, 1, 2, 3 }, _graph);
        }
 public bool Fitness(AbstractTrack[] tracks)
 {
     double[] results = new double[tracks.Length];
     for (int i = 0; i < tracks.Length; i++)
     {
         results[i] = tracks[i].GetTrackLength();
     }
     double minTracksResult = results.Min();
     _actualCountOfReps++;
     if ((0 == _bestResult.CompareTo(minTracksResult)) || (1 == _bestResult.CompareTo(minTracksResult)))
     {
         _bestResult = minTracksResult;
         return false;
     }
     return true;
 }
 public void Selection(AbstractTrack[] parentTracks, AbstractTrack[] childTracks)
 {
     int countOfTracks = parentTracks.Length;
     AbstractTrack[] allTracks = new AbstractTrack[countOfTracks * 2];
     for (int i = 0; i < countOfTracks; i++)
     {
         allTracks[i] = parentTracks[i].Clone();
         allTracks[i + countOfTracks] = childTracks[i].Clone();
     }
     Array.Sort(allTracks);
     for (int i = 0; i < countOfTracks; i++)
     {
         parentTracks[i] = allTracks[i];
         parentTracks[i].TypeOfSelection = SelectionName;
     }
 }
        public void Crossingover(AbstractTrack firstParent, AbstractTrack secondParent, AbstractTrack firstChild,
                                 AbstractTrack secondChild)
        {
            double sumOfProgress = _proxyCrossingoverList.Sum(proxyCrossingover => proxyCrossingover.GetProgress());
            double[] sectorsOfWheel = new double[_proxyCrossingoverList.Count];
            for (int i = 0; i < _proxyCrossingoverList.Count; i++)
            {
                sectorsOfWheel[i] = _proxyCrossingoverList[i].GetProgress() / sumOfProgress * 100; // формула для поиска размера сектров колеса рулетки
            }

            int fallenSector = _random.Next(100);
            double sum = 0;
            for (int j = 0; j < sectorsOfWheel.Length; j++)
            {
                sum += sectorsOfWheel[j];
                if (sum > fallenSector)
                {
                    double currentResult = firstParent.GetTrackLength() < secondParent.GetTrackLength() ? firstParent.GetTrackLength() : secondParent.GetTrackLength();
                    double oneProcentGain = currentResult / 100;

                    DateTime startTime = DateTime.Now;
                    _proxyCrossingoverList[j].GetCrossingover().Crossingover(firstParent, secondParent, firstChild, secondChild);
                    DateTime endTime = DateTime.Now;
                    TimeSpan time = endTime - startTime;
                    _proxyCrossingoverList[j].AddOperationTime(time);

                    double newResult = firstChild.GetTrackLength() < secondChild.GetTrackLength() ? firstChild.GetTrackLength() : secondChild.GetTrackLength();
                    double procentGain;
                    if (newResult < currentResult)
                    {
                        procentGain = 100 - (newResult / oneProcentGain);
                        _proxyCrossingoverList[j].AddGoodStart();
                    }
                    else
                    {
                        procentGain = 0;
                        _proxyCrossingoverList[j].AddBadStart();
                    }
                    _proxyCrossingoverList[j].IncreaseProgress(procentGain);
                    break;
                }
            }
        }
        public void Selection(AbstractTrack[] parentTracks, AbstractTrack[] childTracks)
        {
            if (_index == _proxySelectionList.Count)
            {
                _index = 0;
            }

            int countOfTracks = parentTracks.Length;
            AbstractTrack[] allTracks = new AbstractTrack[parentTracks.Length * 2];
            for (int i = 0; i < countOfTracks; i++)
            {
                allTracks[i] = parentTracks[i].Clone();
                allTracks[i + countOfTracks] = childTracks[i].Clone();
            }
            Array.Sort(allTracks);
            double currentResult = allTracks[0].GetTrackLength();

            DateTime startTime = DateTime.Now;
            _proxySelectionList[_index].GetSelection().Selection(parentTracks, childTracks);
            DateTime endTime = DateTime.Now;
            TimeSpan time = endTime - startTime;
            _proxySelectionList[_index].AddOperationTime(time);

            AbstractTrack[] newParentTracks = new AbstractTrack[parentTracks.Length];
            for (int i = 0; i < countOfTracks; i++)
            {
                newParentTracks[i] = parentTracks[i].Clone();
            }
            Array.Sort(newParentTracks);
            double sumOfParentTracks = newParentTracks.Sum(track => track.GetTrackLength());
            double newResult = newParentTracks[0].GetTrackLength();
            double sumOfBestResult = newResult * newParentTracks.Length;
            if ((0 == newResult.CompareTo(currentResult)) && (0 != sumOfParentTracks.CompareTo(sumOfBestResult)))
            {
                _proxySelectionList[_index].AddGoodStart();
            }
            else
            {
                _proxySelectionList[_index].AddBadStart();
            }
            _index++;
        }
 private static void DestroyBadRip(AbstractTrack mutant, int firstGen, int secondGen)
 {
     int countOfAlleles = mutant.Genotype.Length;
     for (int i = 0; i < countOfAlleles; i++)
     {
         if (mutant.Genotype[i] == firstGen)
         {
             Random newRandom = new Random();
             int newPoint = newRandom.Next(countOfAlleles);
             while (mutant.Genotype[newPoint] == firstGen || mutant.Genotype[newPoint] == secondGen)
             {
                 newPoint = newRandom.Next(countOfAlleles);
             }
             mutant.Genotype[i] = mutant.Genotype[newPoint];
             mutant.Genotype[newPoint] = firstGen;
             mutant.TypeOfMutation = MutationName;
             break;
         }
     }
 }
示例#17
0
        public Configuration()
        {
            AliasCrossingover = new List<string>();
            AliasMutations = new List<string>();
            AliasSelection = new List<string>();

            ConfigName = "Новая конфигурация";
            AlgMode = AlgorithmMode.Singl;
            CountOfReplays = 1;
            ProbOfCrossingover = 100;
            ProbOfMutation = 100;
            FitnessParam = 10;
            Mutation = new NotRandomMutation();
            AliasMutations.Add(Mutation.GetName());
            Crossingover = new CyclicalCrossingover();
            AliasCrossingover.Add(Crossingover.GetName());
            Selection = new TournamentSelection();
            AliasSelection.Add(Selection.GetName());
            Graph = new UndirectedConnectedGraph(10);
            FitnessFunction = new BestReps((int)FitnessParam);
            Tracks = new AbstractTrack[10];
            Random r = new Random();
            for (int i = 0; i < Tracks.Length; i++)
            {
                int[] points = new int[10];
                for (int j = 0; j < points.Length; j++)
                {
                    points[j] = -1;
                }
                int newRandomChromosome = r.Next(points.Length - 1);
                for (int j = 0; j < points.Length; j++)
                {
                    while (points.Contains(newRandomChromosome))
                    {
                        newRandomChromosome = r.Next(points.Length);
                    }
                    points[j] = newRandomChromosome;
                }
                Tracks[i] = new ClosedTrack(points, Graph);
            }
        }
 public void Selection(AbstractTrack[] parentTracks, AbstractTrack[] childTracks)
 {
     int countOfTracks = parentTracks.Length;
     AbstractTrack[] allTracks = new AbstractTrack[countOfTracks * 2];
     for (int i = 0; i < countOfTracks; i++)
     {
         allTracks[i] = parentTracks[i];
         allTracks[i + countOfTracks] = childTracks[i];
     }
                                                                 // todo чето тут не так!!!!!!!!!!!!!!!!
     Random random = new Random();
     double sumOfFitness = 0;
     for (int i = 0; i < countOfTracks * 2; i++)
     {
         sumOfFitness += allTracks[i].GetTrackLength();
     }
     double[] sectorsOfWheel = new double[countOfTracks * 2];
     for (int i = 0; i < countOfTracks * 2; i++)
     {
         sectorsOfWheel[i] = allTracks[i].GetTrackLength() / sumOfFitness * 100; // формула для поиска размера сектров колеса рулетки
     }
     for (int i = 0; i < countOfTracks; i++)
     {
         int fallenSector = random.Next(100);
         double sum = 0;
         for (int j = 0; j < sectorsOfWheel.Length; j++)
         {
             sum += sectorsOfWheel[j];
             if (sum > fallenSector)
             {
                 parentTracks[i] = allTracks[j];
                 parentTracks[i].TypeOfSelection = SelectionName;
                 break;
             }
         }
     }
 }
        public void Crossingover(AbstractTrack firstParent, AbstractTrack secondParent, AbstractTrack firstChild,
                                 AbstractTrack secondChild)
        {
            int countOfAlleles = firstParent.Genotype.Length;
            for (int i = 0; i < countOfAlleles; i++)
            {
                firstChild.Genotype[i] = firstParent.Genotype[i];
                secondChild.Genotype[i] = secondParent.Genotype[i];
            }

            Random randomPositionOne = new Random();
            Random randomPositionTwo = new Random();
            int positionOne = randomPositionOne.Next(1, countOfAlleles - 1);
            int positionTwo = randomPositionTwo.Next(1, countOfAlleles - 1);
            while (positionOne == positionTwo)
            {
                positionTwo = randomPositionTwo.Next(1, countOfAlleles);
            }
            if (positionOne > positionTwo)
            {
                int buff = positionOne;
                positionOne = positionTwo;
                positionTwo = buff;
            }
            double delimetr = Math.Ceiling((Convert.ToDouble(positionOne + positionTwo)/2.0));
            for (int i = positionOne, j = positionTwo; i < delimetr; i++, j--)
            {
                int buff = firstChild.Genotype[i];
                firstChild.Genotype[i] = firstChild.Genotype[j];
                firstChild.Genotype[j] = buff;
                buff = secondChild.Genotype[i];
                secondChild.Genotype[i] = secondChild.Genotype[j];
                secondChild.Genotype[j] = buff;
            }
            firstChild.TypeOfCrossingover = CrossingoverName;
            secondChild.TypeOfCrossingover = CrossingoverName;
        }
 public void Crossingover(AbstractTrack firstParent, AbstractTrack secondParent, AbstractTrack firstChild, AbstractTrack secondChild)
 {
     int countOfAlleles = firstParent.Genotype.Length;
     Random randomPosition = new Random();
     int position = randomPosition.Next(1, countOfAlleles);
     for (int i = 0; i < position; i++)
     {
         firstChild.Genotype[i] = firstParent.Genotype[i];
         secondChild.Genotype[i] = secondParent.Genotype[i];
     }
     for (int i = 0, j = position, k = position; i < countOfAlleles; i++)
     {
         if (!firstChild.Genotype.Contains(secondParent.Genotype[i]))
         {
             firstChild.Genotype[j++] = secondParent.Genotype[i];
         }
         if (!secondChild.Genotype.Contains(firstParent.Genotype[i]))
         {
             secondChild.Genotype[k++] = firstParent.Genotype[i];
         }
     }
     firstChild.TypeOfCrossingover = CrossingoverName;
     secondChild.TypeOfCrossingover = CrossingoverName;
 }
示例#21
0
 private void SaveTrack(AbstractTrack track)
 {
     DB_GeneticsDataSet.PersonsRow newPersonRow = _geneticsDataSet.Persons.NewPersonsRow();
     newPersonRow.Id = Guid.NewGuid();
     newPersonRow.Item = track.GetItem();
     newPersonRow.Track = track.ToString();
     newPersonRow.Length = track.GetTrackLength();
     newPersonRow.TypeOfCrossingover = track.TypeOfCrossingover;
     newPersonRow.TypeOfMutation = track.TypeOfMutation;
     newPersonRow.TypeOfSelection = track.TypeOfSelection;
     newPersonRow.NumberOfGeneration = _numberOfGenerations;
     newPersonRow.FirstParent = track.FirstParent;
     newPersonRow.SecondParent = track.SecondParent;
     newPersonRow.Launch = _launchId;
     newPersonRow.BestRip = track.GetBestRib().ToString();
     newPersonRow.WorstRip = track.GetWorstRib().ToString();
     newPersonRow.TypeOfTrack = track.GetTypeOfTrack();
     _geneticsDataSet.Persons.Rows.Add(newPersonRow);
     _personsTableAdapter.Update(newPersonRow);
     _geneticsDataSet.Persons.AcceptChanges();
 }
 public void Crossingover(AbstractTrack firstParent, AbstractTrack secondParent, AbstractTrack firstChild,
                          AbstractTrack secondChild)
 {
     ChildrenFill(firstParent, secondParent, firstChild);
     ChildrenFill(secondParent, firstParent, secondChild);
 }
示例#23
0
 public void StartGA()
 {
     AbstractTrack.ClearCount();
     AbstractTrack[] tracks = new AbstractTrack[_newConfig.Tracks.Length];
     for (int i = 0; i < tracks.Length; i++)
     {
         tracks[i] = _newConfig.Tracks[i].Clone();
     }
     _newGA = new GEngine(tracks, _newConfig.ProbOfCrossingover, _newConfig.ProbOfMutation, _newConfig.FitnessFunction, _newConfig.Mutation, _newConfig.Crossingover, _newConfig.Selection);
     _threadRunGA = new Thread(_newGA.Run);
     _threadRunGA.Name ="LaborOfGA";
     try
     {
         _threadRunGA.Start();
     }
     catch (ThreadAbortException)
     {
         CleaningDB();
     }
 }
示例#24
0
 private void Crossingover(AbstractTrack firstParent, AbstractTrack secondParent, AbstractTrack firstChild, AbstractTrack secondChaild)
 {
     _crossingover.Crossingover(firstParent, secondParent, firstChild, secondChaild);
 }
示例#25
0
 private void Mutation(AbstractTrack mutant)
 {
     _mutation.Mutation(mutant);
 }
 public void Mutation(AbstractTrack mutant)
 {
     Rib worstRib = mutant.GetWorstRib();
     DestroyBadRip(mutant, worstRib.StartNode, worstRib.EndNode);
 }
示例#27
0
        public void Run()
        {
            DB_GeneticsDataSet.LaunchesRow newLaunchRow = _geneticsDataSet.Launches.NewLaunchesRow();
            newLaunchRow.Id = _launchId;
            _startTime = DateTime.Now;

            newLaunchRow.StartTime = _startTime;
            newLaunchRow.EndTime = _startTime;
            newLaunchRow.OperationTime = _startTime.ToString();
            newLaunchRow.TypeOfMutation = _mutation.GetName();
            newLaunchRow.TypeOfSelection = _selection.GetName();
            newLaunchRow.TypeOfCrossingover = _crossingover.GetName();
            newLaunchRow.FitnessFunction = _fitnessFunction.GetName();
            newLaunchRow.NumberOfGenerations = "0";
            newLaunchRow.BestResult = "0";
            _geneticsDataSet.Launches.Rows.Add(newLaunchRow);
            _launchTableAdapter.Update(newLaunchRow);
            _geneticsDataSet.Launches.AcceptChanges();

            Random random = new Random();
            while (_fitnessFunction.Fitness(_tracks))
            {
                _numberOfGenerations++;
                foreach (AbstractTrack track in _tracks)
                {
                    SaveTrack(track);
                }
                AbstractTrack[] newGeneration = new AbstractTrack[_countOfPerson];
                for (int i = 0; i < _countOfPerson; i++)
                {
                    int crossNotWillBe = random.Next(ProcentTreshhold);
                    if (_pCrossingover > crossNotWillBe)
                    {
                        // Скрещивание
                        int coupleIndex = random.Next(_countOfPerson - 1);
                        AbstractTrack firstChild = _tracks[i].EmptyClone();
                        AbstractTrack secondChild = _tracks[i].EmptyClone();

                        this.Crossingover(_tracks[i], _tracks[coupleIndex], firstChild, secondChild);
                        firstChild.FirstParent = _tracks[i].GetItem();
                        firstChild.SecondParent = _tracks[coupleIndex].GetItem();
                        secondChild.FirstParent = _tracks[i].GetItem();
                        secondChild.SecondParent = _tracks[coupleIndex].GetItem();

                        SaveTrack(secondChild);
                        SaveTrack(firstChild);

                        if (firstChild.GetTrackLength() <= secondChild.GetTrackLength())
                        {
                            MutationWrapper(firstChild, newGeneration, random, i);
                        }
                        else
                        {
                            MutationWrapper(secondChild, newGeneration, random, i);
                        }
                    }
                    else
                    {
                        AbstractTrack mutant = _tracks[i].Clone();
                        MutationWrapper(mutant, newGeneration, random, i);
                    }
                }
                // Селекция
                this.Selection(_tracks, newGeneration);
                for (int i = 0; i < _tracks.Length; i++)
                {
                    SaveTrack(_tracks[i]);
                }
            }
            _endTime = DateTime.Now;
            _operationTime = _endTime - _startTime;

            newLaunchRow.EndTime = _endTime;
            newLaunchRow.OperationTime = _operationTime.ToString();
            newLaunchRow.NumberOfGenerations = _numberOfGenerations.ToString();
            newLaunchRow.BestResult = FitnessFunction.BestResult.ToString();
            _launchTableAdapter.Update(newLaunchRow);
            _geneticsDataSet.Launches.AcceptChanges();
        }
示例#28
0
        private void MutationWrapper(AbstractTrack child, AbstractTrack[] newGeneration, Random random, int i)
        {
            child.TypeOfSelection = AbstractSelection.WithoutSelection;
            child.TypeOfCrossingover = AbstractCrossingover.WithoutCrossingover;

            int mutationNotWillBe = random.Next(ProcentTreshhold);
            if (_pMutation > mutationNotWillBe)
            {
                // Мутация
                this.Mutation(child);
                newGeneration[i] = child;
                SaveTrack(child);
            }
            else
            {
                newGeneration[i] = child;
            }
        }
示例#29
0
 private void Selection(AbstractTrack[] parentTracks, AbstractTrack[] childTracks)
 {
     _selection.Selection(parentTracks, childTracks);
 }