Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Encodings.PermutationEncoding/3.3/PermutationEncoding.cs @ 17587

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

#2521: refactoring in progress

File size: 9.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.Data;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.PluginInfrastructure;
32
33namespace HeuristicLab.Encodings.PermutationEncoding {
34  [Item("PermutationEncoding", "Describes a permutation encoding.")]
35  [StorableType("E30E7507-44BA-4021-8F56-C3FC5569A6FE")]
36  public sealed class PermutationEncoding : VectorEncoding<Permutation> {
37    #region encoding parameters
38    [Storable] public IValueParameter<EnumValue<PermutationTypes>> PermutationTypeParameter { get; private set; }
39    public PermutationTypes Type {
40      get { return PermutationTypeParameter.Value.Value; }
41      set {
42        if (Type == value) return;
43        PermutationTypeParameter.Value.Value = value;
44      }
45    }
46    #endregion
47
48    [StorableConstructor]
49    private PermutationEncoding(StorableConstructorFlag _) : base(_) { }
50    [StorableHook(HookType.AfterDeserialization)]
51    private void AfterDeserialization() {
52      DiscoverOperators();
53      RegisterParameterEvents();
54    }
55
56    public override IDeepCloneable Clone(Cloner cloner) { return new PermutationEncoding(this, cloner); }
57    private PermutationEncoding(PermutationEncoding original, Cloner cloner)
58      : base(original, cloner) {
59      PermutationTypeParameter = cloner.Clone(original.PermutationTypeParameter);
60      RegisterParameterEvents();
61    }
62
63
64    public PermutationEncoding() : this("Permutation", 10, PermutationTypes.Absolute) { }
65    public PermutationEncoding(string name) : this(name, 10, PermutationTypes.Absolute) { }
66    public PermutationEncoding(int length) : this("Permutation", length, PermutationTypes.Absolute) { }
67    public PermutationEncoding(string name, int length, PermutationTypes type)
68      : base(name, length) {
69      PermutationTypeParameter = new ValueParameter<EnumValue<PermutationTypes>>(Name + ".Type", new EnumValue<PermutationTypes>(type));
70      Parameters.Add(PermutationTypeParameter);
71
72      SolutionCreator = new RandomPermutationCreator();
73      DiscoverOperators();
74      RegisterParameterEvents();
75    }
76
77    private void RegisterParameterEvents() {
78      EnumValueParameterChangeHandler<PermutationTypes>.Create(PermutationTypeParameter, () => {
79        ConfigureOperators(Operators);
80        OnTypeChanged();
81      });
82    }
83
84    #region Operator Discovery
85    private static readonly IEnumerable<Type> encodingSpecificOperatorTypes;
86    static PermutationEncoding() {
87      encodingSpecificOperatorTypes = new List<Type>() {
88          typeof (IPermutationOperator),
89          typeof (IPermutationCreator),
90          typeof (IPermutationCrossover),
91          typeof (IPermutationManipulator),
92          typeof (IPermutationMultiNeighborhoodShakingOperator),
93          typeof (IPermutationMoveOperator),
94          typeof (IPermutationInversionMoveOperator),
95          typeof (IPermutationScrambleMoveOperator),
96          typeof (IPermutationSwap2MoveOperator),                   
97          typeof (IPermutationTranslocationMoveOperator),
98          typeof (IPermutationLocalImprovementOperator),
99          typeof (IPermutationSolutionOperator),
100          typeof (IPermutationSolutionsOperator),
101      };
102    }
103    private void DiscoverOperators() {
104      var assembly = typeof(IPermutationOperator).Assembly;
105      var discoveredTypes = ApplicationManager.Manager.GetTypes(encodingSpecificOperatorTypes, assembly, true, false, false);
106      var operators = discoveredTypes.Select(t => (IOperator)Activator.CreateInstance(t));
107      var newOperators = operators.Except(Operators, new TypeEqualityComparer<IOperator>()).ToList();
108
109      ConfigureOperators(newOperators);
110      foreach (var @operator in newOperators)
111        AddOperator(@operator);
112    }
113    #endregion
114
115    public override void ConfigureOperators(IEnumerable<IItem> operators) {
116      base.ConfigureOperators(operators);
117      ConfigureCreators(operators.OfType<IPermutationCreator>());
118      ConfigureCrossovers(operators.OfType<IPermutationCrossover>());
119      ConfigureManipulators(operators.OfType<IPermutationManipulator>());
120      ConfigureShakingOperators(operators.OfType<IPermutationMultiNeighborhoodShakingOperator>());
121      ConfigureMoveOperators(operators.OfType<IPermutationMoveOperator>());
122      ConfigureInversionMoveOperators(operators.OfType<IPermutationInversionMoveOperator>());
123      ConfigureScrambleMoveOperators(operators.OfType<IPermutationScrambleMoveOperator>());
124      ConfigureSwap2MoveOperators(operators.OfType<IPermutationSwap2MoveOperator>());
125      ConfigureTranslocationMoveOperators(operators.OfType<IPermutationTranslocationMoveOperator>());
126      ConfigureLocalImprovementOperators(operators.OfType<IPermutationLocalImprovementOperator>());
127      ConfigureSolutionOperators(operators.OfType<IPermutationSolutionOperator>());
128      ConfigureSolutionsOperators(operators.OfType<IPermutationSolutionsOperator>());
129    }
130
131    #region specific operator wiring
132    private void ConfigureCreators(IEnumerable<IPermutationCreator> creators) {
133      foreach (var creator in creators) {
134        creator.LengthParameter.ActualName = LengthParameter.Name;
135        creator.PermutationTypeParameter.Value.Value = Type;
136      }
137    }
138    private void ConfigureCrossovers(IEnumerable<IPermutationCrossover> crossovers) {
139      foreach (var crossover in crossovers) {
140        crossover.ChildParameter.ActualName = Name;
141        crossover.ParentsParameter.ActualName = Name;
142      }
143    }
144    private void ConfigureManipulators(IEnumerable<IPermutationManipulator> manipulators) {
145      // IPermutationManipulator does not contain additional parameters (already contained in IPermutationSolutionOperator)
146    }
147    private void ConfigureShakingOperators(IEnumerable<IPermutationMultiNeighborhoodShakingOperator> shakingOperators) {
148      foreach (var shakingOperator in shakingOperators) {
149        shakingOperator.PermutationParameter.ActualName = Name;
150      }
151    }
152    private void ConfigureMoveOperators(IEnumerable<IPermutationMoveOperator> moveOperators) {
153      foreach (var moveOperator in moveOperators) {
154        moveOperator.PermutationParameter.ActualName = Name;
155      }
156    }
157    private void ConfigureInversionMoveOperators(IEnumerable<IPermutationInversionMoveOperator> inversionMoveOperators) {
158      foreach (var inversionMoveOperator in inversionMoveOperators) {
159        inversionMoveOperator.InversionMoveParameter.ActualName = Name + ".InversionMove";
160      }
161    }
162    private void ConfigureScrambleMoveOperators(IEnumerable<IPermutationScrambleMoveOperator> scrambleMoveOperators) {
163      foreach (var scrambleMoveOperator in scrambleMoveOperators) {
164        scrambleMoveOperator.ScrambleMoveParameter.ActualName = Name + ".ScrambleMove";
165      }
166    }
167    private void ConfigureSwap2MoveOperators(IEnumerable<IPermutationSwap2MoveOperator> swap2MoveOperators) {
168      foreach (var swap2MoveOperator in swap2MoveOperators) {
169        swap2MoveOperator.Swap2MoveParameter.ActualName = Name + ".Swap2Move";
170      }
171    }
172    private void ConfigureTranslocationMoveOperators(IEnumerable<IPermutationTranslocationMoveOperator> translocationMoveOperators) {
173      foreach (var translocationMoveOperator in translocationMoveOperators) {
174        translocationMoveOperator.TranslocationMoveParameter.ActualName = Name + ".TranslocationMove";
175      }
176    }
177    private void ConfigureLocalImprovementOperators(IEnumerable<IPermutationLocalImprovementOperator> localImprovementOperators) {
178      // IPermutationLocalImprovementOperator does not contain additional parameters (already contained in IPermutationSolutionOperator)
179    }
180    private void ConfigureSolutionOperators(IEnumerable<IPermutationSolutionOperator> solutionOperators) {
181      foreach (var solutionOperator in solutionOperators) {
182        solutionOperator.PermutationParameter.ActualName = Name;
183      }
184    }
185    private void ConfigureSolutionsOperators(IEnumerable<IPermutationSolutionsOperator> solutionsOperators) {
186      foreach (var solutionsOperator in solutionsOperators) {
187        solutionsOperator.PermutationsParameter.ActualName = Name;
188      }
189    }
190    #endregion
191
192    public event EventHandler TypeChanged;
193    private void OnTypeChanged() {
194      TypeChanged?.Invoke(this, EventArgs.Empty);
195    }
196  }
197}
Note: See TracBrowser for help on using the repository browser.