Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ScatterSearch (trunk integration)/HeuristicLab.Problems.LinearAssignment/3.3/HungarianAlgorithm.cs @ 8086

Last change on this file since 8086 was 8086, checked in by jkarder, 12 years ago

#1331:

  • synced branch with trunk
  • added custom interface (ISimilarityBasedOperator) to mark operators that conduct similarity calculation
  • similarity calculators are now parameterized by the algorithm
  • deleted SolutionPool2TierUpdateMethod
  • deleted KnapsackMultipleGuidesPathRelinker
  • moved IImprovementOperator, IPathRelinker and ISimilarityCalculator to HeuristicLab.Optimization
  • added parameter descriptions
  • fixed plugin references
  • fixed count of EvaluatedSolutions
  • fixed check for duplicate solutions
  • minor code improvements
File size: 5.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.Linq;
24using HeuristicLab.Analysis;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Operators;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Problems.LinearAssignment {
33  /// <summary>
34  /// A genetic algorithm.
35  /// </summary>
36  [Item("HungarianAlgorithm", "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.")]
37  [Creatable("Algorithms")]
38  [StorableClass]
39  public sealed class HungarianAlgorithm : EngineAlgorithm {
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    #region Parameter Properties
51    private ValueParameter<MultiAnalyzer> AnalyzerParameter {
52      get { return (ValueParameter<MultiAnalyzer>)Parameters["Analyzer"]; }
53    }
54
55    #endregion
56
57    #region Properties
58    public MultiAnalyzer Analyzer {
59      get { return AnalyzerParameter.Value; }
60      set { AnalyzerParameter.Value = value; }
61    }
62    private LinearAssignmentProblemSolver Solver {
63      get { return (LinearAssignmentProblemSolver)OperatorGraph.InitialOperator; }
64    }
65    #endregion
66
67    [StorableConstructor]
68    private HungarianAlgorithm(bool deserializing) : base(deserializing) { }
69    private HungarianAlgorithm(HungarianAlgorithm original, Cloner cloner)
70      : base(original, cloner) {
71      // TODO: clone your private fields here
72      AttachEventHandlers();
73    }
74    public HungarianAlgorithm()
75      : base() {
76      Parameters.Add(new ValueParameter<MultiAnalyzer>("Analyzer", "The operator used to analyze the result.", new MultiAnalyzer()));
77
78      var solver = new LinearAssignmentProblemSolver();
79      OperatorGraph.InitialOperator = solver;
80
81      var placeholder = new Placeholder();
82      placeholder.Name = "(Analyzer)";
83      placeholder.OperatorParameter.ActualName = AnalyzerParameter.Name;
84      solver.Successor = placeholder;
85
86      UpdateAnalyzers();
87      AttachEventHandlers();
88    }
89
90    public override IDeepCloneable Clone(Cloner cloner) {
91      return new HungarianAlgorithm(this, cloner);
92    }
93
94    public override void Prepare() {
95      if (Problem != null) base.Prepare();
96    }
97
98    #region Events
99    protected override void OnProblemChanged() {
100      Problem.SolutionCreatorChanged += new EventHandler(Problem_SolutionCreatorChanged);
101      Problem.EvaluatorChanged += new EventHandler(Problem_EvaluatorChanged);
102      UpdateAnalyzers();
103      Parameterize();
104      base.OnProblemChanged();
105    }
106
107    private void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
108      Parameterize();
109    }
110
111    private void Problem_EvaluatorChanged(object sender, EventArgs e) {
112      Parameterize();
113    }
114
115    protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
116      UpdateAnalyzers();
117      base.Problem_OperatorsChanged(sender, e);
118    }
119    #endregion
120
121    #region Helpers
122    [StorableHook(HookType.AfterDeserialization)]
123    private void AttachEventHandlers() {
124      if (Problem != null) {
125        Problem.SolutionCreatorChanged += new EventHandler(Problem_SolutionCreatorChanged);
126        Problem.EvaluatorChanged += new EventHandler(Problem_EvaluatorChanged);
127      }
128    }
129    private void UpdateAnalyzers() {
130      Analyzer.Operators.Clear();
131      if (Problem != null) {
132        foreach (var analyzer in Problem.OperatorsParameter.Value.OfType<IAnalyzer>()) {
133          foreach (IScopeTreeLookupParameter param in analyzer.Parameters.OfType<IScopeTreeLookupParameter>())
134            param.Depth = 0;
135          Analyzer.Operators.Add(analyzer);
136        }
137      }
138    }
139    private void Parameterize() {
140      if (Problem != null) {
141        Solver.AssignmentParameter.ActualName = Problem.SolutionCreator.PermutationParameter.ActualName;
142        Solver.CostsParameter.ActualName = Problem.CostsParameter.Name;
143        Solver.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
144      }
145    }
146    #endregion
147  }
148}
Note: See TracBrowser for help on using the repository browser.