Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP-MoveOperators/HeuristicLab.Problems.LinearAssignment/3.3/LinearAssignmentProblem.cs @ 8085

Last change on this file since 8085 was 8022, checked in by abeham, 12 years ago

#1855:

  • Transformed LAP into a SingleObjectiveHeuristicOptimizationProblem
  • Added HungarianAlgorithm as separate algorithm for solving the problem
File size: 8.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.Drawing;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.PermutationEncoding;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32using HeuristicLab.PluginInfrastructure;
33
34namespace HeuristicLab.Problems.LinearAssignment {
35  [Item("LinearAssignmentProblem", "In the linear assignment problem (LAP) an assignment of workers to jobs has to be found such that each worker is assigned to exactly one job, each job is assigned to exactly one worker and the sum of the resulting costs needs to be minimal.")]
36  [Creatable("Problems")]
37  [StorableClass]
38  public sealed class LinearAssignmentProblem : SingleObjectiveHeuristicOptimizationProblem<ILAPEvaluator, IPermutationCreator> {
39    public static readonly string CostsDescription = "The cost matrix that describes the assignment of rows to columns.";
40
41    public override Image ItemImage {
42      get { return HeuristicLab.Common.Resources.VSImageLibrary.Type; }
43    }
44
45    #region Parameter Properties
46    public IValueParameter<DoubleMatrix> CostsParameter {
47      get { return (IValueParameter<DoubleMatrix>)Parameters["Costs"]; }
48    }
49    public IValueParameter<ItemSet<Permutation>> BestKnownSolutionsParameter {
50      get { return (IValueParameter<ItemSet<Permutation>>)Parameters["BestKnownSolutions"]; }
51    }
52    public IValueParameter<Permutation> BestKnownSolutionParameter {
53      get { return (IValueParameter<Permutation>)Parameters["BestKnownSolution"]; }
54    }
55    #endregion
56
57    #region Properties
58    public DoubleMatrix Costs {
59      get { return CostsParameter.Value; }
60      set { CostsParameter.Value = value; }
61    }
62    public ItemSet<Permutation> BestKnownSolutions {
63      get { return BestKnownSolutionsParameter.Value; }
64      set { BestKnownSolutionsParameter.Value = value; }
65    }
66    public Permutation BestKnownSolution {
67      get { return BestKnownSolutionParameter.Value; }
68      set { BestKnownSolutionParameter.Value = value; }
69    }
70    #endregion
71
72    [Storable]
73    private BestLAPSolutionAnalyzer bestLAPSolutionAnalyzer;
74
75    [StorableConstructor]
76    private LinearAssignmentProblem(bool deserializing) : base(deserializing) { }
77    private LinearAssignmentProblem(LinearAssignmentProblem original, Cloner cloner)
78      : base(original, cloner) {
79      this.bestLAPSolutionAnalyzer = cloner.Clone(original.bestLAPSolutionAnalyzer);
80      AttachEventHandlers();
81    }
82    public LinearAssignmentProblem()
83      : base(new LAPEvaluator(), new RandomPermutationCreator()) {
84      Parameters.Add(new ValueParameter<DoubleMatrix>("Costs", CostsDescription, new DoubleMatrix(3, 3)));
85      Parameters.Add(new OptionalValueParameter<ItemSet<Permutation>>("BestKnownSolutions", "The list of best known solutions which is updated whenever a new better solution is found or may be the optimal solution if it is known beforehand.", null));
86      Parameters.Add(new OptionalValueParameter<Permutation>("BestKnownSolution", "The best known solution which is updated whenever a new better solution is found or may be the optimal solution if it is known beforehand.", null));
87     
88      ((ValueParameter<DoubleMatrix>)CostsParameter).ReactOnValueToStringChangedAndValueItemImageChanged = false;
89
90      bestLAPSolutionAnalyzer = new BestLAPSolutionAnalyzer();
91      SolutionCreator.PermutationParameter.ActualName = "Assignment";
92      InitializeOperators();
93      Parameterize();
94      AttachEventHandlers();
95    }
96
97    public override IDeepCloneable Clone(Cloner cloner) {
98      return new LinearAssignmentProblem(this, cloner);
99    }
100
101    #region Events
102    protected override void OnEvaluatorChanged() {
103      base.OnEvaluatorChanged();
104      Parameterize();
105    }
106    protected override void OnOperatorsChanged() {
107      base.OnOperatorsChanged();
108      Parameterize();
109    }
110    protected override void OnSolutionCreatorChanged() {
111      base.OnSolutionCreatorChanged();
112      SolutionCreator.PermutationParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged);
113      Parameterize();
114    }
115    private void Costs_RowsChanged(object sender, EventArgs e) {
116      if (Costs.Rows != Costs.Columns) {
117        ((IStringConvertibleMatrix)Costs).Columns = Costs.Rows;
118        Parameterize();
119      }
120    }
121    private void Costs_ColumnsChanged(object sender, EventArgs e) {
122      if (Costs.Rows != Costs.Columns) {
123        ((IStringConvertibleMatrix)Costs).Rows = Costs.Columns;
124        Parameterize();
125      }
126    }
127    private void SolutionCreator_PermutationParameter_ActualNameChanged(object sender, EventArgs e) {
128      Parameterize();
129    }
130    #endregion
131
132    #region Helpers
133    [StorableHook(HookType.AfterDeserialization)]
134    private void AfterDeserialization() {
135      AttachEventHandlers();
136    }
137
138    private void AttachEventHandlers() {
139      Costs.RowsChanged += new EventHandler(Costs_RowsChanged);
140      Costs.ColumnsChanged += new EventHandler(Costs_ColumnsChanged);
141      SolutionCreator.PermutationParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged);
142    }
143
144    private void InitializeOperators() {
145      Operators.AddRange(ApplicationManager.Manager.GetInstances<IPermutationOperator>());
146      Operators.RemoveAll(x => x is IMoveOperator);
147      Operators.Add(bestLAPSolutionAnalyzer);
148    }
149
150    private void Parameterize() {
151      SolutionCreator.LengthParameter.Value = new IntValue(Costs.Rows);
152      SolutionCreator.LengthParameter.Hidden = false;
153      Evaluator.CostsParameter.ActualName = CostsParameter.Name;
154      Evaluator.CostsParameter.Hidden = true;
155      Evaluator.AssignmentParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
156      Evaluator.AssignmentParameter.Hidden = true;
157
158      foreach (var op in Operators.OfType<IPermutationCrossover>()) {
159        op.ParentsParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
160        op.ParentsParameter.Hidden = true;
161        op.ChildParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
162        op.ChildParameter.Hidden = true;
163      }
164
165      foreach (var op in Operators.OfType<IPermutationManipulator>()) {
166        op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
167        op.PermutationParameter.Hidden = true;
168      }
169
170      foreach (var op in Operators.OfType<IPermutationMultiNeighborhoodShakingOperator>()) {
171        op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
172        op.PermutationParameter.Hidden = true;
173      }
174
175      bestLAPSolutionAnalyzer.AssignmentParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
176      bestLAPSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name;
177      bestLAPSolutionAnalyzer.BestKnownSolutionParameter.ActualName = BestKnownSolutionParameter.Name;
178      bestLAPSolutionAnalyzer.BestKnownSolutionsParameter.ActualName = BestKnownSolutionsParameter.Name;
179      bestLAPSolutionAnalyzer.CostsParameter.ActualName = CostsParameter.Name;
180      bestLAPSolutionAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
181      bestLAPSolutionAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
182    }
183    #endregion
184  }
185}
Note: See TracBrowser for help on using the repository browser.