Beispiel #1
0
        static public void tryRemoveChange(Individual I)
        {
            HashSet <int>    availableTowns = new HashSet <int>(aT);
            ContainerForDuos townsToRemove  = new ContainerForDuos(Params.rand.Next(1, I.Count / 2));

            for (int i = 1; i < I.Count - 1; i++)
            {
                if (I[i] == 1)
                {
                    continue;
                }
                double value = (Params.distances[I[i - 1], I[i]] + Params.distances[I[i], I[i + 1]]) / Params.profits[I[i]] * Params.profits[I[i]] * Params.profits[I[i]];
                if (townsToRemove.Count < townsToRemove.size)
                {
                    townsToRemove.Add(new Duo(value, I[i]));
                }
                else
                {
                    if (value > townsToRemove[townsToRemove.size - 1].value)
                    {
                        townsToRemove.Add(new Duo(value, I[i]));
                    }
                }
            }
            foreach (Duo D in townsToRemove)
            {
                I.path.Remove(D.index);
            }
            I.evaluatePath();
            I.path = tryInserting(I.path);
            I.evaluatePath();
        }
Beispiel #2
0
        public void generateFixedIndividual(int startingTown, HashSet <int> usedTowns)
        {
            HashSet <int>    availableTowns = new HashSet <int>();
            ContainerForDuos bestTowns      = new ContainerForDuos(5);
            PathCalculator   calc           = new PathCalculator();

            double pathDistance = 0;

            for (int i = 0; i <= Parameters.numberOfTowns; i++)
            {
                if (i == startingTown || usedTowns.Contains(i))
                {
                    continue;
                }

                availableTowns.Add(i);
            }
            List <int> newPath = new List <int>();

            newPath.Add(startingTown);

            profit  = 0;
            profit += Parameters.profits[startingTown];
            do
            {
                int k = Parameters.rand.Next(Parameters.To, Parameters.Td);
                bestTowns = new ContainerForDuos(k);
                foreach (int i in availableTowns)
                {
                    double value = calculateValue(newPath[newPath.Count - 1], i, pathDistance, startingTown);
                    if (value != -1)
                    {
                        bestTowns.Add(new Duo(value, i));
                    }
                }
                if (bestTowns.Count == 0)
                {
                    pathDistance += Parameters.distances[newPath[newPath.Count - 1], startingTown];
                    newPath.Add(startingTown);
                    break;
                }
                else
                {
                    int t = Parameters.rand.Next(bestTowns.Count - 1);
                    pathDistance += Parameters.distances[newPath[newPath.Count - 1], bestTowns.town(t)];
                    newPath.Add(bestTowns.town(t));
                    profit += Parameters.profits[bestTowns.town(t)];
                    availableTowns.Remove(bestTowns.town(t));
                }
            }while (newPath[newPath.Count - 1] != startingTown);
            length = pathDistance;
            path   = newPath;
            insertCapital();
            partialTwoOpt(5);
            evaluatePath();
        }