public void CalculateSpecimensValues(Specimen[] population)
        {
            var classifier = new SpecimenClassifier(_data);

            foreach (var specimen in population)
            {
                specimen.Value = classifier.CalculateSpecimenValue(specimen);
            }
        }
示例#2
0
        public string[] StartGreedy()
        {
            var data           = LoadData();
            var distanceMatrix = data.DistanceMatrix;

            DistanceMatrixBuilder.MatrixToString(data.DistanceMatrix);
            var result = new string[Config.GenerationsNumber];

            var bestValue = double.NegativeInfinity;

            var allCities = data.Cities.ToArray();

            for (var j = 0; j < allCities.Length; j++)
            {
                var city             = allCities[j];
                var citiesSequence   = new List <City>(allCities.Length);
                var nonVisitedCities = new List <City>(allCities);

                citiesSequence.Add(city);
                nonVisitedCities.Remove(city);

                while (nonVisitedCities.Count != 0)
                {
                    var lastCity         = citiesSequence.Last();
                    var otherCities      = nonVisitedCities.Where(otherCity => otherCity.Index != lastCity.Index).ToArray();
                    var shortestDistance = otherCities.Min(otherCity =>
                                                           distanceMatrix[lastCity.Index - 1, otherCity.Index - 1]);
                    var closestCity = otherCities.First(otherCity =>
                                                        distanceMatrix[otherCity.Index - 1, lastCity.Index - 1].Equals(shortestDistance));

                    citiesSequence.Add(closestCity);
                    nonVisitedCities.Remove(closestCity);
                }

                var citiesIndexSequence = new int[citiesSequence.Count];
                for (var i = 0; i < citiesSequence.Count; i++)
                {
                    citiesIndexSequence[i] = citiesSequence[i].Index;
                }

                var specimen = new Specimen(data, citiesIndexSequence);

                var classifier = new SpecimenClassifier(data);
                specimen.Value = classifier.CalculateSpecimenValue(specimen);
                if (specimen.Value > bestValue)
                {
                    bestValue = specimen.Value;
                }

                WriteLine($"{j}   {bestValue} ");
                result[j] = bestValue.ToString();
            }

            return(result);
        }