Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Problems.LinearAssignment/3.3/HungarianAlgorithm.cs @ 17530

Last change on this file since 17530 was 17530, checked in by abeham, 4 years ago

#2521: Refactored linear assignment problem

File size: 2.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Threading;
24using HEAL.Attic;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Encodings.PermutationEncoding;
28using HeuristicLab.Optimization;
29
30namespace HeuristicLab.Problems.LinearAssignment {
31  /// <summary>
32  /// A genetic algorithm.
33  /// </summary>
34  [Item("Hungarian Algorithm", "The Hungarian algorithm can be used to solve the linear assignment problem in O(n^3). It is also known as the Kuhn–Munkres algorithm or Munkres assignment algorithm.")]
35  [Creatable(CreatableAttribute.Categories.SingleSolutionAlgorithms, Priority = 170)]
36  [StorableType("2A2C1C1B-E0C3-4757-873B-C3F7C6D11A01")]
37  public sealed class HungarianAlgorithm : BasicAlgorithm {
38    public override bool SupportsPause => false;
39
40    #region Problem Properties
41    public override Type ProblemType {
42      get { return typeof(LinearAssignmentProblem); }
43    }
44    public new LinearAssignmentProblem Problem {
45      get { return (LinearAssignmentProblem)base.Problem; }
46      set { base.Problem = value; }
47    }
48    #endregion
49
50    [StorableConstructor]
51    private HungarianAlgorithm(StorableConstructorFlag _) : base(_) { }
52    private HungarianAlgorithm(HungarianAlgorithm original, Cloner cloner) : base(original, cloner) { }
53    public HungarianAlgorithm()
54      : base() {
55      Problem = new LinearAssignmentProblem();
56    }
57
58    public override IDeepCloneable Clone(Cloner cloner) {
59      return new HungarianAlgorithm(this, cloner);
60    }
61
62    protected override void Run(CancellationToken cancellationToken) {
63      var assignment = LinearAssignmentProblemSolver.Solve(Problem.Costs, out double quality);
64
65      Problem.Analyze(new[] { new Permutation(PermutationTypes.Absolute, assignment) },
66        new[] { quality }, Results, null);
67    }
68  }
69}
Note: See TracBrowser for help on using the repository browser.