コード例 #1
0
 public override void Execute(Instance instance)
 {
     if (instance is TSPInstance) {
         this.result = new Result("Result: " + instance.Name + " Alg " + this.Name);
     }
     else {
         this.result = new Result("Type mismatch");
     }
 }
コード例 #2
0
        public override void Execute(Instance instance)
        {
            if ( !(instance is SMInstance)) {
                this.result = new SMResult("Type mismatch");
                return;
            }
            SMInstance smi = (SMInstance)instance;

            int n = smi.Size;

            int[] mMatch = new int[n];
            int[] mProposals = new int[n];
            int[] wMatch = new int[n];
            Stack<int> unmatched = new Stack<int>(n);

            for (int i = 0; i < n; i++) {
                mMatch[i] = -1;
                unmatched.Push(i);
                mProposals[i] = 0;

                wMatch[i] = -1;
            }

            while (unmatched.Count > 0) {
                int m = unmatched.Pop();
                int w = smi.MPrefMatrix[m][mProposals[m]];
                mProposals[m]++;

                if (wMatch[w] == -1) {
                    mMatch[m] = w;
                    wMatch[w] = m;
                }
                else {
                    int m1 = wMatch[w];
                    if (smi.WRankMatrix[w][m] < smi.WRankMatrix[w][m1]) {
                        mMatch[m] = w;
                        wMatch[w] = m;
                        mMatch[m1] = -1;
                        unmatched.Push(m1);
                    }
                    else {
                        unmatched.Push(m);
                    }
                }

            }

            this.result = new SMResult(mMatch, wMatch, smi);
        }
コード例 #3
0
        public override void Execute(Instance instance)
        {
            if (!(instance is SMInstance)) {
                this.result = new SMResult("Type mismatch");
                return;
            }
            SMInstance smi = (SMInstance)instance;

            StableMarriageAlgorithm smAlg = new ManFirstAlgorithm();
            smAlg.Execute(smi.SwapRoles());

            SMResult mAlgResult = (SMResult) smAlg.Result;

            this.result = new SMResult(mAlgResult.wMatch, mAlgResult.mMatch, smi);
        }
コード例 #4
0
        // Since this algorithm can cycle, we build in a cut off on the number of iterations
        // Arbitrarily, this is chosen to be N^ - I suspect there could be longer runs without cycling.
        public override void Execute(Instance instance)
        {
            if (!(instance is SMInstance)) {
                this.result = new SMResult("Type mismatch");
                return;
            }
            SMInstance smi = (SMInstance)instance;

            int[] mMatch = new int[smi.Size];
            int[] wMatch = new int[smi.Size];

            FindInitialMatching(mMatch, wMatch, smi);

            int cutoff = mMatch.Length * mMatch.Length;

            int count = 0;

            int m;
            int w;
            while (FindUnhappyPair(mMatch, wMatch, smi, out m, out w)){
                // Swap pairs
                int w1 = mMatch[m];
                int m1 = wMatch[w];
                mMatch[m] = w;
                wMatch[w] = m;
                mMatch[m1] = w1;
                wMatch[w1] = m1;

                if (count > cutoff)  // Should at least break from an infinite loop
                    break;
                count++;
               //             System.Console.WriteLine("Elope " + m + ", " + w + "Dump " + m1 + ", " + w1);
            }

            this.result = new SMResult(mMatch, wMatch, smi);
        }
コード例 #5
0
ファイル: Algorithm.cs プロジェクト: rjanderson/Algorithms421
 public abstract void Execute(Instance instance);