Example #1
0
        public void MutateERO(Population population, List <City> citiesList)
        {
            _calc.OrderPopulation(population);
            int  sizeOfPopulation = population.PopulationList.Count;
            int  sizeOfRoad       = population.PopulationList[0].CitiesList.Count;
            Road bestRoad         = population.PopulationList[0];

            int licznikPoprawy = 0;

            List <NeighborList> NLList = new List <NeighborList>();

            for (int i = 0; i < sizeOfRoad; i++)
            {
                NLList.Add(new NeighborList(citiesList[i].CityName, 0));
            }


            do
            {
                decimal toKill1 = Convert.ToDecimal(sizeOfPopulation) * 0.2m;
                decimal toKill2 = Convert.ToDecimal(sizeOfPopulation) - toKill1;

                population.PopulationList.RemoveRange(Convert.ToInt16(toKill1), Convert.ToInt16(toKill2));

                int z = 0;
                for (int i = population.PopulationList.Count; i < sizeOfPopulation; i++)
                {
                    for (int j = 0; j < sizeOfRoad; j++)
                    {
                        NeighborList jList = new NeighborList();
                        jList.CityName = citiesList[j].CityName;
                        NLList[j]      = jList;
                    }
                    int         newSizeOfPopulation = population.PopulationList.Count;
                    int         parent1Index        = _rand.Next(0, newSizeOfPopulation);
                    int         parent2Index        = _rand.Next(0, newSizeOfPopulation);
                    Road        parent1             = population.PopulationList[parent1Index];
                    Road        parent2             = population.PopulationList[parent2Index];
                    Road        child        = new Road();
                    City        nullCity     = new City(0, 0, "N");
                    List <City> nullCityList = new List <City>();
                    for (int j = 0; j < sizeOfRoad; j++)
                    {
                        nullCityList.Add(nullCity);
                    }
                    child.CitiesList = nullCityList;

                    string[] parent1Table = new string[sizeOfRoad];
                    string[] parent2Table = new string[sizeOfRoad];
                    string[] childTable   = new string[sizeOfRoad];


                    for (int j = 0; j < sizeOfRoad; j++)
                    {
                        parent1Table[j] = parent1.CitiesList[j].CityName;
                        parent2Table[j] = parent2.CitiesList[j].CityName;
                    }
                    NLList = generateNeighborLists(NLList, parent2Table, parent1Table);

                    childTable[0]   = parent1Table[0];
                    parent1Table[0] = "X";

                    for (int j = 0; j < parent2Table.Length; j++)
                    {
                        if (parent2Table[j] == childTable[0])
                        {
                            parent2Table[j] = "X";
                        }
                    }


                    int idx = NLList.Select((value, index) => new { value, index })
                              .Where(x => x.value.CityName == childTable[0]).Select(x => x.index).FirstOrDefault();

                    NeighborList zaz = new NeighborList();
                    zaz.CityName = "X";
                    NLList[idx]  = zaz;

                    for (int j = 1; j < sizeOfRoad; j++)
                    {
                        int           r             = 0;
                        List <string> minimalCities = new List <string>();
                        int           minumum       = 4;
                        for (int k = 0; k < sizeOfRoad; k++)
                        {
                            if (NLList[k].Neighbors < minumum && NLList[k].CityName != "X")
                            {
                                minumum = NLList[k].Neighbors;
                            }
                        }
                        for (int k = 0; k < sizeOfRoad; k++)
                        {
                            if (NLList[k].Neighbors == minumum)
                            {
                                minimalCities.Add(NLList[k].CityName);
                            }
                        }

                        if (minimalCities.Count == 0)
                        {
                            Console.WriteLine("AAA");
                        }

                        try
                        {
                            r = _rand.Next(0, minimalCities.Count);
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine("ERROR: Empty cities list with minimal neighbours!!!");
                            throw;
                        }

                        childTable[j] = minimalCities[r];
                        for (int k = 0; k < parent2Table.Length; k++)
                        {
                            if (parent2Table[k] == minimalCities[r])
                            {
                                parent2Table[k] = "X";
                            }
                        }
                        for (int k = 0; k < parent1Table.Length; k++)
                        {
                            if (parent1Table[k] == minimalCities[r])
                            {
                                parent1Table[k] = "X";
                            }
                        }
                        int indeks = NLList.Select((value, index) => new { value, index })
                                     .Where(x => x.value.CityName == childTable[j]).Select(x => x.index).FirstOrDefault();

                        NeighborList zaa = new NeighborList();
                        zaa.CityName   = "X";
                        r              = indeks;
                        NLList[indeks] = zaa;
                    }
                    City[] citiesTable = new City[sizeOfRoad];
                    for (int j = 0; j < sizeOfRoad; j++)
                    {
                        City c  = new City(0, 0, "X");
                        var  cc = citiesList.Where(x => x.CityName == childTable[j]).GroupBy(x => x.CityName)
                                  .Select(x => x.FirstOrDefault());
                        foreach (var VARIABLE in cc)
                        {
                            c = VARIABLE;
                        }
                        citiesTable[j] = c;
                    }

                    for (int j = 0; j < sizeOfRoad; j++)
                    {
                        child.CitiesList[j] = citiesTable[j];
                    }
                    _calc.CalculateTotalDistance(child, cykl);
                    population.PopulationList.Add(child);
                }
                decimal oldBest   = bestRoad.TotalDistance;
                decimal pretender = population.PopulationList[0].TotalDistance;

                if (population.PopulationList[0].TotalDistance < bestRoad.TotalDistance)
                {
                    bestRoad       = population.PopulationList[0];
                    licznikPoprawy = 0;
                }
                else
                {
                    licznikPoprawy++;
                }
                Iterations++;
                Console.WriteLine($"Iteracja {Iterations}, current best: {bestRoad.TotalDistance}");
                z++;
            } while (licznikPoprawy <= 20);
        }