public void DoAlgorithm(CancellationToken token) { InitFirstIndividual(); Random rand = new Random(); while (_optionsViewModel.MaxGenerationCount > _currentGenerationNum) { Individual ind = new Individual(); ind.CityModels = new CityModel[_currentBest.CityModels.Length]; for (int i = 0; i < _currentBest.CityModels.Length; i++) { ind.CityModels[i] = _currentBest.CityModels[i]; } int source; int destination; source = rand.Next(ind.CityModels.Length - 1); while ((destination = rand.Next(ind.CityModels.Length - 1)) == source) { ; } ShuffleCities(ind.CityModels, source, destination); ind.CalculateOverallDistance(); if (ind.OverallDistance < _currentBest.OverallDistance) { _bestGenerationNumber = _currentGenerationNum; _currentBest = ind; } if (OnAlgorithmStateHasChangedEvent != null) { OnAlgorithmStateHasChangedEvent(_currentGenerationNum, _currentBest, _bestGenerationNumber); } if (OnLogChangedEvent != null) { OnLogChangedEvent(new Tuple <int, ulong, double, ulong>(_currentGenerationNum, _currentBest.OverallDistance, 0d, 0)); } if (token.IsCancellationRequested) { break; } _currentGenerationNum++; } if (OnAlgorithmFinishedEvent != null) { OnAlgorithmFinishedEvent(); } }
private void InitFirstIndividual() { _currentBest = new Individual(); _currentBest.CityModels = new CityModel[_cityList.Count]; int pos = 0; foreach (var cityModel in _cityList) { _currentBest.CityModels[pos] = cityModel; pos++; } Random rand = new Random(); for (int i = 0; i < _currentBest.CityModels.Length; i++) { ShuffleCities(_currentBest.CityModels, rand.Next(_currentBest.CityModels.Length), rand.Next(_currentBest.CityModels.Length)); } _currentBest.CalculateOverallDistance(); }