Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 11588 was 11588, checked in by mkommend, 9 years ago

#2174: Adapted encodings to store its specific parameters in the standard parameter collection.

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