public void SecondIdIsFive()
        {
            var matrix = preferenceCalculationClass.CreateEntryMatrix(slots, players, entries);
            var test   = HungarianAlgorithm.FindAssignments(matrix);

            Assert.Equal(5, test[1]);
        }
Example #2
0
        public void JobAssignment()
        {
            // J = Job | W = Worker
            //     J1  J2  J3  J4
            // W1  82  83  69  92
            // W2  77  37  49  92
            // W3  11  69  5   86
            // W4  8   9   98  23

            int[,] matrix =
            {
                { 82, 83, 69, 92 },
                { 77, 37, 49, 92 },
                { 11, 69,  5, 86 },
                {  8,  9, 98, 23 }
            };
            var algorithm = new HungarianAlgorithm(matrix);

            algorithm.Compute();

            Assert.IsNotNull(algorithm.AgentsTasks);
            int[] tasks = algorithm.AgentsTasks;
            Assert.AreEqual(2, tasks[0]); // J1 to be done by W3
            Assert.AreEqual(1, tasks[1]); // J2 to be done by W2
            Assert.AreEqual(0, tasks[2]); // J3 to be done by W1
            Assert.AreEqual(3, tasks[3]); // J4 to be done by W4
        }
Example #3
0
    /* Assignment problem - HUNGARIAN ALGORITHM */
    public void assignClustersToDrones(Cluster[] clusters)
    {
        /* compute matrix dimensions with dummy rows and columns */
        int N = UAVs.Length;

        int[,] costs = new int[N, N];        //{{20,22,14,24},{20,19,12,20},{13,10,18,16},{22,23,9,28}};

        /*compute matrix costs*/
        for (int m = 0; m < N; m++)
        {
            for (int n = 0; n < N; n++)
            {
                if (m < UAVs.Length && n < clusters.Length)
                {
                    costs[m, n] = (int)Mathf.Floor(Vector3.Magnitude(UAVs[m].transform.position - (clusters[n]).Centroid));
                }
                else
                {
                    costs[m, n] = int.MaxValue;
                }
            }
        }

        /*Munkres - Hungarian Algorithm*/
        int [] res = HungarianAlgorithm.FindAssignments(costs);

        /*Assign clusters to UAVs*/
        for (int i = 0; i < UAVs.Length; i++)
        {
            if (res[i] < clusters.Length)
            {
                UAVs[i].assignCluster(clusters[res[i]]);
            }
        }
    }
Example #4
0
        private async void calculateMatrix(object sender, RoutedEventArgs e)
        {
            try
            {
                matrixToCalc = getMatrixFromGrid(gridTextBox);
                Matrix matrixCopy = getMatrixFromGrid(gridTextBox);
                matrixCopy = HungarianAlgorithm.subMinFromRow(matrixCopy);
                matrixCopy = HungarianAlgorithm.subMinFromCols(matrixCopy);
                matrixCopy = HungarianAlgorithm.markLines(matrixCopy);
                matrixCopy = HungarianAlgorithm.testResult(matrixCopy);

                for (int i = 0; i < matrixCopy.getMatrix().GetLength(0); ++i)
                {
                    for (int j = 0; j < matrixCopy.getMatrix().GetLength(1); ++j)
                    {
                        if (matrixCopy.getMatrix()[i, j] == 999)
                        {
                            matrixCopy.setCell(i, j, 1);
                            gridTextBox[i, j].Background = new SolidColorBrush((Color)Application.Current.Resources["SystemAccentColorLight2"]);
                        }
                        else
                        {
                            matrixCopy.setCell(i, j, 0);
                        }
                    }
                }

                int cost = 0;
                for (int i = 0; i < matrixToCalc.getMatrix().GetLength(0); ++i)
                {
                    for (int j = 0; j < matrixToCalc.getMatrix().GetLength(1); ++j)
                    {
                        cost += matrixToCalc.getMatrix()[i, j] * matrixCopy.getMatrix()[i, j];
                    }
                }
                resultGrid.Visibility        = Visibility.Visible;
                resultGrid.Opacity           = 0;
                resultGrid.OpacityTransition = new ScalarTransition()
                {
                    Duration = new TimeSpan(0, 0, 0, 0, 500)
                };
                resultGrid.Opacity   = 1;
                resultTextBlock.Text = "Wynik: " + cost.ToString();

                matrixGrid.Visibility        = Visibility.Visible;
                matrixGrid.Opacity           = 0;
                matrixGrid.OpacityTransition = new ScalarTransition()
                {
                    Duration = new TimeSpan(0, 0, 0, 0, 500)
                };
                matrixGrid.Opacity = 1;
            }
            catch (Exception exp)
            {
                ContentDialog dialog = new ContentDialog {
                    Title = "Error", Content = exp.Message, CloseButtonText = "ok"
                };
                ContentDialogResult result = await dialog.ShowAsync();
            }
        }
 public void Run_InfiniteCost_ThrowsException()
 {
     Assert.Throws <ArgumentException>(() => HungarianAlgorithm.Run(new[, ] {
         { float.PositiveInfinity }
     }));
     Assert.Throws <ArgumentException>(() => HungarianAlgorithm.Run(new[, ] {
         { float.NegativeInfinity }
     }));
 }
        public void Compute()
        {
            int[,] matrix = { { 1, 2, 3 }, { 3, 3, 3 }, { 3, 3, 2 } };
            var algorithm = new HungarianAlgorithm(matrix);

            int[] res = algorithm.Compute();
            Assert.AreEqual(res[0], 0);
            Assert.AreEqual(res[1], 1);
            Assert.AreEqual(res[2], 2);
        }
 public void Run_MoreTargetsThanSources_AssignsSourcesToCheapest()
 {
     Assert.Throws <ArgumentException>(() => HungarianAlgorithm.Run(
                                           new float[, ]
     {
         { 1, 9, 3 },
         { 3, 9, 1 }
     }
                                           ));
 }
Example #8
0
        static string[] products;  //= { "aaaaaaaaaaaaaaaaa","PlayStationsdfsdf", "Xboxreiyujfcfh", "Iphonegdsgsdtetr", "Pravecgdupoijh" , "Carsfdsdbaaaaaaaaaaaaaafds", "Captivafsdfoiteraaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaggndsfkgvdsfk" };

        static void Main(string[] args)
        {
            if (args.Length > 0)
            {
                ReadFile(args[0]);
            }
            else
            {
                Console.WriteLine("No input file!"); return;
            }

            //1. Generate assignment matrix
            double[,] AssignmentMatrix = new double[customers.Length, products.Length];

            //rows are customers
            //columns are products
            double maxSS = 0;

            for (int i = 0; i < customers.Length; i++)
            {
                for (int j = 0; j < products.Length; j++)
                {
                    double SS = CalculateSS(products[j], customers[i]);
                    if (SS > maxSS)
                    {
                        maxSS = SS;
                    }
                    AssignmentMatrix[i, j] = SS;
                }
            }

            //2. Invert because we are searching for max not min
            for (int i = 0; i < AssignmentMatrix.GetLength(0); i++)
            {
                for (int j = 0; j < AssignmentMatrix.GetLength(1); j++)
                {
                    AssignmentMatrix[i, j] = maxSS - AssignmentMatrix[i, j];
                }
            }

            //3. Apply Hungarian algorithm
            int[] result = HungarianAlgorithm.FindAssignments(AssignmentMatrix);

            //4. Save result
            double TotalSS = 0;

            for (int i = 0; i < result.Length; i++)
            {
                //Console.WriteLine("Customer=" + customers[i] + ",product=" + products[result[i]]);
                TotalSS += AssignmentMatrix[i, result[i]] + maxSS;
            }
            //Console.WriteLine("TotalSS="+TotalSS);
            //Console.ReadKey();
            WriteFile("output.txt", result, TotalSS);
        }
 public void Run_MoreSourcesThanTargets_ThrowsException()
 {
     Assert.Throws <ArgumentException>(() => HungarianAlgorithm.Run(
                                           new float[, ]
     {
         { 1, 3 },
         { 9, 9 },
         { 3, 1 }
     }
                                           ));
 }
        public void HungarianAlgorithmWithAmbiguousGraphShouldGiveCostsWithAmbiguousValue()
        {
            var graph        = new Graph(_dirPathSample + "v2Graph.json");
            var correctCosts = CreateCorrectAmbiguousCostsForGraphV2();

            var hungarianAlgorithm = new HungarianAlgorithm(graph);
            var costs = hungarianAlgorithm.FindMinimumCost();

            ShowCosts(costs);
            CostsListsAreEqual(costs, correctCosts).Should().BeTrue();
        }
        public void GetIterations()
        {
            int[,] matrix = { { 1, 2, 3 }, { 3, 3, 3 }, { 3, 3, 2 } };
            var algorithm = new HungarianAlgorithm(matrix);

            HungarianIteration[] iterations = algorithm.GetIterations().ToArray();
            int[] res = algorithm.AgentsTasks;
            Assert.AreEqual(res[0], 0);
            Assert.AreEqual(res[1], 1);
            Assert.AreEqual(res[2], 2);
            Assert.AreEqual(iterations.Length, 3);
        }
Example #12
0
        public void RunCheck()
        {
            var matrix = new[, ] {
                { 1, 2, 3 }, { 3, 3, 3 }, { 3, 3, 2 }
            };
            var algorithm = new HungarianAlgorithm(matrix);
            var res       = algorithm.Run();

            Assert.AreEqual(res[0], 0);
            Assert.AreEqual(res[1], 1);
            Assert.AreEqual(res[2], 2);
        }
Example #13
0
        public void IterationsCheck()
        {
            var matrix = new[, ] {
                { 1, 2, 3 }, { 3, 3, 3 }, { 3, 3, 2 }
            };
            var algorithm  = new HungarianAlgorithm(matrix);
            var iterations = algorithm.GetIterations;
            var res        = algorithm.AgentsTasks;

            Assert.AreEqual(res[0], 0);
            Assert.AreEqual(res[1], 1);
            Assert.AreEqual(res[2], 2);
            Assert.AreEqual(iterations.Count, 3);
        }
Example #14
0
        public JsonResult CalculateMatrix()
        {
            const int SIZE = 5000;

            var matrix = generateMatrix(SIZE);

            var algorithm = new HungarianAlgorithm(matrix);

            Thread.Sleep(1000);

            var result = algorithm.Run();

            return(Json(result, JsonRequestBehavior.AllowGet));
        }
        public void Run_TwoSources_AssignsLowestCostTarget(float f, short s1, short s2, short s3, short s4)
        {
            // The behaviour for equal matching is not defined.
            if (Abs(s1 + s4 - s3 - s2) == 0 || Abs(f) < 1e-10 || Abs(f) > 1e10)
            {
                return;
            }
            var(f1, f2, f3, f4) = (f * s1, f *s2, f *s3, f *s4);

            var result = HungarianAlgorithm.Run(new[, ] {
                { f1, f2 }, { f3, f4 }
            });

            Assert.Equal(f1 + f4 < f3 + f2 ? new[] { 0, 1 } : new[] { 1, 0 }, result);
        }
Example #16
0
        public void Constructor()
        {
            int[,] costs = new int[0, 0];
            var algorithm = new HungarianAlgorithm(costs);

            Assert.IsNull(algorithm.AgentsTasks);

            costs = new[, ]
            {
                { 1, 2, 3 },
                { 1, 2, 3 },
            };
            algorithm = new HungarianAlgorithm(costs);
            Assert.IsNull(algorithm.AgentsTasks);
        }
Example #17
0
        public void SimpleAssignment()
        {
            int[,] matrix =
            {
                { 1, 2, 3 },
                { 3, 3, 3 },
                { 3, 3, 2 }
            };
            var algorithm = new HungarianAlgorithm(matrix);

            int[] tasks = algorithm.Compute();

            Assert.AreEqual(0, tasks[0]);
            Assert.AreEqual(1, tasks[1]);
            Assert.AreEqual(2, tasks[2]);
        }
Example #18
0
    void mapDataPointsToBars()
    {
        var costMatrix = buildCostMatrix(_dataPoints);

        _barsAssignments = HungarianAlgorithm.FindAssignments(costMatrix);

        for (int i = 0; i < _dataPoints.Count; i++)
        {
            var point      = _dataPoints[i];
            var bar        = bars[_barsAssignments[i] * 2];
            var traperzoid = bar.GetComponent <TrapezoidBarBehavior>();
            traperzoid.mapDataPoint = point;
            traperzoid._level       = 1 / _maxDataPointValue * point.Value;
            traperzoid.ReCalculateScale();

            drawProjectionLine(point, bar);
        }
    }
Example #19
0
        public void Reshape(Mesh mesh)
        {
            float[,] costs = new float[vertexCount(), mesh.vertexCount()];
            for (int i = 0; i < vertexCount(); i++)
            {
                for (int j = 0; j < mesh.vertexCount(); j++)
                {
                    costs[i, j] = (float)Vector.Distance(vertices[i].v, mesh.vertices[j].v);
                }
            }

            int[] mapping = HungarianAlgorithm.FindAssignments(costs);

            for (int i = 0; i < mapping.Length; i++)
            {
                vertices[i].v = mesh.vertices[mapping[i]].v;
            }

            //Console.WriteLine(mapping.Length);
        }
Example #20
0
        private void ComputeAssignments(PrimitiveCurve[] curves, CurveCategories category)
        {
            // get distance transforms of sketch curves according to the given category
            var distanceTransforms =
                (from idx in Enumerable.Range(0, sessionData.SketchObjects.Length)
                 where sessionData.SketchObjects[idx].CurveCategory == category
                 select new { Index = idx, DistanceTransform = sessionData.DistanceTransforms[idx] }
                ).ToArray();


            // compute matching costs using distance transform integral
            int[,] matchingMatrix = new int[curves.Length, distanceTransforms.Length];
            var matchingMatrixIndices =
                from i in Enumerable.Range(0, curves.Length)
                from j in Enumerable.Range(0, distanceTransforms.Length)
                select new { I = i, J = j };

            Parallel.ForEach(matchingMatrixIndices, item =>
            {
                var i                = item.I;
                var j                = item.J;
                var integral         = DistanceTransformIntegral.Compute(curves[i].Points, distanceTransforms[j].DistanceTransform);
                matchingMatrix[i, j] = (int)Math.Round(integral);
            });

            // compute minimum-cost assignments of primitive curves to sketch curves
            var matchingMatrixCopy = (int[, ])matchingMatrix.Clone();
            var assignments        = HungarianAlgorithm.FindMaxWeightAssignments(matchingMatrixCopy);

            // assign object curves to sketch curves according to the computed assignments
            for (int i = 0; i < assignments.Length; ++i)
            {
                var assignedTo      = assignments[i];
                var assignedToCurve = sessionData.SketchObjects[distanceTransforms[assignedTo].Index];
                curves[i].AssignedTo   = assignedToCurve;
                curves[i].ClosestPoint =
                    DistanceTransformIntegral.MinDistancePoint(
                        curves[i].Points,
                        distanceTransforms[assignedTo].DistanceTransform);
            }
        }
Example #21
0
        /// <summary>
        /// 计算名次车号
        /// 既第一名是几号车, 第二名是几号车...
        /// </summary>
        /// <param name="pkId">期号</param>
        public List <int> CalculateRanks(int pkId)
        {
            // 下注金额按(名次+车号)求和
            var betAmounts = GetBetAmounts(pkId);

            // 没有人下注, 返回随机名次
            if (betAmounts.Sum(a => a.Amount) == 0)
            {
                var ranks = RandomUtil.GetRandomList(1, 10);

                return(ranks);
            }

            // 计算下注百分比
            var betRates = CalculateBetRates(betAmounts);
            // 奖池百分比 转换成 矩阵, 用于计算最小中奖名次 [TODO]大于1必不中
            var matrix = GetMatrix(betRates);
            //// 名次下注百分比
            //var rankRates = GetRankRates(betRates);

            /////////////////////test//////////////////////////////
            //var matrixStr = GetMatrixStr(matrix);
            ////////////////////test///////////////////////////////

            // 计算最小中奖名次
            var minCostMatrix = new HungarianAlgorithm(matrix).Run();

            // 验证最小中奖名次的 [奖池百分比] 之和 是否小于 100%, 小于则返回, 否则返回null
            if (IsValidRanks(matrix, minCostMatrix))
            {
                // 计算名次:比赛结果:车号顺序
                var ranks = GetRanks(minCostMatrix, betRates);

                return(ranks);
            }
            else
            {
                return(null);
            }
        }
Example #22
0
        public void ngramset_edit_distance(List <string> mnemonics1, List <string> mnemonics2, int n)
        {
            Ngram ngram1 = new Ngram(mnemonics1, n);
            Ngram ngram2 = new Ngram(mnemonics2, n);

            int set_size = ngram1.ngramSet.Count;
            //int reduced_set_size = set_size  - Math.Max(mnemonics1.FindIndex(delegate (string data) { return data == ""; }), mnemonics2.FindIndex(delegate (string data) { return data == ""; }));
            int ngram_size = n;

            int[,] matrix = ngram_matrix(ngram1.ngramSet, ngram2.ngramSet);
            var hungarian = new HungarianAlgorithm(matrix);

            int[] hungarian_indexes = hungarian.Run();

            double ngram_edit_distance = getTotal(matrix, hungarian_indexes) / set_size / ngram_size;
            double edit_distance       = calc_edit_distance(mnemonics1, mnemonics2);
            double slope1_ratio        = calculate_slope1_ratio(hungarian_indexes);
            double index_similarity2   = calculate_continuous_equal_slope(hungarian_indexes, 2);
            double index_similarity3   = calculate_continuous_equal_slope(hungarian_indexes, 3);

            this.similarity = new ngram_similarity(ngram_edit_distance, edit_distance, slope1_ratio, index_similarity2, index_similarity3);
        }
 public void Run_EmptyCostMatrix_ReturnsEmptyResult()
 {
     Assert.Empty(HungarianAlgorithm.Run(new float[0, 0]));
 }
Example #24
0
        public void JobAssignmentIterations()
        {
            // J = Job | W = Worker
            //     J1  J2  J3  J4
            // W1  82  83  69  92
            // W2  77  37  49  92
            // W3  11  69  5   86
            // W4  8   9   98  23

            int[,] matrix =
            {
                { 82, 83, 69, 92 },
                { 77, 37, 49, 92 },
                { 11, 69,  5, 86 },
                {  8,  9, 98, 23 }
            };
            var algorithm = new HungarianAlgorithm(matrix);

            HungarianIteration[] iterations = algorithm.GetIterations().ToArray();

            Assert.IsNotNull(algorithm.AgentsTasks);
            int[] tasks = algorithm.AgentsTasks;
            Assert.AreEqual(2, tasks[0]); // J1 to be done by W3
            Assert.AreEqual(1, tasks[1]); // J2 to be done by W2
            Assert.AreEqual(0, tasks[2]); // J3 to be done by W1
            Assert.AreEqual(3, tasks[3]); // J4 to be done by W4

            Assert.AreEqual(11, iterations.Length);
            CollectionAssert.AreEqual(
                new[]
            {
                new[, ] {
                    { 13, 14, 0, 23 }, { 40, 0, 12, 55 }, { 6, 64, 0, 81 }, { 0, 1, 90, 15 }
                },
                new[, ] {
                    { 13, 14, 0, 23 }, { 40, 0, 12, 55 }, { 6, 64, 0, 81 }, { 0, 1, 90, 15 }
                },
                new[, ] {
                    { 13, 14, 0, 23 }, { 40, 0, 12, 55 }, { 6, 64, 0, 81 }, { 0, 1, 90, 15 }
                },
                new[, ] {
                    { 13, 14, 0, 8 }, { 40, 0, 12, 40 }, { 6, 64, 0, 66 }, { 0, 1, 90, 0 }
                },
                new[, ] {
                    { 13, 14, 0, 8 }, { 40, 0, 12, 40 }, { 6, 64, 0, 66 }, { 0, 1, 90, 0 }
                },
                new[, ] {
                    { 13, 14, 0, 8 }, { 40, 0, 12, 40 }, { 6, 64, 0, 66 }, { 0, 1, 90, 0 }
                },
                new[, ] {
                    { 7, 14, 0, 2 }, { 34, 0, 12, 34 }, { 0, 64, 0, 60 }, { 0, 7, 96, 0 }
                },
                new[, ] {
                    { 7, 14, 0, 2 }, { 34, 0, 12, 34 }, { 0, 64, 0, 60 }, { 0, 7, 96, 0 }
                },
                new[, ] {
                    { 7, 14, 0, 2 }, { 34, 0, 12, 34 }, { 0, 64, 0, 60 }, { 0, 7, 96, 0 }
                },
                new[, ] {
                    { 7, 14, 0, 2 }, { 34, 0, 12, 34 }, { 0, 64, 0, 60 }, { 0, 7, 96, 0 }
                },
                new[, ] {
                    { 7, 14, 0, 2 }, { 34, 0, 12, 34 }, { 0, 64, 0, 60 }, { 0, 7, 96, 0 }
                }
            },
                iterations.Select(iteration => iteration.Matrix));
            CollectionAssert.AreEqual(
                new[]
            {
                new[, ] {
                    { 0, 0, 1, 0 }, { 0, 1, 0, 0 }, { 0, 0, 0, 0 }, { 1, 0, 0, 0 }
                },
                new[, ] {
                    { 0, 0, 1, 0 }, { 0, 1, 0, 0 }, { 0, 0, 0, 0 }, { 1, 0, 0, 0 }
                },
                new[, ] {
                    { 0, 0, 1, 0 }, { 0, 1, 0, 0 }, { 0, 0, 0, 0 }, { 1, 0, 0, 0 }
                },
                new[, ] {
                    { 0, 0, 1, 0 }, { 0, 1, 0, 0 }, { 0, 0, 0, 0 }, { 1, 0, 0, 0 }
                },
                new[, ] {
                    { 0, 0, 1, 0 }, { 0, 1, 0, 0 }, { 0, 0, 0, 0 }, { 1, 0, 0, 2 }
                },
                new[, ] {
                    { 0, 0, 1, 0 }, { 0, 1, 0, 0 }, { 0, 0, 0, 0 }, { 1, 0, 0, 2 }
                },
                new[, ] {
                    { 0, 0, 1, 0 }, { 0, 1, 0, 0 }, { 0, 0, 0, 0 }, { 1, 0, 0, 2 }
                },
                new[, ] {
                    { 0, 0, 1, 0 }, { 0, 1, 0, 0 }, { 2, 0, 0, 0 }, { 1, 0, 0, 2 }
                },
                new[, ] {
                    { 0, 0, 1, 0 }, { 0, 1, 0, 0 }, { 1, 0, 0, 0 }, { 0, 0, 0, 1 }
                },
                new[, ] {
                    { 0, 0, 1, 0 }, { 0, 1, 0, 0 }, { 1, 0, 0, 0 }, { 0, 0, 0, 1 }
                },
                new[, ] {
                    { 0, 0, 1, 0 }, { 0, 1, 0, 0 }, { 1, 0, 0, 0 }, { 0, 0, 0, 1 }
                }
            },
                iterations.Select(iteration => iteration.Mask));
            CollectionAssert.AreEqual(
                new[]
            {
                new[] { false, false, false, false },
                new[] { false, false, false, false },
                new[] { false, false, false, false },
                new[] { false, false, false, false },
                new[] { false, false, false, true },
                new[] { false, false, false, true },
                new[] { false, false, false, true },
                new[] { false, false, false, true },
                new[] { false, false, false, false },
                new[] { false, false, false, false },
                new[] { false, false, false, false }
            },
                iterations.Select(iteration => iteration.RowsCovered));
            CollectionAssert.AreEqual(
                new[]
            {
                new[] { false, false, false, false },
                new[] { true, true, true, false },
                new[] { true, true, true, false },
                new[] { true, true, true, false },
                new[] { false, true, true, false },
                new[] { false, true, true, false },
                new[] { false, true, true, false },
                new[] { false, true, true, false },
                new[] { false, false, false, false },
                new[] { true, true, true, true },
                new[] { true, true, true, true }
            },
                iterations.Select(iteration => iteration.ColumnsCovered));
            CollectionAssert.AreEqual(
                new[]
            {
                HungarianAlgorithm.Steps.Init,
                HungarianAlgorithm.Steps.Step1,
                HungarianAlgorithm.Steps.Step2,
                HungarianAlgorithm.Steps.Step4,
                HungarianAlgorithm.Steps.Step2,
                HungarianAlgorithm.Steps.Step2,
                HungarianAlgorithm.Steps.Step4,
                HungarianAlgorithm.Steps.Step2,
                HungarianAlgorithm.Steps.Step3,
                HungarianAlgorithm.Steps.Step1,
                HungarianAlgorithm.Steps.End
            },
                iterations.Select(iteration => iteration.Step));
        }
 public void Run_Metric_MatchClosest()
 {
     Assert.Equal(new[] { 1, 2, 0 },
                  HungarianAlgorithm.Run(new[] { -100, 0, 100 }, new[] { 111, -111, 42 }, (i1, i2) => Abs(i2 - i1)));
 }
 public void Run_EmptyCostMatrix_DoesNotFail()
 {
     HungarianAlgorithm.Run(new float[0, 0]);
 }
 public void Run_OneSource_AssignsTarget(float cost)
 {
     Assert.Equal(new[] { 0 }, HungarianAlgorithm.Run(new[, ] {
         { cost }
     }));
 }
 public void Run_NaNCost_ThrowsException()
 {
     Assert.Throws <ArgumentException>(() => HungarianAlgorithm.Run(new[, ] {
         { float.NaN }
     }));
 }
Example #29
0
        public void SimpleAssignmentIterations()
        {
            int[,] matrix =
            {
                { 1, 2, 3 },
                { 3, 3, 3 },
                { 3, 3, 2 }
            };
            var algorithm = new HungarianAlgorithm(matrix);

            HungarianIteration[] iterations = algorithm.GetIterations().ToArray();

            int[] tasks = algorithm.AgentsTasks;
            Assert.AreEqual(0, tasks[0]);
            Assert.AreEqual(1, tasks[1]);
            Assert.AreEqual(2, tasks[2]);

            Assert.AreEqual(3, iterations.Length);
            CollectionAssert.AreEqual(
                new[]
            {
                new[, ] {
                    { 0, 1, 2 }, { 0, 0, 0 }, { 1, 1, 0 }
                },
                new[, ] {
                    { 0, 1, 2 }, { 0, 0, 0 }, { 1, 1, 0 }
                },
                new[, ] {
                    { 0, 1, 2 }, { 0, 0, 0 }, { 1, 1, 0 }
                }
            },
                iterations.Select(iteration => iteration.Matrix));
            CollectionAssert.AreEqual(
                new[]
            {
                new[, ] {
                    { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 }
                },
                new[, ] {
                    { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 }
                },
                new[, ] {
                    { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 }
                }
            },
                iterations.Select(iteration => iteration.Mask));
            CollectionAssert.AreEqual(
                new[]
            {
                new[] { false, false, false },
                new[] { false, false, false },
                new[] { false, false, false }
            },
                iterations.Select(iteration => iteration.RowsCovered));
            CollectionAssert.AreEqual(
                new[]
            {
                new[] { false, false, false },
                new[] { true, true, true },
                new[] { true, true, true }
            },
                iterations.Select(iteration => iteration.ColumnsCovered));
            CollectionAssert.AreEqual(
                new[]
            {
                HungarianAlgorithm.Steps.Init,
                HungarianAlgorithm.Steps.Step1,
                HungarianAlgorithm.Steps.End
            },
                iterations.Select(iteration => iteration.Step));
        }
 public void Run_Vector3s_MathClosest()
 {
     Assert.Equal(new[] { 1, 0 },
                  HungarianAlgorithm.Run(new[] { Vector3.Zero, Vector3.UnitY }, new[] { Vector3.One, Vector3.Zero }));
 }