示例#1
0
        public static Matrix RandomPermutationMatrix(int N)
        {
            ////Step 1: generate a random vector with distinct integer entries
            //Random rand = new Random();
            //Matrix v = new Matrix(1, N);
            //while (true)
            //{
            //    for (int j = 1; j <= N; j++)
            //    {
            //        v[1, j] = rand.Next();
            //    }

            //    bool matches = false;

            //    for (int i = 1; i <= N - 1; i++)
            //        for (int j = i + 1; j <= N; j++)
            //        {
            //            if (v[1, i] == v[1, j])
            //            {
            //                matches = true;
            //                j = N + 1;
            //                i = N;
            //            }
            //        }


            //    if (!matches)
            //        break;
            //}

            ////Step 2: Calculate the order of the random numbers
            //Matrix order = new Matrix(1, N);
            //for (int n = 1; n <= N; n++)
            //{
            //    order[1, n] = N;
            //    var x = v[1, n];
            //    for (int m = 1; m <= N; m++)
            //    {
            //        if (x > v[1, m])
            //            order[1, n]--;
            //    }
            //}

            Permutation Perm = Permutation.RandomPermutation(N);

            //Step 3: Generate a permutation matrix from the order
            Matrix P = new Matrix(N, N);

            for (int i = 1; i <= N; i++)
            {
                for (int j = 1; j <= N; j++)
                {
                    if (Perm[i] == j)
                    {
                        P[i, j] = 1;
                    }
                    else
                    {
                        P[i, j] = 0;
                    }
                }
            }

            return(P);
        }
示例#2
0
        void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            Permutation optimalSeatingPlan = (Permutation)e.Result;

            this.textBox1.Text = String.Format("Max Utility: {0}", optimizer.Utility(optimalSeatingPlan));
        }