Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 9596 was 8206, checked in by gkronber, 12 years ago

#1847: merged r8084:8205 from trunk into GP move operators branch

File size: 9.9 KB
RevLine 
[7873]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;
[8022]24using System.Linq;
[7873]25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
[8022]28using HeuristicLab.Encodings.PermutationEncoding;
[7873]29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[8022]32using HeuristicLab.PluginInfrastructure;
[7873]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]
[8206]38  public sealed class LinearAssignmentProblem : SingleObjectiveHeuristicOptimizationProblem<ILAPEvaluator, IPermutationCreator>, IStorableContent {
[8022]39    public static readonly string CostsDescription = "The cost matrix that describes the assignment of rows to columns.";
[8206]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.";
[8022]42
[8206]43    public string Filename { get; set; }
44
[7873]45    public override Image ItemImage {
46      get { return HeuristicLab.Common.Resources.VSImageLibrary.Type; }
47    }
48
49    #region Parameter Properties
50    public IValueParameter<DoubleMatrix> CostsParameter {
51      get { return (IValueParameter<DoubleMatrix>)Parameters["Costs"]; }
52    }
[8022]53    public IValueParameter<ItemSet<Permutation>> BestKnownSolutionsParameter {
54      get { return (IValueParameter<ItemSet<Permutation>>)Parameters["BestKnownSolutions"]; }
[7873]55    }
[8022]56    public IValueParameter<Permutation> BestKnownSolutionParameter {
57      get { return (IValueParameter<Permutation>)Parameters["BestKnownSolution"]; }
[7873]58    }
[8206]59    public IValueParameter<StringArray> RowNamesParameter {
60      get { return (IValueParameter<StringArray>)Parameters["RowNames"]; }
61    }
62    public IValueParameter<StringArray> ColumnNamesParameter {
63      get { return (IValueParameter<StringArray>)Parameters["ColumnNames"]; }
64    }
[7873]65    #endregion
66
67    #region Properties
68    public DoubleMatrix Costs {
69      get { return CostsParameter.Value; }
70      set { CostsParameter.Value = value; }
71    }
[8206]72    public StringArray RowNames {
73      get { return RowNamesParameter.Value; }
74      set { RowNamesParameter.Value = value; }
75    }
76    public StringArray ColumnNames {
77      get { return ColumnNamesParameter.Value; }
78      set { ColumnNamesParameter.Value = value; }
79    }
[8022]80    public ItemSet<Permutation> BestKnownSolutions {
81      get { return BestKnownSolutionsParameter.Value; }
82      set { BestKnownSolutionsParameter.Value = value; }
[7873]83    }
[8022]84    public Permutation BestKnownSolution {
85      get { return BestKnownSolutionParameter.Value; }
86      set { BestKnownSolutionParameter.Value = value; }
[7873]87    }
88    #endregion
89
[8022]90    [Storable]
91    private BestLAPSolutionAnalyzer bestLAPSolutionAnalyzer;
92
[7873]93    [StorableConstructor]
94    private LinearAssignmentProblem(bool deserializing) : base(deserializing) { }
95    private LinearAssignmentProblem(LinearAssignmentProblem original, Cloner cloner)
96      : base(original, cloner) {
[8022]97      this.bestLAPSolutionAnalyzer = cloner.Clone(original.bestLAPSolutionAnalyzer);
[7873]98      AttachEventHandlers();
99    }
100    public LinearAssignmentProblem()
[8022]101      : base(new LAPEvaluator(), new RandomPermutationCreator()) {
102      Parameters.Add(new ValueParameter<DoubleMatrix>("Costs", CostsDescription, new DoubleMatrix(3, 3)));
103      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));
104      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));
[8206]105      Parameters.Add(new OptionalValueParameter<StringArray>("RowNames", RowNamesDescription));
106      Parameters.Add(new OptionalValueParameter<StringArray>("ColumnNames", ColumnNamesDescription));
107
[7873]108      ((ValueParameter<DoubleMatrix>)CostsParameter).ReactOnValueToStringChangedAndValueItemImageChanged = false;
[8206]109      ((OptionalValueParameter<StringArray>)RowNamesParameter).ReactOnValueToStringChangedAndValueItemImageChanged = false;
110      ((OptionalValueParameter<StringArray>)ColumnNamesParameter).ReactOnValueToStringChangedAndValueItemImageChanged = false;
[7873]111
[8206]112      RowNames = new StringArray(new string[] { "Eric", "Robert", "Allison" });
113      ColumnNames = new StringArray(new string[] { "MRI", "Blood test", "Angiogram" });
114      Costs[0, 0] = 4; Costs[0, 1] = 5; Costs[0, 2] = 3;
115      Costs[1, 0] = 6; Costs[1, 1] = 6; Costs[1, 2] = 4;
116      Costs[2, 0] = 5; Costs[2, 1] = 5; Costs[2, 2] = 1;
117
[8022]118      bestLAPSolutionAnalyzer = new BestLAPSolutionAnalyzer();
119      SolutionCreator.PermutationParameter.ActualName = "Assignment";
120      InitializeOperators();
121      Parameterize();
[7873]122      AttachEventHandlers();
123    }
124
125    public override IDeepCloneable Clone(Cloner cloner) {
126      return new LinearAssignmentProblem(this, cloner);
127    }
128
129    #region Events
[8022]130    protected override void OnEvaluatorChanged() {
131      base.OnEvaluatorChanged();
132      Parameterize();
133    }
134    protected override void OnOperatorsChanged() {
135      base.OnOperatorsChanged();
136      Parameterize();
137    }
138    protected override void OnSolutionCreatorChanged() {
139      base.OnSolutionCreatorChanged();
140      SolutionCreator.PermutationParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged);
141      Parameterize();
142    }
[7873]143    private void Costs_RowsChanged(object sender, EventArgs e) {
[8022]144      if (Costs.Rows != Costs.Columns) {
[7873]145        ((IStringConvertibleMatrix)Costs).Columns = Costs.Rows;
[8022]146        Parameterize();
147      }
[7873]148    }
149    private void Costs_ColumnsChanged(object sender, EventArgs e) {
[8022]150      if (Costs.Rows != Costs.Columns) {
[7873]151        ((IStringConvertibleMatrix)Costs).Rows = Costs.Columns;
[8022]152        Parameterize();
153      }
[7873]154    }
[8022]155    private void SolutionCreator_PermutationParameter_ActualNameChanged(object sender, EventArgs e) {
156      Parameterize();
157    }
[7873]158    #endregion
159
160    #region Helpers
161    [StorableHook(HookType.AfterDeserialization)]
[7934]162    private void AfterDeserialization() {
[7873]163      AttachEventHandlers();
164    }
165
166    private void AttachEventHandlers() {
167      Costs.RowsChanged += new EventHandler(Costs_RowsChanged);
168      Costs.ColumnsChanged += new EventHandler(Costs_ColumnsChanged);
[8022]169      SolutionCreator.PermutationParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged);
[7873]170    }
[8022]171
172    private void InitializeOperators() {
173      Operators.AddRange(ApplicationManager.Manager.GetInstances<IPermutationOperator>());
174      Operators.RemoveAll(x => x is IMoveOperator);
175      Operators.Add(bestLAPSolutionAnalyzer);
176    }
177
178    private void Parameterize() {
179      SolutionCreator.LengthParameter.Value = new IntValue(Costs.Rows);
180      SolutionCreator.LengthParameter.Hidden = false;
181      Evaluator.CostsParameter.ActualName = CostsParameter.Name;
182      Evaluator.CostsParameter.Hidden = true;
183      Evaluator.AssignmentParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
184      Evaluator.AssignmentParameter.Hidden = true;
185
186      foreach (var op in Operators.OfType<IPermutationCrossover>()) {
187        op.ParentsParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
188        op.ParentsParameter.Hidden = true;
189        op.ChildParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
190        op.ChildParameter.Hidden = true;
191      }
192
193      foreach (var op in Operators.OfType<IPermutationManipulator>()) {
194        op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
195        op.PermutationParameter.Hidden = true;
196      }
197
198      foreach (var op in Operators.OfType<IPermutationMultiNeighborhoodShakingOperator>()) {
199        op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
200        op.PermutationParameter.Hidden = true;
201      }
202
203      bestLAPSolutionAnalyzer.AssignmentParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
204      bestLAPSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name;
205      bestLAPSolutionAnalyzer.BestKnownSolutionParameter.ActualName = BestKnownSolutionParameter.Name;
206      bestLAPSolutionAnalyzer.BestKnownSolutionsParameter.ActualName = BestKnownSolutionsParameter.Name;
207      bestLAPSolutionAnalyzer.CostsParameter.ActualName = CostsParameter.Name;
208      bestLAPSolutionAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
209      bestLAPSolutionAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
210    }
[7873]211    #endregion
212  }
213}
Note: See TracBrowser for help on using the repository browser.