Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.LinearAssignment/3.3/LinearAssignmentProblem.cs @ 8151

Last change on this file since 8151 was 8093, checked in by abeham, 12 years ago

#1855:

  • Added possibility to name row and column in matrix (to have more interpretable results)
  • Added a "standard" assignment problem
File size: 9.9 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    public static readonly string RowNamesDescription = "The elements represented by the rows of the costs matrix.";
41    public static readonly string ColumnNamesDescription = "The elements represented by the columns of the costs matrix.";
42
43    public override Image ItemImage {
44      get { return HeuristicLab.Common.Resources.VSImageLibrary.Type; }
45    }
46
47    #region Parameter Properties
48    public IValueParameter<DoubleMatrix> CostsParameter {
49      get { return (IValueParameter<DoubleMatrix>)Parameters["Costs"]; }
50    }
51    public IValueParameter<ItemSet<Permutation>> BestKnownSolutionsParameter {
52      get { return (IValueParameter<ItemSet<Permutation>>)Parameters["BestKnownSolutions"]; }
53    }
54    public IValueParameter<Permutation> BestKnownSolutionParameter {
55      get { return (IValueParameter<Permutation>)Parameters["BestKnownSolution"]; }
56    }
57    public IValueParameter<StringArray> RowNamesParameter {
58      get { return (IValueParameter<StringArray>)Parameters["RowNames"]; }
59    }
60    public IValueParameter<StringArray> ColumnNamesParameter {
61      get { return (IValueParameter<StringArray>)Parameters["ColumnNames"]; }
62    }
63    #endregion
64
65    #region Properties
66    public DoubleMatrix Costs {
67      get { return CostsParameter.Value; }
68      set { CostsParameter.Value = value; }
69    }
70    public StringArray RowNames {
71      get { return RowNamesParameter.Value; }
72      set { RowNamesParameter.Value = value; }
73    }
74    public StringArray ColumnNames {
75      get { return ColumnNamesParameter.Value; }
76      set { ColumnNamesParameter.Value = value; }
77    }
78    public ItemSet<Permutation> BestKnownSolutions {
79      get { return BestKnownSolutionsParameter.Value; }
80      set { BestKnownSolutionsParameter.Value = value; }
81    }
82    public Permutation BestKnownSolution {
83      get { return BestKnownSolutionParameter.Value; }
84      set { BestKnownSolutionParameter.Value = value; }
85    }
86    #endregion
87
88    [Storable]
89    private BestLAPSolutionAnalyzer bestLAPSolutionAnalyzer;
90
91    [StorableConstructor]
92    private LinearAssignmentProblem(bool deserializing) : base(deserializing) { }
93    private LinearAssignmentProblem(LinearAssignmentProblem original, Cloner cloner)
94      : base(original, cloner) {
95      this.bestLAPSolutionAnalyzer = cloner.Clone(original.bestLAPSolutionAnalyzer);
96      AttachEventHandlers();
97    }
98    public LinearAssignmentProblem()
99      : base(new LAPEvaluator(), new RandomPermutationCreator()) {
100      Parameters.Add(new ValueParameter<DoubleMatrix>("Costs", CostsDescription, new DoubleMatrix(3, 3)));
101      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));
102      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));
103      Parameters.Add(new OptionalValueParameter<StringArray>("RowNames", RowNamesDescription));
104      Parameters.Add(new OptionalValueParameter<StringArray>("ColumnNames", ColumnNamesDescription));
105
106      ((ValueParameter<DoubleMatrix>)CostsParameter).ReactOnValueToStringChangedAndValueItemImageChanged = false;
107      ((OptionalValueParameter<StringArray>)RowNamesParameter).ReactOnValueToStringChangedAndValueItemImageChanged = false;
108      ((OptionalValueParameter<StringArray>)ColumnNamesParameter).ReactOnValueToStringChangedAndValueItemImageChanged = false;
109
110      RowNames = new StringArray(new string[] { "Human", "Von Neumann machine", "Quantum computer" });
111      ColumnNames = new StringArray(new string[] { "Find a person's e-mail address", "Compute first 1000 decimals of Pi", "Factorize large integers" });
112      Costs[0, 0] = 1; Costs[0, 1] = 10; Costs[0, 2] = 100;
113      Costs[1, 0] = 10; Costs[1, 1] = 1; Costs[1, 2] = 100;
114      Costs[2, 0] = 100; Costs[2, 1] = 10; Costs[2, 1] = 1;
115
116      bestLAPSolutionAnalyzer = new BestLAPSolutionAnalyzer();
117      SolutionCreator.PermutationParameter.ActualName = "Assignment";
118      InitializeOperators();
119      Parameterize();
120      AttachEventHandlers();
121    }
122
123    public override IDeepCloneable Clone(Cloner cloner) {
124      return new LinearAssignmentProblem(this, cloner);
125    }
126
127    #region Events
128    protected override void OnEvaluatorChanged() {
129      base.OnEvaluatorChanged();
130      Parameterize();
131    }
132    protected override void OnOperatorsChanged() {
133      base.OnOperatorsChanged();
134      Parameterize();
135    }
136    protected override void OnSolutionCreatorChanged() {
137      base.OnSolutionCreatorChanged();
138      SolutionCreator.PermutationParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged);
139      Parameterize();
140    }
141    private void Costs_RowsChanged(object sender, EventArgs e) {
142      if (Costs.Rows != Costs.Columns) {
143        ((IStringConvertibleMatrix)Costs).Columns = Costs.Rows;
144        Parameterize();
145      }
146    }
147    private void Costs_ColumnsChanged(object sender, EventArgs e) {
148      if (Costs.Rows != Costs.Columns) {
149        ((IStringConvertibleMatrix)Costs).Rows = Costs.Columns;
150        Parameterize();
151      }
152    }
153    private void SolutionCreator_PermutationParameter_ActualNameChanged(object sender, EventArgs e) {
154      Parameterize();
155    }
156    #endregion
157
158    #region Helpers
159    [StorableHook(HookType.AfterDeserialization)]
160    private void AfterDeserialization() {
161      AttachEventHandlers();
162    }
163
164    private void AttachEventHandlers() {
165      Costs.RowsChanged += new EventHandler(Costs_RowsChanged);
166      Costs.ColumnsChanged += new EventHandler(Costs_ColumnsChanged);
167      SolutionCreator.PermutationParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged);
168    }
169
170    private void InitializeOperators() {
171      Operators.AddRange(ApplicationManager.Manager.GetInstances<IPermutationOperator>());
172      Operators.RemoveAll(x => x is IMoveOperator);
173      Operators.Add(bestLAPSolutionAnalyzer);
174    }
175
176    private void Parameterize() {
177      SolutionCreator.LengthParameter.Value = new IntValue(Costs.Rows);
178      SolutionCreator.LengthParameter.Hidden = false;
179      Evaluator.CostsParameter.ActualName = CostsParameter.Name;
180      Evaluator.CostsParameter.Hidden = true;
181      Evaluator.AssignmentParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
182      Evaluator.AssignmentParameter.Hidden = true;
183
184      foreach (var op in Operators.OfType<IPermutationCrossover>()) {
185        op.ParentsParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
186        op.ParentsParameter.Hidden = true;
187        op.ChildParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
188        op.ChildParameter.Hidden = true;
189      }
190
191      foreach (var op in Operators.OfType<IPermutationManipulator>()) {
192        op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
193        op.PermutationParameter.Hidden = true;
194      }
195
196      foreach (var op in Operators.OfType<IPermutationMultiNeighborhoodShakingOperator>()) {
197        op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
198        op.PermutationParameter.Hidden = true;
199      }
200
201      bestLAPSolutionAnalyzer.AssignmentParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
202      bestLAPSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name;
203      bestLAPSolutionAnalyzer.BestKnownSolutionParameter.ActualName = BestKnownSolutionParameter.Name;
204      bestLAPSolutionAnalyzer.BestKnownSolutionsParameter.ActualName = BestKnownSolutionsParameter.Name;
205      bestLAPSolutionAnalyzer.CostsParameter.ActualName = CostsParameter.Name;
206      bestLAPSolutionAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
207      bestLAPSolutionAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
208    }
209    #endregion
210  }
211}
Note: See TracBrowser for help on using the repository browser.