Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Encodings.LinearLinkageEncoding/3.4/LinearLinkageEncoding.cs

Last change on this file was 17699, checked in by abeham, 4 years ago

#2521: Made encodings non-generic classes (the TEncodedSolution type parameter is not actually used), this will make it considerably easier to port the VRP to the new architecture

File size: 5.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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.Collections.Generic;
24using System.Linq;
25using HEAL.Attic;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Optimization;
29using HeuristicLab.PluginInfrastructure;
30
31namespace HeuristicLab.Encodings.LinearLinkageEncoding {
32  [Item("Linear Linkage Encoding", "Describes a linear linkage (LLE) encoding.")]
33  [StorableType("7AE11F39-E6BD-4FC7-8112-0A5EDCBFBDB6")]
34  public sealed class LinearLinkageEncoding : VectorEncoding {
35
36    [StorableConstructor]
37    private LinearLinkageEncoding(StorableConstructorFlag _) : base(_) { }
38    [StorableHook(HookType.AfterDeserialization)]
39    private void AfterDeserialization() {
40      DiscoverOperators();
41    }
42
43    public override IDeepCloneable Clone(Cloner cloner) { return new LinearLinkageEncoding(this, cloner); }
44    private LinearLinkageEncoding(LinearLinkageEncoding original, Cloner cloner) : base(original, cloner) { }
45
46    public LinearLinkageEncoding() : this("LLE", 10) { }
47    public LinearLinkageEncoding(string name) : this(name, 10) { }
48    public LinearLinkageEncoding(int length) : this("LLE", length) { }
49    public LinearLinkageEncoding(string name, int length)
50      : base(name, length) {
51      DiscoverOperators();
52    }
53
54    #region Operator Discovery
55    private static readonly IEnumerable<Type> encodingSpecificOperatorTypes;
56    static LinearLinkageEncoding() {
57      encodingSpecificOperatorTypes = new List<Type>() {
58          typeof (ILinearLinkageOperator),
59          typeof (ILinearLinkageCreator),
60          typeof (ILinearLinkageCrossover),
61          typeof (ILinearLinkageManipulator),
62          typeof (ILinearLinkageShakingOperator),
63          typeof (ILinearLinkageMoveOperator)
64      };
65    }
66    private void DiscoverOperators() {
67      var assembly = typeof(ILinearLinkageOperator).Assembly;
68      var discoveredTypes = ApplicationManager.Manager.GetTypes(encodingSpecificOperatorTypes, assembly, true, false, false);
69      var operators = discoveredTypes.Select(t => (IOperator)Activator.CreateInstance(t));
70      var newOperators = operators.Except(Operators, new TypeEqualityComparer<IOperator>()).ToList();
71
72      ConfigureOperators(newOperators);
73      foreach (var @operator in newOperators)
74        AddOperator(@operator);
75    }
76    #endregion
77
78    public override void ConfigureOperators(IEnumerable<IItem> operators) {
79      base.ConfigureOperators(operators);
80      ConfigureCreators(operators.OfType<ILinearLinkageCreator>());
81      ConfigureCrossovers(operators.OfType<ILinearLinkageCrossover>());
82      ConfigureManipulators(operators.OfType<ILinearLinkageManipulator>());
83      ConfigureShakingOperators(operators.OfType<ILinearLinkageShakingOperator>());
84      ConfigureMoveOperators(operators.OfType<ILinearLinkageMoveOperator>());
85      ConfigureSwap2MoveOperators(operators.OfType<ILinearLinkageSwap2MoveOperator>());
86    }
87
88    #region specific operator wiring
89    private void ConfigureCreators(IEnumerable<ILinearLinkageCreator> creators) {
90      foreach (var creator in creators) {
91        creator.LengthParameter.ActualName = LengthParameter.Name;
92        creator.LLEParameter.ActualName = Name;
93      }
94    }
95    private void ConfigureCrossovers(IEnumerable<ILinearLinkageCrossover> crossovers) {
96      foreach (var crossover in crossovers) {
97        crossover.ChildParameter.ActualName = Name;
98        crossover.ParentsParameter.ActualName = Name;
99      }
100    }
101    private void ConfigureManipulators(IEnumerable<ILinearLinkageManipulator> manipulators) {
102      foreach (var manipulator in manipulators) {
103        manipulator.LLEParameter.ActualName = Name;
104      }
105    }
106    private void ConfigureShakingOperators(IEnumerable<ILinearLinkageShakingOperator> shakingOperators) {
107      foreach (var shakingOperator in shakingOperators) {
108        shakingOperator.LLEParameter.ActualName = Name;
109      }
110    }
111    private void ConfigureMoveOperators(IEnumerable<ILinearLinkageMoveOperator> moveOperators) {
112      foreach (var moveOperator in moveOperators) {
113        moveOperator.LLEParameter.ActualName = Name;
114      }
115    }
116    private void ConfigureSwap2MoveOperators(IEnumerable<ILinearLinkageSwap2MoveOperator> swap2MoveOperators) {
117      foreach (var swap2MoveOperator in swap2MoveOperators) {
118        swap2MoveOperator.Swap2MoveParameter.ActualName = Name + ".Swap2Move";
119      }
120    }
121    #endregion
122  }
123}
Note: See TracBrowser for help on using the repository browser.