예제 #1
0
        public ISolution Run(IMatrix <int> matrix)
        {
            var startSolution = auxilary.Run(matrix);

            int[] x = startSolution[0].ToArray();
            int[] y = startSolution[1].ToArray();

            var solution = new ArraySolution(x, y);
            var m        = new SplittedMatrix(matrix, solution);

            while (true)
            {
                bool result = false;

                for (int I = 0; I < x.Length; I++)
                {
                    for (int J = 0; J < y.Length; J++)
                    {
                        result |= LocalProcedure(x, y, m, I, J);
                    }
                }

                if (!result)
                {
                    break;
                }
            }

            return(solution);
        }
예제 #2
0
        public void RemoveDuplicates_ShouldSucceed(int[] array, int expectedSize, int[] expectedArray)
        {
            // Act
            var size = new ArraySolution().RemoveDuplicates(array);

            // Assert
            Assert.Equal(expectedSize, size);

            for (int i = 0; i < expectedSize; ++i)
            {
                Assert.Equal(expectedArray[i], array[i]);
            }
        }
예제 #3
0
        public ISolution Run(IMatrix <int> matrix)
        {
            double best_f = double.MaxValue;

            int[] best_x         = null;
            int[] best_y         = null;
            bool  best_x_changed = false;

            int m = matrix.Size(0);
            int n = matrix.Size(1);

            int[] x = new int[M - 1];
            int[] y = new int[N - 1];

            var solution = new ArraySolution(x, y);

            if (M == 1)
            {
                best_x = x;

                if (N == 1)
                {
                    best_y = y;
                }
                else
                {
                    int currentY = 0;
                    y[0] = 1;

                    while (true)
                    {
                        if (y[currentY] > n + currentY - N)
                        {
                            currentY--;
                            if (currentY < 0)
                            {
                                break;
                            }
                            y[currentY]++;
                        }
                        else if (currentY < N - 2)
                        {
                            currentY++;
                            y[currentY] = y[currentY - 1] + 1;
                        }
                        else
                        {
                            double f = MinMaxCriterium.Calculate(matrix, solution);

                            if (f < best_f)
                            {
                                best_f = f;

                                best_y = (int[])y.Clone();
                            }

                            y[currentY]++;
                        }
                    }
                }
            }
            else
            {
                int currentX = 0;
                x[0] = 1;

                while (true)
                {
                    if (x[currentX] > m + currentX - M)
                    {
                        currentX--;
                        if (currentX < 0)
                        {
                            break;
                        }
                        x[currentX]++;
                    }
                    else if (currentX < M - 2)
                    {
                        currentX++;
                        x[currentX] = x[currentX - 1] + 1;
                    }
                    else
                    {
                        int currentY = 0;
                        y[0] = 1;

                        while (true)
                        {
                            if (y[currentY] > n + currentY - N)
                            {
                                currentY--;
                                if (currentY < 0)
                                {
                                    break;
                                }
                                y[currentY]++;
                            }
                            else if (currentY < N - 2)
                            {
                                currentY++;
                                y[currentY] = y[currentY - 1] + 1;
                            }
                            else
                            {
                                double f = MinMaxCriterium.Calculate(matrix, solution);

                                if (f < best_f)
                                {
                                    best_f = f;

                                    best_y         = (int[])y.Clone();
                                    best_x_changed = true;
                                }

                                y[currentY]++;
                            }
                        }

                        if (best_x_changed)
                        {
                            best_x         = (int[])x.Clone();
                            best_x_changed = false;
                        }

                        x[currentX]++;
                    }
                }
            }
            return(new ArraySolution(best_x, best_y));
        }