Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2817-BinPackingSpeedup/HeuristicLab.Problems.BinPacking/3.3/2D/PermutationEncoding/PermutationProblem.cs @ 15448

Last change on this file since 15448 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: 4.1 KB
Line 
1#region License Information
2
3/* HeuristicLab
4 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
5 *
6 * This file is part of HeuristicLab.
7 *
8 * HeuristicLab is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * HeuristicLab is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#endregion
23
24using System.Linq;
25using HeuristicLab.Analysis;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Encodings.PermutationEncoding;
29using HeuristicLab.Optimization;
30using HeuristicLab.Optimization.Operators;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32
33namespace HeuristicLab.Problems.BinPacking2D {
34  [Item("Bin Packing Problem (2D, permutation encoding) (BPP)", "Represents a two-dimensional bin-packing problem using only bins with identical measures and bins/items with rectangular shapes.")]
35  [StorableClass]
36  [Creatable(Category = CreatableAttribute.Categories.CombinatorialProblems, Priority = 300)]
37  public sealed class PermutationProblem : ProblemBase<PermutationEncoding, Permutation> {
38    // persistence
39    [StorableConstructor]
40    private PermutationProblem(bool deserializing) : base(deserializing) { }
41
42    // cloning
43    private PermutationProblem(PermutationProblem original, Cloner cloner)
44      : base(original, cloner) {
45      RegisterEventHandlers();
46    }
47
48    public PermutationProblem()
49      : base() {
50      Decoder = new ExtremePointPermutationDecoder(); // default decoder
51
52      Encoding = new PermutationEncoding(EncodedSolutionName, Items.Count, PermutationTypes.Absolute);
53      AddOperators();
54      Parameterize();
55      RegisterEventHandlers();
56    }
57    public override IDeepCloneable Clone(Cloner cloner) {
58      return new PermutationProblem(this, cloner);
59    }
60
61    [StorableHook(HookType.AfterDeserialization)]
62    private void AfterDeserialization() {
63      RegisterEventHandlers();
64    }
65
66    protected override void OnEncodingChanged() {
67      base.OnEncodingChanged();
68      Parameterize();
69    }
70
71    private void AddOperators() {
72      Operators.Add(new TranslocationMoveEvaluator());
73      Operators.Add(new Swap2MoveEvaluator());
74
75      Operators.RemoveAll(x => x is SingleObjectiveMoveGenerator);
76      Operators.RemoveAll(x => x is SingleObjectiveMoveMaker);
77      Operators.RemoveAll(x => x is SingleObjectiveMoveEvaluator);
78      Operators.Add(new HammingSimilarityCalculator());
79      Operators.Add(new QualitySimilarityCalculator());
80      Operators.Add(new PopulationSimilarityAnalyzer(Operators.OfType<ISolutionSimilarityCalculator>()));
81
82      Encoding.ConfigureOperators(Operators.OfType<IOperator>());
83
84      foreach (var op in Operators.OfType<IOperator<Permutation>>()) {
85        op.BinShapeParameter.ActualName = BinShapeParameter.Name;
86        op.ItemsParameter.ActualName = ItemsParameter.Name;
87        op.SolutionEvaluatorParameter.ActualName = SolutionEvaluatorParameter.Name;
88        op.DecoderParameter.ActualName = DecoderParameter.Name;
89      }
90    }
91
92    private void RegisterEventHandlers() {
93      // update encoding length when number of items is changed
94      ItemsParameter.ValueChanged += (sender, args) => Parameterize();
95    }
96
97    private void Parameterize() {
98      Encoding.Length = Items.Count;
99      foreach (var similarityCalculator in Operators.OfType<ISolutionSimilarityCalculator>()) {
100        similarityCalculator.SolutionVariableName = Encoding.SolutionCreator.PermutationParameter.ActualName;
101        similarityCalculator.QualityVariableName = Evaluator.QualityParameter.ActualName;
102      }
103    }
104  }
105}
Note: See TracBrowser for help on using the repository browser.