Ejemplo n.º 1
0
        private State DeleteEdges(State state, int row, int col)
        {
            state.GetCityTo()[row]   = col;
            state.GetCityFrom()[col] = row;
            state.SetCitiesInSolution(state.GetCitiesInSolution() + 1);

            if (state.GetCitiesInSolution() < _cities.Length - 1)
            {
                int start = row;
                int end   = col;

                while (state.GetCityFrom()[start] != -1)
                {
                    start = state.GetCityFrom()[start];
                }

                while (state.GetCityTo()[end] != -1)
                {
                    end = state.GetCityTo()[end];
                }

                while (start != col)
                {
                    state.Matrix.GetMatrix()[end, start] = double.PositiveInfinity;
                    state.Matrix.GetMatrix()[col, start] = double.PositiveInfinity;

                    start = state.GetCityTo()[start];
                }
            }

            return(state);
        }
Ejemplo n.º 2
0
        // Simply used for generating the results of the solution.
        private PathCalculation GeneratePathResult(State solution, City[] cities, double elapsedTime)
        {
            if (solution == null)
            {
                return(new PathCalculation(null, elapsedTime, bssfUpdates));
            }

            List <City> pathResult = new List <City>();

            int currentCity = solution.GetCityTo()[0];

            while (pathResult.Count < cities.Length)
            {
                pathResult.Add(cities[currentCity]);
                currentCity = solution.GetCityTo()[currentCity];
            }

            return(new PathCalculation(pathResult.ToArray(), elapsedTime, bssfUpdates));
        }
Ejemplo n.º 3
0
        private void PrintMatrix(State state)
        {
            Console.WriteLine("------------------------------------");

            Matrix matrix = state.Matrix;

            for (int i = 0; i < state.GetCityTo().Length; i++)
            {
                Console.WriteLine(i + " --> " + state.GetCityTo()[i]);
            }

            Console.WriteLine("------------------------------------");
            for (int i = 0; i < matrix.GetMatrix().GetLength(0); i++)
            {
                for (int j = 0; j < matrix.GetMatrix().GetLength(0); j++)
                {
                    Console.Write((Double.IsPositiveInfinity(matrix.GetMatrix()[i, j]) ? "INF" : matrix.GetMatrix()[i, j].ToString()) + "\t");
                }

                Console.Write("\n");
            }
        }
Ejemplo n.º 4
0
        private State GenerateReducedMatrix(State state)
        {
            Matrix matrix = state.Matrix;

            // This is the total cost of reduction.
            double sumDifference = 0;

            // Reduce row by row
            for (int i = 0; i < matrix.GetMatrix().GetLength(0); i++)
            {
                // Only reduce the row if a cell in this row has NOT already been select as part of the path.
                if (state.GetCityTo()[i] == -1)
                {
                    double rowMin = double.PositiveInfinity;

                    for (int j = 0; j < matrix.GetMatrix().GetLength(0); j++)
                    {
                        if (matrix.GetMatrix()[i, j] < rowMin)
                        {
                            rowMin = matrix.GetMatrix()[i, j];
                        }
                    }

                    if (!double.IsInfinity(rowMin))
                    {
                        for (int j = 0; j < matrix.GetMatrix().GetLength(0); j++)
                        {
                            matrix.GetMatrix()[i, j] -= rowMin;
                        }
                    }

                    sumDifference += rowMin;
                }
            }

            // Reduce column by column
            for (int i = 0; i < matrix.GetMatrix().GetLength(0); i++)
            {
                // Only reduce the col if a cell in this col has NOT already been select as part of the path.
                if (state.GetCityFrom()[i] == -1)
                {
                    double colMin = double.PositiveInfinity;

                    for (int j = 0; j < matrix.GetMatrix().GetLength(0); j++)
                    {
                        if (matrix.GetMatrix()[j, i] < colMin)
                        {
                            colMin = matrix.GetMatrix()[j, i];
                        }
                    }

                    if (!double.IsInfinity(colMin))
                    {
                        for (int j = 0; j < matrix.GetMatrix().GetLength(0); j++)
                        {
                            matrix.GetMatrix()[j, i] -= colMin;
                        }
                    }

                    sumDifference += colMin;
                }
            }

            state.LowerBound += sumDifference;
            state.Matrix      = matrix;

            return(state);
        }