Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 15481 was 15069, checked in by abeham, 7 years ago

#2706:

  • Added or updated similarity calculators and population similarity analysis for several problems (BinPacking, LAP, Orienteering, Parameter optimization, PTSP, QAP, TF, TSP, VRP)
  • Made TSPSimilarityCalculator obsolete since it's essentially the same as the one in the permutation plugin
  • Made QAPPopulationDiversityAnalyzer obsolete as it is replaced by the newer PopulationSimilarityAnalyzer
  • Removed genotype specific similarity code in QAPPermutationProximityCalculator (again identical to the permutation plugin)
  • Changed QAPSimilarityCalculator to perform phenotype similarity instead of genotype similarity (has not been previously used)
File size: 10.8 KB
RevLine 
[7873]1#region License Information
2/* HeuristicLab
[14185]3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[7873]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;
[15069]25using HeuristicLab.Analysis;
[7873]26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
[8022]29using HeuristicLab.Encodings.PermutationEncoding;
[7873]30using HeuristicLab.Optimization;
[15069]31using HeuristicLab.Optimization.Operators;
[7873]32using HeuristicLab.Parameters;
33using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[8022]34using HeuristicLab.PluginInfrastructure;
[7873]35
36namespace HeuristicLab.Problems.LinearAssignment {
[13173]37  [Item("Linear Assignment Problem (LAP)", "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 is minimal (or maximal).")]
[12504]38  [Creatable(CreatableAttribute.Categories.CombinatorialProblems, Priority = 130)]
[7873]39  [StorableClass]
[8183]40  public sealed class LinearAssignmentProblem : SingleObjectiveHeuristicOptimizationProblem<ILAPEvaluator, IPermutationCreator>, IStorableContent {
[8022]41    public static readonly string CostsDescription = "The cost matrix that describes the assignment of rows to columns.";
[8093]42    public static readonly string RowNamesDescription = "The elements represented by the rows of the costs matrix.";
43    public static readonly string ColumnNamesDescription = "The elements represented by the columns of the costs matrix.";
[8022]44
[8183]45    public string Filename { get; set; }
46
[7873]47    public override Image ItemImage {
48      get { return HeuristicLab.Common.Resources.VSImageLibrary.Type; }
49    }
50
51    #region Parameter Properties
52    public IValueParameter<DoubleMatrix> CostsParameter {
53      get { return (IValueParameter<DoubleMatrix>)Parameters["Costs"]; }
54    }
[8022]55    public IValueParameter<ItemSet<Permutation>> BestKnownSolutionsParameter {
56      get { return (IValueParameter<ItemSet<Permutation>>)Parameters["BestKnownSolutions"]; }
[7873]57    }
[8022]58    public IValueParameter<Permutation> BestKnownSolutionParameter {
59      get { return (IValueParameter<Permutation>)Parameters["BestKnownSolution"]; }
[7873]60    }
[8093]61    public IValueParameter<StringArray> RowNamesParameter {
62      get { return (IValueParameter<StringArray>)Parameters["RowNames"]; }
63    }
64    public IValueParameter<StringArray> ColumnNamesParameter {
65      get { return (IValueParameter<StringArray>)Parameters["ColumnNames"]; }
66    }
[7873]67    #endregion
68
69    #region Properties
70    public DoubleMatrix Costs {
71      get { return CostsParameter.Value; }
72      set { CostsParameter.Value = value; }
73    }
[8093]74    public StringArray RowNames {
75      get { return RowNamesParameter.Value; }
76      set { RowNamesParameter.Value = value; }
77    }
78    public StringArray ColumnNames {
79      get { return ColumnNamesParameter.Value; }
80      set { ColumnNamesParameter.Value = value; }
81    }
[8022]82    public ItemSet<Permutation> BestKnownSolutions {
83      get { return BestKnownSolutionsParameter.Value; }
84      set { BestKnownSolutionsParameter.Value = value; }
[7873]85    }
[8022]86    public Permutation BestKnownSolution {
87      get { return BestKnownSolutionParameter.Value; }
88      set { BestKnownSolutionParameter.Value = value; }
[7873]89    }
90    #endregion
91
[8022]92    [Storable]
93    private BestLAPSolutionAnalyzer bestLAPSolutionAnalyzer;
94
[7873]95    [StorableConstructor]
96    private LinearAssignmentProblem(bool deserializing) : base(deserializing) { }
97    private LinearAssignmentProblem(LinearAssignmentProblem original, Cloner cloner)
98      : base(original, cloner) {
[8022]99      this.bestLAPSolutionAnalyzer = cloner.Clone(original.bestLAPSolutionAnalyzer);
[7873]100      AttachEventHandlers();
101    }
102    public LinearAssignmentProblem()
[8022]103      : base(new LAPEvaluator(), new RandomPermutationCreator()) {
104      Parameters.Add(new ValueParameter<DoubleMatrix>("Costs", CostsDescription, new DoubleMatrix(3, 3)));
105      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));
106      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));
[8093]107      Parameters.Add(new OptionalValueParameter<StringArray>("RowNames", RowNamesDescription));
108      Parameters.Add(new OptionalValueParameter<StringArray>("ColumnNames", ColumnNamesDescription));
109
[7873]110      ((ValueParameter<DoubleMatrix>)CostsParameter).ReactOnValueToStringChangedAndValueItemImageChanged = false;
[8093]111      ((OptionalValueParameter<StringArray>)RowNamesParameter).ReactOnValueToStringChangedAndValueItemImageChanged = false;
112      ((OptionalValueParameter<StringArray>)ColumnNamesParameter).ReactOnValueToStringChangedAndValueItemImageChanged = false;
[7873]113
[8183]114      RowNames = new StringArray(new string[] { "Eric", "Robert", "Allison" });
115      ColumnNames = new StringArray(new string[] { "MRI", "Blood test", "Angiogram" });
116      Costs[0, 0] = 4; Costs[0, 1] = 5; Costs[0, 2] = 3;
117      Costs[1, 0] = 6; Costs[1, 1] = 6; Costs[1, 2] = 4;
118      Costs[2, 0] = 5; Costs[2, 1] = 5; Costs[2, 2] = 1;
[8093]119
[8022]120      bestLAPSolutionAnalyzer = new BestLAPSolutionAnalyzer();
121      SolutionCreator.PermutationParameter.ActualName = "Assignment";
122      InitializeOperators();
123      Parameterize();
[7873]124      AttachEventHandlers();
125    }
126
127    public override IDeepCloneable Clone(Cloner cloner) {
128      return new LinearAssignmentProblem(this, cloner);
129    }
130
131    #region Events
[8022]132    protected override void OnEvaluatorChanged() {
133      base.OnEvaluatorChanged();
134      Parameterize();
135    }
136    protected override void OnOperatorsChanged() {
137      base.OnOperatorsChanged();
138      Parameterize();
139    }
140    protected override void OnSolutionCreatorChanged() {
141      base.OnSolutionCreatorChanged();
142      SolutionCreator.PermutationParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged);
143      Parameterize();
144    }
[7873]145    private void Costs_RowsChanged(object sender, EventArgs e) {
[8022]146      if (Costs.Rows != Costs.Columns) {
[7873]147        ((IStringConvertibleMatrix)Costs).Columns = Costs.Rows;
[8022]148        Parameterize();
149      }
[7873]150    }
151    private void Costs_ColumnsChanged(object sender, EventArgs e) {
[8022]152      if (Costs.Rows != Costs.Columns) {
[7873]153        ((IStringConvertibleMatrix)Costs).Rows = Costs.Columns;
[8022]154        Parameterize();
155      }
[7873]156    }
[11087]157    private void Costs_Reset(object sender, EventArgs e) {
158      Parameterize();
159    }
[8022]160    private void SolutionCreator_PermutationParameter_ActualNameChanged(object sender, EventArgs e) {
161      Parameterize();
162    }
[7873]163    #endregion
164
165    #region Helpers
166    [StorableHook(HookType.AfterDeserialization)]
[7934]167    private void AfterDeserialization() {
[7873]168      AttachEventHandlers();
169    }
170
171    private void AttachEventHandlers() {
172      Costs.RowsChanged += new EventHandler(Costs_RowsChanged);
173      Costs.ColumnsChanged += new EventHandler(Costs_ColumnsChanged);
[11087]174      Costs.Reset += new EventHandler(Costs_Reset);
[8022]175      SolutionCreator.PermutationParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged);
[7873]176    }
[8022]177
178    private void InitializeOperators() {
179      Operators.AddRange(ApplicationManager.Manager.GetInstances<IPermutationOperator>());
180      Operators.RemoveAll(x => x is IMoveOperator);
181      Operators.Add(bestLAPSolutionAnalyzer);
[15069]182
183      Operators.Add(new HammingSimilarityCalculator());
184      Operators.Add(new QualitySimilarityCalculator());
185      Operators.Add(new PopulationSimilarityAnalyzer(Operators.OfType<ISolutionSimilarityCalculator>()));
[8022]186    }
187
188    private void Parameterize() {
189      SolutionCreator.LengthParameter.Value = new IntValue(Costs.Rows);
[11087]190      SolutionCreator.LengthParameter.Hidden = true;
191      SolutionCreator.PermutationTypeParameter.Value = new PermutationType(PermutationTypes.Absolute);
192      SolutionCreator.PermutationTypeParameter.Hidden = true;
[8022]193      Evaluator.CostsParameter.ActualName = CostsParameter.Name;
194      Evaluator.CostsParameter.Hidden = true;
195      Evaluator.AssignmentParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
196      Evaluator.AssignmentParameter.Hidden = true;
197
198      foreach (var op in Operators.OfType<IPermutationCrossover>()) {
199        op.ParentsParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
200        op.ParentsParameter.Hidden = true;
201        op.ChildParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
202        op.ChildParameter.Hidden = true;
203      }
204
205      foreach (var op in Operators.OfType<IPermutationManipulator>()) {
206        op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
207        op.PermutationParameter.Hidden = true;
208      }
209
210      foreach (var op in Operators.OfType<IPermutationMultiNeighborhoodShakingOperator>()) {
211        op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
212        op.PermutationParameter.Hidden = true;
213      }
214
[15069]215      foreach (var similarityCalculator in Operators.OfType<ISolutionSimilarityCalculator>()) {
216        similarityCalculator.SolutionVariableName = SolutionCreator.PermutationParameter.ActualName;
217        similarityCalculator.QualityVariableName = Evaluator.QualityParameter.ActualName;
218      }
219
[8022]220      bestLAPSolutionAnalyzer.AssignmentParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
221      bestLAPSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name;
222      bestLAPSolutionAnalyzer.BestKnownSolutionParameter.ActualName = BestKnownSolutionParameter.Name;
223      bestLAPSolutionAnalyzer.BestKnownSolutionsParameter.ActualName = BestKnownSolutionsParameter.Name;
224      bestLAPSolutionAnalyzer.CostsParameter.ActualName = CostsParameter.Name;
225      bestLAPSolutionAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
226      bestLAPSolutionAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
227    }
[7873]228    #endregion
229  }
230}
Note: See TracBrowser for help on using the repository browser.