Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17745 was 17745, checked in by mkommend, 4 years ago

#2971: Added first draft of results implementation and problem adaptation.

File size: 2.8 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;
29using HeuristicLab.Random;
30
31namespace HeuristicLab.Problems.LinearAssignment {
32  /// <summary>
33  /// A genetic algorithm.
34  /// </summary>
35  [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.")]
36  [Creatable(CreatableAttribute.Categories.SingleSolutionAlgorithms, Priority = 170)]
37  [StorableType("2A2C1C1B-E0C3-4757-873B-C3F7C6D11A01")]
38  public sealed class HungarianAlgorithm : BasicAlgorithm {
39    public override bool SupportsPause => false;
40
41    #region Problem Properties
42    public override Type ProblemType {
43      get { return typeof(LinearAssignmentProblem); }
44    }
45    public new LinearAssignmentProblem Problem {
46      get { return (LinearAssignmentProblem)base.Problem; }
47      set { base.Problem = value; }
48    }
49    #endregion
50
51    [StorableConstructor]
52    private HungarianAlgorithm(StorableConstructorFlag _) : base(_) { }
53    private HungarianAlgorithm(HungarianAlgorithm original, Cloner cloner) : base(original, cloner) { }
54    public HungarianAlgorithm()
55      : base() {
56      Problem = new LinearAssignmentProblem();
57    }
58
59    public override IDeepCloneable Clone(Cloner cloner) {
60      return new HungarianAlgorithm(this, cloner);
61    }
62
63    protected override void Run(CancellationToken cancellationToken) {
64      var assignment = LinearAssignmentProblemSolver.Solve(Problem.Costs, out double quality);
65
66      var context = new SingleObjectiveSolutionContext<Permutation>(new Permutation(PermutationTypes.Absolute, assignment));
67      context.EvaluationResult = new SingleObjectiveEvaluationResult(quality);
68
69      var random = new FastRandom(42);
70      Problem.Analyze(new[] { context }, random);
71    }
72  }
73}
Note: See TracBrowser for help on using the repository browser.