Ejemplo n.º 1
0
        int MoveFullRows(SortResult vector, int start)
        {
            int nextSwapRow = vector.GetNumberOfElements() - 1;

            // bubble complete rows to the bottom starting 'start'
            int i = start;

            while (i <= nextSwapRow)
            {
                var allNonZero = true;
                for (int j = 0; j < vector.GetNumberOfElements() && allNonZero; j++)
                {
                    if (i != j)
                    {
                        if (TrueMatrixValue(vector, i, j) == 0)
                        {
                            allNonZero = false;
                        }
                    }
                }

                if (allNonZero) // swap indexes
                {
                    vector.Swap(nextSwapRow, i);
                    nextSwapRow--;
                }
                else
                {
                    i++;
                }
            }

            return(nextSwapRow);
        }
Ejemplo n.º 2
0
        int MoveZeroColumns(SortResult vector, int start, int end)
        {
            int j        = start;
            int nextSwap = end; //vector.Size - 1;

            while (j <= nextSwap)
            {
                var allZeros = true;
                for (int i = 0; i < vector.GetNumberOfElements() && allZeros; i++)
                {
                    if (i != j)
                    {
                        if (TrueMatrixValue(vector, i, j) != 0)
                        {
                            allZeros = false;
                        }
                    }
                }

                if (allZeros)  // swap indexes
                {
                    vector.Swap(nextSwap, j);
                    nextSwap--;

                    // stay on new column in position j
                }
                else
                {
                    j++;
                }
            }

            return(nextSwap);
        }
Ejemplo n.º 3
0
        int MoveZeroRows(SortResult vector)
        {
            int nextSwapRow = 0;

            // bubble zero rows to the top - starting from the bottom - to leave any rows already moved
            // by the user stay in place

            int i = vector.GetNumberOfElements() - 1;

            while (i >= nextSwapRow)
            {
                var allZero = true;
                for (int j = 0; j < vector.GetNumberOfElements() && allZero; j++)
                {
                    if (i != j)  // the diagonal must obviously be ignored
                    {
                        if (TrueMatrixValue(vector, i, j) != 0)
                        {
                            allZero = false;
                        }
                    }
                }

                if (allZero) // swap indexes
                {
                    vector.Swap(nextSwapRow, i);
                    nextSwapRow++; // points to next swap position

                    // don't decrement i as it has not yet been tested - we've chnaged the order remember !!
                }
                else
                {
                    i--; // next row
                }
            }

            return(nextSwapRow);
        }
Ejemplo n.º 4
0
        long Score(SortResult vector)
        {
            // We're trying to maximise the number of empty cells in the upper triangle
            // filled in cells are pushed down - for a set of two orderings the one with the higher
            // score should be the most preferable


            // calculate score for cells in upper triangle (TODO possible optimisation between start and end)
            long score = 0;

            for (int i = 0; i < vector.GetNumberOfElements() - 1; i++)
            {
                for (int j = i + 1; j < vector.GetNumberOfElements(); j++)
                {
                    if (TrueMatrixValue(vector, i, j) == 0)
                    {
                        score += CellScore(i, j, vector.GetNumberOfElements());
                    }
                }
            }

            return(score);
        }
Ejemplo n.º 5
0
        public SortResult Sort()
        {
            SortResult vector = new SortResult(_element.Children.Count);

            if (_element.Children.Count > 1)
            {
                List <int> newOrder = _element.Children.OrderBy(x => x.Name).Select(x => x.Id).ToList();

                for (int i = 0; i < vector.GetNumberOfElements(); i++)
                {
                    int id = _element.Children[i].Id;
                    vector.SetIndex(newOrder.IndexOf(id), i);
                }
            }

            return(vector);
        }
Ejemplo n.º 6
0
        void ToBlockTriangular(SortResult vector, int start, int end)
        {
            bool doLoop;

            long currentScore = Score(vector);

            // For holding Permutations already examined during one iteration of the outer while loop
            IDictionary <Permutation, object> permMap =
                new Dictionary <Permutation, object>(vector.GetNumberOfElements() * vector.GetNumberOfElements() / 2);

            do
            {
                doLoop = false;

                for (int i = start; i <= end; i++)              // i is index on rows
                {
                    for (int j = end; j > i; j--)               // cols in upper triangle
                    {
                        if (TrueMatrixValue(vector, i, j) != 0) // not zero so we want to possibley move it
                        {
                            // now find first zero from the left hand side

                            for (int x = start; x <= end; x++) // here x represents the line index
                            {
                                for (int y = start; y <= end; y++)
                                {
                                    if (x != y)  // ignore the diagonal
                                    {
                                        if (TrueMatrixValue(vector, x, y) == 0)
                                        {
                                            Permutation p = new Permutation(j, y);

                                            if (!permMap.ContainsKey(p))
                                            {
                                                permMap.Add(p, null);

                                                //check score of potential new vector
                                                vector.Swap(j, y);

                                                var newScore = Score(vector);

                                                if (newScore > currentScore)
                                                {
                                                    currentScore = newScore;
                                                    doLoop       = true; // increasing score so continue
                                                }
                                                else
                                                {
                                                    vector.Swap(j, y); //swap back to original
                                                }
                                            }
                                            // else permutation already used
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                permMap.Clear();
            }while (doLoop);
        }