Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
0
        public Data(int capacityOfKnapsack, double minSpeed, double maxSpeed, City[] cities, Item[] items)
        {
            CapacityOfKnapsack = capacityOfKnapsack;
            MinSpeed           = minSpeed;
            MaxSpeed           = maxSpeed;
            Cities             = cities;
            Items = items;
            var builder = new DistanceMatrixBuilder();

            DistanceMatrix = builder.MakeMatrix(Cities);
        }