Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2521: work in progress

File size: 11.1 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 : Encoding<Permutation> {
37    #region encoding parameters
38    [Storable]
39    private IFixedValueParameter<IntValue> lengthParameter;
40    public IFixedValueParameter<IntValue> LengthParameter {
41      get { return lengthParameter; }
42      set {
43        if (value == null) throw new ArgumentNullException("Length parameter must not be null.");
44        if (value.Value == null) throw new ArgumentNullException("Length parameter value must not be null.");
45        if (lengthParameter == value) return;
46
47        if (lengthParameter != null) Parameters.Remove(lengthParameter);
48        lengthParameter = value;
49        Parameters.Add(lengthParameter);
50        OnLengthParameterChanged();
51      }
52    }
53
54    [Storable]
55    private IFixedValueParameter<PermutationType> permutationTypeParameter;
56    public IFixedValueParameter<PermutationType> PermutationTypeParameter {
57      get { return permutationTypeParameter; }
58      set {
59        if (value == null) throw new ArgumentNullException("Permutation type parameter must not be null.");
60        if (value.Value == null) throw new ArgumentNullException("Permutation type parameter value must not be null.");
61        if (permutationTypeParameter == value) return;
62
63        if (permutationTypeParameter != null) Parameters.Remove(permutationTypeParameter);
64        permutationTypeParameter = value;
65        Parameters.Add(permutationTypeParameter);
66        OnPermutationTypeParameterChanged();
67      }
68    }
69    #endregion
70
71    public int Length {
72      get { return LengthParameter.Value.Value; }
73      set { LengthParameter.Value.Value = value; }
74    }
75
76    public PermutationTypes Type {
77      get { return PermutationTypeParameter.Value.Value; }
78      set { PermutationTypeParameter.Value.Value = value; }
79    }
80
81    [StorableConstructor]
82    private PermutationEncoding(StorableConstructorFlag _) : base(_) { }
83    [StorableHook(HookType.AfterDeserialization)]
84    private void AfterDeserialization() {
85      RegisterParameterEvents();
86      DiscoverOperators();
87    }
88
89    public override IDeepCloneable Clone(Cloner cloner) { return new PermutationEncoding(this, cloner); }
90    private PermutationEncoding(PermutationEncoding original, Cloner cloner)
91      : base(original, cloner) {
92      lengthParameter = cloner.Clone(original.lengthParameter);
93      permutationTypeParameter = cloner.Clone(original.permutationTypeParameter);
94      RegisterParameterEvents();
95    }
96
97
98    public PermutationEncoding() : this("Permutation", 10, PermutationTypes.Absolute) { }
99    public PermutationEncoding(string name) : this(name, 10, PermutationTypes.Absolute) { }
100    public PermutationEncoding(int length) : this("Permutation", length, PermutationTypes.Absolute) { }
101    public PermutationEncoding(string name, int length, PermutationTypes type)
102      : base(name) {
103      lengthParameter = new FixedValueParameter<IntValue>(Name + ".Length", new IntValue(length));
104      permutationTypeParameter = new FixedValueParameter<PermutationType>(Name + ".Type", new PermutationType(type));
105      Parameters.Add(lengthParameter);
106      Parameters.Add(permutationTypeParameter);
107
108      SolutionCreator = new RandomPermutationCreator();
109      RegisterParameterEvents();
110      DiscoverOperators();
111    }
112
113    private void OnLengthParameterChanged() {
114      RegisterLengthParameterEvents();
115      ConfigureOperators(Operators);
116    }
117
118    private void OnPermutationTypeParameterChanged() {
119      RegisterPermutationTypeParameterEvents();
120      ConfigureOperators(Operators);
121    }
122
123    private void RegisterParameterEvents() {
124      RegisterLengthParameterEvents();
125      RegisterPermutationTypeParameterEvents();
126    }
127    private void RegisterLengthParameterEvents() {
128      LengthParameter.Value.ValueChanged += (o, s) => ConfigureOperators(Operators);
129    }
130    private void RegisterPermutationTypeParameterEvents() {
131      PermutationTypeParameter.Value.ValueChanged += (o, s) => ConfigureOperators(Operators);
132    }
133
134    #region Operator Discovery
135    private static readonly IEnumerable<Type> encodingSpecificOperatorTypes;
136    static PermutationEncoding() {
137      encodingSpecificOperatorTypes = new List<Type>() {
138          typeof (IPermutationOperator),
139          typeof (IPermutationCreator),
140          typeof (IPermutationCrossover),
141          typeof (IPermutationManipulator),
142          typeof (IPermutationMultiNeighborhoodShakingOperator),
143          typeof (IPermutationMoveOperator),
144          typeof (IPermutationInversionMoveOperator),
145          typeof (IPermutationScrambleMoveOperator),
146          typeof (IPermutationSwap2MoveOperator),                   
147          typeof (IPermutationTranslocationMoveOperator),
148          typeof (IPermutationLocalImprovementOperator),
149          typeof (IPermutationSolutionOperator),
150          typeof (IPermutationSolutionsOperator),
151      };
152    }
153    private void DiscoverOperators() {
154      var assembly = typeof(IPermutationOperator).Assembly;
155      var discoveredTypes = ApplicationManager.Manager.GetTypes(encodingSpecificOperatorTypes, assembly, true, false, false);
156      var operators = discoveredTypes.Select(t => (IOperator)Activator.CreateInstance(t));
157      var newOperators = operators.Except(Operators, new TypeEqualityComparer<IOperator>()).ToList();
158
159      ConfigureOperators(newOperators);
160      foreach (var @operator in newOperators)
161        AddOperator(@operator);
162    }
163    #endregion
164
165    public override void ConfigureOperators(IEnumerable<IItem> operators) {
166      base.ConfigureOperators(operators);
167      ConfigureCreators(operators.OfType<IPermutationCreator>());
168      ConfigureCrossovers(operators.OfType<IPermutationCrossover>());
169      ConfigureManipulators(operators.OfType<IPermutationManipulator>());
170      ConfigureShakingOperators(operators.OfType<IPermutationMultiNeighborhoodShakingOperator>());
171      ConfigureMoveOperators(operators.OfType<IPermutationMoveOperator>());
172      ConfigureInversionMoveOperators(operators.OfType<IPermutationInversionMoveOperator>());
173      ConfigureScrambleMoveOperators(operators.OfType<IPermutationScrambleMoveOperator>());
174      ConfigureSwap2MoveOperators(operators.OfType<IPermutationSwap2MoveOperator>());
175      ConfigureTranslocationMoveOperators(operators.OfType<IPermutationTranslocationMoveOperator>());
176      ConfigureLocalImprovementOperators(operators.OfType<IPermutationLocalImprovementOperator>());
177      ConfigureSolutionOperators(operators.OfType<IPermutationSolutionOperator>());
178      ConfigureSolutionsOperators(operators.OfType<IPermutationSolutionsOperator>());
179    }
180
181    #region specific operator wiring
182    private void ConfigureCreators(IEnumerable<IPermutationCreator> creators) {
183      foreach (var creator in creators) {
184        creator.LengthParameter.ActualName = LengthParameter.Name;
185        creator.PermutationTypeParameter.Value.Value = Type;
186      }
187    }
188    private void ConfigureCrossovers(IEnumerable<IPermutationCrossover> crossovers) {
189      foreach (var crossover in crossovers) {
190        crossover.ChildParameter.ActualName = Name;
191        crossover.ParentsParameter.ActualName = Name;
192      }
193    }
194    private void ConfigureManipulators(IEnumerable<IPermutationManipulator> manipulators) {
195      // IPermutationManipulator does not contain additional parameters (already contained in IPermutationSolutionOperator)
196    }
197    private void ConfigureShakingOperators(IEnumerable<IPermutationMultiNeighborhoodShakingOperator> shakingOperators) {
198      foreach (var shakingOperator in shakingOperators) {
199        shakingOperator.PermutationParameter.ActualName = Name;
200      }
201    }
202    private void ConfigureMoveOperators(IEnumerable<IPermutationMoveOperator> moveOperators) {
203      foreach (var moveOperator in moveOperators) {
204        moveOperator.PermutationParameter.ActualName = Name;
205      }
206    }
207    private void ConfigureInversionMoveOperators(IEnumerable<IPermutationInversionMoveOperator> inversionMoveOperators) {
208      foreach (var inversionMoveOperator in inversionMoveOperators) {
209        inversionMoveOperator.InversionMoveParameter.ActualName = Name + ".InversionMove";
210      }
211    }
212    private void ConfigureScrambleMoveOperators(IEnumerable<IPermutationScrambleMoveOperator> scrambleMoveOperators) {
213      foreach (var scrambleMoveOperator in scrambleMoveOperators) {
214        scrambleMoveOperator.ScrambleMoveParameter.ActualName = Name + ".ScrambleMove";
215      }
216    }
217    private void ConfigureSwap2MoveOperators(IEnumerable<IPermutationSwap2MoveOperator> swap2MoveOperators) {
218      foreach (var swap2MoveOperator in swap2MoveOperators) {
219        swap2MoveOperator.Swap2MoveParameter.ActualName = Name + ".Swap2Move";
220      }
221    }
222    private void ConfigureTranslocationMoveOperators(IEnumerable<IPermutationTranslocationMoveOperator> translocationMoveOperators) {
223      foreach (var translocationMoveOperator in translocationMoveOperators) {
224        translocationMoveOperator.TranslocationMoveParameter.ActualName = Name + ".TranslocationMove";
225      }
226    }
227    private void ConfigureLocalImprovementOperators(IEnumerable<IPermutationLocalImprovementOperator> localImprovementOperators) {
228      // IPermutationLocalImprovementOperator does not contain additional parameters (already contained in IPermutationSolutionOperator)
229    }
230    private void ConfigureSolutionOperators(IEnumerable<IPermutationSolutionOperator> solutionOperators) {
231      foreach (var solutionOperator in solutionOperators) {
232        solutionOperator.PermutationParameter.ActualName = Name;
233      }
234    }
235    private void ConfigureSolutionsOperators(IEnumerable<IPermutationSolutionsOperator> solutionsOperators) {
236      foreach (var solutionsOperator in solutionsOperators) {
237        solutionsOperator.PermutationsParameter.ActualName = Name;
238      }
239    }
240    #endregion
241  }
242}
Note: See TracBrowser for help on using the repository browser.