source: branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/PermutationEncoding.cs @ 11746

Last change on this file since 11746 was 11746, checked in by mkommend, 5 years ago

#2174: Fixed operator wiring bugs in Integer- and PermutationEncoding.

File size: 9.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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 HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.PermutationEncoding;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31using HeuristicLab.PluginInfrastructure;
32
33namespace HeuristicLab.Problems.Programmable {
34  [Item("PermutationEncoding", "Describes a permutation encoding.")]
35  [StorableClass]
36  public sealed class PermutationEncoding : Encoding<IPermutationCreator> {
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(bool deserializing) : base(deserializing) { }
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(int length) : this("Permuration", length, PermutationTypes.Absolute) { }
100    public PermutationEncoding(string name, int length, PermutationTypes type)
101      : base(name) {
102      lengthParameter = new FixedValueParameter<IntValue>(Name + ".Length", new IntValue(length));
103      permutationTypeParameter = new FixedValueParameter<PermutationType>(Name + ".Type", new PermutationType(type));
104      Parameters.Add(lengthParameter);
105      Parameters.Add(permutationTypeParameter);
106
107      SolutionCreator = new RandomPermutationCreator();
108      RegisterParameterEvents();
109      DiscoverOperators();
110    }
111
112    private void OnLengthParameterChanged() {
113      RegisterLengthParameterEvents();
114      ConfigureOperators(Operators);
115    }
116
117    private void OnPermutationTypeParameterChanged() {
118      RegisterPermutationTypeParameterEvents();
119      ConfigureOperators(Operators);
120    }
121
122    private void RegisterParameterEvents() {
123      RegisterLengthParameterEvents();
124      RegisterPermutationTypeParameterEvents();
125    }
126    private void RegisterLengthParameterEvents() {
127      LengthParameter.Value.ValueChanged += (o, s) => ConfigureOperators(Operators);
128    }
129    private void RegisterPermutationTypeParameterEvents() {
130      PermutationTypeParameter.Value.ValueChanged += (o, s) => ConfigureOperators(Operators);
131    }
132
133    #region Operator Discovery
134    private static readonly IEnumerable<Type> encodingSpecificOperatorTypes;
135    static PermutationEncoding() {
136      encodingSpecificOperatorTypes = new List<Type>() {
137          typeof (IPermutationOperator),
138          typeof (IPermutationCreator),
139          typeof (IPermutationCrossover),
140          typeof (IPermutationManipulator),
141          typeof (IPermutationMultiNeighborhoodShakingOperator),
142          typeof (IPermutationMoveOperator),
143          typeof (IPermutationInversionMoveOperator),
144          typeof (IPermutationScrambleMoveOperator),
145          typeof (IPermutationSwap2MoveOperator),                   
146          typeof (IPermutationTranslocationMoveOperator)
147      };
148    }
149    private void DiscoverOperators() {
150      var discoveredTypes = ApplicationManager.Manager.GetTypes(encodingSpecificOperatorTypes, true, false, false);
151      var operators = discoveredTypes.Select(t => (IOperator)Activator.CreateInstance(t));
152      var newOperators = operators.Except(Operators, new TypeEqualityComparer<IOperator>()).ToList();
153
154      ConfigureOperators(newOperators);
155      foreach (var @operator in newOperators)
156        encodingOperators.Add(@operator);
157    }
158    #endregion
159
160    public override void ConfigureOperators(IEnumerable<IOperator> operators) {
161      ConfigureCreators(operators.OfType<IPermutationCreator>());
162      ConfigureCrossovers(operators.OfType<IPermutationCrossover>());
163      ConfigureManipulators(operators.OfType<IPermutationManipulator>());
164      ConfigureShakingOperators(operators.OfType<IPermutationMultiNeighborhoodShakingOperator>());
165      ConfigureMoveOperators(operators.OfType<IPermutationMoveOperator>());
166      ConfigureInversionMoveOperators(operators.OfType<IPermutationInversionMoveOperator>());
167      ConfigureScrambleMoveOperators(operators.OfType<IPermutationScrambleMoveOperator>());
168      ConfigureSwap2MoveOperators(operators.OfType<IPermutationSwap2MoveOperator>());
169      ConfigureTranslocationMoveOperators(operators.OfType<IPermutationTranslocationMoveOperator>());
170    }
171
172    #region specific operator wiring
173    private void ConfigureCreators(IEnumerable<IPermutationCreator> creators) {
174      foreach (var creator in creators) {
175        creator.LengthParameter.ActualName = LengthParameter.Name;
176        creator.PermutationParameter.ActualName = Name;
177        creator.PermutationTypeParameter.Value.Value = Type;
178      }
179    }
180    private void ConfigureCrossovers(IEnumerable<IPermutationCrossover> crossovers) {
181      foreach (var crossover in crossovers) {
182        crossover.ChildParameter.ActualName = Name;
183        crossover.ParentsParameter.ActualName = Name;
184      }
185    }
186    private void ConfigureManipulators(IEnumerable<IPermutationManipulator> manipulators) {
187      foreach (var manipulator in manipulators) {
188        manipulator.PermutationParameter.ActualName = Name;
189      }
190    }
191    private void ConfigureShakingOperators(IEnumerable<IPermutationMultiNeighborhoodShakingOperator> shakingOperators) {
192      foreach (var shakingOperator in shakingOperators) {
193        shakingOperator.PermutationParameter.ActualName = Name;
194      }
195    }
196    private void ConfigureMoveOperators(IEnumerable<IPermutationMoveOperator> moveOperators) {
197      foreach (var moveOperator in moveOperators) {
198        moveOperator.PermutationParameter.ActualName = Name;
199      }
200    }
201    private void ConfigureInversionMoveOperators(IEnumerable<IPermutationInversionMoveOperator> inversionMoveOperators) {
202      foreach (var inversionMoveOperator in inversionMoveOperators) {
203        inversionMoveOperator.InversionMoveParameter.ActualName = Name + ".InversionMove";
204      }
205    }
206    private void ConfigureScrambleMoveOperators(IEnumerable<IPermutationScrambleMoveOperator> scrambleMoveOperators) {
207      foreach (var scrambleMoveOperator in scrambleMoveOperators) {
208        scrambleMoveOperator.ScrambleMoveParameter.ActualName = Name + ".ScambleMove";
209      }
210    }
211    private void ConfigureSwap2MoveOperators(IEnumerable<IPermutationSwap2MoveOperator> swap2MoveOperators) {
212      foreach (var swap2MoveOperator in swap2MoveOperators) {
213        swap2MoveOperator.Swap2MoveParameter.ActualName = Name + ".Swap2Move";
214      }
215    }
216    private void ConfigureTranslocationMoveOperators(IEnumerable<IPermutationTranslocationMoveOperator> translocationMoveOperators) {
217      foreach (var translocationMoveOperator in translocationMoveOperators) {
218        translocationMoveOperator.TranslocationMoveParameter.ActualName = Name + ".TranslocationMove";
219      }
220    }
221
222    #endregion
223  }
224}
Note: See TracBrowser for help on using the repository browser.